Explorar o código

Remove and move _execute check if nodes match.

Piotr Jasiun %!s(int64=10) %!d(string=hai) anos
pai
achega
d91c09eb3a

+ 26 - 1
packages/ckeditor5-engine/src/document/moveoperation.js

@@ -5,7 +5,12 @@
 
 'use strict';
 
-CKEDITOR.define( [ 'document/operation', 'document/nodelist' ], function( Operation, NodeList ) {
+CKEDITOR.define( [
+	'document/operation',
+	'document/nodelist',
+	'ckeditorerror',
+	'utils'
+], function( Operation, NodeList, CKEditorError, utils ) {
 	/**
 	 *
 	 *
@@ -30,6 +35,26 @@ CKEDITOR.define( [ 'document/operation', 'document/nodelist' ], function( Operat
 			var targetOffset = this.targetPosition.offset;
 			var nodeList = this.nodeList;
 
+			if ( CKEDITOR.isDebug ) {
+				var i = 0;
+
+				for ( var node of this.nodeList ) {
+					if ( !utils.isEqual( sourceElement.children.get( sourceOffset + i ), node ) ) {
+						/**
+						 * The node which should be removed does not exists.
+						 *
+						 * @error operation-move-node-does-not-exists:
+						 * @param {document.MoveOperation} moveOperation
+						 * @param {document.Node} node
+						 */
+						throw new CKEditorError(
+							'operation-move-node-does-not-exists: The node which should be moved does not exists.',
+							{ moveOperation: this, node: this.node } );
+					}
+					i++;
+				}
+			}
+
 			sourceElement.children.remove( sourceOffset, nodeList.length );
 
 			// If we move children in the same element and we remove elements on the position before the target we

+ 31 - 2
packages/ckeditor5-engine/src/document/removeoperation.js

@@ -5,7 +5,13 @@
 
 'use strict';
 
-CKEDITOR.define( [ 'document/operation', 'document/nodelist', 'document/insertoperation' ], function( Operation, NodeList ) {
+CKEDITOR.define( [
+	'document/operation',
+	'document/nodelist',
+	'ckeditorerror',
+	'utils',
+	'document/insertoperation'
+], function( Operation, NodeList, CKEditorError, utils ) {
 	/**
 	 *
 	 *
@@ -23,7 +29,30 @@ CKEDITOR.define( [ 'document/operation', 'document/nodelist', 'document/insertop
 		}
 
 		_execute() {
-			this.position.parent.children.remove( this.position.offset, this.nodeList.length );
+			var children = this.position.parent.children;
+			var offset = this.position.offset;
+
+			if ( CKEDITOR.isDebug ) {
+				var i = 0;
+
+				for ( var node of this.nodeList ) {
+					if ( !utils.isEqual( children.get( offset + i ), node ) ) {
+						/**
+						 * The node which should be removed does not exists.
+						 *
+						 * @error operation-remove-node-does-not-exists:
+						 * @param {document.RemoveOperation} removeOperation
+						 * @param {document.Node} node
+						 */
+						throw new CKEditorError(
+							'operation-remove-node-does-not-exists: The node which should be removed does not exists.',
+							{ removeOperation: this, node: this.node } );
+					}
+					i++;
+				}
+			}
+
+			children.remove( offset, this.nodeList.length );
 		}
 
 		reverseOperation() {

+ 23 - 1
packages/ckeditor5-engine/tests/document/moveoperation.js

@@ -13,7 +13,8 @@ var modules = bender.amd.require(
 	'document/position',
 	'document/character',
 	'document/element',
-	'document/nodelist' );
+	'document/nodelist',
+	'ckeditorerror' );
 
 describe( 'MoveOperation', function() {
 	it( 'should move from one node to another', function() {
@@ -151,4 +152,25 @@ describe( 'MoveOperation', function() {
 		expect( p1.children.get( 0 ).name ).to.be.equal( 'x' );
 		expect( p2.children.length ).to.be.equal( 0 );
 	} );
+
+	if ( CKEDITOR.isDebug ) {
+		it( 'should throw error if nodes to move are different then actual nodes', function() {
+			var Document = modules[ 'document/document' ];
+			var MoveOperation = modules[ 'document/moveoperation' ];
+			var Position = modules[ 'document/position' ];
+			var CKEditorError = modules.ckeditorerror;
+
+			var doc = new Document();
+
+			doc.root.insertChildren( 0, 'xbarx' );
+
+			expect( function() {
+				doc.applyOperation( new MoveOperation(
+					new Position( [ 1 ], doc.root ),
+					new Position( [ 4 ], doc.root ),
+					'y',
+					doc.version ) );
+			} ).to.throw( CKEditorError, /operation-move-node-does-not-exists/ );
+		} );
+	}
 } );

+ 22 - 1
packages/ckeditor5-engine/tests/document/removeoperation.js

@@ -13,7 +13,8 @@ var modules = bender.amd.require(
 	'document/removeoperation',
 	'document/position',
 	'document/character',
-	'document/nodelist' );
+	'document/nodelist',
+	'ckeditorerror' );
 
 describe( 'RemoveOperation', function() {
 	it( 'should remove node', function() {
@@ -126,4 +127,24 @@ describe( 'RemoveOperation', function() {
 		expect( doc.root.children.get( 1 ).character ).to.be.equal( 'a' );
 		expect( doc.root.children.get( 2 ).character ).to.be.equal( 'r' );
 	}  );
+
+	if ( CKEDITOR.isDebug ) {
+		it( 'should throw error if nodes to remove are different then actual nodes', function() {
+			var Document = modules[ 'document/document' ];
+			var RemoveOperation = modules[ 'document/removeoperation' ];
+			var Position = modules[ 'document/position' ];
+			var CKEditorError = modules.ckeditorerror;
+
+			var doc = new Document();
+
+			doc.root.insertChildren( 0, 'foo' );
+
+			expect( function() {
+				doc.applyOperation( new RemoveOperation(
+					new Position( [ 0 ], doc.root ),
+					'bar',
+					doc.version ) );
+			} ).to.throw( CKEditorError, /operation-remove-node-does-not-exists/ );
+		} );
+	}
 } );