Browse Source

Added root getter to the MoveOperation.

Oskar Wróbel 8 years ago
parent
commit
58e5bdee5a

+ 9 - 0
packages/ckeditor5-engine/src/model/operation/moveoperation.js

@@ -74,6 +74,15 @@ export default class MoveOperation extends Operation {
 	}
 
 	/**
+	 * @inheritDoc
+	 */
+	get root() {
+		// Note that range cannot be moved within different documents e.g. from docFrag to document root so
+		// root of source and target positions will be always the same.
+		return this.targetPosition.root;
+	}
+
+	/**
 	 * Creates and returns an operation that has the same parameters as this operation.
 	 *
 	 * @returns {module:engine/model/operation/moveoperation~MoveOperation} Clone of this operation.

+ 28 - 0
packages/ckeditor5-engine/tests/model/operation/moveoperation.js

@@ -267,6 +267,34 @@ describe( 'MoveOperation', () => {
 		expect( clone.baseVersion ).to.equal( baseVersion );
 	} );
 
+	describe( 'root', () => {
+		it( 'should return root for document', () => {
+			const op = new MoveOperation(
+				new Position( root, [ 0, 0 ] ),
+				1,
+				new Position( root, [ 1, 0 ] ),
+				doc.version
+			);
+
+			expect( op.root ).to.equal( root );
+		} );
+
+		it( 'should return root for document fragment', () => {
+			const docFrag = doc.batch().createDocumentFragment();
+
+			doc.batch().appendText( 'abc', null, docFrag );
+
+			const op = new MoveOperation(
+				new Position( docFrag, [ 0 ] ),
+				1,
+				new Position( docFrag, [ 2 ] ),
+				doc.version
+			);
+
+			expect( op.root ).to.equal( docFrag );
+		} );
+	} );
+
 	describe( 'getMovedRangeStart', () => {
 		it( 'should return move operation target position transformed by removing move operation source range', () => {
 			const sourcePosition = new Position( root, [ 0, 2 ] );