/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/* globals document */
import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import HorizontalLineEditing from '../src/horizontallineediting';
describe( 'HorizontalLineCommand', () => {
let editor, model, editorElement, command;
testUtils.createSinonSandbox();
beforeEach( () => {
editorElement = document.createElement( 'div' );
document.body.appendChild( editorElement );
return ClassicTestEditor
.create( editorElement, {
plugins: [ Paragraph, HorizontalLineEditing ]
} )
.then( newEditor => {
editor = newEditor;
model = editor.model;
command = editor.commands.get( 'horizontalLine' );
} );
} );
afterEach( () => {
return editor.destroy()
.then( () => {
editorElement.remove();
} );
} );
describe( 'isEnabled', () => {
it( 'should be true when the selection directly in the root', () => {
model.enqueueChange( 'transparent', () => {
setModelData( model, '[]' );
command.refresh();
expect( command.isEnabled ).to.be.true;
} );
} );
it( 'should be true when the selection is in empty block', () => {
setModelData( model, '[]' );
expect( command.isEnabled ).to.be.true;
} );
it( 'should be true when the selection directly in a paragraph', () => {
setModelData( model, 'foo[]' );
expect( command.isEnabled ).to.be.true;
} );
it( 'should be true when the selection directly in a block', () => {
model.schema.register( 'block', { inheritAllFrom: '$block' } );
model.schema.extend( '$text', { allowIn: 'block' } );
editor.conversion.for( 'downcast' ).elementToElement( { model: 'block', view: 'block' } );
setModelData( model, 'foo[]' );
expect( command.isEnabled ).to.be.true;
} );
it( 'should be false when the selection is on other horizontal line element', () => {
setModelData( model, '[]' );
expect( command.isEnabled ).to.be.false;
} );
it( 'should be false when the selection is on other object', () => {
model.schema.register( 'object', { isObject: true, allowIn: '$root' } );
editor.conversion.for( 'downcast' ).elementToElement( { model: 'object', view: 'object' } );
setModelData( model, '[]' );
expect( command.isEnabled ).to.be.false;
} );
it( 'should be true when the selection is inside block element inside isLimit element which allows horizontal line', () => {
model.schema.register( 'table', { allowWhere: '$block', isLimit: true, isObject: true, isBlock: true } );
model.schema.register( 'tableRow', { allowIn: 'table', isLimit: true } );
model.schema.register( 'tableCell', { allowIn: 'tableRow', isLimit: true } );
model.schema.extend( '$block', { allowIn: 'tableCell' } );
editor.conversion.for( 'downcast' ).elementToElement( { model: 'table', view: 'table' } );
editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableRow', view: 'tableRow' } );
editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableCell', view: 'tableCell' } );
setModelData( model, '
foo[]
' );
} );
it( 'should be false when schema disallows horizontal line', () => {
model.schema.register( 'block', { inheritAllFrom: '$block' } );
model.schema.extend( 'paragraph', { allowIn: 'block' } );
// Block horizontal line in block.
model.schema.addChildCheck( ( context, childDefinition ) => {
if ( childDefinition.name === 'horizontalLine' && context.last.name === 'block' ) {
return false;
}
} );
editor.conversion.for( 'downcast' ).elementToElement( { model: 'block', view: 'block' } );
setModelData( model, '[]' );
expect( command.isEnabled ).to.be.false;
} );
} );
describe( 'execute()', () => {
beforeEach( () => {
model.schema.register( 'heading1', { inheritAllFrom: '$block' } );
editor.conversion.elementToElement( { model: 'heading1', view: 'h1' } );
model.schema.register( 'media', { allowWhere: '$block' } );
editor.conversion.elementToElement( { model: 'media', view: 'div' } );
} );
it( 'should create a single batch', () => {
setModelData( model, 'foo[]' );
const spy = sinon.spy();
model.document.on( 'change', spy );
command.execute();
sinon.assert.calledOnce( spy );
} );
it( 'should insert a horizontal line in an empty root and select it (a paragraph cannot be inserted)', () => {
// Block a paragraph in $root.
model.schema.addChildCheck( ( context, childDefinition ) => {
if ( childDefinition.name === 'paragraph' && context.last.name === '$root' ) {
return false;
}
} );
setModelData( model, '[]' );
command.execute();
expect( getModelData( model ) ).to.equal( '[]' );
} );
it( 'should split an element where selection is placed and insert a horizontal line (non-collapsed selection)', () => {
setModelData( model, 'f[o]o' );
command.execute();
expect( getModelData( model ) ).to.equal(
'f[]o'
);
} );
it( 'should split an element where selection is placed and insert a horizontal line (collapsed selection)', () => {
setModelData( model, 'fo[]o' );
command.execute();
expect( getModelData( model ) ).to.equal(
'fo[]o'
);
} );
it( 'should create an empty paragraph after inserting a horizontal line after a paragraph and place selection inside', () => {
setModelData( model, 'foo[]' );
command.execute();
expect( getModelData( model ) ).to.equal(
'foo[]'
);
} );
it( 'should create an empty paragraph after inserting a horizontal line after a heading and place selection inside', () => {
setModelData( model, 'foo[]' );
command.execute();
expect( getModelData( model ) ).to.equal(
'foo[]'
);
} );
it( 'should create an empty paragraph after inserting a horizontal line and next element must not having text', () => {
setModelData( model, 'foo[]' );
command.execute();
expect( getModelData( model ) ).to.equal(
'foo[]'
);
} );
it( 'should create an empty paragraph after inserting a horizontal line in heading and next element must not having text', () => {
setModelData( model, 'foo[]' );
command.execute();
expect( getModelData( model ) ).to.equal(
'foo[]'
);
} );
it( 'should not create an empty paragraph if a horizontal line split an element with text', () => {
setModelData( model, 'foo[]bar' );
command.execute();
expect( getModelData( model ) ).to.equal(
'foo[]bar'
);
} );
it( 'should replace an empty paragraph with a horizontal line and insert another paragraph next to', () => {
setModelData( model, '[]' );
command.execute();
expect( getModelData( model ) ).to.equal(
'[]'
);
} );
it( 'should replace an empty paragraph with a horizontal line and move the selection to next paragraph', () => {
setModelData( model, 'foo[]bar' );
command.execute();
expect( getModelData( model ) ).to.equal(
'foo[]bar'
);
} );
it( 'should replace an empty paragraph with a horizontal line and move the selection to next element that has text', () => {
setModelData( model, 'foo[]bar' );
command.execute();
expect( getModelData( model ) ).to.equal(
'foo[]bar'
);
} );
it( 'should replace an empty block element with a horizontal line and insert a paragraph next to', () => {
setModelData( model, '[]' );
command.execute();
expect( getModelData( model ) ).to.equal(
'[]'
);
} );
it( 'should move the selection to next element if it allows having text (paragraph + heading)', () => {
setModelData( model, 'foo[]bar' );
command.execute();
expect( getModelData( model ) ).to.equal(
'foo[]bar'
);
} );
it( 'should move the selection to next element if it allows having text (heading + paragraph)', () => {
setModelData( model, 'foo[]bar' );
command.execute();
expect( getModelData( model ) ).to.equal(
'foo[]bar'
);
} );
} );
} );