Explorar o código

Move google docs list filter to list.js file.

Mateusz Samsel %!s(int64=6) %!d(string=hai) anos
pai
achega
68221ad94e

+ 0 - 99
packages/ckeditor5-paste-from-office/src/filters/common.js

@@ -1,99 +0,0 @@
-/**
- * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
-
-/**
- * @module paste-from-office/filters/common
- */
-
-/**
- * Removes paragraph wrapping content inside list item.
- *
- * @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} elementOrDocumentFragment
- * @param {module:engine/view/upcastwriter~UpcastWriter} writer
- */
-export function unwrapParagraph( elementOrDocumentFragment, writer ) {
-	const iterableNodes = elementOrDocumentFragment.is( 'element' ) ? elementOrDocumentFragment.getChildren() : elementOrDocumentFragment;
-
-	for ( const element of iterableNodes ) {
-		if ( element.is( 'element', 'p' ) && element.parent.is( 'element', 'li' ) ) {
-			unwrapSingleElement( element, writer );
-		}
-
-		if ( element.is( 'element' ) && element.childCount ) {
-			unwrapParagraph( element, writer );
-		}
-	}
-}
-
-/**
- * Moves nested list inside previous sibling element, what is a proper HTML standard.
- *
- * @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} elementOrDocumentFragment
- * @param {module:engine/view/upcastwriter~UpcastWriter} writer
- */
-export function moveNestedListToListItem( elementOrDocumentFragment, writer ) {
-	// There are 2 situations which are fixed in the for loop:
-	//
-	// 1. Unwrap nested list to avoid situation that UL or OL is direct child of another UL or OL.
-	// OL                                OL
-	// |-> LI                            |-> LI
-	//     |-> OL                            |-> OL
-	//         |-> OL                            |-> LI
-	//         |   |-> OL                        |-> LI
-	//         |       |-> OL
-	//         |           |-> LI
-	//         |-> LI
-	// or list like:
-	// OL                                OL
-	// |-> OL                            |-> LI
-	//     |-> OL
-	//          |-> OL
-	//              |-> LI
-	//
-	// 2. Move list to previous list item:
-	// OL                                OL
-	// |-> LI                            |-> LI
-	// |-> OL                                |-> OL
-	//     |-> LI                                |-> LI
-	const iterableNodes = elementOrDocumentFragment.is( 'element' ) ? elementOrDocumentFragment.getChildren() : elementOrDocumentFragment;
-
-	for ( const element of iterableNodes ) {
-		if ( isList( element ) ) {
-			// 1.
-			if ( isList( element.getChild( 0 ) ) ) {
-				let firstChild = element.getChild( 0 );
-
-				while ( isList( firstChild ) ) {
-					unwrapSingleElement( firstChild, writer );
-					firstChild = element.getChild( 0 );
-				}
-			}
-
-			// 2.
-			const previous = element.previousSibling;
-
-			if ( previous && previous.is( 'element', 'li' ) ) {
-				writer.remove( element );
-				writer.insertChild( previous.childCount, element, previous );
-			}
-		}
-
-		if ( element.is( 'element' ) && element.childCount ) {
-			moveNestedListToListItem( element, writer );
-		}
-	}
-}
-
-function isList( element ) {
-	return element.is( 'element', 'ol' ) || element.is( 'element', 'ul' );
-}
-
-function unwrapSingleElement( element, writer ) {
-	const parent = element.parent;
-	const childIndex = parent.getChildIndex( element );
-
-	writer.remove( element );
-	writer.insertChild( childIndex, element.getChildren(), parent );
-}

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

@@ -230,3 +230,95 @@ function isNewListNeeded( previousItem, currentItem ) {
 	// Even with the same id the list does not have to be continuous (#43).
 	// Even with the same id the list does not have to be continuous (#43).
 	return !previousSibling.is( 'ul' ) && !previousSibling.is( 'ol' );
 	return !previousSibling.is( 'ul' ) && !previousSibling.is( 'ol' );
 }
 }
+
+// Paste from Google Docs
+/**
+ * Removes paragraph wrapping content inside list item.
+ *
+ * @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} elementOrDocumentFragment
+ * @param {module:engine/view/upcastwriter~UpcastWriter} writer
+ */
+export function unwrapParagraph( elementOrDocumentFragment, writer ) {
+	const iterableNodes = elementOrDocumentFragment.is( 'element' ) ? elementOrDocumentFragment.getChildren() : elementOrDocumentFragment;
+
+	for ( const element of iterableNodes ) {
+		if ( element.is( 'element', 'p' ) && element.parent.is( 'element', 'li' ) ) {
+			unwrapSingleElement( element, writer );
+		}
+
+		if ( element.is( 'element' ) && element.childCount ) {
+			unwrapParagraph( element, writer );
+		}
+	}
+}
+
+/**
+ * Moves nested list inside previous sibling element, what is a proper HTML standard.
+ *
+ * @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} elementOrDocumentFragment
+ * @param {module:engine/view/upcastwriter~UpcastWriter} writer
+ */
+export function moveNestedListToListItem( elementOrDocumentFragment, writer ) {
+	// There are 2 situations which are fixed in the for loop:
+	//
+	// 1. Unwrap nested list to avoid situation that UL or OL is direct child of another UL or OL.
+	// OL                                OL
+	// |-> LI                            |-> LI
+	//     |-> OL                            |-> OL
+	//         |-> OL                            |-> LI
+	//         |   |-> OL                        |-> LI
+	//         |       |-> OL
+	//         |           |-> LI
+	//         |-> LI
+	// or list like:
+	// OL                                OL
+	// |-> OL                            |-> LI
+	//     |-> OL
+	//          |-> OL
+	//              |-> LI
+	//
+	// 2. Move list to previous list item:
+	// OL                                OL
+	// |-> LI                            |-> LI
+	// |-> OL                                |-> OL
+	//     |-> LI                                |-> LI
+	const iterableNodes = elementOrDocumentFragment.is( 'element' ) ? elementOrDocumentFragment.getChildren() : elementOrDocumentFragment;
+
+	for ( const element of iterableNodes ) {
+		if ( isList( element ) ) {
+			// 1.
+			if ( isList( element.getChild( 0 ) ) ) {
+				let firstChild = element.getChild( 0 );
+
+				while ( isList( firstChild ) ) {
+					unwrapSingleElement( firstChild, writer );
+					firstChild = element.getChild( 0 );
+				}
+			}
+
+			// 2.
+			const previous = element.previousSibling;
+
+			if ( previous && previous.is( 'element', 'li' ) ) {
+				writer.remove( element );
+				writer.insertChild( previous.childCount, element, previous );
+			}
+		}
+
+		if ( element.is( 'element' ) && element.childCount ) {
+			moveNestedListToListItem( element, writer );
+		}
+	}
+}
+
+function isList( element ) {
+	return element.is( 'element', 'ol' ) || element.is( 'element', 'ul' );
+}
+
+function unwrapSingleElement( element, writer ) {
+	const parent = element.parent;
+	const childIndex = parent.getChildIndex( element );
+
+	writer.remove( element );
+	writer.insertChild( childIndex, element.getChildren(), parent );
+}

+ 1 - 1
packages/ckeditor5-paste-from-office/src/normalizers/genericnormalizer.js

@@ -7,7 +7,7 @@
  * @module paste-from-office/normalizers/genericnormalizer
  * @module paste-from-office/normalizers/genericnormalizer
  */
  */
 
 
-import { unwrapParagraph, moveNestedListToListItem } from '../filters/common';
+import { unwrapParagraph, moveNestedListToListItem } from '../filters/list';
 import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
 import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
 
 
 /**
 /**

+ 1 - 1
packages/ckeditor5-paste-from-office/src/normalizers/googledocsnormalizer.js

@@ -8,7 +8,7 @@
  */
  */
 
 
 import removeBoldWrapper from '../filters/removeboldwrapper';
 import removeBoldWrapper from '../filters/removeboldwrapper';
-import { unwrapParagraph, moveNestedListToListItem } from '../filters/common';
+import { unwrapParagraph, moveNestedListToListItem } from '../filters/list';
 import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
 import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
 
 
 const googleDocsMatch = /id=("|')docs-internal-guid-[-0-9a-f]+("|')/i;
 const googleDocsMatch = /id=("|')docs-internal-guid-[-0-9a-f]+("|')/i;

+ 0 - 187
packages/ckeditor5-paste-from-office/tests/filters/common.js

@@ -1,187 +0,0 @@
-/**
- * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
-
-import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
-import {
-	unwrapParagraph,
-	moveNestedListToListItem
-} from '../../src/filters/common';
-import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
-
-describe( 'PasteFromOffice/filters', () => {
-	const htmlDataProcessor = new HtmlDataProcessor();
-	describe( 'common', () => {
-		let writer;
-
-		before( () => {
-			writer = new UpcastWriter();
-		} );
-
-		describe( 'unwrapParagraph', () => {
-			it( 'should remove paragraph from list item remaining nested elements', () => {
-				const inputData = '<ul><li><p>foo</p></li><li><p><span>bar</span></p></li></ul>';
-				const documentFragment = htmlDataProcessor.toView( inputData );
-
-				unwrapParagraph( documentFragment, writer );
-
-				expect( htmlDataProcessor.toData( documentFragment ) ).to.equal(
-					'<ul><li>foo</li><li><span>bar</span></li></ul>'
-				);
-			} );
-
-			it( 'should remove paragraph from nested list', () => {
-				const inputData = '<ul>' +
-						'<li>' +
-							'<p>one</p>' +
-							'<ol>' +
-								'<li>' +
-									'<p>two</p>' +
-									'<ul>' +
-										'<li>' +
-											'<p>three</p>' +
-										'</li>' +
-									'</ul>' +
-								'</li>' +
-							'</ol>' +
-						'</li>' +
-					'</ul>';
-				const documentFragment = htmlDataProcessor.toView( inputData );
-
-				unwrapParagraph( documentFragment, writer );
-
-				expect( htmlDataProcessor.toData( documentFragment ) ).to.equal(
-					'<ul><li>one<ol><li>two<ul><li>three</li></ul></li></ol></li></ul>'
-				);
-			} );
-		} );
-
-		describe( 'moveNestedListToListItem', () => {
-			it( 'should move nested list to previous list item', () => {
-				const inputData = '<ul>' +
-						'<li>one</li>' +
-						'<ul>' +
-							'<li>two</li>' +
-							'<li>three</li>' +
-						'</ul>' +
-						'<li>four</li>' +
-					'</ul>';
-
-				const documentFragment = htmlDataProcessor.toView( inputData );
-
-				moveNestedListToListItem( documentFragment, writer );
-
-				expect( htmlDataProcessor.toData( documentFragment ) ).to.equal(
-					'<ul><li>one<ul><li>two</li><li>three</li></ul></li><li>four</li></ul>'
-				);
-			} );
-		} );
-
-		describe( 'repeatedly nested lists are normalized', () => {
-			it( 'should unwrap single nested list', () => {
-				const inputData = '<ul><li>foo</li><ul><ul><ul><ul><li>bar</li></ul></ul></ul></ul></ul>';
-				const documentFragment = htmlDataProcessor.toView( inputData );
-
-				moveNestedListToListItem( documentFragment, writer );
-
-				expect( htmlDataProcessor.toData( documentFragment ) ).to.equal(
-					'<ul><li>foo<ul><li>bar</li></ul></li></ul>'
-				);
-			} );
-
-			it( 'should preserve sibling elements in correct relation', () => {
-				const inputData = '<ol>' +
-						'<li>foo</li>' +
-						'<ol>' +
-							'<ol>' +
-								'<ol>' +
-									'<li>one</li>' +
-								'</ol>' +
-								'<li>two</li>' +
-							'</ol>' +
-							'<li>three</li>' +
-							'<ol>' +
-								'<ol>' +
-									'<ol>' +
-										'<li>four</li>' +
-									'</ol>' +
-								'</ol>' +
-							'</ol>' +
-						'</ol>' +
-						'<li>correct' +
-							'<ol>' +
-								'<li>AAA' +
-									'<ol>' +
-										'<li>BBB</li>' +
-										'<li>CCC</li>' +
-									'</ol>' +
-								'</li>' +
-							'</ol>' +
-						'</li>' +
-					'</ol>';
-
-				const documentFragment = htmlDataProcessor.toView( inputData );
-
-				moveNestedListToListItem( documentFragment, writer );
-
-				expect( htmlDataProcessor.toData( documentFragment ) ).to.equal(
-					'<ol>' +
-						'<li>foo' +
-							'<ol>' +
-								'<li>one</li>' +
-								'<li>two</li>' +
-								'<li>three' +
-										'<ol>' +
-											'<li>four</li>' +
-										'</ol>' +
-								'</li>' +
-							'</ol>' +
-						'</li>' +
-						'<li>correct' +
-							'<ol>' +
-								'<li>AAA' +
-									'<ol>' +
-										'<li>BBB</li>' +
-										'<li>CCC</li>' +
-									'</ol>' +
-								'</li>' +
-							'</ol>' +
-						'</li>' +
-					'</ol>'
-				);
-			} );
-
-			it( 'should normalize lists which are start from nested elements', () => {
-				const inputData = '<ol>' +
-						'<ol>' +
-							'<ol>' +
-								'<ol>' +
-									'<li>foo</li>' +
-								'</ol>' +
-							'</ol>' +
-						'</ol>' +
-					'</ol>';
-				const documentFragment = htmlDataProcessor.toView( inputData );
-
-				moveNestedListToListItem( documentFragment, writer );
-
-				expect( htmlDataProcessor.toData( documentFragment ) ).to.equal( '<ol><li>foo</li></ol>' );
-			} );
-
-			it( 'should normalize 2 sibling list independently', () => {
-				const inputData = '<ol>' +
-						'<li>foo</li>' +
-					'</ol>' +
-					'<ul>' +
-						'<li>bar</li>' +
-					'</ul>';
-				const documentFragment = htmlDataProcessor.toView( inputData );
-
-				moveNestedListToListItem( documentFragment, writer );
-
-				expect( htmlDataProcessor.toData( documentFragment ) ).to.equal( '<ol><li>foo</li></ol><ul><li>bar</li></ul>' );
-			} );
-		} );
-	} );
-} );

+ 181 - 2
packages/ckeditor5-paste-from-office/tests/filters/list.js

@@ -6,11 +6,16 @@
 import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
 import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
 import { stringify } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
 import { stringify } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
 import View from '@ckeditor/ckeditor5-engine/src/view/view';
 import View from '@ckeditor/ckeditor5-engine/src/view/view';
+import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
 
 
-import { transformListItemLikeElementsIntoLists } from '../../src/filters/list';
+import {
+	transformListItemLikeElementsIntoLists,
+	unwrapParagraph,
+	moveNestedListToListItem
+} from '../../src/filters/list';
 
 
 describe( 'PasteFromOffice - filters', () => {
 describe( 'PasteFromOffice - filters', () => {
-	describe( 'list', () => {
+	describe( 'list - paste from MS Word', () => {
 		const htmlDataProcessor = new HtmlDataProcessor();
 		const htmlDataProcessor = new HtmlDataProcessor();
 
 
 		describe( 'transformListItemLikeElementsIntoLists()', () => {
 		describe( 'transformListItemLikeElementsIntoLists()', () => {
@@ -67,4 +72,178 @@ describe( 'PasteFromOffice - filters', () => {
 			} );
 			} );
 		} );
 		} );
 	} );
 	} );
+
+	describe( 'list - paste from google docs', () => {
+		const htmlDataProcessor = new HtmlDataProcessor();
+		let writer;
+
+		before( () => {
+			writer = new UpcastWriter();
+		} );
+
+		describe( 'unwrapParagraph', () => {
+			it( 'should remove paragraph from list item remaining nested elements', () => {
+				const inputData = '<ul><li><p>foo</p></li><li><p><span>bar</span></p></li></ul>';
+				const documentFragment = htmlDataProcessor.toView( inputData );
+
+				unwrapParagraph( documentFragment, writer );
+
+				expect( htmlDataProcessor.toData( documentFragment ) ).to.equal(
+					'<ul><li>foo</li><li><span>bar</span></li></ul>'
+				);
+			} );
+
+			it( 'should remove paragraph from nested list', () => {
+				const inputData = '<ul>' +
+						'<li>' +
+							'<p>one</p>' +
+							'<ol>' +
+								'<li>' +
+									'<p>two</p>' +
+									'<ul>' +
+										'<li>' +
+											'<p>three</p>' +
+										'</li>' +
+									'</ul>' +
+								'</li>' +
+							'</ol>' +
+						'</li>' +
+					'</ul>';
+				const documentFragment = htmlDataProcessor.toView( inputData );
+
+				unwrapParagraph( documentFragment, writer );
+
+				expect( htmlDataProcessor.toData( documentFragment ) ).to.equal(
+					'<ul><li>one<ol><li>two<ul><li>three</li></ul></li></ol></li></ul>'
+				);
+			} );
+		} );
+
+		describe( 'moveNestedListToListItem', () => {
+			it( 'should move nested list to previous list item', () => {
+				const inputData = '<ul>' +
+						'<li>one</li>' +
+						'<ul>' +
+							'<li>two</li>' +
+							'<li>three</li>' +
+						'</ul>' +
+						'<li>four</li>' +
+					'</ul>';
+
+				const documentFragment = htmlDataProcessor.toView( inputData );
+
+				moveNestedListToListItem( documentFragment, writer );
+
+				expect( htmlDataProcessor.toData( documentFragment ) ).to.equal(
+					'<ul><li>one<ul><li>two</li><li>three</li></ul></li><li>four</li></ul>'
+				);
+			} );
+		} );
+
+		describe( 'repeatedly nested lists are normalized', () => {
+			it( 'should unwrap single nested list', () => {
+				const inputData = '<ul><li>foo</li><ul><ul><ul><ul><li>bar</li></ul></ul></ul></ul></ul>';
+				const documentFragment = htmlDataProcessor.toView( inputData );
+
+				moveNestedListToListItem( documentFragment, writer );
+
+				expect( htmlDataProcessor.toData( documentFragment ) ).to.equal(
+					'<ul><li>foo<ul><li>bar</li></ul></li></ul>'
+				);
+			} );
+
+			it( 'should preserve sibling elements in correct relation', () => {
+				const inputData = '<ol>' +
+						'<li>foo</li>' +
+						'<ol>' +
+							'<ol>' +
+								'<ol>' +
+									'<li>one</li>' +
+								'</ol>' +
+								'<li>two</li>' +
+							'</ol>' +
+							'<li>three</li>' +
+							'<ol>' +
+								'<ol>' +
+									'<ol>' +
+										'<li>four</li>' +
+									'</ol>' +
+								'</ol>' +
+							'</ol>' +
+						'</ol>' +
+						'<li>correct' +
+							'<ol>' +
+								'<li>AAA' +
+									'<ol>' +
+										'<li>BBB</li>' +
+										'<li>CCC</li>' +
+									'</ol>' +
+								'</li>' +
+							'</ol>' +
+						'</li>' +
+					'</ol>';
+
+				const documentFragment = htmlDataProcessor.toView( inputData );
+
+				moveNestedListToListItem( documentFragment, writer );
+
+				expect( htmlDataProcessor.toData( documentFragment ) ).to.equal(
+					'<ol>' +
+						'<li>foo' +
+							'<ol>' +
+								'<li>one</li>' +
+								'<li>two</li>' +
+								'<li>three' +
+										'<ol>' +
+											'<li>four</li>' +
+										'</ol>' +
+								'</li>' +
+							'</ol>' +
+						'</li>' +
+						'<li>correct' +
+							'<ol>' +
+								'<li>AAA' +
+									'<ol>' +
+										'<li>BBB</li>' +
+										'<li>CCC</li>' +
+									'</ol>' +
+								'</li>' +
+							'</ol>' +
+						'</li>' +
+					'</ol>'
+				);
+			} );
+
+			it( 'should normalize lists which are start from nested elements', () => {
+				const inputData = '<ol>' +
+						'<ol>' +
+							'<ol>' +
+								'<ol>' +
+									'<li>foo</li>' +
+								'</ol>' +
+							'</ol>' +
+						'</ol>' +
+					'</ol>';
+				const documentFragment = htmlDataProcessor.toView( inputData );
+
+				moveNestedListToListItem( documentFragment, writer );
+
+				expect( htmlDataProcessor.toData( documentFragment ) ).to.equal( '<ol><li>foo</li></ol>' );
+			} );
+
+			it( 'should normalize 2 sibling list independently', () => {
+				const inputData = '<ol>' +
+						'<li>foo</li>' +
+					'</ol>' +
+					'<ul>' +
+						'<li>bar</li>' +
+					'</ul>';
+				const documentFragment = htmlDataProcessor.toView( inputData );
+
+				moveNestedListToListItem( documentFragment, writer );
+
+				expect( htmlDataProcessor.toData( documentFragment ) ).to.equal( '<ol><li>foo</li></ol><ul><li>bar</li></ul>' );
+			} );
+		} );
+	} );
 } );
 } );