Explorar o código

Returning number of inserted children in core.treeView.Element appendChildren and insertChildren methods.

Szymon Kupś %!s(int64=9) %!d(string=hai) anos
pai
achega
db2d9f31f1

+ 9 - 2
packages/ckeditor5-engine/src/treeview/element.js

@@ -88,11 +88,13 @@ export default class Element extends Node {
 	 * {@link core.treeView.Element#insert Insert} a child node or a list of child nodes at the end of this node and sets
 	 * the parent of these nodes to this element.
 	 *
-	 * @param {core.treeView.Node|Iterable.<core.treeView.Node>} nodes Node or the list of nodes to be inserted.
 	 * @fires core.treeView.Node#change
+	 * @param {core.treeView.Node|Iterable.<core.treeView.Node>} nodes Node or the list of nodes to be inserted.
+	 * @returns {Number} Number of appended nodes.
+
 	 */
 	appendChildren( nodes ) {
-		this.insertChildren( this.getChildCount(), nodes );
+		return this.insertChildren( this.getChildCount(), nodes );
 	}
 
 	/**
@@ -182,9 +184,11 @@ export default class Element extends Node {
 	 * @param {Number} index Position where nodes should be inserted.
 	 * @param {core.treeView.Node|Iterable.<core.treeView.Node>} nodes Node or the list of nodes to be inserted.
 	 * @fires core.treeView.Node#change
+	 * @returns {Number} Number of inserted nodes.
 	 */
 	insertChildren( index, nodes ) {
 		this._fireChange( 'CHILDREN', this );
+		let count = 0;
 
 		if ( !utils.isIterable( nodes ) ) {
 			nodes = [ nodes ];
@@ -195,7 +199,10 @@ export default class Element extends Node {
 
 			this._children.splice( index, 0, node );
 			index++;
+			count++;
 		}
+
+		return count;
 	}
 
 	/**

+ 10 - 5
packages/ckeditor5-engine/tests/treeview/element.js

@@ -153,24 +153,29 @@ describe( 'Element', () => {
 
 		describe( 'insertion', () => {
 			it( 'should insert children', () => {
-				parent.insertChildren( 0, [ el1, el3 ] );
-				parent.insertChildren( 1, el2 );
+				const count1 = parent.insertChildren( 0, [ el1, el3 ] );
+				const count2 = parent.insertChildren( 1, el2 );
 
 				expect( parent.getChildCount() ).to.equal( 3 );
 				expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
 				expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
 				expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
+				expect( count1 ).to.equal( 2 );
+				expect( count2 ).to.equal( 1 );
 			} );
 
 			it( 'should append children', () => {
-				parent.insertChildren( 0, el1 );
-				parent.appendChildren( el2 );
-				parent.appendChildren( el3 );
+				const count1 = parent.insertChildren( 0, el1 );
+				const count2 = parent.appendChildren( el2 );
+				const count3 = parent.appendChildren( el3 );
 
 				expect( parent.getChildCount() ).to.equal( 3 );
 				expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
 				expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
 				expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
+				expect( count1 ).to.equal( 1 );
+				expect( count2 ).to.equal( 1 );
+				expect( count3 ).to.equal( 1 );
 			} );
 		} );