/** * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor'; import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; import List from '@ckeditor/ckeditor5-list/src/list'; import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold'; import Input from '../src/input'; import Writer from '@ckeditor/ckeditor5-engine/src/model/writer'; import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range'; import ViewText from '@ckeditor/ckeditor5-engine/src/view/text'; import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element'; import ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection'; import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin'; import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard'; import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view'; /* global document */ describe( 'Input feature', () => { let editor, model, modelRoot, view, viewDocument, viewRoot, listenter; testUtils.createSinonSandbox(); beforeEach( () => { listenter = Object.create( EmitterMixin ); const domElement = document.createElement( 'div' ); document.body.appendChild( domElement ); return ClassicTestEditor.create( domElement, { plugins: [ Input, Paragraph, Bold, List ] } ) .then( newEditor => { // Mock image feature. newEditor.model.schema.register( 'image', { allowWhere: '$text' } ); newEditor.conversion.elementToElement( { model: 'image', view: 'img' } ); editor = newEditor; model = editor.model; modelRoot = model.document.getRoot(); view = editor.editing.view; viewDocument = view.document; viewRoot = viewDocument.getRoot(); } ); } ); after( () => { return editor.destroy(); } ); beforeEach( () => { editor.setData( '
foobar
' ); model.change( writer => { writer.setSelection( ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 3, modelRoot.getChild( 0 ), 3 ) ); } ); } ); afterEach( () => { listenter.stopListening(); } ); describe( 'mutations handling', () => { it( 'should handle text mutation', () => { viewDocument.fire( 'mutations', [ { type: 'text', oldText: 'foobar', newText: 'fooxbar', node: viewRoot.getChild( 0 ).getChild( 0 ) } ] ); expect( getModelData( model ) ).to.equal( 'foox{}bar
' ); } ); it( 'should handle text mutation change', () => { viewDocument.fire( 'mutations', [ { type: 'text', oldText: 'foobar', newText: 'foodar', node: viewRoot.getChild( 0 ).getChild( 0 ) } ] ); expect( getModelData( model ) ).to.equal( 'food{}ar
' ); } ); it( 'should handle text node insertion', () => { editor.setData( '' ); viewDocument.fire( 'mutations', [ { type: 'children', oldChildren: [], newChildren: [ new ViewText( 'x' ) ], node: viewRoot.getChild( 0 ) } ] ); expect( getModelData( model ) ).to.equal( 'x{}
' ); } ); it( 'should apply selection attributes to the inserted text', () => { setModelData( model, 'foobar
' ); viewDocument.fire( 'mutations', [ { type: 'text', oldText: 'foo', newText: 'foob', node: viewRoot.getChild( 0 ).getChild( 0 ) }, { type: 'text', oldText: 'bar', newText: 'ar', node: viewRoot.getChild( 0 ).getChild( 1 ).getChild( 0 ) } ] ); expect( getModelData( model ) ).to.equal( 'foob{}ar
' ); } ); it( 'should handle multiple text node insertion', () => { editor.setData( '' ); viewDocument.fire( 'mutations', [ { type: 'children', oldChildren: [], newChildren: [ new ViewText( 'x' ) ], node: viewRoot.getChild( 0 ) }, { type: 'children', oldChildren: [], newChildren: [ new ViewText( 'y' ) ], node: viewRoot.getChild( 1 ) } ] ); expect( getModelData( model ) ).to.equal( 'x
y{}
' ); } ); it( 'should do nothing when two nodes were inserted', () => { editor.setData( '' ); viewDocument.fire( 'mutations', [ { type: 'children', oldChildren: [], newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ], node: viewRoot.getChild( 0 ) } ] ); expect( getModelData( model ) ).to.equal( '[]
' ); } ); it( 'should do nothing when two nodes were inserted and one removed', () => { viewDocument.fire( 'mutations', [ { type: 'children', oldChildren: [ new ViewText( 'foobar' ) ], newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ], node: viewRoot.getChild( 0 ) } ] ); expect( getModelData( model ) ).to.equal( 'foo{}bar
' ); } ); it( 'should handle multiple children in the node', () => { editor.setData( 'foo
foox{}
foo{}bar
' ); } ); it( 'should do nothing when element was inserted', () => { editor.setData( '' ); viewDocument.fire( 'mutations', [ { type: 'children', oldChildren: [], newChildren: [ new ViewElement( 'img' ) ], node: viewRoot.getChild( 0 ) } ] ); expect( getModelData( model ) ).to.equal( '[]
' ); } ); it( 'should set model selection appropriately to view selection passed in mutations event', () => { // This test case emulates spellchecker correction. const viewSelection = new ViewSelection(); viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 6 ); viewDocument.fire( 'mutations', [ { type: 'text', oldText: 'foobar', newText: 'foodar', node: viewRoot.getChild( 0 ).getChild( 0 ) } ], viewSelection ); expect( getModelData( model ) ).to.equal( 'foodar{}
' ); } ); it( 'should use up to one insert and remove operations (spellchecker)', () => { // This test case emulates spellchecker correction. const viewSelection = new ViewSelection(); viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 6 ); testUtils.sinon.spy( Writer.prototype, 'insert' ); testUtils.sinon.spy( Writer.prototype, 'remove' ); viewDocument.fire( 'mutations', [ { type: 'text', oldText: 'foobar', newText: 'fxobxr', node: viewRoot.getChild( 0 ).getChild( 0 ) } ], viewSelection ); expect( Writer.prototype.insert.calledOnce ).to.be.true; expect( Writer.prototype.remove.calledOnce ).to.be.true; } ); it( 'should place selection after when correcting to longer word (spellchecker)', () => { // This test case emulates spellchecker correction. editor.setData( 'Foo hous a
' ); const viewSelection = new ViewSelection(); viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 9 ); viewDocument.fire( 'mutations', [ { type: 'text', oldText: 'Foo hous a', newText: 'Foo house a', node: viewRoot.getChild( 0 ).getChild( 0 ) } ], viewSelection ); expect( getModelData( model ) ).to.equal( 'Foo house{} a
' ); } ); it( 'should place selection after when correcting to shorter word (spellchecker)', () => { // This test case emulates spellchecker correction. editor.setData( 'Bar athat foo
' ); const viewSelection = new ViewSelection(); viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 8 ); viewDocument.fire( 'mutations', [ { type: 'text', oldText: 'Bar athat foo', newText: 'Bar that foo', node: viewRoot.getChild( 0 ).getChild( 0 ) } ], viewSelection ); expect( getModelData( model ) ).to.equal( 'Bar that{} foo
' ); } ); it( 'should place selection after when merging two words (spellchecker)', () => { // This test case emulates spellchecker correction. editor.setData( 'Foo hous e
' ); const viewSelection = new ViewSelection(); viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 9 ); viewDocument.fire( 'mutations', [ { type: 'text', oldText: 'Foo hous e', newText: 'Foo house', node: viewRoot.getChild( 0 ).getChild( 0 ) } ], viewSelection ); expect( getModelData( model ) ).to.equal( 'Foo house{}
' ); } ); it( 'should place non-collapsed selection after changing single character (composition)', () => { editor.setData( 'Foo house
' ); const viewSelection = new ViewSelection(); viewSelection.setTo( viewRoot.getChild( 0 ).getChild( 0 ), 8 ); viewSelection.setFocus( viewRoot.getChild( 0 ).getChild( 0 ), 9 ); viewDocument.fire( 'mutations', [ { type: 'text', oldText: 'Foo house', newText: 'Foo housa', node: viewRoot.getChild( 0 ).getChild( 0 ) } ], viewSelection ); expect( getModelData( model ) ).to.equal( 'Foo hous{a}
' ); } ); it( 'should replace last with space', () => { model.change( writer => { writer.setSelection( ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 ) ); } ); viewDocument.fire( 'mutations', [ { type: 'text', oldText: 'foobar', newText: 'foobar\u00A0', node: viewRoot.getChild( 0 ).getChild( 0 ) } ] ); expect( getModelData( model ) ).to.equal( 'foobar {}
' ); } ); it( 'should replace first with space', () => { model.change( writer => { writer.setSelection( ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 0, modelRoot.getChild( 0 ), 0 ) ); } ); viewDocument.fire( 'mutations', [ { type: 'text', oldText: 'foobar', newText: '\u00A0foobar', node: viewRoot.getChild( 0 ).getChild( 0 ) } ] ); expect( getModelData( model ) ).to.equal( '{}foobar
' ); } ); it( 'should replace all with spaces', () => { model.change( writer => { writer.setSelection( ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 ) ); } ); viewDocument.fire( 'mutations', [ { type: 'text', oldText: 'foobar', newText: 'foobar\u00A0\u00A0\u00A0baz', node: viewRoot.getChild( 0 ).getChild( 0 ) } ] ); expect( getModelData( model ) ).to.equal( 'foobar baz{}
' ); } ); // ckeditor5#718. it( 'should not crash and prevent all changes if view common ancestor of mutations cannot be mapped to model', () => { editor.setData( 'Foo
{}Foo
foy{}ar
' ); } ); it( 'should do nothing on arrow key', () => { model.change( writer => { writer.setSelection( ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ); } ); viewDocument.fire( 'keydown', { keyCode: getCode( 'arrowdown' ) } ); expect( getModelData( model ) ).to.equal( '