Kaynağa Gözat

Added: Introduced `view.Node#getPath`.

Szymon Cofalik 7 yıl önce
ebeveyn
işleme
cf6bec37c7

+ 27 - 0
packages/ckeditor5-engine/src/view/node.js

@@ -119,6 +119,33 @@ export default class Node {
 	}
 
 	/**
+	 * Gets a path to the node. The path is an array containing indices of consecutive ancestors of this node,
+	 * beginning from {@link module:engine/view/node~Node#root root}, down to this node's index.
+	 *
+	 *		const abc = new Text( 'abc' );
+	 *		const foo = new Text( 'foo' );
+	 *		const h1 = new Element( 'h1', null, new Text( 'header' ) );
+	 *		const p = new Element( 'p', null, [ abc, foo ] );
+	 *		const div = new Element( 'div', null, [ h1, p ] );
+	 *		foo.getPath(); // Returns [ 1, 3 ]. `foo` is in `p` which is in `div`. `p` starts at offset 1, while `foo` at 3.
+	 *		h1.getPath(); // Returns [ 0 ].
+	 *		div.getPath(); // Returns [].
+	 *
+	 * @returns {Array.<Number>} The path.
+	 */
+	getPath() {
+		const path = [];
+		let node = this; // eslint-disable-line consistent-this
+
+		while ( node.parent ) {
+			path.unshift( node.index );
+			node = node.parent;
+		}
+
+		return path;
+	}
+
+	/**
 	 * Returns ancestors array of this node.
 	 *
 	 * @param {Object} options Options object.

+ 14 - 0
packages/ckeditor5-engine/tests/view/node.js

@@ -223,6 +223,20 @@ describe( 'Node', () => {
 		} );
 	} );
 
+	describe( 'getPath()', () => {
+		it( 'should return empty array is the element is the root', () => {
+			expect( root.getPath() ).to.deep.equal( [] );
+		} );
+
+		it( 'should return array with indices of given element and its ancestors starting from top-most one', () => {
+			expect( one.getPath() ).to.deep.equal( [ 0 ] );
+			expect( two.getPath() ).to.deep.equal( [ 1 ] );
+			expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
+			expect( charR.getPath() ).to.deep.equal( [ 1, 3 ] );
+			expect( three.getPath() ).to.deep.equal( [ 2 ] );
+		} );
+	} );
+
 	describe( 'getDocument()', () => {
 		it( 'should return null if any parent has not set Document', () => {
 			expect( charA.document ).to.be.null;