Browse Source

Add tests for msword normalizer, improve content normalizer class.

Mateusz Samsel 6 năm trước cách đây
mục cha
commit
223f1aa7b1

+ 15 - 8
packages/ckeditor5-paste-from-office/src/contentnormalizer.js

@@ -9,17 +9,18 @@
 
 export default class ContentNormalizer {
 	constructor( { activationTrigger } ) {
-		this.data = null;
-
 		this.activationTrigger = activationTrigger;
 
-		this.isActive = false;
+		this._reset();
+
 		this._filters = new Set();
 		this._fullContentFilters = new Set();
 	}
 
 	setInputData( data ) {
-		const html = data.dataTransfer.getData( 'text/html' );
+		this._reset();
+
+		const html = data.dataTransfer && data.dataTransfer.getData( 'text/html' );
 		const dataReadFirstTime = data.isTransformedWithPasteFromOffice === undefined;
 		const hasHtmlData = !!html;
 
@@ -27,9 +28,6 @@ export default class ContentNormalizer {
 			this.data = data;
 			this.isActive = true;
 			this.data.isTransformedWithPasteFromOffice = false;
-		} else {
-			this.data = null;
-			this.isActive = false;
 		}
 
 		return this;
@@ -44,7 +42,11 @@ export default class ContentNormalizer {
 	}
 
 	exec() {
-		if ( this.data && !this.data.isTransformedWithPasteFromOffice ) {
+		if ( !this.isActive ) {
+			return;
+		}
+
+		if ( !this.data.isTransformedWithPasteFromOffice ) {
 			this._applyFullContentFilters();
 		}
 
@@ -53,6 +55,11 @@ export default class ContentNormalizer {
 		return this;
 	}
 
+	_reset() {
+		this.data = null;
+		this.isActive = false;
+	}
+
 	_applyFullContentFilters() {
 		if ( !this._fullContentFilters.size ) {
 			return;

+ 4 - 3
packages/ckeditor5-paste-from-office/tests/contentnormalizer.js

@@ -5,13 +5,14 @@
 
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import ContentNormalizer from '../src/contentnormalizer';
+import { createDataTransfer } from './_utils/utils';
 
 describe( 'ContentNormalizer', () => {
 	let normalizer, sinonTrigger;
 	const templateData = {
-		dataTransfer: {
-			getData: () => 'test data'
-		}
+		dataTransfer: createDataTransfer( {
+			'text/html': 'test data'
+		} )
 	};
 
 	testUtils.createSinonSandbox();

+ 98 - 0
packages/ckeditor5-paste-from-office/tests/normalizers/msword.js

@@ -0,0 +1,98 @@
+/**
+ * @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 { 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';
+
+// Functionality of the msword normalizer is tested with autogenerated normalization tests.
+describe( 'PasteFromOffice/normalizers/msword', () => {
+	afterEach( () => {
+		normalizer.setInputData( {} );
+	} );
+
+	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.setInputData( data ).exec();
+
+		expect( data.isTransformedWithPasteFromOffice ).to.be.true;
+	} );
+
+	it( 'should not mark non-word data as processed', () => {
+		const data = {
+			dataTransfer: createDataTransfer( { 'text/html': 'foo bar' } )
+		};
+
+		normalizer.setInputData( data ).exec();
+
+		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>'
+			} )
+		};
+
+		normalizer.setInputData( data ).exec();
+
+		expect( data.content ).to.be.instanceOf( DocumentFragment );
+	} );
+
+	describe( 'activation trigger', () => {
+		describe( 'correct markup', () => {
+			[
+				{
+					'text/html': '<meta name=Generator content="Microsoft Word 15">'
+				},
+				{
+					'text/html': '<html><head><meta name="Generator"  content=Microsoft Word 15></head></html>'
+				}
+			].forEach( ( data, index ) => {
+				it( `should be active for markup #${ index }`, () => {
+					expect( normalizer.isActive ).to.be.false;
+
+					normalizer.setInputData( {
+						dataTransfer: createDataTransfer( data )
+					} );
+
+					expect( normalizer.isActive ).to.be.true;
+				} );
+			} );
+		} );
+
+		describe( 'wrong markup', () => {
+			[
+				{
+					'text/html': '<meta name=Generator content="Other">'
+				},
+				{
+					'text/html': '<p id="docs-internal-guid-12345"></p>'
+				}
+			].forEach( ( data, index ) => {
+				it( `should be not active for wrong markup #${ index }`, () => {
+					expect( normalizer.isActive ).to.be.false;
+
+					normalizer.setInputData( {
+						dataTransfer: createDataTransfer( data )
+					} );
+
+					expect( normalizer.isActive ).to.be.false;
+				} );
+			} );
+		} );
+	} );
+} );