|
|
@@ -1,5 +1,5 @@
|
|
|
/**
|
|
|
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
|
|
|
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
|
|
|
* For licensing, see LICENSE.md.
|
|
|
*/
|
|
|
|
|
|
@@ -13,24 +13,23 @@ import {
|
|
|
} 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 { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
|
|
|
+import { upcastElementToElement, upcastElementToAttribute } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
|
|
|
|
|
|
import ModelDocumentFragment from '@ckeditor/ckeditor5-engine/src/model/documentfragment';
|
|
|
-import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
|
|
|
import ModelText from '@ckeditor/ckeditor5-engine/src/model/text';
|
|
|
-import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position';
|
|
|
import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
|
|
|
|
|
|
describe( 'Paragraph feature', () => {
|
|
|
- let editor, doc, root;
|
|
|
+ let model, editor, doc, root;
|
|
|
|
|
|
beforeEach( () => {
|
|
|
return VirtualTestEditor
|
|
|
.create( { plugins: [ Paragraph ] } )
|
|
|
.then( newEditor => {
|
|
|
editor = newEditor;
|
|
|
- doc = editor.document;
|
|
|
+ model = editor.model;
|
|
|
+ doc = model.document;
|
|
|
root = doc.getRoot();
|
|
|
} );
|
|
|
} );
|
|
|
@@ -40,9 +39,9 @@ describe( 'Paragraph feature', () => {
|
|
|
} );
|
|
|
|
|
|
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;
|
|
|
+ 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', () => {
|
|
|
@@ -53,21 +52,21 @@ describe( 'Paragraph feature', () => {
|
|
|
it( 'should convert paragraph', () => {
|
|
|
editor.setData( '<p>foobar</p>' );
|
|
|
|
|
|
- expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobar</paragraph>' );
|
|
|
+ expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foobar</paragraph>' );
|
|
|
expect( editor.getData() ).to.equal( '<p>foobar</p>' );
|
|
|
} );
|
|
|
|
|
|
it( 'should convert paragraph only', () => {
|
|
|
editor.setData( '<p>foo<b>baz</b>bar</p>' );
|
|
|
|
|
|
- expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobazbar</paragraph>' );
|
|
|
+ expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foobazbar</paragraph>' );
|
|
|
expect( editor.getData() ).to.equal( '<p>foobazbar</p>' );
|
|
|
} );
|
|
|
|
|
|
it( 'should convert multiple paragraphs', () => {
|
|
|
editor.setData( '<p>foo</p><p>baz</p>' );
|
|
|
|
|
|
- expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph><paragraph>baz</paragraph>' );
|
|
|
+ expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph><paragraph>baz</paragraph>' );
|
|
|
expect( editor.getData() ).to.equal( '<p>foo</p><p>baz</p>' );
|
|
|
} );
|
|
|
|
|
|
@@ -75,42 +74,43 @@ describe( 'Paragraph feature', () => {
|
|
|
it( 'should autoparagraph text', () => {
|
|
|
editor.setData( 'foo' );
|
|
|
|
|
|
- expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
|
|
|
+ expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
|
|
|
expect( editor.getData() ).to.equal( '<p>foo</p>' );
|
|
|
} );
|
|
|
|
|
|
it( 'should autoparagraph any inline element', () => {
|
|
|
- editor.document.schema.registerItem( 'span', '$inline' );
|
|
|
- editor.document.schema.allow( { name: '$text', inside: '$inline' } );
|
|
|
+ editor.model.schema.register( 'span', { allowWhere: '$text' } );
|
|
|
+ editor.model.schema.extend( '$text', { allowIn: 'span' } );
|
|
|
|
|
|
- buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView ).fromElement( 'span' ).toElement( 'span' );
|
|
|
- buildViewConverter().for( editor.data.viewToModel ).fromElement( 'span' ).toElement( 'span' );
|
|
|
+ editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'span', view: 'span' } ) );
|
|
|
+ editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'span', view: 'span' } ) );
|
|
|
|
|
|
editor.setData( '<span>foo</span>' );
|
|
|
|
|
|
- expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph><span>foo</span></paragraph>' );
|
|
|
+ expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph><span>foo</span></paragraph>' );
|
|
|
expect( editor.getData() ).to.equal( '<p><span>foo</span></p>' );
|
|
|
} );
|
|
|
|
|
|
it( 'should not autoparagraph text (in clipboard holder)', () => {
|
|
|
- const modelFragment = editor.data.parse( 'foo', '$clipboardHolder' );
|
|
|
+ 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' );
|
|
|
+ model.schema.register( 'specialRoot' );
|
|
|
|
|
|
- const modelFragment = editor.data.parse( 'foo', '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' );
|
|
|
+ model.schema.register( 'heading1', { inheritAllFrom: '$block' } );
|
|
|
+
|
|
|
+ editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'heading1', view: 'h1' } ) );
|
|
|
|
|
|
const modelFragment = editor.data.parse( '<h1>foo</h1>bar<p>bom</p>' );
|
|
|
|
|
|
@@ -126,18 +126,18 @@ describe( 'Paragraph feature', () => {
|
|
|
} );
|
|
|
|
|
|
it( 'should not autoparagraph 3 inline nodes (in clipboardHolder)', () => {
|
|
|
- const modelFragment = editor.data.parse( 'foo<b>bar</b>bom', '$clipboardHolder' );
|
|
|
+ const modelFragment = editor.data.parse( 'foo<b>bar</b>bom', [ '$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' } );
|
|
|
+ model.schema.register( 'div' );
|
|
|
+ model.schema.extend( 'div', { allowIn: '$root' } );
|
|
|
+ model.schema.extend( 'paragraph', { allowIn: 'div' } );
|
|
|
|
|
|
- buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
|
|
|
+ editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'div', view: 'div' } ) );
|
|
|
|
|
|
const modelFragment = editor.data.parse( '<div>foo</div><div>bom<p>bim</p></div>' );
|
|
|
|
|
|
@@ -149,8 +149,9 @@ describe( 'Paragraph feature', () => {
|
|
|
} );
|
|
|
|
|
|
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' );
|
|
|
+ model.schema.register( 'heading1', { inheritAllFrom: '$block' } );
|
|
|
+
|
|
|
+ editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'heading1', view: 'h1' } ) );
|
|
|
|
|
|
const modelFragment = editor.data.parse( '<div><h1>foo</h1>bar</div>' );
|
|
|
|
|
|
@@ -159,8 +160,9 @@ describe( 'Paragraph feature', () => {
|
|
|
} );
|
|
|
|
|
|
it( 'should not autoparagraph text in disallowed element', () => {
|
|
|
- doc.schema.registerItem( 'heading1', '$block' );
|
|
|
- buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
|
|
|
+ model.schema.register( 'heading1', { inheritAllFrom: '$block' } );
|
|
|
+
|
|
|
+ editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'heading1', view: 'h1' } ) );
|
|
|
|
|
|
const modelFragment = editor.data.parse( '<h1><b>foo</b>bar</h1>' );
|
|
|
|
|
|
@@ -169,7 +171,11 @@ describe( 'Paragraph feature', () => {
|
|
|
} );
|
|
|
|
|
|
it( 'should not fail when text is not allowed in paragraph', () => {
|
|
|
- doc.schema.disallow( { name: '$text', inside: [ '$root', 'paragraph' ] } );
|
|
|
+ model.schema.addChildCheck( ( ctx, childDef ) => {
|
|
|
+ if ( ctx.endsWith( '$root paragraph' ) && childDef.name == '$text' ) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } );
|
|
|
|
|
|
const modelFragment = editor.data.parse( 'foo' );
|
|
|
|
|
|
@@ -186,8 +192,8 @@ describe( 'Paragraph feature', () => {
|
|
|
|
|
|
// 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 } );
|
|
|
+ editor.data.upcastDispatcher.on( 'element:li', ( evt, data, conversionApi ) => {
|
|
|
+ conversionApi.consumable.consume( data.input, { name: true } );
|
|
|
}, { priority: 'highest' } );
|
|
|
|
|
|
const modelFragment = editor.data.parse( '<ul><li></li></ul>' );
|
|
|
@@ -197,10 +203,9 @@ describe( 'Paragraph feature', () => {
|
|
|
|
|
|
describe( 'should not strip attribute elements when autoparagraphing texts', () => {
|
|
|
beforeEach( () => {
|
|
|
- doc.schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
|
|
|
- buildViewConverter().for( editor.data.viewToModel )
|
|
|
- .fromElement( 'b' )
|
|
|
- .toAttribute( 'bold', true );
|
|
|
+ model.schema.extend( '$text', { allowAttributes: 'bold' } );
|
|
|
+
|
|
|
+ editor.conversion.for( 'upcast' ).add( upcastElementToAttribute( { view: 'b', model: 'bold' } ) );
|
|
|
} );
|
|
|
|
|
|
it( 'inside document fragment', () => {
|
|
|
@@ -210,20 +215,15 @@ describe( 'Paragraph feature', () => {
|
|
|
} );
|
|
|
|
|
|
it( 'inside converted element', () => {
|
|
|
- doc.schema.registerItem( 'blockquote' );
|
|
|
- doc.schema.allow( { name: 'blockquote', inside: '$root' } );
|
|
|
- doc.schema.allow( { name: '$block', inside: 'blockquote' } );
|
|
|
-
|
|
|
- buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView )
|
|
|
- .fromElement( 'blockquote' )
|
|
|
- .toElement( 'blockquote' );
|
|
|
+ model.schema.register( 'blockQuote', { allowIn: '$root' } );
|
|
|
+ model.schema.extend( '$block', { allowIn: 'blockQuote' } );
|
|
|
|
|
|
- buildViewConverter().for( editor.data.viewToModel ).fromElement( 'blockquote' ).toElement( 'blockquote' );
|
|
|
+ editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'blockQuote', view: 'blockquote' } ) );
|
|
|
|
|
|
const modelFragment = editor.data.parse( '<blockquote>foo<b>bar</b>bom</blockquote>' );
|
|
|
|
|
|
expect( stringifyModel( modelFragment ) )
|
|
|
- .to.equal( '<blockquote><paragraph>foo<$text bold="true">bar</$text>bom</paragraph></blockquote>' );
|
|
|
+ .to.equal( '<blockQuote><paragraph>foo<$text bold="true">bar</$text>bom</paragraph></blockQuote>' );
|
|
|
} );
|
|
|
|
|
|
it( 'inside paragraph-like element', () => {
|
|
|
@@ -244,21 +244,21 @@ describe( 'Paragraph feature', () => {
|
|
|
} );
|
|
|
|
|
|
it( 'should convert h1+h2 (in clipboard holder)', () => {
|
|
|
- const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>', '$clipboardHolder' );
|
|
|
+ const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>', [ '$clipboardHolder' ] );
|
|
|
|
|
|
expect( stringifyModel( modelFragment ) )
|
|
|
.to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
|
|
|
} );
|
|
|
|
|
|
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' } );
|
|
|
+ model.schema.register( 'div' );
|
|
|
+ model.schema.register( 'specialRoot' );
|
|
|
+ model.schema.extend( 'div', { allowIn: 'specialRoot' } );
|
|
|
+ model.schema.extend( '$text', { allowIn: 'div' } );
|
|
|
|
|
|
- buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
|
|
|
+ editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'div', view: 'div' } ) );
|
|
|
|
|
|
- const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2><div>bom</div>', 'specialRoot' );
|
|
|
+ const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2><div>bom</div>', [ 'specialRoot' ] );
|
|
|
|
|
|
expect( stringifyModel( modelFragment ) )
|
|
|
.to.equal( '<div>bom</div>' );
|
|
|
@@ -272,7 +272,7 @@ describe( 'Paragraph feature', () => {
|
|
|
} );
|
|
|
|
|
|
it( 'should convert ul,ol>li (in clipboard holder)', () => {
|
|
|
- const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>', '$clipboardHolder' );
|
|
|
+ const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>', [ '$clipboardHolder' ] );
|
|
|
|
|
|
expect( stringifyModel( modelFragment ) )
|
|
|
.to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
|
|
|
@@ -288,7 +288,7 @@ describe( 'Paragraph feature', () => {
|
|
|
// "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( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>', '$clipboardHolder' );
|
|
|
+ const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>', [ '$clipboardHolder' ] );
|
|
|
|
|
|
expect( stringifyModel( modelFragment ) )
|
|
|
.to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
|
|
|
@@ -301,17 +301,17 @@ describe( 'Paragraph feature', () => {
|
|
|
.to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
|
|
|
} );
|
|
|
|
|
|
- // "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( '<ul><li><p>a</p>b</li></ul>', '$clipboardHolder' );
|
|
|
+ const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>', [ '$clipboardHolder' ] );
|
|
|
|
|
|
expect( stringifyModel( modelFragment ) )
|
|
|
.to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
|
|
|
} );
|
|
|
|
|
|
it( 'should convert td', () => {
|
|
|
- const modelFragment = editor.data.parse( '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>' );
|
|
|
+ const modelFragment = editor.data.parse(
|
|
|
+ '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>'
|
|
|
+ );
|
|
|
|
|
|
expect( stringifyModel( modelFragment ) )
|
|
|
.to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
|
|
|
@@ -320,7 +320,7 @@ describe( 'Paragraph feature', () => {
|
|
|
it( 'should convert td (in clipboardHolder)', () => {
|
|
|
const modelFragment = editor.data.parse(
|
|
|
'<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>',
|
|
|
- '$clipboardHolder'
|
|
|
+ [ '$clipboardHolder' ]
|
|
|
);
|
|
|
|
|
|
expect( stringifyModel( modelFragment ) )
|
|
|
@@ -328,11 +328,11 @@ describe( 'Paragraph feature', () => {
|
|
|
} );
|
|
|
|
|
|
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' } );
|
|
|
+ model.schema.register( 'div' );
|
|
|
+ model.schema.extend( 'div', { allowIn: '$root' } );
|
|
|
+ model.schema.extend( 'paragraph', { allowIn: 'div' } );
|
|
|
|
|
|
- buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
|
|
|
+ editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'div', view: 'div' } ) );
|
|
|
|
|
|
const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
|
|
|
|
|
|
@@ -362,12 +362,18 @@ describe( 'Paragraph feature', () => {
|
|
|
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( '<ul><li></li><ul>' );
|
|
|
+
|
|
|
+ expect( stringifyModel( modelFragment ) ).to.equal( '' );
|
|
|
+ } );
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
describe( 'editing pipeline conversion', () => {
|
|
|
it( 'should convert paragraph', () => {
|
|
|
- setModelData( doc, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
|
|
|
+ setModelData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
|
|
|
|
|
|
expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p>bar</p>' );
|
|
|
} );
|
|
|
@@ -377,7 +383,7 @@ describe( 'Paragraph feature', () => {
|
|
|
it( 'wraps text and place selection at the beginning of that paragraph', () => {
|
|
|
editor.setData( 'foo' );
|
|
|
|
|
|
- expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo</paragraph>' );
|
|
|
+ expect( getModelData( model ) ).to.equal( '<paragraph>[]foo</paragraph>' );
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
@@ -391,61 +397,47 @@ describe( 'Paragraph feature', () => {
|
|
|
editor.setData( '<p>Foobar</p>' );
|
|
|
|
|
|
// 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
|
|
|
+ // change 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( '<p>Foobar</p>' );
|
|
|
|
|
|
- doc.enqueueChanges( () => {
|
|
|
- doc.batch().remove( ModelRange.createIn( root ) );
|
|
|
+ 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".
|
|
|
- doc.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 ) {
|
|
|
- doc.enqueueChanges( () => {
|
|
|
- doc.batch().insert( ModelPosition.createAt( root ), new ModelElement( 'heading' ) );
|
|
|
- } );
|
|
|
+ it( 'should not fix root which does not allow paragraph', () => {
|
|
|
+ model.schema.addChildCheck( ( ctx, childDef ) => {
|
|
|
+ if ( ctx.endsWith( '$root' ) && childDef.name == 'paragraph' ) {
|
|
|
+ return false;
|
|
|
}
|
|
|
} );
|
|
|
|
|
|
- doc.enqueueChanges( () => {
|
|
|
- doc.batch().remove( ModelRange.createIn( 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', () => {
|
|
|
- doc.schema.disallow( { name: 'paragraph', inside: '$root' } );
|
|
|
-
|
|
|
- doc.enqueueChanges( () => {
|
|
|
- doc.batch().remove( ModelRange.createIn( root ) );
|
|
|
- } );
|
|
|
expect( editor.getData() ).to.equal( '' );
|
|
|
} );
|
|
|
|
|
|
- it( 'should not fix root if change was in transparent batch', () => {
|
|
|
- editor.setData( '<p>Foobar</p>' );
|
|
|
+ it( 'should fix empty roots in the right batch', () => {
|
|
|
+ let removeBatch, attributeBatch;
|
|
|
|
|
|
- doc.enqueueChanges( () => {
|
|
|
- doc.batch( 'transparent' ).remove( ModelRange.createIn( root ) );
|
|
|
+ model.enqueueChange( writer => {
|
|
|
+ removeBatch = writer.batch;
|
|
|
+ writer.remove( ModelRange.createIn( root ) );
|
|
|
+
|
|
|
+ model.enqueueChange( writer => {
|
|
|
+ attributeBatch = writer.batch;
|
|
|
+ writer.setAttribute( 'foo', 'bar', root );
|
|
|
+ } );
|
|
|
} );
|
|
|
|
|
|
- expect( editor.getData() ).to.equal( '' );
|
|
|
+ expect( Array.from( removeBatch.deltas, delta => delta.type ) ).to.include.members( [ 'insert' ] );
|
|
|
+ expect( Array.from( attributeBatch.deltas, delta => delta.type ) ).to.not.include.members( [ 'insert' ] );
|
|
|
} );
|
|
|
} );
|
|
|
|