|
|
@@ -11,12 +11,14 @@ import buildViewConverter from 'ckeditor5/engine/conversion/buildviewconverter.
|
|
|
import buildModelConverter from 'ckeditor5/engine/conversion/buildmodelconverter.js';
|
|
|
|
|
|
import ModelDocumentFragment from 'ckeditor5/engine/model/documentfragment.js';
|
|
|
+import ModelElement from 'ckeditor5/engine/model/element.js';
|
|
|
import ModelText from 'ckeditor5/engine/model/text.js';
|
|
|
import ModelSelection from 'ckeditor5/engine/model/selection.js';
|
|
|
|
|
|
import ViewDocumentFragment from 'ckeditor5/engine/view/documentfragment.js';
|
|
|
|
|
|
-import { getData, setData, stringify, parse } from 'ckeditor5/engine/dev-utils/model.js';
|
|
|
+import { getData, setData, stringify, parse as parseModel } from 'ckeditor5/engine/dev-utils/model.js';
|
|
|
+import { parse as parseView } from 'ckeditor5/engine/dev-utils/view.js';
|
|
|
|
|
|
import count from 'ckeditor5/utils/count.js';
|
|
|
|
|
|
@@ -172,6 +174,46 @@ describe( 'DataController', () => {
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
+ describe( 'toModel', () => {
|
|
|
+ beforeEach( () => {
|
|
|
+ schema.registerItem( 'paragraph', '$block' );
|
|
|
+
|
|
|
+ buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should convert content of an element', () => {
|
|
|
+ const viewElement = parseView( '<p>foo</p>' );
|
|
|
+ const modelElement = data.toModel( viewElement );
|
|
|
+
|
|
|
+ expect( modelElement ).to.be.instanceOf( ModelElement );
|
|
|
+ expect( stringify( modelElement ) ).to.equal( '<paragraph>foo</paragraph>' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should convert content of an element', () => {
|
|
|
+ const viewFragment = parseView( '<p>foo</p><p>bar</p>' );
|
|
|
+ const modelFragment = data.toModel( viewFragment );
|
|
|
+
|
|
|
+ expect( modelFragment ).to.be.instanceOf( ModelDocumentFragment );
|
|
|
+ expect( stringify( modelFragment ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should accept parsing context', () => {
|
|
|
+ modelDocument.createRoot( 'inlineRoot', 'inlineRoot' );
|
|
|
+
|
|
|
+ schema.registerItem( 'inlineRoot' );
|
|
|
+ schema.allow( { name: '$text', inside: 'inlineRoot' } );
|
|
|
+
|
|
|
+ const viewFragment = new ViewDocumentFragment( [ parseView( 'foo' ) ] );
|
|
|
+ const modelFragmentInRoot = data.toModel( viewFragment );
|
|
|
+
|
|
|
+ expect( stringify( modelFragmentInRoot ) ).to.equal( '' );
|
|
|
+
|
|
|
+ const modelFragmentInInlineRoot = data.toModel( viewFragment, 'inlineRoot' );
|
|
|
+
|
|
|
+ expect( stringify( modelFragmentInInlineRoot ) ).to.equal( 'foo' );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
describe( 'set', () => {
|
|
|
it( 'should set data to root', () => {
|
|
|
schema.allow( { name: '$text', inside: '$root' } );
|
|
|
@@ -303,13 +345,13 @@ describe( 'DataController', () => {
|
|
|
} );
|
|
|
|
|
|
it( 'should stringify a content of an element', () => {
|
|
|
- const modelElement = parse( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
|
|
|
+ const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
|
|
|
|
|
|
expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
|
|
|
} );
|
|
|
|
|
|
it( 'should stringify a content of a document fragment', () => {
|
|
|
- const modelDocumentFragment = parse( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
|
|
|
+ const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
|
|
|
|
|
|
expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
|
|
|
} );
|
|
|
@@ -324,7 +366,7 @@ describe( 'DataController', () => {
|
|
|
} );
|
|
|
|
|
|
it( 'should convert a content of an element', () => {
|
|
|
- const modelElement = parse( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
|
|
|
+ const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
|
|
|
|
|
|
const viewDocumentFragment = data.toView( modelElement );
|
|
|
|
|
|
@@ -338,7 +380,7 @@ describe( 'DataController', () => {
|
|
|
} );
|
|
|
|
|
|
it( 'should convert a document fragment', () => {
|
|
|
- const modelDocumentFragment = parse( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
|
|
|
+ const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
|
|
|
|
|
|
const viewDocumentFragment = data.toView( modelDocumentFragment );
|
|
|
|