Browse Source

Added default parameter to Element in treeView and treeModel. Updated tests.

Szymon Kupś 10 years ago
parent
commit
77f604ba7a

+ 3 - 7
packages/ckeditor5-engine/src/treemodel/element.js

@@ -116,15 +116,11 @@ export default class Element extends Node {
 	 * All attached nodes should be modified using the {@link core.treeModel.operation.RemoveOperation}.
 	 *
 	 * @param {Number} index Position of the first node to remove.
-	 * @param {Number} [number] Number of nodes to remove.
+	 * @param {Number} [howMany=1] Number of nodes to remove.
 	 * @returns {core.treeModel.NodeList} The list of removed nodes.
 	 */
-	removeChildren( index, number ) {
-		if ( typeof number === 'undefined' ) {
-			number = 1;
-		}
-
-		let nodeList = this._children.remove( index, number );
+	removeChildren( index, howMany = 1 ) {
+		let nodeList = this._children.remove( index, howMany );
 
 		for ( let node of nodeList._nodes ) {
 			node.parent = null;

+ 4 - 8
packages/ckeditor5-engine/src/treeview/element.js

@@ -222,22 +222,18 @@ export default class Element extends Node {
 	 * Removes number of child nodes starting at the given index and set the parent of these nodes to `null`.
 	 *
 	 * @param {Number} index Number of the first node to remove.
-	 * @param {Number} [number] Number of nodes to remove.
+	 * @param {Number} [howMany=1] Number of nodes to remove.
 	 * @returns {Array.<core.treeView.Node>} The array of removed nodes.
 	 * @fires core.treeView.Node#change
 	 */
-	removeChildren( index, number ) {
+	removeChildren( index, howMany = 1 ) {
 		this._fireChange( 'CHILDREN', this );
 
-		if ( typeof number === 'undefined' ) {
-			number = 1;
-		}
-
-		for ( let i = index; i < index + number; i++ ) {
+		for ( let i = index; i < index + howMany; i++ ) {
 			this._children[ i ].parent = null;
 		}
 
-		return this._children.splice( index, number );
+		return this._children.splice( index, howMany );
 	}
 
 	/**

+ 14 - 0
packages/ckeditor5-engine/tests/treemodel/element.js

@@ -94,6 +94,20 @@ describe( 'Element', () => {
 			expect( removed.get( 1 ).character ).to.equal( 'b' );
 			expect( removed.get( 2 ).character ).to.equal( 'a' );
 		} );
+
+		it( 'should remove one child when second parameter is not specified', () => {
+			let element = new Element( 'elem', [], [ 'foo' ] );
+			let removed = element.removeChildren( 2 );
+
+			expect( element.getChildCount() ).to.equal( 2 );
+			expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
+			expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
+
+			expect( removed ).to.be.instanceof( NodeList );
+			expect( removed.length ).to.equal( 1 );
+
+			expect( removed.get( 0 ).character ).to.equal( 'o' );
+		} );
 	} );
 
 	describe( 'getChildIndex', () => {

+ 15 - 0
packages/ckeditor5-engine/tests/treeview/element.js

@@ -225,6 +225,21 @@ describe( 'Element', () => {
 				expect( el3.parent ).to.be.null;
 				expect( el4.parent ).equal( parent );
 			} );
+
+			it( 'should remove one child when second parameter is not specified', () => {
+				parent.appendChildren( el1 );
+				parent.appendChildren( el2 );
+				parent.appendChildren( el3 );
+
+				const removed = parent.removeChildren( 1 );
+
+				expect( parent.getChildCount() ).to.equal( 2 );
+				expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
+				expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el3' );
+
+				expect( removed.length ).to.equal( 1 );
+				expect( removed[ 0 ] ).to.have.property( 'name' ).that.equals( 'el2' );
+			} );
 		} );
 	} );