/** * @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 { default as EnterCommand, enterBlock } from '/ckeditor5/enter/entercommand.js'; import { getData, setData } from '/tests/engine/_utils/model.js'; let editor, doc, schema; beforeEach( () => { return ModelTestEditor.create() .then( newEditor => { editor = newEditor; doc = editor.document; const 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; } ); it( 'uses paragraph as default block', () => { schema.registerItem( 'paragraph', '$block' ); setData( doc, 'foo[]' ); editor.execute( 'enter' ); expect( getData( doc, { withoutSelection: true } ) ).to.equal( 'foo' ); } ); } ); describe( 'enterBlock', () => { describe( 'collapsed selection', () => { test( 'does nothing in the root', 'foo[]bar', 'foo[]bar', { defaultBlockName: 'p' } ); test( 'splits block', '

x

foo[]bar

y

', '

x

foo

[]bar

y

', { defaultBlockName: 'p' } ); test( 'splits block (other than default)', '

x

foo[]bar

y

', '

x

foo[]bar

y

', { defaultBlockName: 'p' } ); test( 'splits block at the end', '

x

foo[]

y

', '

x

foo

[]

y

', { defaultBlockName: 'p' } ); test( 'splits block at the beginning', '

x

[]foo

y

', '

x

[]foo

y

', { defaultBlockName: 'p' } ); test( 'splits block at the beginning (other than default)', '

x

[]foo

y

', '

x

[]foo

y

', { defaultBlockName: 'p' } ); test( 'creates default block when leaving other block', 'foo[]

x

', 'foo

[]

x

', { defaultBlockName: 'p' } ); test( 'does not rename when default block is not allowed', 'foo[]

x

', 'foo[]

x

', { defaultBlockName: 'xxx' } ); test( 'inserts new block after empty one', '

x

[]

y

', '

x

[]

y

', { defaultBlockName: 'p' } ); test( 'inserts new block after empty one (other than default)', '

x

[]

y

', '

x

[]

y

', { defaultBlockName: 'p' } ); } ); describe( 'non-collapsed selection', () => { test( 'only deletes the content when directly in the root', 'fo[ob]ar', 'fo[]ar', { defaultBlockName: 'p' } ); test( 'deletes text and splits', '

ab[cd]ef

ghi

', '

ab

[]ef

ghi

', { defaultBlockName: 'p' } ); test( 'deletes text and splits (other than default)', 'ab[cd]ef', 'ab[]ef', { defaultBlockName: 'p' } ); test( 'places selection in the 2nd element', 'ab[c

d]ef

ghi

', 'ab

[]ef

ghi

', { defaultBlockName: 'p' } ); test( 'leaves one empty element after one was fully selected', '

x

[abcdef]

y

', '

x

[]

y

', { defaultBlockName: 'p' } ); test( 'leaves one (default) empty element after one was fully selected', '[abcdef]', '

[]

', { defaultBlockName: 'p' } ); test( 'leaves one (default) empty element after two were fully selected', '[abc

def]

', '

[]

', { defaultBlockName: 'p' } ); it( 'leaves one (default) 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; enterBlock( doc.batch(), doc.selection, { defaultBlockName: 'p' } ); expect( getData( doc ) ).to.equal( '

[]

' ); } ); it( 'uses composer.deleteContents', () => { const spy = sinon.spy(); doc.composer.on( 'deleteContents', spy ); setData( doc, '

[x]

' ); enterBlock( doc.batch(), doc.selection, { defaultBlockName: 'p' } ); expect( spy.calledOnce ).to.be.true; } ); } ); function test( title, input, output, options ) { it( title, () => { setData( doc, input ); enterBlock( doc.batch(), doc.selection, options ); expect( getData( doc ) ).to.equal( output ); } ); } } );