/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
import ParagraphCommand from '../src/paragraphcommand';
import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
describe( 'ParagraphCommand', () => {
let editor, model, document, command, root, schema;
beforeEach( () => {
return ModelTestEditor.create().then( newEditor => {
editor = newEditor;
model = editor.model;
document = model.document;
schema = model.schema;
command = new ParagraphCommand( editor );
root = document.getRoot();
editor.commands.add( 'paragraph', command );
schema.register( 'paragraph', { inheritAllFrom: '$block' } );
schema.register( 'heading1', { inheritAllFrom: '$block' } );
schema.register( 'notBlock' );
schema.extend( 'notBlock', { allowIn: '$root' } );
schema.extend( '$text', { allowIn: 'notBlock' } );
} );
} );
afterEach( () => {
command.destroy();
} );
describe( 'value', () => {
it( 'responds to changes in selection (collapsed selection)', () => {
setData( model, 'foo[]bar' );
expect( command.value ).to.be.false;
setData( model, 'foo[]bar' );
expect( command.value ).to.be.true;
} );
it( 'responds to changes in selection (non–collapsed selection)', () => {
setData( model, '[foo]bar' );
expect( command.value ).to.be.false;
setData( model, '[foobar]' );
expect( command.value ).to.be.false;
setData( model, 'foo[bar]' );
expect( command.value ).to.be.true;
setData( model, 'foo[bar]' );
expect( command.value ).to.be.true;
setData( model, '[barfoo]' );
expect( command.value ).to.be.true;
} );
it( 'has proper value when inside non-block element', () => {
setData( model, '[foo]' );
expect( command.value ).to.be.false;
} );
it( 'has proper value when moved from block to element that is not a block', () => {
setData( model, '[foo]foo' );
const element = document.getRoot().getChild( 1 );
model.change( writer => {
writer.setSelection( writer.createRangeIn( element ) );
} );
expect( command.value ).to.be.false;
} );
it( 'should be refreshed after calling refresh()', () => {
setData( model, '[foo]foo' );
const element = document.getRoot().getChild( 1 );
model.change( writer => {
writer.setSelection( writer.createRangeIn( element ) );
expect( command.value ).to.be.true;
command.refresh();
expect( command.value ).to.be.false;
} );
} );
} );
describe( 'execute()', () => {
it( 'should update value after execution', () => {
setData( model, '[]' );
command.execute();
expect( getData( model ) ).to.equal( '[]' );
expect( command.value ).to.be.true;
} );
// https://github.com/ckeditor/ckeditor5-paragraph/issues/24
it( 'should not rename blocks which cannot become paragraphs (paragraph is not allowed in their parent)', () => {
model.schema.register( 'restricted', { allowIn: '$root' } );
model.schema.register( 'fooBlock', {
inheritAllFrom: '$block',
allowIn: 'restricted'
} );
model.schema.addChildCheck( ( ctx, childDef ) => {
if ( ctx.endsWith( 'restricted' ) && childDef.name == 'paragraph' ) {
return false;
}
} );
setData(
model,
'a[bc' +
'' +
'de]f'
);
command.execute();
expect( getData( model ) ).to.equal(
'a[bc' +
'' +
'de]f'
);
} );
it( 'should not rename blocks which cannot become paragraphs (block is an object)', () => {
model.schema.register( 'image', {
isBlock: true,
isObject: true,
allowIn: '$root'
} );
setData(
model,
'a[bc' +
'' +
'de]f'
);
command.execute();
expect( getData( model ) ).to.equal(
'a[bc' +
'' +
'de]f'
);
} );
it( 'should not rename blocks which already are pargraphs', () => {
setData( model, 'foo[bar]' );
model.change( writer => {
expect( writer.batch.operations.length ).to.equal( 0 );
command.execute();
expect( writer.batch.operations.length ).to.equal( 1 );
} );
} );
describe( 'custom options', () => {
it( 'should use parent batch', () => {
setData( model, 'foo[]bar' );
model.change( writer => {
expect( writer.batch.operations.length ).to.equal( 0 );
command.execute();
expect( writer.batch.operations.length ).to.above( 0 );
} );
} );
it( 'should use provided selection', () => {
setData( model, 'foo[]barbazqux' );
const secondToLastHeading = root.getChild( 1 );
const lastHeading = root.getChild( 2 );
const selection = model.createSelection( model.createRange(
model.createPositionAt( secondToLastHeading, 0 ),
model.createPositionAt( lastHeading, 1 )
) );
command.execute( { selection } );
expect( getData( model ) ).to.equal(
'foo[]barbazqux'
);
} );
} );
describe( 'collapsed selection', () => {
it( 'does nothing when executed with already applied', () => {
setData( model, 'foo[]bar' );
command.execute();
expect( getData( model ) ).to.equal( 'foo[]bar' );
} );
it( 'converts topmost blocks', () => {
schema.register( 'inlineImage', { allowWhere: '$text' } );
schema.extend( '$text', { allowIn: 'inlineImage' } );
setData( model, 'foo[]bar' );
command.execute();
expect( getData( model ) ).to.equal( 'foo[]bar' );
} );
} );
describe( 'non-collapsed selection', () => {
it( 'converts all elements where selection is applied', () => {
schema.register( 'heading2', { inheritAllFrom: '$block' } );
setData( model, 'foo[barbaz]' );
command.execute();
expect( getData( model ) ).to.equal(
'foo[barbaz]'
);
} );
it( 'converts all elements even if already anchored in paragraph', () => {
schema.register( 'heading2', { inheritAllFrom: '$block' } );
setData( model, 'foo[bar]' );
command.execute();
expect( getData( model ) ).to.equal( 'foo[bar]' );
} );
} );
} );
describe( 'isEnabled', () => {
it( 'should be enabled when inside another block', () => {
setData( model, 'f{}oo' );
expect( command.isEnabled ).to.be.true;
} );
it( 'should be disabled if inside non-block', () => {
setData( model, 'f{}oo' );
expect( command.isEnabled ).to.be.false;
} );
it( 'should be disabled if selection is placed on non-block element', () => {
setData( model, '[foo]' );
expect( command.isEnabled ).to.be.false;
} );
} );
} );