Преглед на файлове

Tests: test refactoring.

Krzysztof Krztoń преди 7 години
родител
ревизия
8aac002eb4

+ 49 - 1
packages/ckeditor5-paste-from-office/tests/_utils/utils.js

@@ -3,8 +3,10 @@
  * For licensing, see LICENSE.md.
  */
 
+import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
+
 import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
-import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
+import { stringify, getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
 
 /**
  * Checks whether for a given editor instance pasting specific content (input) gives expected result (output).
@@ -50,3 +52,49 @@ export function createDataTransfer( data ) {
 		}
 	};
 }
+
+const spacesElementsOnlyRegex = />(\s+)<\//g;
+
+/**
+ * Compares two HTML strings.
+ *
+ * This function is designed for comparing normalized data so expected input is preprocessed before comparing:
+ *
+ *		* Tabs on the lines beginning are removed.
+ *		* Line breaks and empty lines are removed.
+ *		* Space preceding `<o:p></o:p>` tag is replaced with `&nbsp;`.
+ *		* Elements with spaces only have them replaced with `&nbsp;`'s.
+ *
+ * The expected input should be prepared in the above in mind which means every element containing text nodes must start
+ * and end in the same line. So expected input may be formatted like:
+ *
+ * 		<span lang=PL style='mso-ansi-language:PL'>	03<span style='mso-spacerun:yes'>   </span><o:p></o:p></span>
+ *
+ * 	but not like:
+ *
+ * 		<span lang=PL style='mso-ansi-language:PL'>
+ * 			03<span style='mso-spacerun:yes'>   </span>
+ * 			<o:p></o:p>
+ * 		</span>
+ *
+ * 	because tabulator preceding `03` text will be treated as formatting character and will be removed.
+ *
+ * @param {String} actual
+ * @param {String} expected
+ */
+export function expectNormalized( actual, expected ) {
+	let expectedNormalized = expected
+		// Replace tabs on the lines beginning as normalized input files are formatted.
+		.replace( /^\t*</gm, '<' )
+		// Replace line breaks (after closing tags), as they may produce additional spaces during HTML normalization.
+		.replace( /[\r\n]/gm, '' )
+		// Replaces space before Word `<o:p></o:p>` tags so it is not removed during `normalizeHtml()` function call.
+		.replace( / <o:p>/g, '\u00A0<o:p>' );
+
+	// Replace spaces with `&nbsp;` inside elements with spaces only to prevent them from being removed during normalization.
+	expectedNormalized = expectedNormalized.replace( spacesElementsOnlyRegex, ( match, spaces ) => {
+		return `>${ Array( spaces.length + 1 ).join( '\u00A0' ) }</`;
+	} );
+
+	expect( stringify( actual ) ).to.equal( normalizeHtml( expectedNormalized ) );
+}

+ 2 - 1
packages/ckeditor5-paste-from-office/tests/data/integration/basic-styles.js

@@ -13,6 +13,7 @@ import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
 import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
 import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline';
 import Strikethrough from '@ckeditor/ckeditor5-basic-styles/src/strikethrough';
+import PasteFromOffice from '../../../src/pastefromoffice';
 
 import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { expectPaste } from '../../_utils/utils';
@@ -33,7 +34,7 @@ describe( 'Basic Styles – integration', () => {
 		document.body.appendChild( element );
 
 		return ClassicTestEditor
-			.create( element, { plugins: [ Clipboard, Paragraph, Heading, Bold, Italic, Underline, Strikethrough ] } )
+			.create( element, { plugins: [ Clipboard, Paragraph, Heading, Bold, Italic, Underline, Strikethrough, PasteFromOffice ] } )
 			.then( editorInstance => {
 				editor = editorInstance;
 			} );

+ 1 - 10
packages/ckeditor5-paste-from-office/tests/data/normalization/list.js

@@ -7,8 +7,7 @@ import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtest
 import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
 import PasteFromOffice from '../../../src/pastefromoffice';
 
-import { stringify } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
-import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
+import { expectNormalized } from '../../_utils/utils';
 
 import simple from '../../_data/list/simple/input.word2016.html';
 import styled from '../../_data/list/styled/input.word2016.html';
@@ -75,11 +74,3 @@ describe( 'List – normalization', () => {
 		expectNormalized( pasteFromOfficePlugin._normalizeWordInput( heading7, editor ), heading7Normalized );
 	} );
 } );
-
-function expectNormalized( normalizedInput, expectedInput ) {
-	let expected = expectedInput.replace( /> /g, '>&nbsp;' ).replace( / </g, '&nbsp;<' );
-	expected = normalizeHtml( expected );
-	expected = expected.replace( />\s+</g, '><' );
-
-	expect( stringify( normalizedInput ) ).to.equal( expected );
-}

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

@@ -0,0 +1,46 @@
+/**
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
+import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
+import PasteFromOffice from '../../../src/pastefromoffice';
+
+import { expectNormalized } from '../../_utils/utils';
+
+import simple from '../../_data/spacing/simple/input.word2016.html';
+import singleLine from '../../_data/spacing/single-line/input.word2016.html';
+import multiLine from '../../_data/spacing/multi-line/input.word2016.html';
+
+import simpleNormalized from '../../_data/spacing/simple/normalized.word2016.html';
+import singleLineNormalized from '../../_data/spacing/single-line/normalized.word2016.html';
+import multiLineNormalized from '../../_data/spacing/multi-line/normalized.word2016.html';
+
+describe( 'Spacing – normalization', () => {
+	let editor, pasteFromOfficePlugin;
+
+	beforeEach( () => {
+		return VirtualTestEditor
+			.create( {
+				plugins: [ Clipboard, PasteFromOffice ]
+			} )
+			.then( newEditor => {
+				editor = newEditor;
+
+				pasteFromOfficePlugin = editor.plugins.get( 'PasteFromOffice' );
+			} );
+	} );
+
+	it( 'normalizes simple single spacing', () => {
+		expectNormalized( pasteFromOfficePlugin._normalizeWordInput( simple, editor ), simpleNormalized );
+	} );
+
+	it( 'normalizes multiple spacing in a single line', () => {
+		expectNormalized( pasteFromOfficePlugin._normalizeWordInput( singleLine, editor ), singleLineNormalized );
+	} );
+
+	it( 'normalizes multiple spacing in multiple lines', () => {
+		expectNormalized( pasteFromOfficePlugin._normalizeWordInput( multiLine, editor ), multiLineNormalized );
+	} );
+} );