| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- /**
- * @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 ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
- import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
- import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import Range from '@ckeditor/ckeditor5-engine/src/model/range';
- import Position from '@ckeditor/ckeditor5-engine/src/model/position';
- import InputCommand from '../src/inputcommand';
- import ChangeBuffer from '../src/changebuffer';
- import Input from '../src/input';
- describe( 'InputCommand', () => {
- let editor, doc, buffer;
- testUtils.createSinonSandbox();
- before( () => {
- return ModelTestEditor.create( )
- .then( newEditor => {
- editor = newEditor;
- doc = editor.document;
- const inputCommand = new InputCommand( editor, 20 );
- editor.commands.set( 'input', inputCommand );
- buffer = inputCommand.buffer;
- doc.schema.registerItem( 'p', '$block' );
- doc.schema.registerItem( 'h1', '$block' );
- } );
- } );
- beforeEach( () => {
- buffer.size = 0;
- } );
- describe( 'buffer', () => {
- it( 'has buffer getter', () => {
- expect( editor.commands.get( 'input' ).buffer ).to.be.an.instanceof( ChangeBuffer );
- } );
- it( 'has a buffer limit configured to default value of 20', () => {
- expect( editor.commands.get( 'input' )._buffer ).to.have.property( 'limit', 20 );
- } );
- it( 'has a buffer configured to config.typing.undoStep', () => {
- return VirtualTestEditor.create( {
- plugins: [ Input ],
- typing: {
- undoStep: 5
- }
- } )
- .then( editor => {
- expect( editor.commands.get( 'input' )._buffer ).to.have.property( 'limit', 5 );
- } );
- } );
- } );
- describe( 'execute', () => {
- it( 'uses enqueueChanges', () => {
- setData( doc, '<p>foo[]bar</p>' );
- const spy = testUtils.sinon.spy( doc, 'enqueueChanges' );
- editor.execute( 'input', {
- text: ''
- } );
- expect( spy.calledOnce ).to.be.true;
- } );
- it( 'should lock and unlock buffer', () => {
- setData( doc, '<p>foo[]bar</p>' );
- const spyLock = testUtils.sinon.spy( buffer, 'lock' );
- const spyUnlock = testUtils.sinon.spy( buffer, 'unlock' );
- editor.execute( 'input', {
- text: ''
- } );
- expect( spyLock.calledOnce ).to.be.true;
- expect( spyUnlock.calledOnce ).to.be.true;
- } );
- it( 'inserts text for collapsed range', () => {
- setData( doc, '<p>foo[]</p>' );
- editor.execute( 'input', {
- text: 'bar',
- range: editor.document.selection.getFirstRange()
- } );
- expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobar[]</p>' );
- expect( buffer.size ).to.be.equal( 3 );
- } );
- it( 'replaces text for range within single element on the beginning', () => {
- setData( doc, '<p>[fooba]r</p>' );
- editor.execute( 'input', {
- text: 'rab',
- range: editor.document.selection.getFirstRange()
- } );
- expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>rab[]r</p>' );
- expect( buffer.size ).to.be.equal( 3 );
- } );
- it( 'replaces text for range within single element in the middle', () => {
- setData( doc, '<p>fo[oba]r</p>' );
- editor.execute( 'input', {
- text: 'bazz',
- range: editor.document.selection.getFirstRange()
- } );
- expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>fobazz[]r</p>' );
- expect( buffer.size ).to.be.equal( 4 );
- } );
- it( 'replaces text for range within single element on the end', () => {
- setData( doc, '<p>fooba[r]</p>' );
- editor.execute( 'input', {
- text: 'zzz',
- range: editor.document.selection.getFirstRange()
- } );
- expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobazzz[]</p>' );
- expect( buffer.size ).to.be.equal( 3 );
- } );
- it( 'replaces text for range within multiple elements', () => {
- setData( doc, '<h1>F[OO</h1><p>b]ar</p>' );
- editor.execute( 'input', {
- text: 'unny c',
- range: editor.document.selection.getFirstRange()
- } );
- expect( getData( doc, { selection: true } ) ).to.be.equal( '<h1>Funny c[</h1><p>]ar</p>' );
- expect( buffer.size ).to.be.equal( 6 );
- } );
- it( 'uses current selection when range is not given', () => {
- setData( doc, '<p>foob[ar]</p>' );
- editor.execute( 'input', {
- text: 'az'
- } );
- expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobaz[]</p>' );
- expect( buffer.size ).to.be.equal( 2 );
- } );
- it( 'only removes content when empty text given', () => {
- setData( doc, '<p>[fo]obar</p>' );
- editor.execute( 'input', {
- text: '',
- range: editor.document.selection.getFirstRange()
- } );
- expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[]obar</p>' );
- expect( buffer.size ).to.be.equal( 0 );
- } );
- it( 'should set selection according to passed resultRange (collapsed)', () => {
- setData( doc, '<p>[foo]bar</p>' );
- editor.execute( 'input', {
- text: 'new',
- resultRange: new Range( new Position( doc.getRoot(), [ 0, 5 ] ) )
- } );
- expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>newba[]r</p>' );
- expect( buffer.size ).to.be.equal( 3 );
- } );
- it( 'should set selection according to passed resultRange (non-collapsed)', () => {
- setData( doc, '<p>[foo]bar</p>' );
- editor.execute( 'input', {
- text: 'new',
- resultRange: new Range( new Position( doc.getRoot(), [ 0, 3 ] ), new Position( doc.getRoot(), [ 0, 6 ] ) )
- } );
- expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>new[bar]</p>' );
- expect( buffer.size ).to.be.equal( 3 );
- } );
- it( 'only removes content when no text given (with default non-collapsed range)', () => {
- setData( doc, '<p>[fo]obar</p>' );
- const spy = testUtils.sinon.spy( doc, 'enqueueChanges' );
- editor.execute( 'input' );
- expect( spy.callCount ).to.be.equal( 1 );
- expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>[]obar</p>' );
- expect( buffer.size ).to.be.equal( 0 );
- } );
- it( 'does not change selection and content when no text given (with default collapsed range)', () => {
- setData( doc, '<p>fo[]obar</p>' );
- const spy = testUtils.sinon.spy( doc, 'enqueueChanges' );
- editor.execute( 'input' );
- expect( spy.callCount ).to.be.equal( 1 );
- expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>fo[]obar</p>' );
- expect( buffer.size ).to.be.equal( 0 );
- } );
- } );
- describe( 'destroy', () => {
- it( 'should destroy change buffer', () => {
- const command = editor.commands.get( 'input' );
- const destroy = command._buffer.destroy = testUtils.sinon.spy();
- command.destroy();
- expect( destroy.calledOnce ).to.be.true;
- expect( command._buffer ).to.be.null;
- } );
- } );
- } );
|