Преглед изворни кода

Added additional parameter to include node itself in treeView.Node.getAncestors.

Szymon Kupś пре 9 година
родитељ
комит
b03ddf3e2a

+ 5 - 4
packages/ckeditor5-engine/src/treeview/node.js

@@ -106,15 +106,16 @@ export default class Node {
 	}
 
 	/**
-	 * Returns ancestors array of node. Node itself is not included in the array.
+	 * Returns ancestors array of this node.
 	 *
+	 * @param {Boolean} [includeNode=false] When set to `true` this node will be also included in parent's array.
 	 * @param {Boolean} [parentFirst=false] When set to `true`, array will be sorted from node's parent to root element,
 	 * otherwise root element will be the first item in the array.
-	 * @returns {Array} Array with ancestors. Empty array is returned when node has no ancestors.
+	 * @returns {Array} Array with ancestors.
 	 */
-	getAncestors( parentFirst ) {
+	getAncestors( includeNode, parentFirst ) {
 		const ancestors = [];
-		let parent = this.parent;
+		let parent = includeNode ? this : this.parent;
 
 		while ( parent !== null ) {
 			ancestors[ parentFirst ? 'push' : 'unshift' ]( parent );

+ 20 - 1
packages/ckeditor5-engine/tests/treeview/node.js

@@ -64,18 +64,37 @@ describe( 'Node', () => {
 			expect( result.length ).to.equal( 0 );
 		} );
 
+		it( 'should return array including node itself if requested', () => {
+			const result = root.getAncestors( true );
+			expect( result ).to.be.an( 'array' );
+			expect( result.length ).to.equal( 1 );
+			expect( result[ 0 ] ).to.equal( root );
+		} );
+
 		it( 'should return array of ancestors', () => {
 			const result = charR.getAncestors();
 			expect( result.length ).to.equal( 2 );
 			expect( result[ 0 ] ).to.equal( root );
 			expect( result[ 1 ] ).to.equal( two );
+
+			const result2 = charR.getAncestors( true );
+			expect( result2.length ).to.equal( 3 );
+			expect( result2[ 0 ] ).to.equal( root );
+			expect( result2[ 1 ] ).to.equal( two );
+			expect( result2[ 2 ] ).to.equal( charR );
 		} );
 
 		it( 'should return array of ancestors starting from parent', () => {
-			const result = charR.getAncestors( true );
+			const result = charR.getAncestors( false, true );
 			expect( result.length ).to.equal( 2 );
 			expect( result[ 0 ] ).to.equal( two );
 			expect( result[ 1 ] ).to.equal( root );
+
+			const result2 = charR.getAncestors( true, true );
+			expect( result2.length ).to.equal( 3 );
+			expect( result2[ 2 ] ).to.equal( root );
+			expect( result2[ 1 ] ).to.equal( two );
+			expect( result2[ 0 ] ).to.equal( charR );
 		} );
 	} );