Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

t/ckeditor5-upload/92: Fix: View event should not bubble up if the `clipboardInp... #63

Merged
merged 1 commit into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/clipboardobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import DomEventObserver from '@ckeditor/ckeditor5-engine/src/view/observer/domeventobserver';
import EventInfo from '@ckeditor/ckeditor5-utils/src/eventinfo';
import DataTransfer from './datatransfer';

/**
Expand Down Expand Up @@ -45,10 +46,19 @@ export default class ClipboardObserver extends DomEventObserver {

const targetRanges = data.dropRange ? [ data.dropRange ] : Array.from( viewDocument.selection.getRanges() );

viewDocument.fire( 'clipboardInput', {
const eventInfo = new EventInfo( viewDocument, 'clipboardInput' );

viewDocument.fire( eventInfo, {
dataTransfer: data.dataTransfer,
targetRanges
} );

// If CKEditor handled the input, do not bubble the original event any further.
// This helps external integrations recognize that fact and act accordingly.
// https://github.com/ckeditor/ckeditor5-upload/issues/92
if ( eventInfo.stop.called ) {
data.stopPropagation();
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ describe( 'Clipboard feature', () => {

viewDocument.fire( 'paste', {
dataTransfer: dataTransferMock,
stopPropagation() {},
preventDefault() {}
} );

Expand Down Expand Up @@ -286,6 +287,7 @@ describe( 'Clipboard feature', () => {

viewDocument.fire( 'paste', {
dataTransfer: dataTransferMock,
stopPropagation() {},
preventDefault() {}
} );

Expand Down
31 changes: 30 additions & 1 deletion tests/clipboardobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import DataTransfer from '../src/datatransfer';
import createViewRoot from '@ckeditor/ckeditor5-engine/tests/view/_utils/createroot';

describe( 'ClipboardObserver', () => {
let view, doc, writer, observer, root, el, range, eventSpy, preventDefaultSpy;
let view, doc, writer, observer, root, el, range, eventSpy, preventDefaultSpy, stopPropagationSpy;

beforeEach( () => {
view = new View();
Expand All @@ -34,6 +34,7 @@ describe( 'ClipboardObserver', () => {

eventSpy = sinon.spy();
preventDefaultSpy = sinon.spy();
stopPropagationSpy = sinon.spy();
} );

it( 'should define domEventType', () => {
Expand Down Expand Up @@ -224,6 +225,34 @@ describe( 'ClipboardObserver', () => {

expect( sinon.assert.callOrder( normalPrioritySpy, eventSpy ) );
} );

// https://github.com/ckeditor/ckeditor5-upload/issues/92
it( 'should stop propagation of the original event if handled by the editor', () => {
const dataTransfer = new DataTransfer( mockDomDataTransfer() );

doc.fire( 'drop', {
dataTransfer,
preventDefault: preventDefaultSpy,
stopPropagation: stopPropagationSpy,
dropRange: range
} );

sinon.assert.notCalled( stopPropagationSpy );

doc.on( 'clipboardInput', evt => {
// E.g. some feature handled the input.
evt.stop();
} );

doc.fire( 'drop', {
dataTransfer,
preventDefault: preventDefaultSpy,
stopPropagation: stopPropagationSpy,
dropRange: range
} );

sinon.assert.calledOnce( stopPropagationSpy );
} );
} );

describe( 'dragover event', () => {
Expand Down