Ver código fonte

Introduced node.positionInParent property.

Piotr Jasiun 10 anos atrás
pai
commit
3e2d47d562

+ 10 - 0
packages/ckeditor5-utils/src/document/node.js

@@ -38,6 +38,16 @@ CKEDITOR.define( function() {
 		}
 
 		/**
+		 * Position of the node in the parent element.
+		 *
+		 * @readonly
+		 * @property {Number} positionInParent
+		 */
+		get positionInParent() {
+			return this.parent ? this.parent.children.indexOf( this ) : null;
+		}
+
+		/**
 		 * Dept of the node, which equals total number of its parents.
 		 *
 		 * @readonly

+ 13 - 0
packages/ckeditor5-utils/tests/document/node.js

@@ -43,6 +43,19 @@ describe( 'tree', function() {
 		root.children.push( three );
 	} );
 
+	it( 'should have proper positionInParent', function() {
+		expect( root ).to.have.property( 'positionInParent' ).that.is.null;
+
+		expect( one ).to.have.property( 'positionInParent' ).that.equals( 0 );
+		expect( two ).to.have.property( 'positionInParent' ).that.equals( 1 );
+		expect( three ).to.have.property( 'positionInParent' ).that.equals( 2 );
+
+		expect( charB ).to.have.property( 'positionInParent' ).that.equals( 0 );
+		expect( charA ).to.have.property( 'positionInParent' ).that.equals( 1 );
+		expect( img ).to.have.property( 'positionInParent' ).that.equals( 2 );
+		expect( charR ).to.have.property( 'positionInParent' ).that.equals( 3 );
+	} );
+
 	it( 'should have proper depth', function() {
 		expect( root ).to.have.property( 'depth' ).that.equals( 0 );