浏览代码

Merge pull request #871 from ckeditor/t/870

Feature: `DataController#insertContent()` now accepts also model items. Closes #870.
Piotrek Koszuliński 8 年之前
父节点
当前提交
a22c6d974b

+ 10 - 2
packages/ckeditor5-engine/src/controller/insertcontent.js

@@ -24,7 +24,7 @@ import log from '@ckeditor/ckeditor5-utils/src/log';
  *
  * @param {module:engine/controller/datacontroller~DataController} dataController The data controller in context of which the insertion
  * should be performed.
- * @param {module:engine/model/documentfragment~DocumentFragment} content The content to insert.
+ * @param {module:engine/model/documentfragment~DocumentFragment|module:engine/model/item~Item} content The content to insert.
  * @param {module:engine/model/selection~Selection} selection Selection into which the content should be inserted.
  * @param {module:engine/model/batch~Batch} [batch] Batch to which deltas will be added. If not specified, then
  * changes will be added to a new batch.
@@ -42,7 +42,15 @@ export default function insertContent( dataController, content, selection, batch
 
 	const insertion = new Insertion( dataController, batch, selection.anchor );
 
-	insertion.handleNodes( content.getChildren(), {
+	let nodesToInsert;
+
+	if ( content.is( 'documentFragment' ) ) {
+		nodesToInsert = content.getChildren();
+	} else {
+		nodesToInsert = [ content ];
+	}
+
+	insertion.handleNodes( nodesToInsert, {
 		// The set of children being inserted is the only set in this context
 		// so it's the first and last (it's a hack ;)).
 		isFirst: true,

+ 34 - 6
packages/ckeditor5-engine/tests/controller/insertcontent.js

@@ -17,11 +17,11 @@ describe( 'DataController', () => {
 
 	describe( 'insertContent', () => {
 		it( 'uses the passed batch', () => {
-			doc = new Document();
+			const doc = new Document();
 			doc.createRoot();
 			doc.schema.allow( { name: '$text', inside: '$root' } );
 
-			dataController = new DataController( doc );
+			const dataController = new DataController( doc );
 
 			const batch = doc.batch();
 
@@ -32,6 +32,36 @@ describe( 'DataController', () => {
 			expect( batch.deltas.length ).to.be.above( 0 );
 		} );
 
+		it( 'accepts DocumentFragment', () => {
+			const doc = new Document();
+			const dataController = new DataController( doc );
+			const batch = doc.batch();
+
+			doc.createRoot();
+			doc.schema.allow( { name: '$text', inside: '$root' } );
+
+			setData( doc, 'x[]x' );
+
+			insertContent( dataController, new DocumentFragment( [ new Text( 'a' ) ] ), doc.selection, batch );
+
+			expect( getData( doc ) ).to.equal( 'xa[]x' );
+		} );
+
+		it( 'accepts Text', () => {
+			const doc = new Document();
+			const dataController = new DataController( doc );
+			const batch = doc.batch();
+
+			doc.createRoot();
+			doc.schema.allow( { name: '$text', inside: '$root' } );
+
+			setData( doc, 'x[]x' );
+
+			insertContent( dataController, new Text( 'a' ), doc.selection, batch );
+
+			expect( getData( doc ) ).to.equal( 'xa[]x' );
+		} );
+
 		describe( 'in simple scenarios', () => {
 			beforeEach( () => {
 				doc = new Document();
@@ -604,6 +634,8 @@ describe( 'DataController', () => {
 		} );
 	} );
 
+	// Helper function that parses given content and inserts it at the cursor position.
+	//
 	// @param {module:engine/model/item~Item|String} content
 	function insertHelper( content ) {
 		if ( typeof content == 'string' ) {
@@ -612,10 +644,6 @@ describe( 'DataController', () => {
 			} );
 		}
 
-		if ( !( content instanceof DocumentFragment ) ) {
-			content = new DocumentFragment( [ content ] );
-		}
-
 		insertContent( dataController, content, doc.selection );
 	}
 } );