Browse Source

Merge pull request #47 from ckeditor/t/44

Fix: Prevent processing the same `inputTransformation` event data many times.
Piotrek Koszuliński 7 years ago
parent
commit
61b25f32eb

+ 4 - 1
packages/ckeditor5-paste-from-office/src/pastefromoffice.js

@@ -41,8 +41,11 @@ export default class PasteFromOffice extends Plugin {
 		this.listenTo( editor.plugins.get( Clipboard ), 'inputTransformation', ( evt, data ) => {
 			const html = data.dataTransfer.getData( 'text/html' );
 
-			if ( isWordInput( html ) ) {
+			if ( data.pasteFromOfficeProcessed !== true && isWordInput( html ) ) {
 				data.content = this._normalizeWordInput( html, data.dataTransfer );
+
+				// Set the flag so if `inputTransformation` is re-fired, PFO will not process it again (#44).
+				data.pasteFromOfficeProcessed = true;
 			}
 		}, { priority: 'high' } );
 	}

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

@@ -60,4 +60,30 @@ describe( 'Paste from Office plugin', () => {
 
 		expect( normalizeSpy.called ).to.false;
 	} );
+
+	it( 'does not process content many times for the same `inputTransformation` event', () => {
+		const clipboard = editor.plugins.get( 'Clipboard' );
+
+		const dataTransfer = createDataTransfer( {
+			'text/html': '<html><head><meta name="Generator"  content=Microsoft Word 15></head></html>'
+		} );
+
+		let eventRefired = false;
+		clipboard.on( 'inputTransformation', ( evt, data ) => {
+			if ( !eventRefired ) {
+				eventRefired = true;
+
+				evt.stop();
+
+				clipboard.fire( 'inputTransformation', data );
+			}
+
+			expect( data.pasteFromOfficeProcessed ).to.true;
+			expect( normalizeSpy.calledOnce ).to.true;
+		}, { priority: 'low' } );
+
+		editor.plugins.get( 'Clipboard' ).fire( 'inputTransformation', { content, dataTransfer } );
+
+		expect( normalizeSpy.calledOnce ).to.true;
+	} );
 } );