/** * @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 Paragraph from '../src/paragraph'; import ParagraphCommand from '../src/paragraphcommand'; import InsertParagraphCommand from '../src/insertparagraphcommand'; 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 ModelDocumentFragment from '@ckeditor/ckeditor5-engine/src/model/documentfragment'; import ModelText from '@ckeditor/ckeditor5-engine/src/model/text'; 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.isRegistered( 'paragraph' ) ).to.be.true; expect( model.schema.checkChild( [ '$root' ], 'paragraph' ) ).to.be.true; expect( model.schema.checkChild( [ 'paragraph' ], '$text' ) ).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.register( 'inline', { allowWhere: '$text' } ); editor.model.schema.extend( '$text', { allowIn: 'inline' } ); editor.conversion.for( 'downcast' ).elementToElement( { model: 'inline', view: 'span' } ); editor.conversion.for( 'upcast' ).elementToElement( { model: 'inline', view: 'span' } ); editor.setData( 'foo' ); expect( getModelData( model, { withoutSelection: true } ) ).to.equal( 'foo' ); expect( editor.getData() ).to.equal( '

foo

' ); } ); it( 'should autoparagraph any inline element with children', () => { editor.model.schema.register( 'inline', { allowWhere: '$text' } ); editor.model.schema.extend( '$text', { allowIn: 'inline' } ); editor.conversion.for( 'downcast' ).elementToElement( { model: 'inline', view: 'span' } ); editor.conversion.for( 'upcast' ).elementToElement( { model: 'inline', view: 'span' } ); editor.setData( 'f123oo' ); expect( getModelData( model, { withoutSelection: true } ) ).to.equal( 'f123oo' ); expect( editor.getData() ).to.equal( '

f123oo

' ); } ); 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.register( 'specialRoot' ); const modelFragment = editor.data.parse( 'foo', [ 'specialRoot' ] ); expect( stringifyModel( modelFragment ) ) .to.equal( '' ); } ); it( 'should autoparagraph text next to allowed element', () => { model.schema.register( 'heading1', { inheritAllFrom: '$block' } ); editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading1', view: 'h1' } ); 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', () => { model.schema.register( 'div' ); model.schema.extend( 'div', { allowIn: '$root' } ); model.schema.extend( 'paragraph', { allowIn: 'div' } ); editor.conversion.for( 'upcast' ).elementToElement( { model: 'div', view: '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', () => { model.schema.register( 'heading1', { inheritAllFrom: '$block' } ); editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading1', view: 'h1' } ); const modelFragment = editor.data.parse( '

foo

bar
' ); expect( stringifyModel( modelFragment ) ) .to.equal( 'foobar' ); } ); it( 'should not autoparagraph text in disallowed element', () => { model.schema.register( 'heading1', { inheritAllFrom: '$block' } ); editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading1', view: 'h1' } ); 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.addChildCheck( ( ctx, childDef ) => { if ( ctx.endsWith( '$root paragraph' ) && childDef.name == '$text' ) { return false; } } ); 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.upcastDispatcher.on( 'element:li', ( evt, data, conversionApi ) => { conversionApi.consumable.consume( data.viewItem, { 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.extend( '$text', { allowAttributes: 'bold' } ); editor.conversion.for( 'upcast' ).elementToAttribute( { view: 'b', model: 'bold' } ); } ); it( 'inside document fragment', () => { const modelFragment = editor.data.parse( 'foobarbom' ); expect( stringifyModel( modelFragment ) ).to.equal( 'foo<$text bold="true">barbom' ); } ); it( 'inside converted element', () => { model.schema.register( 'blockQuote', { allowIn: '$root' } ); model.schema.extend( '$block', { allowIn: 'blockQuote' } ); editor.conversion.for( 'upcast' ).elementToElement( { model: 'blockQuote', view: 'blockquote' } ); const modelFragment = editor.data.parse( '
foobarbom
' ); expect( stringifyModel( modelFragment ) ) .to.equal( '
foo<$text bold="true">barbom
' ); } ); it( 'inside paragraph-like element', () => { const modelFragment = editor.data.parse( '

foo

barbom

' ); expect( stringifyModel( modelFragment ) ) .to.equal( 'foo<$text bold="true">barbom' ); } ); } ); } ); 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.register( 'div' ); model.schema.register( 'specialRoot' ); model.schema.extend( 'div', { allowIn: 'specialRoot' } ); model.schema.extend( '$text', { allowIn: 'div' } ); editor.conversion.for( 'upcast' ).elementToElement( { model: 'div', view: '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' ); } ); 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', () => { model.schema.register( 'div' ); model.schema.extend( 'div', { allowIn: '$root' } ); model.schema.extend( 'paragraph', { allowIn: 'div' } ); editor.conversion.for( 'upcast' ).elementToElement( { model: 'div', view: '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 ); } ); it( 'should not convert empty elements', () => { const modelFragment = editor.data.parse( '