/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import ModelTestEditor from 'tests/core/_utils/modeltesteditor.js'; import EnterCommand from 'ckeditor5/enter/entercommand.js'; import { getData, setData } from 'ckeditor5/engine/dev-utils/model.js'; let editor, doc, schema, command; beforeEach( () => { return ModelTestEditor.create() .then( newEditor => { editor = newEditor; doc = editor.document; command = new EnterCommand( editor ); editor.commands.set( 'enter', command ); schema = doc.schema; // Note: We could use real names like 'paragraph', but that would make test patterns too long. // Plus, this is actually a good test that the algorithm can be used for any model. schema.registerItem( 'img', '$inline' ); schema.registerItem( 'p', '$block' ); schema.registerItem( 'h', '$block' ); schema.allow( { name: '$text', inside: '$root' } ); } ); } ); describe( 'EnterCommand', () => { it( 'enters a block using enqueueChanges', () => { setData( doc, '
foo[]
' ); const spy = sinon.spy( doc, 'enqueueChanges' ); editor.execute( 'enter' ); expect( getData( doc, { withoutSelection: true } ) ).to.equal( 'foo
' ); expect( spy.calledOnce ).to.be.true; } ); } ); describe( '_doExecute', () => { describe( 'collapsed selection', () => { test( 'does nothing in the root', 'foo[]bar', 'foo[]bar' ); test( 'splits block', 'x
foo[]bar
y
', 'x
foo
[]bar
y
' ); test( 'splits block at the end', 'x
foo[]
y
', 'x
foo
[]
y
' ); test( 'splits block at the beginning', 'x
[]foo
y
', 'x
[]foo
y
' ); test( 'inserts new block after empty one', 'x
[]
y
', 'x
[]
y
' ); } ); describe( 'non-collapsed selection', () => { test( 'only deletes the content when directly in the root', 'fo[ob]ar', 'fo[]ar' ); test( 'deletes text and splits', 'ab[cd]ef
ghi
', 'ab
[]ef
ghi
' ); test( 'places selection in the 2nd element', 'd]ef
ghi
', '[]ef
ghi
' ); test( 'leaves one empty element after one was fully selected', 'x
[abcdef]
y
', 'x
[]
y
' ); test( 'leaves one empty element after two were fully selected', '[abc
def]
', '[]
' ); it( 'leaves one empty element after two were fully selected (backward)', () => { setData( doc, '[abc
def]
' ); // @TODO: Add option for setting selection direction to model utils. doc.selection._lastRangeBackward = true; command._doExecute(); expect( getData( doc ) ).to.equal( '[]
' ); } ); it( 'uses composer.deleteContents', () => { const spy = sinon.spy(); doc.composer.on( 'deleteContents', spy ); setData( doc, '[x]
' ); command._doExecute(); expect( spy.calledOnce ).to.be.true; } ); } ); function test( title, input, output ) { it( title, () => { setData( doc, input ); command._doExecute(); expect( getData( doc ) ).to.equal( output ); } ); } } );