/**
* @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
import Paragraph from 'ckeditor5/paragraph/paragraph.js';
import VirtualTestEditor from 'tests/core/_utils/virtualtesteditor.js';
import {
getData as getModelData,
setData as setModelData,
stringify as stringifyModel
} from 'ckeditor5/engine/dev-utils/model.js';
import { getData as getViewData } from 'ckeditor5/engine/dev-utils/view.js';
import buildViewConverter from 'ckeditor5/engine/conversion/buildviewconverter.js';
describe( 'Paragraph feature', () => {
let editor, doc;
beforeEach( () => {
return VirtualTestEditor.create( {
plugins: [ Paragraph ]
} )
.then( newEditor => {
editor = newEditor;
doc = editor.document;
} );
} );
it( 'should be loaded', () => {
expect( editor.plugins.get( Paragraph ) ).to.be.instanceOf( Paragraph );
} );
it( 'should set proper schema rules', () => {
expect( doc.schema.hasItem( 'paragraph' ) ).to.be.true;
expect( doc.schema.check( { name: 'paragraph', inside: '$root' } ) ).to.be.true;
expect( doc.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( doc, { withoutSelection: true } ) ).to.equal( 'foobar' );
expect( editor.getData() ).to.equal( 'foobar
' );
} );
it( 'should convert paragraph only', () => {
editor.setData( 'foobazbar
' );
expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( 'foobazbar' );
expect( editor.getData() ).to.equal( 'foobazbar
' );
} );
it( 'should convert multiple paragraphs', () => {
editor.setData( 'foo
baz
' );
expect( getModelData( doc, { 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( doc, { 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 autoparagraph text next to allowed element', () => {
doc.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 inline nodes into one paragraph', () => {
const modelFragment = editor.data.parse( 'foobarbom' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'foobarbom' );
} );
it( 'should not autoparagraph 3 inline inline nodes (in clipboardHolder)', () => {
const modelFragment = editor.data.parse( 'foobarbom', '$clipboardHolder' );
expect( stringifyModel( modelFragment ) )
.to.equal( 'foobarbom' );
} );
it( 'should autoparagraph text inside converted container', () => {
doc.schema.registerItem( 'div' );
doc.schema.allow( { name: 'div', inside: '$root' } );
doc.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', () => {
doc.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', () => {
doc.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' );
} );
} );
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 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', () => {
doc.schema.registerItem( 'div' );
doc.schema.allow( { name: 'div', inside: '$root' } );
doc.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'
);
} );
} );
} );
describe( 'editing pipeline conversion', () => {
it( 'should convert paragraph', () => {
setModelData( doc, '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( doc ) ).to.equal( '[]foo' );
} );
} );
} );