| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- /*
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
- import Input from '../src/input';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
- import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
- import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
- import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
- 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 } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
- describe( 'Input feature', () => {
- let editor, model, modelRoot, view, viewRoot, listenter;
- before( () => {
- listenter = Object.create( EmitterMixin );
- return VirtualTestEditor.create( {
- plugins: [ Input, Paragraph ]
- } )
- .then( newEditor => {
- // Mock image feature.
- newEditor.document.schema.registerItem( 'image', '$inline' );
- buildModelConverter().for( newEditor.data.modelToView, newEditor.editing.modelToView )
- .fromElement( 'image' )
- .toElement( 'img' );
- buildViewConverter().for( newEditor.data.viewToModel )
- .fromElement( 'img' )
- .toElement( 'image' );
- editor = newEditor;
- model = editor.editing.model;
- modelRoot = model.getRoot();
- view = editor.editing.view;
- viewRoot = view.getRoot();
- } );
- } );
- beforeEach( () => {
- editor.setData( '<p>foobar</p>' );
- model.enqueueChanges( () => {
- model.selection.setRanges( [
- ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 3, modelRoot.getChild( 0 ), 3 )
- ] );
- } );
- } );
- afterEach( () => {
- listenter.stopListening();
- } );
- describe( 'mutations handling', () => {
- it( 'should handle text mutation', () => {
- view.fire( 'mutations', [
- {
- type: 'text',
- oldText: 'foobar',
- newText: 'fooxbar',
- node: viewRoot.getChild( 0 ).getChild( 0 )
- }
- ] );
- expect( getModelData( model ) ).to.equal( '<paragraph>foox[]bar</paragraph>' );
- expect( getViewData( view ) ).to.equal( '<p>foox{}bar</p>' );
- } );
- it( 'should handle text mutation change', () => {
- view.fire( 'mutations', [
- {
- type: 'text',
- oldText: 'foobar',
- newText: 'foodar',
- node: viewRoot.getChild( 0 ).getChild( 0 )
- }
- ] );
- expect( getModelData( model ) ).to.equal( '<paragraph>food[]ar</paragraph>' );
- expect( getViewData( view ) ).to.equal( '<p>food{}ar</p>' );
- } );
- it( 'should handle text node insertion', () => {
- editor.setData( '<p></p>' );
- view.fire( 'mutations', [
- {
- type: 'children',
- oldChildren: [],
- newChildren: [ new ViewText( 'x' ) ],
- node: viewRoot.getChild( 0 )
- }
- ] );
- expect( getModelData( model ) ).to.equal( '<paragraph>x[]</paragraph>' );
- expect( getViewData( view ) ).to.equal( '<p>x{}</p>' );
- } );
- it( 'should do nothing when two nodes were inserted', () => {
- editor.setData( '<p></p>' );
- view.fire( 'mutations', [
- {
- type: 'children',
- oldChildren: [],
- newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
- node: viewRoot.getChild( 0 )
- }
- ] );
- expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
- expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
- } );
- it( 'should do nothing when two nodes were inserted and one removed', () => {
- view.fire( 'mutations', [
- {
- type: 'children',
- oldChildren: [ new ViewText( 'foobar' ) ],
- newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
- node: viewRoot.getChild( 0 )
- }
- ] );
- expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
- expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
- } );
- it( 'should handle multiple children in the node', () => {
- editor.setData( '<p>foo<img></img></p>' );
- view.fire( 'mutations', [
- {
- type: 'children',
- oldChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ) ],
- newChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ), new ViewText( 'x' ) ],
- node: viewRoot.getChild( 0 )
- }
- ] );
- expect( getModelData( model ) ).to.equal( '<paragraph>foo<image></image>x[]</paragraph>' );
- expect( getViewData( view ) ).to.equal( '<p>foo<img></img>x{}</p>' );
- } );
- it( 'should do nothing when node was removed', () => {
- view.fire( 'mutations', [
- {
- type: 'children',
- oldChildren: [ new ViewText( 'foobar' ) ],
- newChildren: [],
- node: viewRoot.getChild( 0 )
- }
- ] );
- expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
- expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
- } );
- it( 'should do nothing when element was inserted', () => {
- editor.setData( '<p></p>' );
- view.fire( 'mutations', [
- {
- type: 'children',
- oldChildren: [],
- newChildren: [ new ViewElement( 'img' ) ],
- node: viewRoot.getChild( 0 )
- }
- ] );
- expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
- expect( getViewData( view ) ).to.equal( '<p>[]</p>' );
- } );
- it( 'should set model selection appropriately to view selection passed in mutations event', () => {
- // This test case emulates spellchecker correction.
- const viewSelection = new ViewSelection();
- viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
- view.fire( 'mutations',
- [ {
- type: 'text',
- oldText: 'foobar',
- newText: 'foodar',
- node: viewRoot.getChild( 0 ).getChild( 0 )
- } ],
- viewSelection
- );
- expect( getModelData( model ) ).to.equal( '<paragraph>foodar[]</paragraph>' );
- expect( getViewData( view ) ).to.equal( '<p>foodar{}</p>' );
- } );
- it( 'should use up to one insert and remove operations', () => {
- // This test case emulates spellchecker correction.
- const viewSelection = new ViewSelection();
- viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 6 );
- sinon.spy( Batch.prototype, 'weakInsert' );
- sinon.spy( Batch.prototype, 'remove' );
- view.fire( 'mutations',
- [ {
- type: 'text',
- oldText: 'foobar',
- newText: 'fxobxr',
- node: viewRoot.getChild( 0 ).getChild( 0 )
- } ],
- viewSelection
- );
- expect( Batch.prototype.weakInsert.calledOnce ).to.be.true;
- expect( Batch.prototype.remove.calledOnce ).to.be.true;
- } );
- } );
- describe( 'keystroke handling', () => {
- it( 'should remove contents', () => {
- model.enqueueChanges( () => {
- model.selection.setRanges( [
- ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
- } );
- listenter.listenTo( view, 'keydown', () => {
- expect( getModelData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
- view.fire( 'mutations', [
- {
- type: 'text',
- oldText: 'foar',
- newText: 'foyar',
- node: viewRoot.getChild( 0 ).getChild( 0 )
- }
- ] );
- }, { priority: 'lowest' } );
- view.fire( 'keydown', { keyCode: getCode( 'y' ) } );
- expect( getModelData( model ) ).to.equal( '<paragraph>foy[]ar</paragraph>' );
- expect( getViewData( view ) ).to.equal( '<p>foy{}ar</p>' );
- } );
- it( 'should do nothing on arrow key', () => {
- model.enqueueChanges( () => {
- model.selection.setRanges( [
- ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
- } );
- view.fire( 'keydown', { keyCode: getCode( 'arrowright' ) } );
- expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
- } );
- it( 'should do nothing on ctrl combinations', () => {
- model.enqueueChanges( () => {
- model.selection.setRanges( [
- ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
- } );
- view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
- expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
- } );
- it( 'should do nothing on non printable keys', () => {
- model.enqueueChanges( () => {
- model.selection.setRanges( [
- ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
- } );
- view.fire( 'keydown', { keyCode: 16 } ); // Shift
- view.fire( 'keydown', { keyCode: 35 } ); // Home
- view.fire( 'keydown', { keyCode: 112 } ); // F1
- expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
- } );
- // #69
- it( 'should do nothing on tab key', () => {
- model.enqueueChanges( () => {
- model.selection.setRanges( [
- ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 2, modelRoot.getChild( 0 ), 4 ) ] );
- } );
- view.fire( 'keydown', { keyCode: 9 } ); // Tab
- expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
- } );
- it( 'should do nothing if selection is collapsed', () => {
- view.fire( 'keydown', { ctrlKey: true, keyCode: getCode( 'c' ) } );
- expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
- } );
- } );
- } );
|