Parcourir la source

Introduce generic nromalizer, to patch content which is wrong but not properly recognized byt other normalizers. What can happen for partial copy-paste from Safari.

Mateusz Samsel il y a 6 ans
Parent
commit
a69bbf139c

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

@@ -0,0 +1,35 @@
+/**
+ * @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/normalizers/genericnormalizer
+ */
+
+import { unwrapParagraph, moveNestedListToListItem } from '../filters/common';
+import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
+
+/**
+ * Normalizer for the content pasted from different sources, where we can detect wrong syntax.
+ *
+ * @implements module:paste-from-office/normalizer~Normalizer
+ */
+export default class GenericNormalizer {
+	/**
+	 * @inheritDoc
+	 */
+	isActive() {
+		return true;
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	execute( data ) {
+		const writer = new UpcastWriter();
+
+		moveNestedListToListItem( data.content, writer );
+		unwrapParagraph( data.content, writer );
+	}
+}

+ 6 - 0
packages/ckeditor5-paste-from-office/src/pastefromoffice.js

@@ -12,6 +12,7 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import GoogleDocsNormalizer from './normalizers/googledocsnormalizer';
 import MSWordNormalizer from './normalizers/mswordnormalizer';
 import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
+import GenericNormalizer from './normalizers/genericnormalizer';
 
 /**
  * The Paste from Office plugin.
@@ -49,6 +50,7 @@ export default class PasteFromOffice extends Plugin {
 	init() {
 		const editor = this.editor;
 		const normalizers = [];
+		const genericNormalizer = new GenericNormalizer();
 
 		normalizers.push( new MSWordNormalizer() );
 		normalizers.push( new GoogleDocsNormalizer() );
@@ -67,6 +69,10 @@ export default class PasteFromOffice extends Plugin {
 					activeNormalizer.execute( data );
 
 					data.isTransformedWithPasteFromOffice = true;
+				} else {
+					genericNormalizer.execute( data );
+
+					data.isTransformedWithPasteFromOffice = true;
 				}
 			},
 			{ priority: 'high' }

+ 22 - 0
packages/ckeditor5-paste-from-office/tests/_data/generic/index.js

@@ -0,0 +1,22 @@
+/**
+ * @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 listInTable from './list-in-table/input.html';
+import listInTableNormalized from './list-in-table/normalized.html';
+import listInTableModel from './list-in-table/model.html';
+
+export const fixtures = {
+	input: {
+		listInTable
+	},
+	normalized: {
+		listInTable: listInTableNormalized
+	},
+	model: {
+		listInTable: listInTableModel
+	}
+};
+
+export const browserFixtures = {};

Fichier diff supprimé car celui-ci est trop grand
+ 1 - 0
packages/ckeditor5-paste-from-office/tests/_data/generic/list-in-table/input.html


Fichier diff supprimé car celui-ci est trop grand
+ 1 - 0
packages/ckeditor5-paste-from-office/tests/_data/generic/list-in-table/lists-in-table.html


+ 1 - 0
packages/ckeditor5-paste-from-office/tests/_data/generic/list-in-table/model.html

@@ -0,0 +1 @@
+<table><tableRow><tableCell><listItem listIndent="0" listType="bulleted">One</listItem><listItem listIndent="0" listType="bulleted">Two</listItem><listItem listIndent="0" listType="bulleted">Three</listItem></tableCell></tableRow></table>

Fichier diff supprimé car celui-ci est trop grand
+ 1 - 0
packages/ckeditor5-paste-from-office/tests/_data/generic/list-in-table/normalized.html


+ 5 - 2
packages/ckeditor5-paste-from-office/tests/_utils/fixtures.js

@@ -12,6 +12,7 @@ import { fixtures as spacing, browserFixtures as spacingBrowser } from '../_data
 import { fixtures as googleDocsBoldWrapper, browserFixtures as googleDocsBoldWrapperBrowser }
 	from '../_data/paste-from-google-docs/bold-wrapper/index';
 import { fixtures as googleDocsList, browserFixtures as googleDocsListBrowser } from '../_data/paste-from-google-docs/lists/index.js';
+import { fixtures as listInTable, browserFixtures as listInTableBrowser } from '../_data/generic/index.js';
 
 // Generic fixtures.
 export const fixtures = {
@@ -21,7 +22,8 @@ export const fixtures = {
 	list,
 	spacing,
 	'google-docs-bold-wrapper': googleDocsBoldWrapper,
-	'google-docs-list': googleDocsList
+	'google-docs-list': googleDocsList,
+	'generic-list-in-table': listInTable
 };
 
 // Browser specific fixtures.
@@ -32,5 +34,6 @@ export const browserFixtures = {
 	list: listBrowser,
 	spacing: spacingBrowser,
 	'google-docs-bold-wrapper': googleDocsBoldWrapperBrowser,
-	'google-docs-list': googleDocsListBrowser
+	'google-docs-list': googleDocsListBrowser,
+	'generic-list-in-table': listInTableBrowser
 };

+ 9 - 0
packages/ckeditor5-paste-from-office/tests/data/integration.js

@@ -100,4 +100,13 @@ describe( 'PasteFromOffice - integration', () => {
 			plugins: [ Clipboard, Paragraph, List, PasteFromOffice ]
 		}
 	} );
+
+	generateTests( {
+		input: 'generic-list-in-table',
+		type: 'integration',
+		browsers,
+		editorConfig: {
+			plugins: [ Clipboard, Paragraph, List, Table, Bold, PasteFromOffice ]
+		}
+	} );
 } );

+ 7 - 0
packages/ckeditor5-paste-from-office/tests/data/normalization.js

@@ -63,4 +63,11 @@ describe( 'PasteFromOffice - normalization', () => {
 		browsers,
 		editorConfig
 	} );
+
+	generateTests( {
+		input: 'generic-list-in-table',
+		type: 'normalization',
+		browsers,
+		editorConfig
+	} );
 } );

+ 17 - 0
packages/ckeditor5-paste-from-office/tests/normalizers/genericnormalizer.js

@@ -0,0 +1,17 @@
+/**
+ * @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 GoogleDocsNormalizer from '../../src/normalizers/genericnormalizer';
+
+// `execute()` of the generic normalizer is tested with autogenerated normalization tests.
+describe( 'GoogleDocsNormalizer', () => {
+	const normalizer = new GoogleDocsNormalizer();
+
+	describe( 'isActive()', () => {
+		it( 'should return always true ', () => {
+			expect( normalizer.isActive() ).to.be.true;
+		} );
+	} );
+} );

+ 15 - 25
packages/ckeditor5-paste-from-office/tests/pastefromoffice.js

@@ -52,36 +52,16 @@ describe( 'PasteFromOffice', () => {
 			it( 'should process data from google docs', () => {
 				checkCorrectData( '<p id="docs-internal-guid-12345678-1234-1234-1234-1234567890ab"></p>' );
 			} );
-
-			function checkCorrectData( inputString ) {
-				const data = setUpData( inputString );
-				const getDataSpy = sinon.spy( data.dataTransfer, 'getData' );
-
-				clipboard.fire( 'inputTransformation', data );
-
-				expect( data.isTransformedWithPasteFromOffice ).to.be.true;
-				sinon.assert.called( getDataSpy );
-			}
 		} );
 
-		describe( 'data which should not be marked with flag', () => {
-			it( 'should not process data with regular html', () => {
-				checkInvalidData( '<p>Hello world</p>' );
+		describe( 'data which should be marked with flag by generic normalizer', () => {
+			it( 'should process data with regular html', () => {
+				checkCorrectData( '<p>Hello world</p>' );
 			} );
 
-			it( 'should not process data with similar headers to MS Word', () => {
-				checkInvalidData( '<meta name=Generator content="Other">' );
+			it( 'should process data with similar headers to MS Word', () => {
+				checkCorrectData( '<meta name=Generator content="Other">' );
 			} );
-
-			function checkInvalidData( inputString ) {
-				const data = setUpData( inputString );
-				const getDataSpy = sinon.spy( data.dataTransfer, 'getData' );
-
-				clipboard.fire( 'inputTransformation', data );
-
-				expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
-				sinon.assert.called( getDataSpy );
-			}
 		} );
 
 		describe( 'data which already have the flag', () => {
@@ -105,6 +85,16 @@ describe( 'PasteFromOffice', () => {
 				sinon.assert.notCalled( getDataSpy );
 			}
 		} );
+
+		function checkCorrectData( inputString ) {
+			const data = setUpData( inputString );
+			const getDataSpy = sinon.spy( data.dataTransfer, 'getData' );
+
+			clipboard.fire( 'inputTransformation', data );
+
+			expect( data.isTransformedWithPasteFromOffice ).to.be.true;
+			sinon.assert.called( getDataSpy );
+		}
 	} );
 
 	// @param {String} inputString html to be processed by paste from office