From e0cfac5b8b834ec4282fc85b888bf1a038afee07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Wr=C3=B3bel?= Date: Wed, 30 May 2018 12:04:17 +0200 Subject: [PATCH] Internal: Disabled `clipboardInput` when editor is read-only. --- src/clipboard.js | 10 ++++++---- tests/clipboard.js | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/clipboard.js b/src/clipboard.js index c9f81ba..2adb263 100644 --- a/src/clipboard.js +++ b/src/clipboard.js @@ -134,13 +134,15 @@ export default class Clipboard extends Plugin { // The clipboard paste pipeline. - this.listenTo( viewDocument, 'clipboardInput', ( evt, data ) => { - // Pasting and dropping is disabled when editor is read-only. - // See: https://github.com/ckeditor/ckeditor5-clipboard/issues/26. + // Pasting and dropping is disabled when editor is read-only. + // See: https://github.com/ckeditor/ckeditor5-clipboard/issues/26. + this.listenTo( viewDocument, 'clipboardInput', evt => { if ( editor.isReadOnly ) { - return; + evt.stop(); } + }, { priority: 'highest' } ); + this.listenTo( viewDocument, 'clipboardInput', ( evt, data ) => { const dataTransfer = data.dataTransfer; let content = ''; diff --git a/tests/clipboard.js b/tests/clipboard.js index 1eae71c..5c6254f 100644 --- a/tests/clipboard.js +++ b/tests/clipboard.js @@ -194,6 +194,29 @@ describe( 'Clipboard feature', () => { sinon.assert.notCalled( spy ); } ); + it( 'stops `clipboardInput` event on highest priority when editor is read-only', () => { + const dataTransferMock = createDataTransfer( { 'text/html': '

x

', 'text/plain': 'y' } ); + const spy = sinon.spy(); + + viewDocument.on( 'clipboardInput', spy, { priority: 'high' } ); + + editor.isReadOnly = true; + + viewDocument.fire( 'clipboardInput', { + dataTransfer: dataTransferMock + } ); + + sinon.assert.notCalled( spy ); + + editor.isReadOnly = false; + + viewDocument.fire( 'clipboardInput', { + dataTransfer: dataTransferMock + } ); + + sinon.assert.calledOnce( spy ); + } ); + it( 'does not insert content if the whole content was invalid', () => { // Whole content is invalid. Even though there is "view" content, the "model" content would be empty. // Do not insert content in this case.