浏览代码

Implemented DataController.toModel.

Piotrek Koszuliński 9 年之前
父节点
当前提交
b39b0c1db1

+ 18 - 2
packages/ckeditor5-engine/src/controller/datacontroller.js

@@ -211,7 +211,7 @@ export default class DataController {
 	 *
 	 * @see #set
 	 * @param {String} data Data to parse.
-	 * @param {String} [context='$root'] Base context in which view will be converted to the model. See:
+	 * @param {String} [context='$root'] Base context in which the view will be converted to the model. See:
 	 * {@link module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher#convert}.
 	 * @returns {module:engine/model/documentfragment~DocumentFragment} Parsed data.
 	 */
@@ -220,7 +220,23 @@ export default class DataController {
 		const viewDocumentFragment = this.processor.toView( data );
 
 		// view -> model
-		return this.viewToModel.convert( viewDocumentFragment, { context: [ context ] } );
+		return this.toModel( viewDocumentFragment, context );
+	}
+
+	/**
+	 * Returns the content of the given {@link module:engine/view/element~Element view element} or
+	 * {@link module:engine/view/documentfragment~DocumentFragment view document fragment} converted by the
+	 * {@link #viewToModel view to model converters} to a
+	 * {@link module:engine/model/documentfragment~DocumentFragment model document fragment}.
+	 *
+	 * @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} viewElementOrFragment
+	 * Element or document fragment which content will be converted.
+	 * @param {String} [context='$root'] Base context in which the view will be converted to the model. See:
+	 * {@link module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher#convert}.
+	 * @returns {module:engine/model/documentfragment~DocumentFragment} Output document fragment.
+	 */
+	toModel( viewElementOrFragment, context = '$root' ) {
+		return this.viewToModel.convert( viewElementOrFragment, { context: [ context ] } );
 	}
 
 	/**

+ 47 - 5
packages/ckeditor5-engine/tests/controller/datacontroller.js

@@ -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 );