/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
import BlockQuoteEngine from '../src/blockquoteengine';
import BlockQuoteCommand from '../src/blockquotecommand';
import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
import Command from '@ckeditor/ckeditor5-core/src/command';
describe( 'BlockQuoteCommand', () => {
let editor, model, command;
beforeEach( () => {
return VirtualTestEditor
.create( {
plugins: [ BlockQuoteEngine ]
} )
.then( newEditor => {
editor = newEditor;
model = editor.model;
model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
model.schema.register( 'heading', { inheritAllFrom: '$block' } );
model.schema.register( 'widget' );
model.schema.extend( 'widget', {
allowIn: '$root',
isLimit: true
} );
model.schema.extend( '$text', { allowIn: 'widget' } );
editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'paragraph', view: 'p' } ) );
editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'heading', view: 'h' } ) );
editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'widget', view: 'widget' } ) );
command = editor.commands.get( 'blockQuote' );
} );
} );
afterEach( () => {
editor.destroy();
} );
it( 'is a command', () => {
expect( BlockQuoteCommand.prototype ).to.be.instanceOf( Command );
expect( command ).to.be.instanceOf( Command );
} );
describe( 'value', () => {
it( 'is false when selection is not in a block quote', () => {
setModelData( model, '
' ); expect( command ).to.have.property( 'value', false ); } ); it( 'is false when selection starts in a blockless space', () => { model.schema.extend( '$text', { allowIn: '$root' } ); setModelData( model, 'x[]x' ); expect( command ).to.have.property( 'value', false ); } ); it( 'is true when selection is in a block quote', () => { setModelData( model, 'y]y
' ); expect( command ).to.have.property( 'value', true ); } ); it( 'is true when selection starts in a block quote', () => { setModelData( model, 'x[]x
x[x
' ); expect( command ).to.have.property( 'isEnabled', true ); } ); it( 'is true when selection starts in a block which can be wrapped with blockQuote', () => { setModelData( model, 'x[]x
' + 'x[]x
abc
x{}x
def
' ); } ); it( 'should wrap multiple blocks', () => { setModelData( model, '' + '' ); expect( getViewData( editor.editing.view ) ).to.equal( 'a[bc ' + 'xx ' + 'de]f ' + '
' ); } ); it( 'should merge with an existing quote', () => { setModelData( model, 'a{bc xx
de}f
' + 'x]x yy
' + '' + 'a[bc ' + 'x]x ' + 'yy ' + '
a{bc x}x
yy
def
' ); } ); it( 'should not merge with a quote preceding the current block', () => { setModelData( model, '' + 'abc
' + 'abc
' ); expect( getViewData( editor.editing.view ) ).to.equal( 'x[]x
' + 'abc
' ); } ); it( 'should not merge with a quote following the current block', () => { setModelData( model, 'x{}x
' ); editor.execute( 'blockQuote' ); expect( getModelData( model ) ).to.equal( 'abc
' + 'x[]x
' ); expect( getViewData( editor.editing.view ) ).to.equal( 'abc
' + 'x{}x
' ); } ); it( 'should merge with an existing quote (more blocks)', () => { setModelData( model, 'abc
' + 'x]x
' + '' + 'a[bc ' + 'def ' + 'x]x ' + '
a{bc def
x}x
ghi
' ); } ); it( 'should not wrap non-block content', () => { setModelData( model, '' + '' + 'a[bc ' + '
' + '' ); expect( getViewData( editor.editing.view ) ).to.equal( 'de]f ' + '
a{bc
' ); } ); it( 'should correctly wrap and merge groups of blocks', () => { setModelData( model, 'de}f
' + 'ghi
' + 'a[bc
' + 'def ghi
' ); expect( getViewData( editor.editing.view ) ).to.equal( 'jk]l
' + 'a{bc
' + 'def
ghi
' ); } ); it( 'should correctly merge a couple of subsequent quotes', () => { setModelData( model, 'jk}l
' + 'def
' + 'jkl
' + '' + 'a[bc ' + 'def ' + 'ghi ' + 'jkl ' + 'mn]o ' + '
x
' + '' + '' + 'a{bc
' + 'def
' + 'ghi
' + 'jkl
' + 'mn}o
' + '
y
' ); } ); it( 'should not wrap a block which can not be in a quote', () => { // blockQuote is allowed in root, but fooBlock can not be inside blockQuote. model.schema.register( 'fooBlock', { inheritAllFrom: '$block' } ); model.schema.addChildCheck( ( ctx, childDef ) => { if ( ctx.endsWith( 'blockQuote' ) && childDef.name == 'fooBlock' ) { return false; } } ); editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'fooBlock', view: 'fooblock' } ) ); setModelData( model, '' + '' + 'a[bc ' + '
' + '' ); } ); it( 'should not wrap a block which parent does not allow quote inside itself', () => { // blockQuote is not be allowed in fooWrapper, but fooBlock can be inside blockQuote. model.schema.register( 'fooWrapper' ); model.schema.register( 'fooBlock', { inheritAllFrom: '$block' } ); model.schema.extend( 'fooWrapper', { allowIn: '$root' } ); model.schema.extend( 'fooBlock', { allowIn: 'fooWrapper' } ); editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'fooWrapper', view: 'foowrapper' } ) ); editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'fooBlock', view: 'fooblock' } ) ); setModelData( model, 'de]f ' + '
' + '' + 'a[bc ' + '
' + '' ); } ); } ); describe( 'removing quote', () => { it( 'should unwrap a single block', () => { setModelData( model, 'de]f ' + '
' + 'x[]x
abc
x{}x
def
' ); } ); it( 'should unwrap multiple blocks', () => { setModelData( model, '' + '' ); editor.execute( 'blockQuote' ); expect( getModelData( model ) ).to.equal( 'a[bc ' + 'xx ' + 'de]f ' + '
a{bc
xx
de}f
' ); } ); it( 'should unwrap only the selected blocks - at the beginning', () => { setModelData( model, '' + '' + 'a[b]c ' + 'xx ' + '
' + '' + 'xx ' + '
xx
a{b}c
xx
yy
' ); } ); it( 'should unwrap only the selected blocks - at the end', () => { setModelData( model, '' + '' + 'abc ' + 'x[x ' + '
' + '' + 'abc ' + '
abc
x{x
de}f
' ); } ); it( 'should unwrap only the selected blocks - in the middle', () => { setModelData( model, '' + '' + 'abc ' + 'c[]de ' + 'fgh ' + '
' + 'abc
' + 'fgh
xx
' + '' + 'abc
c{}de
' + '' + 'fgh
xx
' ); } ); it( 'should remove multiple quotes', () => { setModelData( model, '' + 'a[bc
' + 'def ghi
' ); editor.execute( 'blockQuote' ); expect( getModelData( model ) ).to.equal( 'de]f ghi
' ); expect( getViewData( editor.editing.view ) ).to.equal( 'ghi
a{bc
' + 'xx
' + 'def
ghi
' + 'yy
' + 'de}f
' + '' ); } ); } ); } ); } );ghi