瀏覽代碼

Introduced element.removeChildren method.

Piotr Jasiun 10 年之前
父節點
當前提交
a797578410

+ 19 - 1
packages/ckeditor5-utils/src/document/element.js

@@ -46,7 +46,7 @@ CKEDITOR.define( [ 'document/node', 'document/nodelist' ], function( Node, NodeL
 		}
 
 		/**
-		 * Insert a list of nodes on the given index.
+		 * Insert a list of child nodes on the given index and set the parent of these node to this.
 		 *
 		 * Note that list of children can be modified only in elements not attached yet to the document.
 		 * All attached nodes should be modified using the {@link document.InsertOperation}.
@@ -62,6 +62,24 @@ CKEDITOR.define( [ 'document/node', 'document/nodelist' ], function( Node, NodeL
 				node.parent = this;
 			}
 		}
+
+		/**
+		 * Removes number of child nodes starting at the given index and set the parent of these node to null.
+		 *
+		 * Note that list of children can be modified only in elements not attached yet to the document.
+		 * All attached nodes should be modified using the {@link document.RemoveOperation}.
+		 *
+		 * @param {Number} index Position of the first node to remove.
+		 * @param {Number} number Number of nodes to remove.
+		 */
+
+		removeChildren( index, number ) {
+			for ( var i = index; i < index + number; i++ ) {
+				this.children.get( i ).parent = null;
+			}
+
+			this.children.remove( index, number );
+		}
 	}
 
 	return Element;

+ 1 - 1
packages/ckeditor5-utils/src/document/moveoperation.js

@@ -55,7 +55,7 @@ CKEDITOR.define( [
 				}
 			}
 
-			sourceElement.children.remove( sourceOffset, nodeList.length );
+			sourceElement.removeChildren( sourceOffset, nodeList.length );
 
 			// If we move children in the same element and we remove elements on the position before the target we
 			// need to update a target offset.

+ 6 - 0
packages/ckeditor5-utils/src/document/nodelist.js

@@ -109,6 +109,9 @@ CKEDITOR.define( [
 		/**
 		 * Inserts nodes from the given node list into this node list at the given index.
 		 *
+		 * Note that this method will not change the parent of the nodes. Use {@link document.Element#insertChildren}
+		 * to insert nodes and set parent.
+		 *
 		 * @param {Number} index Position where nodes should be inserted.
 		 * @param {document.NodeList} nodeList List of nodes to insert.
 		 */
@@ -119,6 +122,9 @@ CKEDITOR.define( [
 		/**
 		 * Removes number of nodes starting at the given index.
 		 *
+		 * Note that this method will not change the parent of the nodes. Use {@link document.Element#removeChildren}
+		 * to insert nodes and set parent to be null.
+		 *
 		 * @param {Number} index Position of the first node to remove.
 		 * @param {Number} number Number of nodes to remove.
 		 */

+ 3 - 3
packages/ckeditor5-utils/src/document/removeoperation.js

@@ -29,14 +29,14 @@ CKEDITOR.define( [
 		}
 
 		_execute() {
-			var children = this.position.parent.children;
+			var parent = this.position.parent;
 			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 ) ) {
+					if ( !utils.isEqual( parent.children.get( offset + i ), node ) ) {
 						/**
 						 * The node which should be removed does not exists.
 						 *
@@ -52,7 +52,7 @@ CKEDITOR.define( [
 				}
 			}
 
-			children.remove( offset, this.nodeList.length );
+			parent.removeChildren( offset, this.nodeList.length );
 		}
 
 		reverseOperation() {

+ 21 - 0
packages/ckeditor5-utils/tests/document/element.js

@@ -74,4 +74,25 @@ describe( 'insertChildren', function() {
 		expect( element.children.get( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
 		expect( element.children.get( 4 ) ).to.have.property( 'character' ).that.equals( 'y' );
 	} );
+} );
+
+describe( 'removeChildren', function() {
+	it( 'should add children to the element', function() {
+		var Element = modules[ 'document/element' ];
+
+		var element = new Element( 'elem', [], [ 'foobar' ] );
+		var o = element.children.get( 2 );
+		var b = element.children.get( 3 );
+		var a = element.children.get( 4 );
+		element.removeChildren( 2, 3 );
+
+		expect( element ).to.have.property( 'children' ).with.length( 3 );
+		expect( element.children.get( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
+		expect( element.children.get( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
+		expect( element.children.get( 2 ) ).to.have.property( 'character' ).that.equals( 'r' );
+
+		expect( o ).to.have.property( 'parent' ).that.is.null;
+		expect( b ).to.have.property( 'parent' ).that.is.null;
+		expect( a ).to.have.property( 'parent' ).that.is.null;
+	} );
 } );