Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Support correct normalization of wrong indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
gjhenrique committed Feb 26, 2020
1 parent 59aad0e commit fca9d77
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 106 deletions.
67 changes: 25 additions & 42 deletions src/filters/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,42 @@ export function transformListItemLikeElementsIntoLists( documentFragment, styles
}

let currentList = null;
let currentIndentation = 1;

itemLikeElements.forEach( ( itemLikeElement, i ) => {
const isDifferentList = belongsToTheSameList( itemLikeElements[ i - 1 ], itemLikeElement );
const isDifferentList = isNewListNeeded( itemLikeElements[ i - 1 ], itemLikeElement );
const previousItemLikeElement = isDifferentList ? null : itemLikeElements[ i - 1 ];
const indentationDifference = getIndentationDifference( previousItemLikeElement, itemLikeElement );

// If item from a new list is encountered, reset "currentList" which points to a previous list element.
if ( isDifferentList ) {
currentList = null;
currentIndentation = 1;
}

if ( !currentList || indentationDifference !== 0 ) {
const listStyle = detectListStyle( itemLikeElement, stylesString );

// Current list item has indentation level lower than the previous item.
// Find the list element with correct indentation level where current list item should be inserted.
if ( indentationDifference < 0 ) {
currentList = findParentListAtLevel( currentList, indentationDifference );
if ( !currentList ) {
currentList = insertNewEmptyList( listStyle, itemLikeElement.element, writer );
} else if ( itemLikeElement.indent > currentIndentation ) {
const lastListItem = currentList.getChild( currentList.childCount - 1 );
const lastListItemChild = lastListItem.getChild( lastListItem.childCount - 1 );

currentList = insertNewEmptyList( listStyle, lastListItemChild, writer );

currentIndentation += 1;
} else if ( itemLikeElement.indent < currentIndentation ) {
const differentIndentation = currentIndentation - itemLikeElement.indent;

currentList = findParentListAtLevel( currentList, differentIndentation );

// List might have been inserted with a different list style (triggered by the item nested inside
// it with different list style) so it should be adjusted to the current list item style.
currentIndentation = parseInt( itemLikeElement.indent );
}

if ( itemLikeElement.indent <= currentIndentation ) {
if ( !currentList.is( listStyle.type ) ) {
currentList = writer.rename( listStyle.type, currentList );
}
// Current list item has indentation level greater than the previous item. Insert correctly nested new list.
} else if ( indentationDifference > 0 ) {
if ( currentList ) {
const lastListItem = currentList.getChild( currentList.childCount - 1 );
const lastListItemChild = lastListItem.getChild( lastListItem.childCount - 1 );

// Insert new list element on the end of the last list item in the current list. Since new list element
// will be inserted inside list item we need one level less of nesting ("indentationDifference - 1").
currentList = insertNewEmptyList( listStyle, lastListItemChild, writer, indentationDifference - 1 );
} else {
// First item in the list has indentation.
currentList = insertNewEmptyList( listStyle, itemLikeElement.element, writer, indentationDifference );
}
// Current list item has indentation level at the same level as previous item.
} else {
currentList = insertNewEmptyList( listStyle, itemLikeElement.element, writer );
}
}

Expand Down Expand Up @@ -193,27 +189,14 @@ function detectListStyle( listLikeItem, stylesString ) {
// Usually a result of `detectListStyle()` function.
// @param {module:engine/view/element~Element} element Element after which list is inserted.
// @param {module:engine/view/upcastwriter~UpcastWriter} writer
// @param {Number} [wrap=0] How many times new empty list element should be wrapped into another list to created nested structure.
// @returns {module:engine/view/element~Element} Newly created list element.
function insertNewEmptyList( listStyle, element, writer, wrap = 0 ) {

function insertNewEmptyList( listStyle, element, writer ) {
const parent = element.parent;
const list = new Element( listStyle.type );
const position = parent.getChildIndex( element ) + 1;

let currentList = list;

// Wrap new list into li's depending on indentation level.
if ( wrap > 0 ) {
for ( let i = 0; i < wrap; i++ ) {
const parentList = new Element( listStyle.type );
const parentLi = new Element( 'li' );
writer.appendChild( currentList, parentLi );
writer.appendChild( parentLi, parentList );
currentList = parentList;
}
}

writer.insertChild( position, currentList, parent );
writer.insertChild( position, list, parent );

return list;
}
Expand Down Expand Up @@ -293,7 +276,7 @@ function removeBulletElement( element, writer ) {
// @param {Object} previousItem
// @param {Object} currentItem
// @returns {Boolean}
function belongsToTheSameList( previousItem, currentItem ) {
function isNewListNeeded( previousItem, currentItem ) {
if ( !previousItem ) {
return true;
}
Expand Down Expand Up @@ -339,7 +322,7 @@ function findParentListAtLevel( listElement, indentationDifference ) {

for ( const ancestor of ancestors ) {
if ( ancestor.name === 'ul' || ancestor.name === 'ol' ) {
levelChange--;
levelChange++;
}

if ( levelChange === indentationDifference ) {
Expand Down
3 changes: 1 addition & 2 deletions tests/_data/list/nested-mixed/model.word2016.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<listItem listIndent="0" listType="bulleted">A1</listItem>
<listItem listIndent="1" listType="numbered">B2</listItem>
<listItem listIndent="2" listType="numbered"></listItem>
<listItem listIndent="3" listType="bulleted">C4</listItem>
<listItem listIndent="2" listType="numbered">C4</listItem>
<listItem listIndent="2" listType="numbered">D3</listItem>
<listItem listIndent="0" listType="bulleted">E1</listItem>
<listItem listIndent="1" listType="numbered">F2</listItem>
9 changes: 3 additions & 6 deletions tests/_data/list/nested-mixed/normalized.word2016.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
<li class="MsoListParagraphCxSpMiddle" style="margin-left:72.0pt;mso-add-space:auto;mso-list:l0 level2 lfo1;text-indent:-18.0pt">
<span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin"></span>B2<o:p></o:p>
<ol>
<li>
<ul>
<li class="MsoListParagraphCxSpMiddle" style="margin-left:144.0pt;mso-add-space:auto;mso-list:l0 level4 lfo1;text-indent:-18.0pt">
<span style="font-family:Symbol;mso-bidi-font-family:Symbol;mso-fareast-font-family:Symbol"></span>C4<o:p></o:p>
</li>
</ul>

<li class="MsoListParagraphCxSpMiddle" style="margin-left:144.0pt;mso-add-space:auto;mso-list:l0 level4 lfo1;text-indent:-18.0pt">
<span style="font-family:Symbol;mso-bidi-font-family:Symbol;mso-fareast-font-family:Symbol"></span>C4<o:p></o:p>
</li>

<li class="MsoListParagraphCxSpMiddle" style="margin-left:108.0pt;mso-add-space:auto;mso-list:l0 level3 lfo1;text-indent:-18.0pt">
Expand Down
6 changes: 2 additions & 4 deletions tests/_data/list/nested-multiple/model.word2016.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<listItem listIndent="0" listType="bulleted">A1</listItem>
<listItem listIndent="1" listType="numbered"></listItem>
<listItem listIndent="2" listType="bulleted">B3</listItem>
<listItem listIndent="1" listType="numbered">B3</listItem>
<listItem listIndent="1" listType="numbered">C2</listItem>

<paragraph></paragraph>
<paragraph>Foo Bar...</paragraph>
<paragraph></paragraph>

<listItem listIndent="0" listType="numbered"></listItem>
<listItem listIndent="1" listType="numbered">A2</listItem>
<listItem listIndent="0" listType="numbered">A2</listItem>
<listItem listIndent="0" listType="numbered">B1</listItem>
<listItem listIndent="1" listType="numbered">C2</listItem>

Expand Down
25 changes: 9 additions & 16 deletions tests/_data/list/nested-multiple/normalized.safari.word2016.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
<li class="MsoListParagraphCxSpFirst" style="mso-list:l1 level1 lfo2;text-indent:-18.0pt">
<span style="font-family:Symbol;mso-bidi-font-family:Symbol;mso-fareast-font-family:Symbol"></span>A1<o:p></o:p>
<ol>
<li>
<ul>
<li class="MsoListParagraphCxSpMiddle" style="margin-left:108.0pt;mso-add-space:auto;mso-list:l1 level3 lfo2;text-indent:-18.0pt">
<span style="font-family:Wingdings;mso-bidi-font-family:Wingdings;mso-fareast-font-family:Wingdings"></span>B3<o:p></o:p>
</li>
</ul>
</li>

<li class="MsoListParagraphCxSpMiddle" style="margin-left:108.0pt;mso-add-space:auto;mso-list:l1 level3 lfo2;text-indent:-18.0pt">
<span style="font-family:Wingdings;mso-bidi-font-family:Wingdings;mso-fareast-font-family:Wingdings"></span>B3<o:p></o:p>
</li>

<li class="MsoListParagraphCxSpLast" style="margin-left:72.0pt;mso-add-space:auto;mso-list:l1 level2 lfo2;text-indent:-18.0pt">
<li class="MsoListParagraphCxSpLast" style="margin-left:72.0pt;mso-add-space:auto;mso-list:l1 level2 lfo2;text-indent:-18.0pt">
<span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin"></span>C2<o:p></o:p>
</li>
</ol>
Expand All @@ -22,15 +19,11 @@
<p class="MsoNormal" style="-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;caret-color:rgb(0, 0, 0);color:rgb(0, 0, 0);font-family:Calibri, sans-serif;font-size:medium;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;margin:0cm 0cm 0.0001pt;orphans:auto;text-align:start;text-decoration:none;text-indent:0px;text-transform:none;white-space:normal;widows:auto;word-spacing:0px"><o:p></o:p></p>

<ol>
<li>
<ol>
<li class="MsoListParagraphCxSpFirst" style="margin-left:72.0pt;mso-add-space:auto;mso-list:l0 level2 lfo1;text-indent:-18.0pt">
<span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin"></span>A2<o:p></o:p>
</li>
</ol>
</li>
<li class="MsoListParagraphCxSpFirst" style="margin-left:72.0pt;mso-add-space:auto;mso-list:l0 level2 lfo1;text-indent:-18.0pt">
<span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin"></span>A2<o:p></o:p>
</li>

<li class="MsoListParagraphCxSpMiddle" style="mso-list:l0 level1 lfo1;text-indent:-18.0pt">
<li class="MsoListParagraphCxSpMiddle" style="mso-list:l0 level1 lfo1;text-indent:-18.0pt">
<span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin"></span>B1<o:p></o:p>
<ol>
<li class="MsoListParagraphCxSpLast" style="margin-left:72.0pt;mso-add-space:auto;mso-list:l0 level2 lfo1;text-indent:-18.0pt">
Expand Down
24 changes: 8 additions & 16 deletions tests/_data/list/nested-multiple/normalized.word2016.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@
<li class="MsoListParagraphCxSpFirst" style="mso-list:l1 level1 lfo2;text-indent:-18.0pt">
<span style="font-family:Symbol;mso-bidi-font-family:Symbol;mso-fareast-font-family:Symbol"></span>A1<o:p></o:p>
<ol>
<li>
<ul>
<li class="MsoListParagraphCxSpMiddle" style="margin-left:108.0pt;mso-add-space:auto;mso-list:l1 level3 lfo2;text-indent:-18.0pt">
<span style="font-family:Wingdings;mso-bidi-font-family:Wingdings;mso-fareast-font-family:Wingdings"></span>B3<o:p></o:p>
</li>
</ul>
</li>
<li class="MsoListParagraphCxSpMiddle" style="margin-left:108.0pt;mso-add-space:auto;mso-list:l1 level3 lfo2;text-indent:-18.0pt">
<span style="font-family:Wingdings;mso-bidi-font-family:Wingdings;mso-fareast-font-family:Wingdings"></span>B3<o:p></o:p>
</li>

<li class="MsoListParagraphCxSpLast" style="margin-left:72.0pt;mso-add-space:auto;mso-list:l1 level2 lfo2;text-indent:-18.0pt">
<li class="MsoListParagraphCxSpLast" style="margin-left:72.0pt;mso-add-space:auto;mso-list:l1 level2 lfo2;text-indent:-18.0pt">
<span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin"></span>C2<o:p></o:p>
</li>
</ol>
Expand All @@ -22,15 +18,11 @@
<p class="MsoNormal"><o:p></o:p></p>

<ol>
<li>
<ol>
<li class="MsoListParagraphCxSpFirst" style="margin-left:72.0pt;mso-add-space:auto;mso-list:l0 level2 lfo1;text-indent:-18.0pt">
<span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin"></span>A2<o:p></o:p>
</li>
</ol>
</li>
<li class="MsoListParagraphCxSpFirst" style="margin-left:72.0pt;mso-add-space:auto;mso-list:l0 level2 lfo1;text-indent:-18.0pt">
<span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin"></span>A2<o:p></o:p>
</li>

<li class="MsoListParagraphCxSpMiddle" style="mso-list:l0 level1 lfo1;text-indent:-18.0pt">
<li class="MsoListParagraphCxSpMiddle" style="mso-list:l0 level1 lfo1;text-indent:-18.0pt">
<span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin"></span>B1<o:p></o:p>
<ol>
<li class="MsoListParagraphCxSpLast" style="margin-left:72.0pt;mso-add-space:auto;mso-list:l0 level2 lfo1;text-indent:-18.0pt">
Expand Down
3 changes: 1 addition & 2 deletions tests/_data/list/nested/model.word2016.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<listItem listIndent="0" listType="numbered">A1</listItem>
<listItem listIndent="0" listType="numbered">B1</listItem>
<listItem listIndent="1" listType="numbered">C2</listItem>
<listItem listIndent="2" listType="numbered"></listItem>
<listItem listIndent="3" listType="numbered">D4</listItem>
<listItem listIndent="2" listType="numbered">D4</listItem>
<listItem listIndent="1" listType="numbered">E2</listItem>
<listItem listIndent="2" listType="numbered">F3</listItem>
<listItem listIndent="0" listType="numbered">G1</listItem>
8 changes: 2 additions & 6 deletions tests/_data/list/nested/normalized.word2016.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
<li class="MsoListParagraphCxSpMiddle" style="margin-left:72.0pt;mso-add-space:auto;mso-list:l0 level2 lfo1;text-indent:-18.0pt">
<span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin"></span>C2<o:p></o:p>
<ol>
<li>
<ol>
<li class="MsoListParagraphCxSpMiddle" style="margin-left:144.0pt;mso-add-space:auto;mso-list:l0 level4 lfo1;text-indent:-18.0pt">
<span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin"></span>D4<o:p></o:p>
</li>
</ol>
<li class="MsoListParagraphCxSpMiddle" style="margin-left:144.0pt;mso-add-space:auto;mso-list:l0 level4 lfo1;text-indent:-18.0pt">
<span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin"></span>D4<o:p></o:p>
</li>
</ol>
</li>
Expand Down
20 changes: 8 additions & 12 deletions tests/filters/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ describe( 'PasteFromOffice - filters', () => {
expect( view.childCount ).to.equal( 1 );
expect( stringify( view ) ).to.equal(
`<ol><li ${ level1 }>Foo` +
'<ol><li>' +
`<ol><li ${ level3 }>Bar` +
`<ol><li ${ level4 }>Baz</li></ol></li></ol></li></ol></li></ol>` );
`<ol><li ${ level3 }>Bar` +
`<ol><li ${ level4 }>Baz</li></ol></li></ol></li></ol>` );
} );

it( 'handles indentation in both directions', () => {
Expand All @@ -124,10 +123,8 @@ describe( 'PasteFromOffice - filters', () => {
expect( view.childCount ).to.equal( 1 );
expect( stringify( view ) ).to.equal(
`<ol><li ${ level1 }>Foo` +
'<ol><li>' +
`<ol><li ${ level3 }>Bar` +
`<ol><li ${ level4 }>Baz</li></ol>` +
'</li></ol>' +
`<ol><li ${ level3 }>Bar` +
`<ol><li ${ level4 }>Baz</li></ol>` +
`</li><li ${ level2 }>Bax</li></ol>` +
`</li><li ${ level1 }>123</li></ol>` );
} );
Expand All @@ -154,11 +151,11 @@ describe( 'PasteFromOffice - filters', () => {
'@list l0:level2 { mso-level-number-format: bullet; }' );

expect( view.childCount ).to.equal( 1 );

expect( stringify( view ) ).to.equal(
`<ul><li ${ level1 }>Foo` +
'<ul><li>' +
`<ol><li ${ level3 }>Bar</li></ol>` +
`</li><li ${ level2 }>Baz</li></ul>` +
`<ul><li ${ level3 }>Bar</li>` +
`<li ${ level2 }>Baz</li></ul>` +
'</li></ul>' );
} );

Expand All @@ -170,8 +167,7 @@ describe( 'PasteFromOffice - filters', () => {

expect( view.childCount ).to.equal( 1 );
expect( stringify( view ) ).to.equal(
'<ol><li>' +
`<ol><li ${ level2 }>Foo</li></ol>` +
`<ol><li ${ level2 }>Foo` +
`</li><li ${ level1 }>Bar` +
`<ol><li ${ level2 }>Baz</li></ol></li></ol>` );
} );
Expand Down

0 comments on commit fca9d77

Please sign in to comment.