/**
* @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 CodeBlockEditing from '../src/codeblockediting';
import CodeBlockCommand from '../src/codeblockcommand';
import AlignmentEditing from '@ckeditor/ckeditor5-alignment/src/alignmentediting';
import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
describe( 'CodeBlockCommand', () => {
let editor, model, command;
beforeEach( () => {
return ModelTestEditor
.create( {
plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ]
} )
.then( newEditor => {
editor = newEditor;
model = editor.model;
command = new CodeBlockCommand( editor );
} );
} );
afterEach( () => {
return editor.destroy();
} );
describe( '#value', () => {
it( 'should be true when the first selected element is a codeBlock element (selection inside code block)', () => {
setModelData( model, 'f[]oo' );
expect( command.value ).to.equal( 'foo' );
} );
it( 'should be true when the first selected element is a codeBlock element (other blocks in selection are not code block)', () => {
setModelData( model, 'f[ooba]r' );
expect( command.value ).to.equal( 'foo' );
} );
it( 'should be false when the first selected element is not a code block (all blocks are not code block)', () => {
setModelData( model, 'f[]oo' );
expect( command.value ).to.equal( false );
} );
it( 'should be false when the first selected element is not a code block (selection ends in code block)', () => {
setModelData( model, 'f[ooba]r' );
expect( command.value ).to.equal( false );
} );
} );
describe( '#isEnabled', () => {
it( 'should be true when the first selected block is a codeBlock (selection inside code block)', () => {
setModelData( model, 'f[]oo' );
expect( command.isEnabled ).to.equal( true );
} );
it( 'should be true when the first selected block is a codeBlock (other blocks in selection are not code block)', () => {
setModelData( model, 'f[ooba]r' );
expect( command.isEnabled ).to.equal( true );
} );
it( 'should be true when the first selected block can be a codeBlock (collapsed selection)', () => {
setModelData( model, 'f[]oo' );
expect( command.isEnabled ).to.equal( true );
} );
it( 'should be true when the first selected block can be a codeBlock (non-collapsed selection, ends in code block)', () => {
setModelData( model, 'f[ooba]r' );
expect( command.isEnabled ).to.equal( true );
} );
it( 'should be false when selected element is a limit element (selection on element)', () => {
model.schema.register( 'limit', {
inheritAllFrom: '$block',
isLimit: true
} );
setModelData( model, '[foo]' );
expect( command.isEnabled ).to.equal( false );
} );
it( 'should be false when selection starts in a blockless space', () => {
model.schema.extend( '$text', { allowIn: '$root' } );
setModelData( model, 'x[]x' );
expect( command.isEnabled ).to.equal( false );
} );
it( 'should be false when selected element is a limit element (selection has mixed limit and non-limit elements)', () => {
model.schema.register( 'limit', {
inheritAllFrom: '$block',
isLimit: true
} );
setModelData( model, 'f[ooba]r' );
expect( command.isEnabled ).to.equal( false );
} );
it( 'should be true when limit element is not the first selected element', () => {
model.schema.register( 'limit', {
inheritAllFrom: '$block',
isLimit: true
} );
setModelData( model, 'f[oobarbi]z' );
expect( command.isEnabled ).to.equal( true );
} );
it( 'should make it possible to disallow codeBlock using schema', () => {
model.schema.addChildCheck( ( context, childDef ) => {
if ( context.endsWith( 'blockQuote' ) && childDef.name === 'codeBlock' ) {
return false;
}
} );
setModelData( model, '
f[o]o
' );
expect( command.isEnabled ).to.equal( false );
} );
} );
describe( 'execute()', () => {
it( 'should change selected empty block to codeBlock', () => {
setModelData( model, '[]' );
command.execute();
expect( getModelData( model ) ).to.equal( '[]' );
} );
it( 'should change selected block to codeBlock', () => {
setModelData( model, 'fo[]o' );
command.execute();
expect( getModelData( model ) ).to.equal( 'fo[]o' );
} );
it( 'should change multiple selected block to codeBlock', () => {
setModelData( model, 'f[ooba]r' );
command.execute();
expect( getModelData( model ) ).to.equal(
'f[ooba]r' );
} );
it( 'should merge selected blocks with selected codeBlocks', () => {
setModelData( model, 'f[ooba]r' );
command.execute();
expect( getModelData( model ) ).to.equal(
'f[ooba]r' );
} );
it( 'should not merge codeBlock with siblings when siblings are not selected', () => {
setModelData( model,
'foo' +
'b[a]r' +
'biz'
);
command.execute();
expect( getModelData( model ) ).to.equal(
'foo' +
'b[a]r' +
'biz'
);
} );
it( 'should change selected empty codeBlock to paragraph', () => {
setModelData( model, '[]' );
command.execute();
expect( getModelData( model ) ).to.equal( '[]' );
} );
it( 'should change selected codeBlock to paragraph', () => {
setModelData( model, 'f[o]o' );
command.execute();
expect( getModelData( model ) ).to.equal( 'f[o]o' );
} );
it( 'should change selected multi-line codeBlock to paragraphs', () => {
setModelData( model,
'foob[]arbiz'
);
command.execute();
expect( getModelData( model ) ).to.equal(
'foo' +
'b[]ar' +
'biz'
);
} );
it( 'should filter out attributes from nodes changed to codeBlock', () => {
setModelData( model, '<$text bold="true">f[o]o$text>' );
command.execute();
expect( getModelData( model ) ).to.equal( 'f[o]o' );
} );
it( 'should use forceValue parameter', () => {
setModelData( model, 'f[o]o' );
command.execute( { forceValue: true } );
expect( getModelData( model ) ).to.equal( 'f[o]o' );
} );
it( 'should allow setting the language of the new block', () => {
setModelData( model, 'f[o]o' );
command.execute( { language: 'css' } );
expect( getModelData( model ) ).to.equal( 'f[o]o' );
} );
it( 'should allow changing the language of the existing block', () => {
setModelData( model, 'f[o]o' );
command.execute( { language: 'css', forceValue: true } );
expect( getModelData( model ) ).to.equal( 'f[o]o' );
} );
} );
describe( 'BlockQuote integration', () => {
it( 'should change a paragraph inside a blockQuote to codeBlock', () => {
setModelData( model, 'f[o]o
' );
command.execute();
expect( getModelData( model ) ).to.equal(
'f[o]o
' );
} );
it( 'should change a paragraph inside a blockQuote to codeBlock when blockQuote is selected with siblings', () => {
setModelData( model,
'f[oo' +
'bar
' +
'bi]z'
);
command.execute();
expect( getModelData( model ) ).to.equal(
'f[oo' +
'bar
' +
'bi]z'
);
} );
} );
} );