Explorar el Código

The insertContent() function should also work with DocumentSelection.

Maciej Gołaszewski hace 7 años
padre
commit
c7bf7ac166

+ 1 - 1
packages/ckeditor5-engine/src/model/utils/insertcontent.js

@@ -38,7 +38,7 @@ export default function insertContent( model, content, selectable ) {
 
 		if ( !selectable ) {
 			selection = model.document.selection;
-		} else if ( selectable instanceof Selection ) {
+		} else if ( selectable instanceof Selection || selectable instanceof DocumentSelection ) {
 			selection = selectable;
 		} else {
 			selection = new Selection( selectable );

+ 35 - 1
packages/ckeditor5-engine/tests/model/utils/insertcontent.js

@@ -17,7 +17,7 @@ import Range from '../../../src/model/range';
 describe( 'DataController utils', () => {
 	let model, doc;
 
-	describe( 'insertContent', () => {
+	describe.only( 'insertContent', () => {
 		it( 'should use parent batch', () => {
 			model = new Model();
 			doc = model.document;
@@ -48,6 +48,26 @@ describe( 'DataController utils', () => {
 			} );
 		} );
 
+		it( 'should modify passed selection instance', () => {
+			model = new Model();
+			doc = model.document;
+			doc.createRoot();
+
+			model.schema.extend( '$text', { allowIn: '$root' } );
+			setData( model, 'a[]bc' );
+
+			const selection = new Selection( new Position( doc.getRoot(), [ 2 ] ) );
+			const selectionCopy = new Selection( new Position( doc.getRoot(), [ 2 ] ) );
+
+			expect( selection.isEqual( selectionCopy ) ).to.be.true;
+
+			model.change( () => {
+				insertContent( model, new Text( 'x' ), selection );
+			} );
+
+			expect( selection.isEqual( selectionCopy ) ).to.be.false;
+		} );
+
 		it( 'should be able to insert content at custom position', () => {
 			model = new Model();
 			doc = model.document;
@@ -80,6 +100,20 @@ describe( 'DataController utils', () => {
 			} );
 		} );
 
+		it( 'should be able to insert content at model selection if document selection is passed', () => {
+			model = new Model();
+			doc = model.document;
+			doc.createRoot();
+
+			model.schema.extend( '$text', { allowIn: '$root' } );
+			setData( model, 'a[]bc' );
+
+			model.change( () => {
+				insertContent( model, new Text( 'x' ), model.document.selection );
+				expect( getData( model ) ).to.equal( 'ax[]bc' );
+			} );
+		} );
+
 		it( 'should be able to insert content at model selection if none passed', () => {
 			model = new Model();
 			doc = model.document;