浏览代码

Make interaace from the normalizer. Apply interface to msword and google normalizers. Fix tests.

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

+ 0 - 157
packages/ckeditor5-paste-from-office/src/contentnormalizer.js

@@ -1,157 +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/contentnormalizer
- */
-
-import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
-
-/**
- * Content Normalizer class provides a mechanism to transform input data send through
- * an {@link module:clipboard/clipboard~Clipboard#event:inputTransformation inputTransformation event}. It fixes an input content,
- * which has a source in applications like: MS Word, Google Docs, etc. These applications generate content which frequently
- * is an invalid HTML. Content normalizers transform it, what later might be properly upcast to {@link module:engine/model/model~Model}.
- *
- * Content Normalizers are registered by {@link module:paste-from-office/pastefromoffice~PasteFromOffice} plugin. Each instance is
- * initialized with an activation trigger. Activation trigger is a function which gets content of `text/html` dataTransfer (String) and
- * returns `true` or `false`. Based on this result normalizer applies filters to given data.
- *
- * {@link module:paste-from-office/contentnormalizer~FilterFunction Filters} are function, which are run sequentially, as they were added.
- * Each filter gets data transformed by the previous one.
- *
- * Example definition:
- *
- * 	const normalizer = new ContentNormalizer( contentHtml =>
- * 		contentHtml.includes( 'docs-internal-guid' )
- * 	);
- *
- * 	normalizer.addFilter( ( { data, documentFragment, writer } ) => {
- * 		// filter's content, which transforms data in clipboard
- * 	} );
- *
- * Normalizers are stored inside Paste from Office plugin and are run on
- * {@link module:clipboard/clipboard~Clipboard#event:inputTransformation inputTransformation event}. Below example is simplified and show
- * how to call normalizer directly on clipboard event, what has happen inside
- * {@link module:paste-from-office/pastefromoffice~PasteFromOffice} plugin.
- *
- * 	editor.plugins.get( 'Clipboard' ).on( 'inputTransformation', ( evt, data ) => {
- * 		normalizer.transform( data );
- * 	} );
- *
- * @class
- */
-export default class ContentNormalizer {
-	/**
-	 * Initialize Content Normalizer.
-	 *
-	 * @param {Function} activationTrigger The function which checks for what content should be applied this normalizer.
-	 * It takes an HTML string from the `text/html` dataTransfer as an argument and have to return a boolean value
-	 */
-	constructor( activationTrigger ) {
-		/**
-		 * Keeps a reference to the activation trigger function. The function is used to check if current Content Normalizer instance
-		 * should be applied for given input data. Check is made during the {@link #transform}.
-		 *
-		 * @private
-		 * @type {Function}
-		 */
-		this._activationTrigger = activationTrigger;
-
-		/**
-		 * Keeps a reference to registered filters with {@link #addFilter} method.
-		 *
-		 * @private
-		 * @type {Set}
-		 */
-		this._filters = new Set();
-	}
-
-	/**
-	 * Method checks if passed data should have applied {@link #_filters} registerd in this Content Normalizer.
-	 * If yes, then data are transformed and marked with a flag `isTransformedWithPasteFromOffice=true`.
-	 * In other case data are not modified.
-	 *
-	 * Please notice that presence of `isTransformedWithPasteFromOffice` flag in input data prevent transformation.
-	 * This forbid of running the same normalizer twice or running multiple normalizers over the same data.
-	 *
-	 * @param data input data object it should preserve structure defined in
-	 * {@link module:clipboard/clipboard~Clipboard#event:inputTransformation Clipboard#inputTransformation event}.
-	 */
-	transform( data ) {
-		const html = data.dataTransfer && data.dataTransfer.getData( 'text/html' );
-		const dataReadFirstTime = data.isTransformedWithPasteFromOffice === undefined;
-		const hasHtmlData = !!html;
-
-		if ( hasHtmlData && dataReadFirstTime && this._activationTrigger( html ) ) {
-			this._applyFilters( data );
-			data.isTransformedWithPasteFromOffice = true;
-		}
-	}
-
-	/**
-	 * Adds filter function to Content Normalizer.
-	 * Function is called with configuration object where `data` key keeps reference to input data obtained from
-	 * {@link module:clipboard/clipboard~Clipboard#event:inputTransformation Clipboard#inputTransformation event}
-	 *
-	 * See also: {@link module:paste-from-office/contentnormalizer~FilterFunction}
-	 *
-	 * @param {module:paste-from-office/contentnormalizer~FilterFunction} filterFn
-	 */
-	addFilter( filterFn ) {
-		this._filters.add( filterFn );
-	}
-
-	/**
-	 * Applies filters stored in {@link #_filters} to currently processed data.
-	 *
-	 * @private
-	 * @param {Object} data input data object it should preserve structure defined in
-	 * {@link module:clipboard/clipboard~Clipboard#event:inputTransformation Clipboard#inputTransformation event}.
-	 */
-	_applyFilters( data ) {
-		const writer = new UpcastWriter();
-		const documentFragment = data.content;
-
-		for ( const filter of this._filters ) {
-			filter( { data, documentFragment, writer } );
-		}
-	}
-}
-
-/**
- * Filter function which is used to transform data of
- * {@link module:clipboard/clipboard~Clipboard#event:inputTransformation Clipboard#inputTransformation event}.
- *
- * Filters are used by {@link module:paste-from-office/contentnormalizer~ContentNormalizer}.
- *
- * Examples:
- *
- * 	function removeBoldTagWrapper( { documentFragment, writer } ) {
- * 		for ( const childWithWrapper of documentFragment.getChildren() ) {
- * 			if ( childWithWrapper.is( 'b' ) && childWithWrapper.getStyle( 'font-weight' ) === 'normal' ) {
- * 				const childIndex = documentFragment.getChildIndex( childWithWrapper );
- * 				const removedElement = writer.remove( childWithWrapper )[ 0 ];
- *
- * 				writer.insertChild( childIndex, removedElement.getChildren(), documentFragment );
- * 			}
- * 		}
- * 	}
- *
- * 	function transformWordContent( { data } ) {
- * 		const html = data.dataTransfer.getData( 'text/html' );
- *
- * 		data.content = _normalizeWordInput( html, data.dataTransfer );
- * 	}
- *
- * @callback module:paste-from-office/contentnormalizer~FilterFunction
- * @param {Object} config
- * @param {Object} config.data input data object it should preserve structure defined in
- * {@link module:clipboard/clipboard~Clipboard#event:inputTransformation Clipboard#inputTransformation event}.
- * @param {module:engine/view/upcastwriter~UpcastWriter} config.writer upcast writer which can be used to manipulate
- * with document fragment.
- * @param {module:engine/view/documentfragment~DocumentFragment} config.documentFragment the `data.content` obtained from
- * {@link module:clipboard/clipboard~Clipboard#event:inputTransformation Clipboard#inputTransformation event}
- */

+ 39 - 0
packages/ckeditor5-paste-from-office/src/normalizer.jsdoc

@@ -0,0 +1,39 @@
+/**
+ * @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/normalizer
+ */
+
+/**
+ * Normalizer interface is description of a mechanism which transforms input data send through
+ * an {@link module:clipboard/clipboard~Clipboard#event:inputTransformation inputTransformation event}. Input content is transformed
+ * for data came from office-like applications, for example, : MS Word, Google Docs, etc. These applications generate content,
+ * which frequently has an invalid HTML syntax. Normalizers detects environment specific quirks and transform it to proper HTML syntax,
+ * what later might be properly upcast to the {@link module:engine/model/model~Model}.
+ *
+ * Content Normalizers are registered by {@link module:paste-from-office/pastefromoffice~PasteFromOffice} plugin. Each instance is
+ * initialized with an activation trigger. Activation trigger is a function which gets content of `text/html` dataTransfer (String) and
+ * returns `true` or `false`. Based on this result normalizer applies filters to given data.
+ *
+ * @interface Normalizer
+ */
+
+/**
+ * Method determines if current normalizer should be run for given content. It takes an HTML string from `data.dataTransfer` as an argument
+ * and it should return a boolean value.
+ *
+ * @method #isActive
+ * @param {String} htmlString full content of `dataTransfer.getData( 'text/html' )`
+ * @returns {Boolean}
+ */
+
+/**
+ * Method applies normalization to given data.
+ *
+ * @method #exec
+ * @param {Object} data object obtained from
+ * {@link module:clipboard/clipboard~Clipboard#event:inputTransformation inputTransformation event}
+ */

+ 21 - 11
packages/ckeditor5-paste-from-office/src/normalizers/googledocs.js

@@ -7,21 +7,31 @@
  * @module paste-from-office/normalizer
  */
 
-import ContentNormalizer from '../contentnormalizer';
 import { removeBoldTagWrapper } from '../filters/common';
+import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
 
 /**
- * {@link module:paste-from-office/contentnormalizer~ContentNormalizer} instance dedicated to transforming data obtained from Google Docs.
- * It stores filters which fix quirks detected in Google Docs content.
+ * Normalizer fixing HTML syntax obtained from Google Docs.
  *
- * @type {module:paste-from-office/contentnormalizer~ContentNormalizer}
+ * @implements module:paste-from-office/normalizer~Normalizer
  */
-export const googleDocsNormalizer = ( () => {
-	const normalizer = new ContentNormalizer( contentString =>
-		/id=("|')docs-internal-guid-[-0-9a-f]+("|')/.test( contentString )
-	);
+export default class GoogleDocsNormalizer {
+	/**
+	 * @inheritDoc
+	 */
+	isActive( htmlString ) {
+		return /id=("|')docs-internal-guid-[-0-9a-f]+("|')/.test( htmlString );
+	}
 
-	normalizer.addFilter( removeBoldTagWrapper );
+	/**
+	 * @inheritDoc
+	 */
+	exec( data ) {
+		const writer = new UpcastWriter();
 
-	return normalizer;
-} )();
+		removeBoldTagWrapper( {
+			writer,
+			documentFragment: data.content
+		} );
+	}
+}

+ 19 - 17
packages/ckeditor5-paste-from-office/src/normalizers/msword.js

@@ -7,31 +7,33 @@
  * @module paste-from-office/normalizer
  */
 
-import ContentNormalizer from '../contentnormalizer';
 import { parseHtml } from '../filters/parse';
 import { transformListItemLikeElementsIntoLists } from '../filters/list';
 import { replaceImagesSourceWithBase64 } from '../filters/image';
 
 /**
- * {@link module:paste-from-office/contentnormalizer~ContentNormalizer} instance dedicated to transforming data obtained from MS Word.
- * It stores filters which fix quirks detected in MS Word content.
+ * Normalizer fixing HTML syntax obtained from Microsoft Word documents.
  *
- * @type {module:paste-from-office/contentnormalizer~ContentNormalizer}
+ * @implements module:paste-from-office/normalizer~Normalizer
  */
-export const mswordNormalizer = ( () => {
-	const normalizer = new ContentNormalizer( contentString =>
-		/<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i.test( contentString ) ||
-		/xmlns:o="urn:schemas-microsoft-com/i.test( contentString )
-	);
-
-	normalizer.addFilter( ( { data } ) => {
+export default class MSWordNormalizer {
+	/**
+	 * @inheritDoc
+	 */
+	isActive( htmlString ) {
+		return /<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i.test( htmlString ) ||
+				/xmlns:o="urn:schemas-microsoft-com/i.test( htmlString );
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	exec( data ) {
 		const html = data.dataTransfer.getData( 'text/html' );
 
-		data.content = _normalizeWordInput( html, data.dataTransfer );
-	} );
-
-	return normalizer;
-} )();
+		data.content = normalizeWordInput( html, data.dataTransfer );
+	}
+}
 
 //
 // Normalizes input pasted from Word to format suitable for editor {@link module:engine/model/model~Model}.
@@ -41,7 +43,7 @@ export const mswordNormalizer = ( () => {
 // @param {module:clipboard/datatransfer~DataTransfer} dataTransfer Data transfer instance.
 // @returns {module:engine/view/documentfragment~DocumentFragment} Normalized input.
 //
-function _normalizeWordInput( input, dataTransfer ) {
+function normalizeWordInput( input, dataTransfer ) {
 	const { body, stylesString } = parseHtml( input );
 
 	transformListItemLikeElementsIntoLists( body, stylesString );

+ 19 - 10
packages/ckeditor5-paste-from-office/src/pastefromoffice.js

@@ -9,8 +9,8 @@
 
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 
-import { googleDocsNormalizer } from './normalizers/googledocs';
-import { mswordNormalizer } from './normalizers/msword';
+import GoogleDocsNormalizer from './normalizers/googledocs';
+import MSWordNormalizer from './normalizers/msword';
 import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
 
 /**
@@ -19,10 +19,10 @@ import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  * This plugin handles content pasted from Office apps and transforms it (if necessary)
  * to a valid structure which can then be understood by the editor features.
  *
- * Transformation is made by a set of predefined {@link module:paste-from-office/contentnormalizer~ContentNormalizer}.
+ * Transformation is made by a set of predefined {@link module:paste-from-office/normalizer~Normalizer}.
  * Currently, there are included followed normalizers:
- *   * {@link module:paste-from-office/normalizer.mswordNormalizer MS Word normalizer}
- *   * {@link module:paste-from-office/normalizer.googleDocsNormalizer Google Docs normalizer}
+ *   * {@link module:paste-from-office/normalizer~MSWordNormalizer MS Word normalizer}
+ *   * {@link module:paste-from-office/normalizer~GoogleDocsNormalizer Google Docs normalizer}
  *
  * For more information about this feature check the {@glink api/paste-from-office package page}.
  *
@@ -48,16 +48,25 @@ export default class PasteFromOffice extends Plugin {
 	 */
 	init() {
 		const editor = this.editor;
-		const normalizers = new Set();
+		const normalizers = [];
 
-		normalizers.add( mswordNormalizer );
-		normalizers.add( googleDocsNormalizer );
+		normalizers.push( new MSWordNormalizer() );
+		normalizers.push( new GoogleDocsNormalizer() );
 
 		editor.plugins.get( 'Clipboard' ).on(
 			'inputTransformation',
 			( evt, data ) => {
-				for ( const normalizer of normalizers ) {
-					normalizer.transform( data );
+				if ( data.isTransformedWithPasteFromOffice ) {
+					return;
+				}
+
+				const htmlString = data.dataTransfer.getData( 'text/html' );
+				const activeNormalizer = normalizers.find( normalizer => normalizer.isActive( htmlString ) );
+
+				if ( activeNormalizer ) {
+					activeNormalizer.exec( data );
+
+					data.isTransformedWithPasteFromOffice = true;
 				}
 			},
 			{ priority: 'high' }

+ 0 - 134
packages/ckeditor5-paste-from-office/tests/contentnormalizer.js

@@ -1,134 +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 testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
-import ContentNormalizer from '../src/contentnormalizer';
-import { createDataTransfer } from './_utils/utils';
-import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
-
-describe( 'ContentNormalizer', () => {
-	let normalizer, sinonTrigger;
-	const templateData = {
-		dataTransfer: createDataTransfer( {
-			'text/html': 'test data'
-		} )
-	};
-
-	testUtils.createSinonSandbox();
-
-	beforeEach( () => {
-		sinonTrigger = sinon.fake.returns( true );
-
-		normalizer = new ContentNormalizer( sinonTrigger );
-	} );
-
-	describe( 'constructor()', () => {
-		it( 'should have assigned activation trigger', () => {
-			expect( normalizer._activationTrigger ).to.be.a( 'function' );
-			expect( normalizer._activationTrigger ).to.equal( sinonTrigger );
-		} );
-	} );
-
-	describe( 'transform()', () => {
-		let data;
-
-		beforeEach( () => {
-			data = Object.assign( {}, templateData );
-		} );
-
-		describe( 'valid data', () => {
-			it( 'should mark data as transformed', () => {
-				expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
-
-				normalizer.transform( data );
-
-				expect( data.isTransformedWithPasteFromOffice ).to.be.true;
-			} );
-
-			it( 'should call for activation trigger to check input data', () => {
-				sinon.assert.notCalled( sinonTrigger );
-
-				normalizer.transform( data );
-
-				sinon.assert.calledOnce( sinonTrigger );
-				sinon.assert.calledWith( sinonTrigger, 'test data' );
-			} );
-
-			it( 'should execute filters over data', () => {
-				const filter = sinon.fake();
-				const writer = new UpcastWriter();
-				const documentFragment = writer.createDocumentFragment();
-
-				data.content = documentFragment;
-
-				normalizer.addFilter( filter );
-				normalizer.transform( data );
-
-				sinon.assert.calledOnce( filter );
-				sinon.assert.calledWithMatch( filter, {
-					documentFragment,
-					data,
-					writer: sinon.match.instanceOf( UpcastWriter )
-				} );
-			} );
-
-			it( 'should not process again already transformed data', () => {
-				const filter = sinon.fake();
-
-				// Filters should not be executed
-				data.isTransformedWithPasteFromOffice = true;
-
-				normalizer.addFilter( filter );
-				normalizer.transform( data );
-
-				sinon.assert.notCalled( filter );
-			} );
-		} );
-
-		describe( 'invalid data', () => {
-			let normalizer, sinonTrigger;
-
-			beforeEach( () => {
-				sinonTrigger = sinon.fake.returns( false );
-
-				normalizer = new ContentNormalizer( sinonTrigger );
-			} );
-
-			it( 'should not change data content', () => {
-				normalizer.transform( data );
-
-				expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
-				expect( data ).to.deep.equal( templateData );
-			} );
-
-			it( 'should not fire any filter', () => {
-				const filter = sinon.fake();
-
-				normalizer.addFilter( filter );
-				normalizer.transform( data );
-
-				expect( normalizer._filters.size ).to.equal( 1 );
-				sinon.assert.notCalled( filter );
-			} );
-		} );
-	} );
-
-	describe( 'addFilter()', () => {
-		let filter;
-
-		beforeEach( () => {
-			filter = () => {};
-
-			normalizer.addFilter( filter );
-		} );
-
-		it( 'should add filter to fullContentFilters set', () => {
-			expect( normalizer._filters.size ).to.equal( 1 );
-
-			const firstFilter = [ ...normalizer._filters ][ 0 ];
-			expect( firstFilter ).to.equal( filter );
-		} );
-	} );
-} );

+ 15 - 91
packages/ckeditor5-paste-from-office/tests/normalizers/googledocs.js

@@ -3,105 +3,29 @@
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  */
 
-import { googleDocsNormalizer as normalizer } from '../../src/normalizers/googledocs';
-import ContentNormalizer from '../../src/contentnormalizer';
-import { createDataTransfer } from '../_utils/utils';
-import DocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
-import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
+import GoogleDocsNormalizer from '../../src/normalizers/googledocs';
 
-// Functionality of the google docs normalizer is tested with autogenerated normalization tests.
+// exec() of the google docs normalizer is tested with autogenerated normalization tests.
 describe( 'PasteFromOffice/normalizers/googledocs', () => {
-	const htmlDataProcessor = new HtmlDataProcessor();
+	const normalizer = new GoogleDocsNormalizer();
 
-	it( 'should be instance of content normalizers', () => {
-		expect( normalizer ).to.be.instanceOf( ContentNormalizer );
-	} );
-
-	it( 'should mark data as processed', () => {
-		const gDocs = '<p id="docs-internal-guid-12345678-1234-1234-1234-1234567890ab"></p>';
-		const data = {
-			dataTransfer: createDataTransfer( {
-				'text/html': gDocs
-			} ),
-			content: htmlDataProcessor.toView( gDocs )
-		};
-
-		normalizer.transform( data );
-
-		expect( data.isTransformedWithPasteFromOffice ).to.be.true;
-	} );
-
-	it( 'should not mark non-google-docs data as processed', () => {
-		const data = {
-			dataTransfer: createDataTransfer( { 'text/html': 'foo bar' } )
-		};
-
-		normalizer.transform( data );
-
-		expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
-	} );
-
-	it( 'outputs view#documentFragment', () => {
-		const gDocs = '<p id="docs-internal-guid-12345678-1234-1234-1234-1234567890ab">Foo bar</p>';
-		const data = {
-			dataTransfer: createDataTransfer( {
-				'text/html': gDocs
-			} ),
-			content: htmlDataProcessor.toView( gDocs )
-		};
-
-		normalizer.transform( data );
+	describe( 'isActive()', () => {
+		describe( 'correct data set', () => {
+			it( 'should be active for google docs data', () => {
+				const gDocs = '<p id="docs-internal-guid-12345678-1234-1234-1234-1234567890ab"></p>';
 
-		expect( data.isTransformedWithPasteFromOffice ).to.be.true;
-		expect( data.content ).to.be.instanceOf( DocumentFragment );
-	} );
-
-	describe( 'activation trigger', () => {
-		describe( 'correct markup', () => {
-			[
-				{
-					'text/html': '<meta charset="utf-8"><p id="docs-internal-guid-12345678-1234-1234-1234-1234567890ab">Foo bar</p>'
-				},
-				{
-					// eslint-disable-next-line max-len
-					'text/html': '<meta charset="utf-8"><b style="font-weight:normal;" id="docs-internal-guid-30db46f5-7fff-15a1-e17c-1234567890ab"></b>'
-				}
-			].forEach( ( html, index ) => {
-				it( `should be active for markup #${ index }`, () => {
-					const data = {
-						dataTransfer: createDataTransfer( html ),
-						content: htmlDataProcessor.toView( html[ 'text/html' ] )
-					};
-
-					expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
-
-					normalizer.transform( data );
-
-					expect( data.isTransformedWithPasteFromOffice ).to.be.true;
-				} );
+				expect( normalizer.isActive( gDocs ) ).to.be.true;
 			} );
 		} );
 
-		describe( 'wrong markup', () => {
+		describe( 'wrong data set', () => {
 			[
-				{
-					'text/html': '<p id="random_id">Hello world</p>'
-				},
-				{
-					'text/html': '<meta name=Generator content="Microsoft Word 15">'
-				}
-			].forEach( ( html, index ) => {
-				it( `should be not active for wrong markup #${ index }`, () => {
-					const data = {
-						dataTransfer: createDataTransfer( html ),
-						content: htmlDataProcessor.toView( html[ 'text/html' ] )
-					};
-
-					expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
-
-					normalizer.transform( data );
-
-					expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
+				'<p>foo</p>',
+				'<meta name=Generator content="Microsoft Word 15"><p>Foo bar</p>',
+				'<meta name=Generator content="Microsoft Word 15">'
+			].forEach( ( htmlString, index ) => {
+				it( `should be inactive for: #${ index } data set`, () => {
+					expect( normalizer.isActive( htmlString ) ).to.be.false;
 				} );
 			} );
 		} );

+ 16 - 79
packages/ckeditor5-paste-from-office/tests/normalizers/msword.js

@@ -3,94 +3,31 @@
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  */
 
-import { mswordNormalizer as normalizer } from '../../src/normalizers/msword';
-import ContentNormalizer from '../../src/contentnormalizer';
-import { createDataTransfer } from '../_utils/utils';
-import DocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
+import MSWordNormalizer from '../../src/normalizers/msword';
 
-// Functionality of the msword normalizer is tested with autogenerated normalization tests.
+// `exec()` of the msword normalizer is tested with autogenerated normalization tests.
 describe( 'PasteFromOffice/normalizers/msword', () => {
-	it( 'should be instance of content normalizers', () => {
-		expect( normalizer ).to.be.instanceOf( ContentNormalizer );
-	} );
-
-	it( 'should mark data as processed', () => {
-		const data = {
-			dataTransfer: createDataTransfer( {
-				'text/html': '<meta name=Generator content="Microsoft Word 15">'
-			} )
-		};
-
-		normalizer.transform( data );
-
-		expect( data.isTransformedWithPasteFromOffice ).to.be.true;
-	} );
-
-	it( 'should not mark non-word data as processed', () => {
-		const data = {
-			dataTransfer: createDataTransfer( { 'text/html': 'foo bar' } )
-		};
-
-		normalizer.transform( data );
-
-		expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
-	} );
-
-	it( 'outputs view#documentFragment', () => {
-		const data = {
-			dataTransfer: createDataTransfer( {
-				'text/html': '<meta name=Generator content="Microsoft Word 15"><p>Foo bar</p>'
-			} )
-		};
+	const normalizer = new MSWordNormalizer();
 
-		normalizer.transform( data );
-
-		expect( data.content ).to.be.instanceOf( DocumentFragment );
-	} );
-
-	describe( 'activation trigger', () => {
-		describe( 'correct markup', () => {
+	describe( 'isActive()', () => {
+		describe( 'correct data set', () => {
 			[
-				{
-					'text/html': '<meta name=Generator content="Microsoft Word 15">'
-				},
-				{
-					'text/html': '<html><head><meta name="Generator"  content=Microsoft Word 15></head></html>'
-				}
-			].forEach( ( html, index ) => {
-				it( `should be active for markup #${ index }`, () => {
-					const data = {
-						dataTransfer: createDataTransfer( html )
-					};
-
-					expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
-
-					normalizer.transform( data );
-
-					expect( data.isTransformedWithPasteFromOffice ).to.be.true;
+				'<meta name=Generator content="Microsoft Word 15"><p>Foo bar</p>',
+				'<meta name=Generator content="Microsoft Word 15">'
+			].forEach( ( htmlString, index ) => {
+				it( `should be active for: #${ index } data set`, () => {
+					expect( normalizer.isActive( htmlString ) ).to.be.true;
 				} );
 			} );
 		} );
 
-		describe( 'wrong markup', () => {
+		describe( 'wrong data set', () => {
 			[
-				{
-					'text/html': '<meta name=Generator content="Other">'
-				},
-				{
-					'text/html': '<p id="docs-internal-guid-12345"></p>'
-				}
-			].forEach( ( html, index ) => {
-				it( `should be not active for wrong markup #${ index }`, () => {
-					const data = {
-						dataTransfer: createDataTransfer( html )
-					};
-
-					expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
-
-					normalizer.transform( data );
-
-					expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
+				'<p>foo</p>',
+				'<p id="docs-internal-guid-12345678-1234-1234-1234-1234567890ab"></p>'
+			].forEach( ( htmlString, index ) => {
+				it( `should be inactive to for: #${ index } data set`, () => {
+					expect( normalizer.isActive( htmlString ) ).to.be.false;
 				} );
 			} );
 		} );