Browse Source

Added test for setting markers in batch insert.

Oskar Wróbel 8 years ago
parent
commit
60112b81ee

+ 4 - 0
packages/ckeditor5-engine/src/model/delta/insertdelta.js

@@ -88,6 +88,9 @@ export default class InsertDelta extends Delta {
 /**
  * Inserts a node or nodes at the given position.
  *
+ * When inserted element is a {@link engine/model/documentfragment~DocumentFragment} and has markers its markers will
+ * be set to {@link engine/model/document~Document#markers}.
+ *
  * @chainable
  * @method module:engine/model/batch~Batch#insert
  * @param {module:engine/model/position~Position} position Position of insertion.
@@ -101,6 +104,7 @@ register( 'insert', function( position, nodes ) {
 	delta.addOperation( insert );
 	this.document.applyOperation( insert );
 
+	// When element is a DocumentFragment we need to move its markers to Document#markers.
 	if ( nodes instanceof DocumentFragment ) {
 		for ( const marker of nodes.markers ) {
 			const doc = this.document;

+ 36 - 1
packages/ckeditor5-engine/tests/model/delta/insertdelta.js

@@ -5,15 +5,20 @@
 
 import Document from '../../../src/model/document';
 import Element from '../../../src/model/element';
-import Position from '../../../src/model/position';
+import DocumentFragment from '../../../src/model/documentfragment';
 import Text from '../../../src/model/text';
+import Position from '../../../src/model/position';
+import Range from '../../../src/model/range';
 
 import InsertOperation from '../../../src/model/operation/insertoperation';
+import MarkerOperation from '../../../src/model/operation/markeroperation';
 import InsertDelta from '../../../src/model/delta/insertdelta';
 
 import RemoveDelta from '../../../src/model/delta/removedelta';
 import RemoveOperation from '../../../src/model/operation/removeoperation';
 
+import { stringify } from '../../../src/dev-utils/model';
+
 describe( 'Batch', () => {
 	let doc, root, batch, p, ul, chain;
 
@@ -52,6 +57,36 @@ describe( 'Batch', () => {
 
 			expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
 		} );
+
+		it( 'should transfer markers from given DocumentFragment', () => {
+			const documentFragment = new DocumentFragment( [ new Element( 'li', null, [ new Text( 'foo bar' ) ] ) ] );
+			const marker = new Range( new Position( documentFragment, [ 0, 1 ] ), new Position( documentFragment, [ 0, 5 ] ) );
+
+			documentFragment.markers.set( 'marker', marker );
+
+			batch.insert( new Position( root, [ 3, 0 ] ), documentFragment );
+
+			expect( Array.from( doc.markers ).length ).to.equal( 1 );
+			expect( stringify( root, doc.markers.get( 'marker' ).getRange() ) ).to.equal( 'ab<p></p><ul><li>f[oo b]ar</li></ul>c' );
+		} );
+
+		it( 'should set each marker as separate operation', () => {
+			sinon.spy( doc, 'applyOperation' );
+
+			const documentFragment = new DocumentFragment( [ new Element( 'li', null, [ new Text( 'foo bar' ) ] ) ] );
+			const marker1 = new Range( new Position( documentFragment, [ 0, 1 ] ), new Position( documentFragment, [ 0, 2 ] ) );
+			const marker2 = new Range( new Position( documentFragment, [ 0, 5 ] ), new Position( documentFragment, [ 0, 6 ] ) );
+
+			documentFragment.markers.set( 'marker1', marker1 );
+			documentFragment.markers.set( 'marker2', marker2 );
+
+			batch.insert( new Position( root, [ 3, 0 ] ), documentFragment );
+
+			expect( doc.applyOperation.calledThrice );
+			expect( doc.applyOperation.firstCall.calledWith( sinon.match( ( operation ) => operation instanceof InsertOperation ) ) );
+			expect( doc.applyOperation.secondCall.calledWith( sinon.match( ( operation ) => operation instanceof MarkerOperation ) ) );
+			expect( doc.applyOperation.thirdCall.calledWith( sinon.match( ( operation ) => operation instanceof MarkerOperation ) ) );
+		} );
 	} );
 } );