/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ 'use strict'; import VirtualTestEditor from '/tests/ckeditor5/_utils/virtualtesteditor.js'; import { default as EnterCommand, enterBlock } from '/ckeditor5/enter/entercommand.js'; import { getData, setData } from '/tests/engine/_utils/model.js'; let editor, doc; beforeEach( () => { return VirtualTestEditor.create( ) .then( newEditor => { editor = newEditor; doc = editor.document; doc.createRoot(); const command = new EnterCommand( editor ); editor.commands.set( 'enter', command ); const 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' ); } ); } ); describe( 'EnterCommand', () => { it( 'enters a block using enqueueChanges', () => { const spy = sinon.spy( doc, 'enqueueChanges' ); setData( doc, '

foo

' ); editor.execute( 'enter' ); expect( getData( doc, { withoutSelection: true } ) ).to.equal( '

foo

' ); expect( spy.calledOnce ).to.be.true; } ); } ); describe( 'enterBlock', () => { describe( 'collapsed selection', () => { test( 'does nothing in the root', 'foobar', 'foobar', { defaultBlockName: 'p' } ); test( 'splits block', '

x

foobar

y

', '

x

foo

bar

y

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

x

foobar

y

', '

x

foobar

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', 'foobar', 'foar', { defaultBlockName: 'p' } ); test( 'deletes text and splits', '

abcdef

ghi

', '

ab

ef

ghi

', { defaultBlockName: 'p' } ); test( 'deletes text and splits (other than default)', 'abcdef', 'abef', { defaultBlockName: 'p' } ); test( 'places selection in the 2nd element', 'abc

def

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' } ); test( 'leaves one (default) empty element after two were fully selected (backward)', 'abc

def

', '

', { defaultBlockName: 'p' } ); 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 ); } ); } } );