/** * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import Paragraph from 'ckeditor5-paragraph/src/paragraph'; import VirtualTestEditor from 'ckeditor5-core/tests/_utils/virtualtesteditor'; import { getData as getModelData, setData as setModelData, stringify as stringifyModel } from 'ckeditor5-engine/src/dev-utils/model'; import { getData as getViewData } from 'ckeditor5-engine/src/dev-utils/view'; import buildViewConverter from 'ckeditor5-engine/src/conversion/buildviewconverter'; import ModelDocumentFragment from 'ckeditor5-engine/src/model/documentfragment'; import ModelText from 'ckeditor5-engine/src/model/text'; 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 not autoparagraph text (in a context which does not allow paragraphs', () => { doc.schema.registerItem( 'specialRoot' ); const modelFragment = editor.data.parse( 'foo', 'specialRoot' ); expect( stringifyModel( modelFragment ) ) .to.equal( '' ); } ); 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

bar

bom

' ); 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', () => { 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
bom

bim

' ); expect( stringifyModel( modelFragment ) ) .to.equal( '
foo
' + '
bombim
' ); } ); 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' ); } ); it( 'should not fail when text is not allowed in paragraph', () => { doc.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 ); } ); it( 'does not break converting inline elements', () => { doc.schema.allow( { name: '$inline', attributes: [ 'bold' ] } ); buildViewConverter().for( editor.data.viewToModel ) .fromElement( 'b' ) .toAttribute( 'bold', true ); const modelFragment = editor.data.parse( 'foobarbom' ); // The result of this test is wrong due to https://github.com/ckeditor/ckeditor5-paragraph/issues/10. // It's meant to catch the odd situation in mergeSubsequentParagraphs when data.output may be an array // for a while expect( stringifyModel( modelFragment ) ).to.equal( 'foobarbom' ); } ); // 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( '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)', () => { doc.schema.registerItem( 'div' ); doc.schema.registerItem( 'specialRoot' ); doc.schema.allow( { name: 'div', inside: 'specialRoot' } ); doc.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( '
  1. c
' ); expect( stringifyModel( modelFragment ) ) .to.equal( 'abc' ); } ); it( 'should convert ul,ol>li (in clipboard holder)', () => { const modelFragment = editor.data.parse( '
  1. 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( '
ab
cd
' ); expect( stringifyModel( modelFragment ) ) .to.equal( 'abcd' ); } ); it( 'should convert td (in clipboardHolder)', () => { const modelFragment = editor.data.parse( '
ab
cd
', '$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( '
bom

bim

' ); expect( stringifyModel( modelFragment ) ) .to.equal( '
foobar
' + '
bombim
' ); } ); it( 'should convert li inside disallowed container', () => { const modelFragment = editor.data.parse( '
bom

bim

' ); 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( 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' ); } ); } ); } );