Browse Source

Added missing test and API docs for the new function.

Piotrek Koszuliński 9 years ago
parent
commit
15d6fffff5

+ 11 - 0
packages/ckeditor5-engine/src/model/element.js

@@ -198,6 +198,17 @@ export default class Element extends Node {
 		return nodes;
 	}
 
+	/**
+	 * Returns a descendant node by its path relative to this element.
+	 *
+	 *		// <this>a<b>c</b></this>
+	 *		this.getNodeByPath( [ 0 ] );     // -> "a"
+	 *		this.getNodeByPath( [ 1 ] );     // -> <b>
+	 *		this.getNodeByPath( [ 1, 0 ] );  // -> "c"
+	 *
+	 * @param {Array.<Number>} relativePath Path of the node to find, relative to this element.
+	 * @returns {engine.model.Node}
+	 */
 	getNodeByPath( relativePath ) {
 		let node = this;
 

+ 20 - 0
packages/ckeditor5-engine/tests/model/element.js

@@ -144,6 +144,26 @@ describe( 'Element', () => {
 		} );
 	} );
 
+	describe( 'getNodeByPath', () => {
+		it( 'should return this node if path is empty', () => {
+			const element = new Element( 'elem' );
+
+			expect( element.getNodeByPath( [] ) ).to.equal( element );
+		} );
+
+		it( 'should return a descendant of this node', () => {
+			const image = new Element( 'image' );
+			const element = new Element( 'elem', [], [
+				new Element( 'elem', [], [
+					new Text( 'foo' ),
+					image
+				] )
+			] );
+
+			expect( element.getNodeByPath( [ 0, 1 ] ) ).to.equal( image );
+		} );
+	} );
+
 	describe( 'getChildIndex', () => {
 		it( 'should return child index', () => {
 			let element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );