/** * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor'; import Widget from '../src/widget'; import Typing from '@ckeditor/ckeditor5-typing/src/typing'; import MouseObserver from '@ckeditor/ckeditor5-engine/src/view/observer/mouseobserver'; import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter'; import { toWidget } from '../src/utils'; import ViewContainer from '@ckeditor/ckeditor5-engine/src/view/containerelement'; import ViewEditable from '@ckeditor/ckeditor5-engine/src/view/editableelement'; import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata'; import AttributeContainer from '@ckeditor/ckeditor5-engine/src/view/attributeelement'; import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view'; import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard'; /* global document */ describe( 'Widget', () => { let editor, model, doc, viewDocument; beforeEach( () => { return VirtualTestEditor.create( { plugins: [ Widget, Typing ] } ) .then( newEditor => { editor = newEditor; model = editor.model; doc = model.document; viewDocument = editor.editing.view; model.schema.register( 'widget', { inheritAllFrom: '$block', isObject: true } ); model.schema.register( 'paragraph', { inheritAllFrom: '$block', allowIn: 'div' } ); model.schema.register( 'inline', { allowWhere: '$text', isObject: true } ); model.schema.register( 'nested', { allowIn: 'widget', isLimit: true } ); model.schema.extend( '$text', { allowIn: [ 'nested', 'editable' ] } ); model.schema.register( 'editable', { allowIn: [ 'widget', '$root' ] } ); // Image feature. model.schema.register( 'image', { allowIn: '$root', isObject: true, isBlock: true } ); // Block-quote feature. model.schema.register( 'blockQuote', { allowIn: '$root' } ); model.schema.extend( '$block', { allowIn: 'blockQuote' } ); // Div element which helps nesting elements. model.schema.register( 'div', { allowIn: [ 'blockQuote', 'div' ] } ); buildModelConverter().for( editor.editing.modelToView ) .fromElement( 'paragraph' ) .toElement( 'p' ); buildModelConverter().for( editor.editing.modelToView ) .fromElement( 'widget' ) .toElement( () => { const b = new AttributeContainer( 'b' ); const div = new ViewContainer( 'div', null, b ); return toWidget( div, { label: 'element label' } ); } ); buildModelConverter().for( editor.editing.modelToView ) .fromElement( 'inline' ) .toElement( 'figure' ); buildModelConverter().for( editor.editing.modelToView ) .fromElement( 'nested' ) .toElement( () => new ViewEditable( 'figcaption', { contenteditable: true } ) ); buildModelConverter().for( editor.editing.modelToView ) .fromElement( 'editable' ) .toElement( () => new ViewEditable( 'figcaption', { contenteditable: true } ) ); buildModelConverter().for( editor.editing.modelToView ) .fromElement( 'image' ) .toElement( 'img' ); buildModelConverter().for( editor.editing.modelToView ) .fromElement( 'blockQuote' ) .toElement( 'blockquote' ); buildModelConverter().for( editor.editing.modelToView ) .fromElement( 'div' ) .toElement( 'div' ); } ); } ); it( 'should be loaded', () => { expect( editor.plugins.get( Widget ) ).to.be.instanceOf( Widget ); } ); it( 'should add MouseObserver', () => { expect( editor.editing.view.getObserver( MouseObserver ) ).to.be.instanceof( MouseObserver ); } ); it( 'should create selection over clicked widget', () => { setModelData( model, '[]' ); const viewDiv = viewDocument.getRoot().getChild( 0 ); const domEventDataMock = { target: viewDiv, preventDefault: sinon.spy() }; viewDocument.fire( 'mousedown', domEventDataMock ); expect( getModelData( model ) ).to.equal( '[]' ); sinon.assert.calledOnce( domEventDataMock.preventDefault ); } ); it( 'should create selection when clicked in nested element', () => { setModelData( model, '[]' ); const viewDiv = viewDocument.getRoot().getChild( 0 ); const viewB = viewDiv.getChild( 0 ); const domEventDataMock = { target: viewB, preventDefault: sinon.spy() }; viewDocument.fire( 'mousedown', domEventDataMock ); expect( getModelData( model ) ).to.equal( '[]' ); sinon.assert.calledOnce( domEventDataMock.preventDefault ); } ); it( 'should do nothing if clicked inside nested editable', () => { setModelData( model, '[]foo bar' ); const viewDiv = viewDocument.getRoot().getChild( 0 ); const viewFigcaption = viewDiv.getChild( 0 ); const domEventDataMock = { target: viewFigcaption, preventDefault: sinon.spy() }; viewDocument.fire( 'mousedown', domEventDataMock ); sinon.assert.notCalled( domEventDataMock.preventDefault ); } ); it( 'should do nothing if clicked in non-widget element', () => { setModelData( model, '[]foo bar' ); const viewP = viewDocument.getRoot().getChild( 0 ); const domEventDataMock = { target: viewP, preventDefault: sinon.spy() }; viewDocument.focus(); viewDocument.fire( 'mousedown', domEventDataMock ); expect( getModelData( model ) ).to.equal( '[]foo bar' ); sinon.assert.notCalled( domEventDataMock.preventDefault ); } ); it( 'should not focus editable if already is focused', () => { setModelData( model, '' ); const widget = viewDocument.getRoot().getChild( 0 ); const domEventDataMock = { target: widget, preventDefault: sinon.spy() }; const focusSpy = sinon.spy( viewDocument, 'focus' ); viewDocument.isFocused = true; viewDocument.fire( 'mousedown', domEventDataMock ); sinon.assert.calledOnce( domEventDataMock.preventDefault ); sinon.assert.notCalled( focusSpy ); expect( getModelData( model ) ).to.equal( '[]' ); } ); it( 'should apply fake view selection if model selection is on widget element', () => { setModelData( model, '[foo bar]' ); expect( getViewData( viewDocument ) ).to.equal( '[
foo bar
]' ); expect( viewDocument.selection.isFake ).to.be.true; } ); it( 'should use element\'s label to set fake selection if one is provided', () => { setModelData( model, '[foo bar]' ); expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'element label' ); } ); it( 'should add selected class when no only a widget is selected', () => { setModelData( model, '[foo]' ); expect( viewDocument.selection.isFake ).to.be.false; expect( getViewData( viewDocument ) ).to.equal( '[' + '

foo

' + '
' + '
' + ']' ); } ); it( 'fake selection should be empty if widget is not selected', () => { setModelData( model, 'foofoo bar' ); expect( viewDocument.selection.fakeSelectionLabel ).to.equal( '' ); } ); it( 'should toggle selected class', () => { setModelData( model, 'foo[foo]' ); expect( getViewData( viewDocument ) ).to.equal( '

foo

[
foo
]' ); model.change( () => { doc.selection.removeAllRanges(); } ); expect( getViewData( viewDocument ) ).to.equal( '

{}foo

foo
' ); } ); it( 'should do nothing when selection is placed in other editable', () => { setModelData( model, 'foo bar[baz]' ); expect( getViewData( viewDocument ) ).to.equal( '
' + '
foo bar
' + '' + '
' + '
{baz}
' ); } ); describe( 'keys handling', () => { describe( 'arrows', () => { test( 'should move selection forward from selected object - right arrow', '[]foo', keyCodes.arrowright, '[]foo' ); test( 'should move selection forward from selected object - down arrow', '[]foo', keyCodes.arrowdown, '[]foo' ); test( 'should move selection backward from selected object - left arrow', 'foo[]', keyCodes.arrowleft, 'foo[]' ); test( 'should move selection backward from selected object - up arrow', 'foo[]', keyCodes.arrowup, 'foo[]' ); test( 'should move selection to next widget - right arrow', '[]', keyCodes.arrowright, '[]' ); test( 'should move selection to next widget - down arrow', '[]', keyCodes.arrowdown, '[]' ); test( 'should move selection to previous widget - left arrow', '[]', keyCodes.arrowleft, '[]' ); test( 'should move selection to previous widget - up arrow', '[]', keyCodes.arrowup, '[]' ); test( 'should do nothing on non-collapsed selection next to object - right arrow', 'ba[r]', keyCodes.arrowright, 'ba[r]' ); test( 'should do nothing on non-collapsed selection next to object - down arrow', 'ba[r]', keyCodes.arrowdown, 'ba[r]' ); test( 'should do nothing on non-collapsed selection next to object - left arrow', '[b]ar', keyCodes.arrowleft, '[b]ar' ); test( 'should do nothing on non-collapsed selection next to object - up arrow', '[b]ar', keyCodes.arrowup, '[b]ar' ); test( 'should not move selection if there is no correct location - right arrow', 'foo[]', keyCodes.arrowright, 'foo[]' ); test( 'should not move selection if there is no correct location - down arrow', 'foo[]', keyCodes.arrowdown, 'foo[]' ); test( 'should not move selection if there is no correct location - left arrow', '[]foo', keyCodes.arrowleft, '[]foo' ); test( 'should not move selection if there is no correct location - up arrow', '[]foo', keyCodes.arrowup, '[]foo' ); test( 'should do nothing if other key is pressed', '[]foo', // Use a safe key (alt) to not trigger the Input features "unsafe keys" handler. 18, '[]foo' ); it( 'should prevent default behaviour when there is no correct location - document end', () => { const keydownHandler = sinon.spy(); const domEventDataMock = { keyCode: keyCodes.arrowright, preventDefault: sinon.spy(), }; setModelData( model, 'foo[]' ); viewDocument.on( 'keydown', keydownHandler ); viewDocument.fire( 'keydown', domEventDataMock ); expect( getModelData( model ) ).to.equal( 'foo[]' ); sinon.assert.calledOnce( domEventDataMock.preventDefault ); sinon.assert.notCalled( keydownHandler ); } ); it( 'should prevent default behaviour when there is no correct location - document start', () => { const keydownHandler = sinon.spy(); const domEventDataMock = { keyCode: keyCodes.arrowleft, preventDefault: sinon.spy(), }; setModelData( model, '[]foo' ); viewDocument.on( 'keydown', keydownHandler ); viewDocument.fire( 'keydown', domEventDataMock ); expect( getModelData( model ) ).to.equal( '[]foo' ); sinon.assert.calledOnce( domEventDataMock.preventDefault ); sinon.assert.notCalled( keydownHandler ); } ); test( 'should move selection to object element - right arrow', 'foo[]', keyCodes.arrowright, 'foo[]' ); test( 'should move selection to object element - down arrow', 'foo[]', keyCodes.arrowdown, 'foo[]' ); test( 'should move selection to object element - left arrow', '[]foo', keyCodes.arrowleft, '[]foo' ); test( 'should move selection to object element - up arrow', '[]foo', keyCodes.arrowup, '[]foo' ); test( 'do nothing on non objects - right arrow', 'foo[]bar', keyCodes.arrowright, 'foo[]bar' ); test( 'do nothing on non objects - down arrow', 'foo[]bar', keyCodes.arrowdown, 'foo[]bar' ); test( 'do nothing on non objects - left arrow', 'foo[]bar', keyCodes.arrowleft, 'foo[]bar' ); test( 'do nothing on non objects - up arrow', 'foo[]bar', keyCodes.arrowup, 'foo[]bar' ); test( 'should work correctly with modifier key: right arrow + ctrl', '[]foo', { keyCode: keyCodes.arrowright, ctrlKey: true }, '[]foo' ); test( 'should work correctly with modifier key: right arrow + alt', '[]foo', { keyCode: keyCodes.arrowright, altKey: true }, '[]foo' ); test( 'should work correctly with modifier key: right arrow + shift', '[]foo', { keyCode: keyCodes.arrowright, shiftKey: true }, '[]foo' ); test( 'should work correctly with modifier key: down arrow + ctrl', '[]foo', { keyCode: keyCodes.arrowdown, ctrlKey: true }, '[]foo' ); test( 'should work correctly with modifier key: down arrow + alt', '[]foo', { keyCode: keyCodes.arrowdown, altKey: true }, '[]foo' ); test( 'should work correctly with modifier key: down arrow + shift', '[]foo', { keyCode: keyCodes.arrowdown, shiftKey: true }, '[]foo' ); test( 'should work correctly with modifier key: left arrow + ctrl', 'foo[]', { keyCode: keyCodes.arrowleft, ctrlKey: true }, 'foo[]' ); test( 'should work correctly with modifier key: left arrow + alt', 'foo[]', { keyCode: keyCodes.arrowleft, altKey: true }, 'foo[]' ); test( 'should work correctly with modifier key: left arrow + shift', 'foo[]', { keyCode: keyCodes.arrowleft, shiftKey: true }, 'foo[]' ); test( 'should work correctly with modifier key: up arrow + ctrl', 'foo[]', { keyCode: keyCodes.arrowup, ctrlKey: true }, 'foo[]' ); test( 'should work correctly with modifier key: up arrow + alt', 'foo[]', { keyCode: keyCodes.arrowup, altKey: true }, 'foo[]' ); test( 'should work correctly with modifier key: up arrow + shift', 'foo[]', { keyCode: keyCodes.arrowup, shiftKey: true }, 'foo[]' ); test( 'should do nothing if there is more than one selection in model', '[foo][bar]', keyCodes.arrowright, '[foo][bar]' ); test( 'should work if selection is in nested element (left arrow)', 'foo' + '' + '
' + '
' + '
' + '[]' + '
' + '
' + '
' + 'foo', keyCodes.arrowleft, 'foo' + '[]' + '
' + '
' + '
' + '' + '
' + '
' + '
' + 'foo' ); test( 'should work if selection is in nested element (up arrow)', 'foo' + '' + '
' + '
' + '
' + '[]' + '
' + '
' + '
' + 'foo', keyCodes.arrowup, 'foo' + '[]' + '
' + '
' + '
' + '' + '
' + '
' + '
' + 'foo' ); test( 'should work if selection is in nested element (right arrow)', 'foo' + '
' + '
' + '
' + '[]' + '
' + '
' + '
' + '' + 'foo', keyCodes.arrowright, 'foo' + '
' + '
' + '
' + '' + '
' + '
' + '
' + '[]' + 'foo' ); test( 'should work if selection is in nested element (down arrow)', 'foo' + '
' + '
' + '
' + '[]' + '
' + '
' + '
' + '' + 'foo', keyCodes.arrowdown, 'foo' + '
' + '
' + '
' + '' + '
' + '
' + '
' + '[]' + 'foo' ); } ); describe( 'Ctrl+A', () => { test( 'should select the entire content of the nested editable', 'foo[]bar', { keyCode: keyCodes.a, ctrlKey: true }, '[foo]bar' ); test( 'should not change the selection if outside of the nested editable', 'foo[]bar', { keyCode: keyCodes.a, ctrlKey: true }, 'foo[]bar' ); test( 'should selected whole content when widget is selected', 'foo[]bar', { keyCode: keyCodes.a, ctrlKey: true }, '[foobar]', '[

foo

bar

]' ); } ); function test( name, data, keyCodeOrMock, expected, expectedView ) { it( name, () => { const domEventDataMock = ( typeof keyCodeOrMock == 'object' ) ? keyCodeOrMock : { keyCode: keyCodeOrMock }; setModelData( model, data ); viewDocument.fire( 'keydown', new DomEventData( viewDocument, { target: document.createElement( 'div' ), preventDefault() {} }, domEventDataMock ) ); expect( getModelData( model ) ).to.equal( expected ); if ( expectedView ) { expect( getViewData( viewDocument ) ).to.equal( expectedView ); } } ); } } ); describe( 'delete integration', () => { function test( name, input, direction, expected ) { it( name, () => { setModelData( model, input ); const scrollStub = sinon.stub( viewDocument, 'scrollToTheSelection' ); const domEventDataMock = { keyCode: direction == 'backward' ? keyCodes.backspace : keyCodes.delete, }; viewDocument.fire( 'keydown', new DomEventData( viewDocument, { target: document.createElement( 'div' ), preventDefault() {} }, domEventDataMock ) ); expect( getModelData( model ) ).to.equal( expected ); scrollStub.restore(); } ); } // Let's make this integration tests real which will help covering // cases like https://github.com/ckeditor/ckeditor5/issues/753. // Originally, this test file used the Delete feature only which was not "integrational" enough. it( 'tests are executed with the Typing feature', () => { expect( editor.plugins.get( 'Typing' ) ).to.not.be.undefined; } ); test( 'should select widget when backspace is pressed', '[]foo', 'backward', '[]foo' ); test( 'should remove empty element after selecting widget when backspace is pressed', '[]', 'backward', '[]' ); test( 'should select widget when delete is pressed', 'foo[]', 'forward', 'foo[]' ); test( 'should remove empty element after selecting widget when delete is pressed', '[]', 'forward', '[]' ); test( 'should not select widget on non-collapsed selection', '[f]oo', 'backward', '[]oo' ); test( 'should not affect non-object elements', 'foo[]bar', 'backward', 'foo[]bar' ); test( 'should not modify backward delete default behaviour in single paragraph boundaries', '[]foo', 'backward', '[]foo' ); test( 'should not modify forward delete default behaviour in single paragraph boundaries', 'foo[]', 'forward', 'foo[]' ); test( 'should delete selected widget with paragraph before - backward', 'foo[]', 'backward', 'foo[]' ); test( 'should delete selected widget with paragraph before - forward', 'foo[]', 'forward', 'foo[]' ); test( 'should delete selected widget with paragraph after - backward', '[]foo', 'backward', '[]foo' ); test( 'should delete selected widget with paragraph after - forward', '[]foo', 'forward', '[]foo' ); test( 'should delete selected widget between paragraphs - backward', 'bar[]foo', 'backward', 'bar[]foo' ); test( 'should delete selected widget between paragraphs - forward', 'bar[]foo', 'forward', 'bar[]foo' ); test( 'should delete selected widget preceded by another widget - backward', '[]', 'backward', '[]' ); test( 'should delete selected widget preceded by another widget - forward', '[]', 'forward', '[]' ); test( 'should delete selected widget before another widget - forward', '[]', 'forward', '[]' ); test( 'should delete selected widget before another widget - backward', '[]', 'backward', '[]' ); test( 'should delete selected widget between other widgets - forward', '[]', 'forward', '[]' ); test( 'should delete selected widget between other widgets - backward', '[]', 'backward', '[]' ); test( 'should select inline objects - backward', 'foo[]bar', 'backward', 'foo[]bar' ); test( 'should select inline objects - forward', 'foo[]bar', 'forward', 'foo[]bar' ); test( 'should delete selected inline objects - backward', 'foo[]bar', 'backward', 'foo[]bar' ); test( 'should delete selected inline objects - forward', 'foo[]bar', 'forward', 'foo[]bar' ); test( 'should use standard delete behaviour when after first letter - backward', 'a[]', 'backward', '[]' ); test( 'should use standard delete behaviour when before first letter - forward', '[]a', 'forward', '[]' ); it( 'should prevent default behaviour and stop event propagation', () => { setModelData( model, 'foo[]' ); const scrollStub = sinon.stub( viewDocument, 'scrollToTheSelection' ); const deleteSpy = sinon.spy(); viewDocument.on( 'delete', deleteSpy ); const domEventDataMock = { target: document.createElement( 'div' ), preventDefault: sinon.spy() }; viewDocument.fire( 'delete', new DomEventData( viewDocument, domEventDataMock, { direction: 'forward', unit: 'character', sequence: 0 } ) ); sinon.assert.calledOnce( domEventDataMock.preventDefault ); sinon.assert.notCalled( deleteSpy ); scrollStub.restore(); } ); test( 'should remove the entire empty element if it is next to a widget', 'foo' + '' + '
[]
' + 'foo', 'backward', 'foo[]foo' ); test( 'should remove the entire empty element (deeper structure) if it is next to a widget', 'foo' + '' + '
[]
' + 'foo', 'backward', 'foo' + '[]' + 'foo' ); test( 'should remove the entire empty element (deeper structure) if it is next to a widget (forward delete)', 'foo' + '
[]
' + '' + 'foo', 'forward', 'foo' + '[]' + 'foo' ); test( 'should not remove the entire element which is not empty and the element is next to a widget', 'foo' + '' + '
[]
' + 'foo', 'backward', 'foo' + '[]' + '
' + 'foo' ); test( 'should not remove the entire element which is not empty and the element is next to a widget (forward delete)', 'foo' + '
Foo[]
' + '' + 'foo', 'forward', 'foo' + '
Foo
' + '[]' + 'foo' ); test( 'should not remove the entire element (deeper structure) which is not empty and the element is next to a widget', 'foo' + '' + '
' + '
' + '
' + '[]' + '
' + '
' + '' + '
' + 'foo', 'backward', 'foo' + '[]' + '
' + '' + '
' + 'foo' ); test( 'should do nothing if the nested element is not empty and the element is next to a widget', 'foo' + '' + '
' + '
' + '
' + 'Foo[]' + '
' + '
' + '
' + 'foo', 'backward', 'foo' + '' + '
' + '
' + '
' + 'Fo[]' + '
' + '
' + '
' + 'foo' ); it( 'does nothing when editor when read only mode is enabled (delete)', () => { const scrollStub = sinon.stub( viewDocument, 'scrollToTheSelection' ); setModelData( model, 'foo' + '' + '
[]
' + 'foo' ); editor.isReadOnly = true; const domEventDataMock = { target: document.createElement( 'div' ), preventDefault: sinon.spy() }; viewDocument.fire( 'delete', new DomEventData( viewDocument, domEventDataMock, { direction: 'backward', unit: 'character', sequence: 0 } ) ); expect( getModelData( model ) ).to.equal( 'foo' + '' + '
[]
' + 'foo' ); scrollStub.restore(); } ); it( 'does nothing when editor when read only mode is enabled (forward delete)', () => { const scrollStub = sinon.stub( viewDocument, 'scrollToTheSelection' ); setModelData( model, 'foo' + '' + '
[]
' + 'foo' ); editor.isReadOnly = true; const domEventDataMock = { target: document.createElement( 'div' ), preventDefault: sinon.spy() }; viewDocument.fire( 'delete', new DomEventData( viewDocument, domEventDataMock, { direction: 'forward', unit: 'character', sequence: 0 } ) ); expect( getModelData( model ) ).to.equal( 'foo' + '' + '
[]
' + 'foo' ); scrollStub.restore(); } ); } ); } );