From 7f01cbd018d8bff7791a45191a242e0343ff343e Mon Sep 17 00:00:00 2001 From: Mateusz Baginski Date: Thu, 2 Jan 2025 14:44:04 +0100 Subject: [PATCH] Add `isExternal` flag tests. --- .../tests/clipboardpipeline.js | 70 ++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/packages/ckeditor5-clipboard/tests/clipboardpipeline.js b/packages/ckeditor5-clipboard/tests/clipboardpipeline.js index 9d9153872eb..264774a945d 100644 --- a/packages/ckeditor5-clipboard/tests/clipboardpipeline.js +++ b/packages/ckeditor5-clipboard/tests/clipboardpipeline.js @@ -462,10 +462,78 @@ describe( 'ClipboardPipeline feature', () => { expect( spy.callCount ).to.equal( 1 ); } ); + describe( 'isExternal flag', () => { + it( 'should be set to true when pasting content from outside the editor', () => { + const dataTransferMock = createDataTransfer( { 'text/html': '

external content

' } ); + const inputTransformationSpy = sinon.spy(); + + clipboardPlugin.on( 'inputTransformation', ( evt, data ) => { + inputTransformationSpy( data.isExternal ); + } ); + + viewDocument.fire( 'paste', { + dataTransfer: dataTransferMock, + preventDefault: () => {}, + stopPropagation: () => {} + } ); + + sinon.assert.calledWith( inputTransformationSpy, true ); + } ); + + it( 'should be set to false when pasting content copied from the same editor', () => { + const spy = sinon.spy(); + + setModelData( editor.model, 'f[oo]bar' ); + + // Copy selected content. + const dataTransferMock = createDataTransfer(); + + viewDocument.fire( 'copy', { + dataTransfer: dataTransferMock, + preventDefault: () => {} + } ); + + clipboardPlugin.on( 'inputTransformation', ( evt, data ) => { + spy( data.isExternal ); + } ); + + // Paste the copied content. + viewDocument.fire( 'paste', { + dataTransfer: dataTransferMock, + preventDefault: () => {}, + stopPropagation: () => {} + } ); + + sinon.assert.calledWith( spy, false ); + } ); + + it( 'should be propagated to contentInsertion event', () => { + const dataTransferMock = createDataTransfer( { 'text/html': '

external content

' } ); + const contentInsertionSpy = sinon.spy(); + + clipboardPlugin.on( 'contentInsertion', ( evt, data ) => { + contentInsertionSpy( data.isExternal ); + } ); + + viewDocument.fire( 'paste', { + dataTransfer: dataTransferMock, + preventDefault: () => {}, + stopPropagation: () => {} + } ); + + sinon.assert.calledWith( contentInsertionSpy, true ); + } ); + } ); + function createDataTransfer( data ) { + const state = Object.create( data || {} ); + return { getData( type ) { - return data[ type ]; + return state[ type ]; + }, + setData( type, newData ) { + state[ type ] = newData; } }; }