浏览代码

Fixed: `RemoveOperation#getReversed()` and `ReinsertOperation#getReversed()` were incorrect after `RemoveOperation` no longer creates `$graveyardHolder`.

Szymon Cofalik 8 年之前
父节点
当前提交
b05aff7ddb

+ 3 - 1
packages/ckeditor5-engine/src/model/operation/reinsertoperation.js

@@ -46,7 +46,9 @@ export default class ReinsertOperation extends MoveOperation {
 	 * @returns {module:engine/model/operation/removeoperation~RemoveOperation}
 	 */
 	getReversed() {
-		return new RemoveOperation( this.targetPosition, this.howMany, this.sourcePosition, this.baseVersion + 1 );
+		const newTargetPosition = this.sourcePosition._getTransformedByInsertion( this.targetPosition, this.howMany );
+
+		return new RemoveOperation( this.getMovedRangeStart(), this.howMany, newTargetPosition, this.baseVersion + 1 );
 	}
 
 	/**

+ 3 - 1
packages/ckeditor5-engine/src/model/operation/removeoperation.js

@@ -53,7 +53,9 @@ export default class RemoveOperation extends MoveOperation {
 		if ( this.isPermanent ) {
 			return new NoOperation( this.baseVersion + 1 );
 		} else {
-			return new ReinsertOperation( this.targetPosition, this.howMany, this.sourcePosition, this.baseVersion + 1 );
+			const newTargetPosition = this.sourcePosition._getTransformedByInsertion( this.targetPosition, this.howMany );
+
+			return new ReinsertOperation( this.getMovedRangeStart(), this.howMany, newTargetPosition, this.baseVersion + 1 );
 		}
 	}
 

+ 9 - 1
packages/ckeditor5-engine/tests/model/operation/reinsertoperation.js

@@ -61,7 +61,7 @@ describe( 'ReinsertOperation', () => {
 		expect( clone.baseVersion ).to.equal( operation.baseVersion );
 	} );
 
-	it( 'should create a correct RemoveOperation as a reverse', () => {
+	it( 'should create RemoveOperation as a reverse', () => {
 		graveyard.appendChildren( new Element( 'x' ) );
 
 		const reverse = operation.getReversed();
@@ -73,6 +73,14 @@ describe( 'ReinsertOperation', () => {
 		expect( reverse.targetPosition.isEqual( graveyardPosition ) ).to.be.true;
 	} );
 
+	it( 'should create correct RemoveOperation when reversed if target position was in graveyard', () => {
+		const operation = new ReinsertOperation( new Position( doc.graveyard, [ 0 ] ), 1, new Position( doc.graveyard, [ 3 ] ), 0 );
+		const reverse = operation.getReversed();
+
+		expect( reverse.sourcePosition.path ).to.deep.equal( [ 2 ] );
+		expect( reverse.targetPosition.path ).to.deep.equal( [ 0 ] );
+	} );
+
 	it( 'should undo reinsert set of nodes by applying reverse operation', () => {
 		const reverse = operation.getReversed();
 

+ 8 - 0
packages/ckeditor5-engine/tests/model/operation/removeoperation.js

@@ -100,6 +100,14 @@ describe( 'RemoveOperation', () => {
 		expect( reverse.targetPosition.isEqual( position ) ).to.be.true;
 	} );
 
+	it( 'should create correct ReinsertOperation when reversed if source range was in graveyard', () => {
+		const operation = new RemoveOperation( new Position( doc.graveyard, [ 2 ] ), 1, new Position( doc.graveyard, [ 0 ] ), 0 );
+		const reverse = operation.getReversed();
+
+		expect( reverse.sourcePosition.path ).to.deep.equal( [ 0 ] );
+		expect( reverse.targetPosition.path ).to.deep.equal( [ 3 ] );
+	} );
+
 	it( 'should create NoOperation when reversed if was permanent', () => {
 		const position = new Position( root, [ 0 ] );
 		const operation = new RemoveOperation( position, 2, new Position( doc.graveyard, [ 0 ] ), 0 );