/** * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor'; import DeleteCommand from '../src/deletecommand'; import UndoEngine from '@ckeditor/ckeditor5-undo/src/undoengine'; import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; describe( 'DeleteCommand integration', () => { let editor, doc; beforeEach( () => { return ModelTestEditor .create( { plugins: [ UndoEngine ], typing: { undoStep: 3 } } ) .then( newEditor => { editor = newEditor; doc = editor.document; const command = new DeleteCommand( editor, 'backward' ); editor.commands.add( 'delete', command ); // Mock paragraph feature. doc.schema.registerItem( 'paragraph', '$block' ); doc.schema.allow( { name: 'paragraph', inside: '$block' } ); doc.schema.registerItem( 'img', '$inline' ); doc.schema.allow( { name: '$text', inside: 'img' } ); doc.schema.objects.add( 'img' ); } ); } ); afterEach( () => { return editor.destroy(); } ); function assertOutput( output ) { expect( getData( doc ) ).to.equal( output ); } describe( 'with undo', () => { it( 'deletes characters (and group changes in batches) and rollbacks', () => { setData( doc, '123456789[]' ); for ( let i = 0; i < 3; ++i ) { editor.execute( 'delete' ); } editor.execute( 'undo' ); assertOutput( '123456789[]' ); } ); it( 'deletes characters (and group changes in batches) and rollbacks - test step', () => { setData( doc, '123456789[]' ); for ( let i = 0; i < 6; ++i ) { editor.execute( 'delete' ); } editor.execute( 'undo' ); assertOutput( '123456[]' ); editor.execute( 'undo' ); assertOutput( '123456789[]' ); } ); it( 'deletes elements (and group changes in batches) and rollbacks', () => { setData( doc, '123456[]' ); for ( let i = 0; i < 3; ++i ) { editor.execute( 'delete' ); } editor.execute( 'undo' ); assertOutput( '123456[]' ); } ); it( 'merges elements (and group changes in batches) and rollbacks', () => { setData( doc, '123456[]78' ); for ( let i = 0; i < 6; ++i ) { editor.execute( 'delete' ); } editor.execute( 'undo' ); // Deleted 6,5,4, does not count. // It's not the most elegant solution, but is the best if we don't want to make complicated algorithm. assertOutput( '123[]78' ); editor.execute( 'undo' ); // Selection restoing in undo is not 100% correct so slight miss-settings are expected as long as // the selection makes any sense and is near the correct position. assertOutput( '12345678[]' ); } ); it( 'merges elements (and group changes in batches) and rollbacks - non-collapsed selection', () => { setData( doc, '12345[67]8' ); editor.execute( 'delete' ); editor.execute( 'delete' ); editor.execute( 'delete' ); editor.execute( 'undo' ); assertOutput( '1234[]8' ); editor.execute( 'undo' ); assertOutput( '12345[67]8' ); } ); } ); describe( 'with DataController.deleteContent', () => { beforeEach( () => { doc.schema.registerItem( 'h1', '$block' ); } ); it( 'should replace content with paragraph - if whole content is selected', () => { setData( doc, '

[foo

bar]' ); editor.execute( 'delete' ); assertOutput( '[]' ); } ); it( 'should not replace content with paragraph - if not whole content is selected', () => { setData( doc, '

f[oo

bar]' ); editor.execute( 'delete' ); assertOutput( '

f[]

' ); } ); it( 'should not replace content with paragraph - if selection was collapsed', () => { setData( doc, '

[]' ); editor.execute( 'delete' ); assertOutput( '

[]

' ); } ); } ); } );