Explorar el Código

Add unit test from Firefox source, rename test case, simplify getInputType method, remove dataSource from autoamtic tests which was confusing.

Mateusz Samsel hace 6 años
padre
commit
cd5c25eca1

+ 9 - 11
packages/ckeditor5-paste-from-office/src/pastefromoffice.js

@@ -120,17 +120,15 @@ export default class PasteFromOffice extends Plugin {
 	 * @return {String|null} type of app which is source of a data or null
 	 */
 	static _getInputType( html ) {
-		if ( html ) {
-			if (
-				/<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i.test( html ) ||
-				/xmlns:o="urn:schemas-microsoft-com/i.test( html )
-			) {
-				// Microsoft Word detection
-				return 'msword';
-			} else if ( /id=("|')docs-internal-guid-[-0-9a-f]+("|')/.test( html ) ) {
-				// Google Docs detection
-				return 'gdocs';
-			}
+		if (
+			/<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i.test( html ) ||
+			/xmlns:o="urn:schemas-microsoft-com/i.test( html )
+		) {
+			// Microsoft Word detection
+			return 'msword';
+		} else if ( /id=("|')docs-internal-guid-[-0-9a-f]+("|')/.test( html ) ) {
+			// Google Docs detection
+			return 'gdocs';
 		}
 
 		return null;

+ 9 - 3
packages/ckeditor5-paste-from-office/tests/_data/paste-from-google-docs/index.js

@@ -4,20 +4,26 @@
  */
 
 import simpleText from './simple-text/input.html';
+import simpleTextFromFirefox from './simple-text-from-firefox/input.html';
 
 import simpleTextNormalized from './simple-text/normalized.html';
+import simpleTextFromFirefoxNormalized from './simple-text-from-firefox/normalized.html';
 
 import simpleTextModel from './simple-text/model.html';
+import simpleTextFromFirefoxModel from './simple-text-from-firefox/model.html';
 
 export const fixtures = {
 	input: {
-		simpleText
+		simpleText,
+		simpleTextFromFirefox
 	},
 	normalized: {
-		simpleText: simpleTextNormalized
+		simpleText: simpleTextNormalized,
+		simpleTextFromFirefox: simpleTextFromFirefoxNormalized
 	},
 	model: {
-		simpleText: simpleTextModel
+		simpleText: simpleTextModel,
+		simpleTextFromFirefox: simpleTextFromFirefoxModel
 	}
 };
 

+ 1 - 0
packages/ckeditor5-paste-from-office/tests/_data/paste-from-google-docs/simple-text-from-firefox/input.html

@@ -0,0 +1 @@
+<meta charset="utf-8"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;" id="docs-internal-guid-e4b9bad6-7fff-c086-3135-e187abe577a0"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Hello world</span></p>

+ 1 - 0
packages/ckeditor5-paste-from-office/tests/_data/paste-from-google-docs/simple-text-from-firefox/model.html

@@ -0,0 +1 @@
+<paragraph>Hello world</paragraph>

+ 1 - 0
packages/ckeditor5-paste-from-office/tests/_data/paste-from-google-docs/simple-text-from-firefox/normalized.html

@@ -0,0 +1 @@
+<p dir="ltr" id="docs-internal-guid-e4b9bad6-7fff-c086-3135-e187abe577a0" style="line-height:1.38;margin-bottom:0pt;margin-top:0pt"><span style="background-color:transparent;color:#000000;font-family:Arial;font-size:11pt;font-style:normal;font-variant:normal;font-weight:400;text-decoration:none;vertical-align:baseline;white-space:pre-wrap">Hello world</span></p>

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 1 - 0
packages/ckeditor5-paste-from-office/tests/_data/paste-from-google-docs/simple-text-from-firefox/simple-text-from-firefox.html


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

@@ -9,7 +9,7 @@ import { fixtures as image, browserFixtures as imageBrowser } from '../_data/ima
 import { fixtures as link, browserFixtures as linkBrowser } from '../_data/link/index.js';
 import { fixtures as list, browserFixtures as listBrowser } from '../_data/list/index.js';
 import { fixtures as spacing, browserFixtures as spacingBrowser } from '../_data/spacing/index.js';
-import { fixtures as simpleText, browserFixtures as simpleTextBrowser } from '../_data/paste-from-google-docs/index';
+import { fixtures as googleDocs, browserFixtures as googleDocsBrowser } from '../_data/paste-from-google-docs/index';
 
 // Generic fixtures.
 export const fixtures = {
@@ -18,7 +18,7 @@ export const fixtures = {
 	link,
 	list,
 	spacing,
-	simpleText
+	'google-docs': googleDocs
 };
 
 // Browser specific fixtures.
@@ -28,5 +28,5 @@ export const browserFixtures = {
 	link: linkBrowser,
 	list: listBrowser,
 	spacing: spacingBrowser,
-	simpleTextBrowser
+	'google-docs': googleDocsBrowser
 };

+ 7 - 14
packages/ckeditor5-paste-from-office/tests/_utils/utils.js

@@ -45,7 +45,6 @@ export function createDataTransfer( data ) {
  * @param {Object} config
  * @param {String} config.type Type of tests to generate, could be 'normalization' or 'integration'.
  * @param {String} config.input Name of the fixtures group. Usually stored in `/tests/_data/groupname/`.
- * @param {String} config.dataSource Name of the office app, which was used to generate data.
  * @param {Array.<String>} config.browsers List of all browsers for which to generate tests.
  * @param {Object} [config.editorConfig] Editor config which is passed to editor `create()` method.
  * @param {Object} [config.skip] List of fixtures for any browser to skip. The supported format is:
@@ -59,10 +58,6 @@ export function generateTests( config ) {
 		throw new Error( `Invalid tests type - \`config.type\`: '${ config.type }'.` );
 	}
 
-	if ( !config.dataSource ) {
-		throw new Error( 'No `config.dataSource` option provided.' );
-	}
-
 	if ( !config.input ) {
 		throw new Error( 'No `config.input` option provided.' );
 	}
@@ -75,18 +70,16 @@ export function generateTests( config ) {
 	const generateSuiteFn = config.type === 'normalization' ? generateNormalizationTests : generateIntegrationTests;
 
 	describe( config.type, () => {
-		describe( config.dataSource, () => {
-			describe( config.input, () => {
-				const editorConfig = config.editorConfig || {};
+		describe( config.input, () => {
+			const editorConfig = config.editorConfig || {};
 
-				for ( const group of Object.keys( groups ) ) {
-					const skip = config.skip && config.skip[ group ] ? config.skip[ group ] : [];
+			for ( const group of Object.keys( groups ) ) {
+				const skip = config.skip && config.skip[ group ] ? config.skip[ group ] : [];
 
-					if ( groups[ group ] ) {
-						generateSuiteFn( group, groups[ group ], editorConfig, skip );
-					}
+				if ( groups[ group ] ) {
+					generateSuiteFn( group, groups[ group ], editorConfig, skip );
 				}
-			} );
+			}
 		} );
 	} );
 }

+ 1 - 7
packages/ckeditor5-paste-from-office/tests/data/integration.js

@@ -26,7 +26,6 @@ describe( 'Paste from Office - automatic', () => {
 	generateTests( {
 		input: 'basic-styles',
 		type: 'integration',
-		dataSource: 'MS Word',
 		browsers,
 		editorConfig: {
 			plugins: [ Clipboard, Paragraph, Heading, Bold, Italic, Underline, Strikethrough, PasteFromOffice ]
@@ -39,7 +38,6 @@ describe( 'Paste from Office - automatic', () => {
 	generateTests( {
 		input: 'image',
 		type: 'integration',
-		dataSource: 'MS Word',
 		browsers,
 		editorConfig: {
 			plugins: [ Clipboard, Paragraph, Image, Table, PasteFromOffice ]
@@ -55,7 +53,6 @@ describe( 'Paste from Office - automatic', () => {
 	generateTests( {
 		input: 'link',
 		type: 'integration',
-		dataSource: 'MS Word',
 		browsers,
 		editorConfig: {
 			plugins: [ Clipboard, Paragraph, Heading, Bold, Link, ShiftEnter, PasteFromOffice ]
@@ -68,7 +65,6 @@ describe( 'Paste from Office - automatic', () => {
 	generateTests( {
 		input: 'list',
 		type: 'integration',
-		dataSource: 'MS Word',
 		browsers,
 		editorConfig: {
 			plugins: [ Clipboard, Paragraph, Heading, Bold, Italic, Underline, Link, List, PasteFromOffice ]
@@ -81,7 +77,6 @@ describe( 'Paste from Office - automatic', () => {
 	generateTests( {
 		input: 'spacing',
 		type: 'integration',
-		dataSource: 'MS Word',
 		browsers,
 		editorConfig: {
 			plugins: [ Clipboard, Paragraph, Bold, Italic, Underline, PasteFromOffice ]
@@ -89,9 +84,8 @@ describe( 'Paste from Office - automatic', () => {
 	} );
 
 	generateTests( {
-		input: 'simpleText',
+		input: 'google-docs',
 		type: 'integration',
-		dataSource: 'Google Docs',
 		browsers,
 		editorConfig: {
 			plugins: [ Clipboard, Paragraph, Bold, PasteFromOffice ]

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

@@ -18,7 +18,6 @@ describe( 'Paste from Office - automatic', () => {
 	generateTests( {
 		input: 'basic-styles',
 		type: 'normalization',
-		dataSource: 'MS Word',
 		browsers,
 		editorConfig
 	} );
@@ -26,7 +25,6 @@ describe( 'Paste from Office - automatic', () => {
 	generateTests( {
 		input: 'image',
 		type: 'normalization',
-		dataSource: 'MS Word',
 		browsers,
 		editorConfig
 	} );
@@ -34,7 +32,6 @@ describe( 'Paste from Office - automatic', () => {
 	generateTests( {
 		input: 'link',
 		type: 'normalization',
-		dataSource: 'MS Word',
 		browsers,
 		editorConfig
 	} );
@@ -42,7 +39,6 @@ describe( 'Paste from Office - automatic', () => {
 	generateTests( {
 		input: 'list',
 		type: 'normalization',
-		dataSource: 'MS Word',
 		browsers,
 		editorConfig
 	} );
@@ -50,15 +46,13 @@ describe( 'Paste from Office - automatic', () => {
 	generateTests( {
 		input: 'spacing',
 		type: 'normalization',
-		dataSource: 'MS Word',
 		browsers,
 		editorConfig
 	} );
 
 	generateTests( {
-		input: 'simpleText',
+		input: 'google-docs',
 		type: 'normalization',
-		dataSource: 'Google Docs',
 		browsers,
 		editorConfig
 	} );

+ 70 - 0
packages/ckeditor5-paste-from-office/tests/pastefromoffice.js

@@ -7,6 +7,7 @@ import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
 import DocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
 
 import PasteFromOffice from '../src/pastefromoffice';
 import { createDataTransfer } from './_utils/utils';
@@ -32,6 +33,10 @@ describe( 'Paste from Office plugin', () => {
 				} );
 		} );
 
+		afterEach( () => {
+			editor.destroy();
+		} );
+
 		it( 'runs normalizations if Word meta tag detected #1', () => {
 			const dataTransfer = createDataTransfer( {
 				'text/html': '<meta name=Generator content="Microsoft Word 15">'
@@ -88,4 +93,69 @@ describe( 'Paste from Office plugin', () => {
 			expect( normalizeSpy.calledOnce ).to.true;
 		} );
 	} );
+
+	describe( '_normalizeGoogleDocsInput()', () => {
+		before( () => {
+			content = new DocumentFragment();
+		} );
+
+		beforeEach( () => {
+			return VirtualTestEditor
+				.create( {
+					plugins: [ Clipboard, PasteFromOffice ]
+				} )
+				.then( newEditor => {
+					editor = newEditor;
+					normalizeSpy = testUtils.sinon.spy( PasteFromOffice, '_normalizeGoogleDocsInput' );
+				} );
+		} );
+
+		afterEach( () => {
+			editor.destroy();
+		} );
+
+		it( 'runs normalizations if Google docs meta tag detected #1', () => {
+			const fakeClipboardData = '<meta charset=\'utf-8\'><meta charset="utf-8"><b style="font-weight:normal;"' +
+				' id="docs-internal-guid-45309eee-7fff-33a3-6dbd-a7e55da535b5"></b>';
+			const dataTransfer = createDataTransfer( {
+				'text/html': fakeClipboardData
+			} );
+			const htmlDataProcessor = new HtmlDataProcessor();
+			const content = htmlDataProcessor.toView( fakeClipboardData );
+
+			editor.plugins.get( 'Clipboard' ).fire( 'inputTransformation', { content, dataTransfer } );
+
+			expect( normalizeSpy.calledOnce ).to.true;
+		} );
+	} );
+
+	describe( '_getInputType()', () => {
+		[
+			{
+				input: '<html><head><meta name="Generator"  content=Microsoft Word 15></head></html>',
+				output: 'msword'
+			},
+			{
+				input: '<meta name=Generator content="Microsoft Word 15"></meta>',
+				output: 'msword'
+			},
+			{
+				input: '<meta name=Generator content="Other"></meta>',
+				output: null
+			},
+			{
+				input: '<meta charset=\'utf-8\'><meta charset="utf-8"><b style="font-weight:normal;"' +
+					' id="docs-internal-guid-45309eee-7fff-33a3-6dbd-a7e55da535b5"></b>',
+				output: 'gdocs'
+			}
+		].forEach( ( value, index ) => {
+			it( `should return proper input type for test case: #${ index }.`, () => {
+				if ( value.output ) {
+					expect( PasteFromOffice._getInputType( value.input ) ).to.equal( value.output );
+				} else {
+					expect( PasteFromOffice._getInputType( value.input ) ).to.be.null;
+				}
+			} );
+		} );
+	} );
 } );