/** * @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, 'x
foo[]bar
y
', 'x
foo
[]bar
y
', { defaultBlockName: 'p' } ); test( 'splits block (other than default)', 'x
y
', 'x
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
y
', 'x
y
', { defaultBlockName: 'p' } ); test( 'creates default block when leaving other block', 'x
', '[]
x
', { defaultBlockName: 'p' } ); test( 'does not rename when default block is not allowed', 'x
', '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)', 'd]ef
ghi
', '[]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', '[]
', { defaultBlockName: 'p' } ); test( 'leaves one (default) empty element after two were fully selected', 'def]
', '[]
', { defaultBlockName: 'p' } ); it( 'leaves one (default) empty element after two were fully selected (backward)', () => { setData( doc, '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 ); } ); } } );