浏览代码

Replace recurrence in unwrapParagraph with treewalker.

Mateusz Samsel 6 年之前
父节点
当前提交
60aa006de1

+ 7 - 7
packages/ckeditor5-paste-from-office/src/filters/list.js

@@ -239,15 +239,15 @@ function isNewListNeeded( previousItem, currentItem ) {
  * @param {module:engine/view/upcastwriter~UpcastWriter} writer
  */
 export function unwrapParagraphInListItem( elementOrDocumentFragment, writer ) {
-	const iterableNodes = elementOrDocumentFragment.is( 'element' ) ? elementOrDocumentFragment.getChildren() : elementOrDocumentFragment;
+	for ( const value of writer.createRangeIn( elementOrDocumentFragment ) ) {
+		const element = value.item;
 
-	for ( const element of iterableNodes ) {
-		if ( element.is( 'p' ) && element.parent.is( 'li' ) ) {
-			unwrapSingleElement( element, writer );
-		}
+		if ( element.is( 'li' ) ) {
+			const firstChild = element.getChild( 0 );
 
-		if ( element.is( 'element' ) && element.childCount ) {
-			unwrapParagraphInListItem( element, writer );
+			if ( firstChild.is( 'p' ) ) {
+				unwrapSingleElement( firstChild, writer );
+			}
 		}
 	}
 }

+ 9 - 0
packages/ckeditor5-paste-from-office/tests/filters/list.js

@@ -117,6 +117,15 @@ describe( 'PasteFromOffice - filters', () => {
 					'<ul><li>one<ol><li>two<ul><li>three</li></ul></li></ol></li></ul>'
 				);
 			} );
+
+			it( 'should do nothing for correct lists', () => {
+				const inputData = '<ol><li>foo</li><li>bar<ul><li>baz</li></ul></li></ol>';
+				const documentFragment = htmlDataProcessor.toView( inputData );
+
+				unwrapParagraphInListItem( documentFragment, writer );
+
+				expect( htmlDataProcessor.toData( documentFragment ) ).to.equal( '<ol><li>foo</li><li>bar<ul><li>baz</li></ul></li></ol>' );
+			} );
 		} );
 
 		describe( 'fixListIndentation', () => {