/** * @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 HeadingCommand from '/ckeditor5/heading/headingcommand.js'; import Range from '/ckeditor5/engine/model/range.js'; import { setData, getData } from '/ckeditor5/engine/dev-utils/model.js'; const formats = [ { id: 'paragraph', viewElement: 'p', default: true }, { id: 'heading1', viewElement: 'h2' }, { id: 'heading2', viewElement: 'h3' }, { id: 'heading3', viewElement: 'h4' } ]; describe( 'HeadingCommand', () => { let editor, document, command, root, schema; beforeEach( () => { return ModelTestEditor.create() .then( newEditor => { editor = newEditor; document = editor.document; command = new HeadingCommand( editor, formats ); schema = document.schema; for ( let format of formats ) { schema.registerItem( format.id, '$block' ); } schema.registerItem( 'b', '$inline' ); root = document.getRoot(); } ); } ); afterEach( () => { command.destroy(); } ); describe( 'value', () => { for ( let format of formats ) { test( format ); } function test( format ) { it( `equals ${ format.id } when collapsed selection is placed inside ${ format.id } element`, () => { setData( document, `<${ format.id }>foobar` ); const element = root.getChild( 0 ); document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) ); expect( command.value ).to.equal( format ); } ); } it( 'should be equal to defaultFormat if format has not been found', () => { schema.registerItem( 'div', '$block' ); setData( document, '
xyz
' ); const element = root.getChild( 0 ); document.selection.addRange( Range.createFromParentsAndOffsets( element, 1, element, 1 ) ); expect( command.value ).to.equal( command.defaultFormat ); } ); } ); describe( '_doExecute', () => { describe( 'custom options', () => { it( 'should use provided batch', () => { const batch = editor.document.batch(); setData( document, 'foo[]bar' ); expect( batch.deltas.length ).to.equal( 0 ); command._doExecute( { batch } ); expect( batch.deltas.length ).to.be.above( 0 ); } ); } ); describe( 'collapsed selection', () => { let convertTo = formats[ formats.length - 1 ]; for ( let format of formats ) { test( format, convertTo ); convertTo = format; } it( 'uses paragraph as default value', () => { setData( document, 'foo[]bar' ); command._doExecute(); expect( getData( document ) ).to.equal( 'foo[]bar' ); } ); it( 'converts to default format when executed with already applied format', () => { setData( document, 'foo[]bar' ); command._doExecute( { formatId: 'heading1' } ); expect( getData( document ) ).to.equal( 'foo[]bar' ); } ); it( 'converts topmost blocks', () => { setData( document, 'foo[]bar' ); command._doExecute( { formatId: 'heading1' } ); expect( getData( document ) ).to.equal( 'foo[]bar' ); } ); function test( from, to ) { it( `converts ${ from.id } to ${ to.id } on collapsed selection`, () => { setData( document, `<${ from.id }>foo[]bar` ); command._doExecute( { formatId: to.id } ); expect( getData( document ) ).to.equal( `<${ to.id }>foo[]bar` ); } ); } } ); describe( 'non-collapsed selection', () => { let convertTo = formats[ formats.length - 1 ]; for ( let format of formats ) { test( format, convertTo ); convertTo = format; } it( 'converts all elements where selection is applied', () => { setData( document, 'foo[bar]baz' ); command._doExecute( { formatId: 'paragraph' } ); expect( getData( document ) ).to.equal( 'foo[bar]baz' ); } ); it( 'resets to default value all elements with same format', () => { setData( document, 'foo[barbaz]' ); command._doExecute( { formatId: 'heading1' } ); expect( getData( document ) ).to.equal( 'foo[barbaz]' ); } ); function test( from, to ) { it( `converts ${ from.id } to ${ to.id } on non-collapsed selection`, () => { setData( document, `<${ from.id }>foo[bar<${ from.id }>baz]qux` ); command._doExecute( { formatId: to.id } ); expect( getData( document ) ).to.equal( `<${ to.id }>foo[bar<${ to.id }>baz]qux` ); } ); } } ); } ); } );