/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
import Paragraph from '../src/paragraph';
import ParagraphCommand from '../src/paragraphcommand';
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
import {
getData as getModelData,
setData as setModelData,
stringify as stringifyModel
} from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
import ModelDocumentFragment from '@ckeditor/ckeditor5-engine/src/model/documentfragment';
import ModelText from '@ckeditor/ckeditor5-engine/src/model/text';
import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
describe( 'Paragraph feature', () => {
let model, editor, doc, root;
beforeEach( () => {
return VirtualTestEditor
.create( { plugins: [ Paragraph ] } )
.then( newEditor => {
editor = newEditor;
model = editor.model;
doc = model.document;
root = doc.getRoot();
} );
} );
it( 'should be loaded', () => {
expect( editor.plugins.get( Paragraph ) ).to.be.instanceOf( Paragraph );
} );
it( 'should set proper schema rules', () => {
expect( model.schema.hasItem( 'paragraph' ) ).to.be.true;
expect( model.schema.check( { name: 'paragraph', inside: '$root' } ) ).to.be.true;
expect( model.schema.check( { name: '$inline', inside: 'paragraph' } ) ).to.be.true;
} );
it( 'should have a static paragraphLikeElements property', () => {
expect( Paragraph ).to.have.property( 'paragraphLikeElements' );
} );
describe( 'data pipeline conversions', () => {
it( 'should convert paragraph', () => {
editor.setData( '
foobar
' );
expect( getModelData( model, { withoutSelection: true } ) ).to.equal( 'foobar' );
expect( editor.getData() ).to.equal( 'foobar
' );
} );
it( 'should convert paragraph only', () => {
editor.setData( 'foobazbar
' );
expect( getModelData( model, { withoutSelection: true } ) ).to.equal( 'foobazbar' );
expect( editor.getData() ).to.equal( 'foobazbar
' );
} );
it( 'should convert multiple paragraphs', () => {
editor.setData( 'foo
baz
' );
expect( getModelData( model, { withoutSelection: true } ) ).to.equal( 'foobaz' );
expect( editor.getData() ).to.equal( 'foo
baz
' );
} );
describe( 'generic text converter (text autoparagraphing)', () => {
it( 'should autoparagraph text', () => {
editor.setData( 'foo' );
expect( getModelData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
expect( editor.getData() ).to.equal( 'foo
' );
} );
it( 'should autoparagraph any inline element', () => {
editor.model.schema.registerItem( 'span', '$inline' );
editor.model.schema.allow( { name: '$text', inside: '$inline' } );
buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView ).fromElement( 'span' ).toElement( 'span' );
buildViewConverter().for( editor.data.viewToModel ).fromElement( 'span' ).toElement( 'span' );
editor.setData( 'foo' );
expect( getModelData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
expect( editor.getData() ).to.equal( 'foo
' );
} );
it( 'should not autoparagraph text (in clipboard holder)', () => {
const modelFragment = editor.data.parse( 'foo', '$clipboardHolder' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'foo' );
} );
it( 'should not autoparagraph text (in a context which does not allow paragraphs', () => {
model.schema.registerItem( 'specialRoot' );
const modelFragment = editor.data.parse( 'foo', 'specialRoot' );
expect( stringifyModel( modelFragment ) )
.to.equal( '' );
} );
it( 'should autoparagraph text next to allowed element', () => {
model.schema.registerItem( 'heading1', '$block' );
buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
const modelFragment = editor.data.parse( 'foo
barbom
' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'foobarbom' );
} );
it( 'should autoparagraph 3 inline nodes into one paragraph', () => {
const modelFragment = editor.data.parse( 'foobarbom' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'foobarbom' );
} );
it( 'should not autoparagraph 3 inline nodes (in clipboardHolder)', () => {
const modelFragment = editor.data.parse( 'foobarbom', '$clipboardHolder' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'foobarbom' );
} );
it( 'should autoparagraph text inside converted container', () => {
model.schema.registerItem( 'div' );
model.schema.allow( { name: 'div', inside: '$root' } );
model.schema.allow( { name: 'paragraph', inside: 'div' } );
buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
const modelFragment = editor.data.parse( 'foo
' );
expect( stringifyModel( modelFragment ) )
.to.equal(
'' +
''
);
} );
it( 'should autoparagraph text inside disallowed element next to allowed element', () => {
model.schema.registerItem( 'heading1', '$block' );
buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
const modelFragment = editor.data.parse( 'foo
bar' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'foobar' );
} );
it( 'should not autoparagraph text in disallowed element', () => {
model.schema.registerItem( 'heading1', '$block' );
buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
const modelFragment = editor.data.parse( 'foobar
' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'foobar' );
} );
it( 'should not fail when text is not allowed in paragraph', () => {
model.schema.disallow( { name: '$text', inside: [ '$root', 'paragraph' ] } );
const modelFragment = editor.data.parse( 'foo' );
expect( stringifyModel( modelFragment ) ).to.equal( '' );
} );
it( 'creates normalized model', () => {
const modelFragment = editor.data.parse( 'foobarbom' );
expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
} );
// This test was taken from the list package.
it( 'does not break when some converter returns nothing', () => {
editor.data.viewToModel.on( 'element:li', ( evt, data, consumable ) => {
consumable.consume( data.input, { name: true } );
}, { priority: 'highest' } );
const modelFragment = editor.data.parse( '' );
expect( stringifyModel( modelFragment ) ).to.equal( '' );
} );
describe( 'should not strip attribute elements when autoparagraphing texts', () => {
beforeEach( () => {
model.schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
buildViewConverter().for( editor.data.viewToModel )
.fromElement( 'b' )
.toAttribute( 'bold', true );
} );
it( 'inside document fragment', () => {
const modelFragment = editor.data.parse( 'foobarbom' );
expect( stringifyModel( modelFragment ) ).to.equal( 'foo<$text bold="true">bar$text>bom' );
} );
it( 'inside converted element', () => {
model.schema.registerItem( 'blockquote' );
model.schema.allow( { name: 'blockquote', inside: '$root' } );
model.schema.allow( { name: '$block', inside: 'blockquote' } );
buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView )
.fromElement( 'blockquote' )
.toElement( 'blockquote' );
buildViewConverter().for( editor.data.viewToModel ).fromElement( 'blockquote' ).toElement( 'blockquote' );
const modelFragment = editor.data.parse( 'foobarbom
' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'foo<$text bold="true">bar$text>bom
' );
} );
it( 'inside paragraph-like element', () => {
const modelFragment = editor.data.parse( 'foo
barbom
' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'foo<$text bold="true">bar$text>bom' );
} );
} );
} );
describe( 'generic block converter (paragraph-like element handling)', () => {
it( 'should convert h1+h2', () => {
const modelFragment = editor.data.parse( 'foo
bar
' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'foobar' );
} );
it( 'should convert h1+h2 (in clipboard holder)', () => {
const modelFragment = editor.data.parse( 'foo
bar
', '$clipboardHolder' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'foobar' );
} );
it( 'should not convert h1+h2 (in a context which does not allow paragraphs)', () => {
model.schema.registerItem( 'div' );
model.schema.registerItem( 'specialRoot' );
model.schema.allow( { name: 'div', inside: 'specialRoot' } );
model.schema.allow( { name: '$text', inside: 'div' } );
buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
const modelFragment = editor.data.parse( 'foo
bar
bom
', 'specialRoot' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'bom
' );
} );
it( 'should convert ul,ol>li', () => {
const modelFragment = editor.data.parse( '- c
' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'abc' );
} );
it( 'should convert ul,ol>li (in clipboard holder)', () => {
const modelFragment = editor.data.parse( '- c
', '$clipboardHolder' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'abc' );
} );
it( 'should convert ul>li>ul>li+li', () => {
const modelFragment = editor.data.parse( '' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'abc' );
} );
// "b" is not autoparagraphed because clipboard holder allows text nodes.
// There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
it( 'should convert ul>li>ul>li+li (in clipboard holder)', () => {
const modelFragment = editor.data.parse( '', '$clipboardHolder' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'abc' );
} );
it( 'should convert ul>li>p,text', () => {
const modelFragment = editor.data.parse( '' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'ab' );
} );
// "b" is not autoparagraphed because clipboard holder allows text nodes.
// There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
it( 'should convert ul>li>p,text (in clipboard holder)', () => {
const modelFragment = editor.data.parse( '', '$clipboardHolder' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'ab' );
} );
it( 'should convert td', () => {
const modelFragment = editor.data.parse(
''
);
expect( stringifyModel( modelFragment ) )
.to.equal( 'abcd' );
} );
it( 'should convert td (in clipboardHolder)', () => {
const modelFragment = editor.data.parse(
'',
'$clipboardHolder'
);
expect( stringifyModel( modelFragment ) )
.to.equal( 'abcd' );
} );
it( 'should convert li inside converted container', () => {
model.schema.registerItem( 'div' );
model.schema.allow( { name: 'div', inside: '$root' } );
model.schema.allow( { name: 'paragraph', inside: 'div' } );
buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
const modelFragment = editor.data.parse( '' );
expect( stringifyModel( modelFragment ) )
.to.equal(
'' +
''
);
} );
it( 'should convert li inside disallowed container', () => {
const modelFragment = editor.data.parse( '' );
expect( stringifyModel( modelFragment ) )
.to.equal(
'foobar' +
'bombim'
);
} );
it( 'creates normalized model', () => {
const modelFragment = editor.data.parse( 'foobar' );
expect( stringifyModel( modelFragment ) ).to.equal( 'foobar' );
expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
} );
} );
} );
describe( 'editing pipeline conversion', () => {
it( 'should convert paragraph', () => {
setModelData( model, 'foobar' );
expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '
foo
bar
' );
} );
} );
describe( 'autoparagraphing on data load', () => {
it( 'wraps text and place selection at the beginning of that paragraph', () => {
editor.setData( 'foo' );
expect( getModelData( model ) ).to.equal( '[]foo' );
} );
} );
describe( 'post-fixing empty roots', () => {
it( 'should fix empty roots after editor is initialised', () => {
expect( doc.getRoot().childCount ).to.equal( 1 );
expect( doc.getRoot().getChild( 0 ).is( 'paragraph' ) ).to.be.true;
} );
it( 'should fix root if it becomes empty', () => {
editor.setData( 'Foobar
' );
// Since `setData` first removes all contents from editor and then sets content during same enqueue
// changes block, the line below checks whether fixing empty roots does not kick too early and does not
// fix root if it is not needed.
expect( editor.getData() ).to.equal( 'Foobar
' );
model.change( writer => {
writer.remove( ModelRange.createIn( root ) );
} );
expect( doc.getRoot().childCount ).to.equal( 1 );
expect( doc.getRoot().getChild( 0 ).is( 'paragraph' ) ).to.be.true;
} );
it( 'should not fix root if it got content during changesDone event', () => {
// "Autoheading feature".
model.schema.registerItem( 'heading', '$block' );
buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView )
.fromElement( 'heading' )
.toElement( 'h1' );
doc.on( 'changesDone', () => {
const root = doc.getRoot();
if ( root.isEmpty ) {
model.change( writer => {
writer.insertElement( 'heading', root );
} );
}
} );
model.change( writer => {
writer.remove( ModelRange.createIn( root ) );
} );
expect( doc.getRoot().childCount ).to.equal( 1 );
expect( doc.getRoot().getChild( 0 ).name ).to.equal( 'heading' );
} );
it( 'should not fix root which does not allow paragraph', () => {
model.schema.disallow( { name: 'paragraph', inside: '$root' } );
model.change( writer => {
writer.remove( ModelRange.createIn( root ) );
} );
expect( editor.getData() ).to.equal( '' );
} );
it( 'should not fix root if change was in transparent batch', () => {
editor.setData( 'Foobar
' );
model.enqueueChange( 'transparent', writer => {
writer.remove( ModelRange.createIn( root ) );
} );
expect( editor.getData() ).to.equal( '' );
} );
} );
describe( 'command', () => {
it( 'should be set in the editor', () => {
expect( editor.commands.get( 'paragraph' ) ).to.be.instanceof( ParagraphCommand );
} );
} );
} );