Kaynağa Gözat

Changed Node.positionInParent to Node.getIndex().

Piotr Jasiun 10 yıl önce
ebeveyn
işleme
1cb0232799

+ 25 - 12
packages/ckeditor5-utils/src/document/node.js

@@ -5,7 +5,7 @@
 
 'use strict';
 
-CKEDITOR.define( [ 'document/attribute', 'utils' ], function( Attribute, utils ) {
+CKEDITOR.define( [ 'document/attribute', 'utils', 'ckeditorerror' ], function( Attribute, utils, CKEditorError ) {
 	/**
 	 * Abstract document tree node class.
 	 *
@@ -42,19 +42,32 @@ CKEDITOR.define( [ 'document/attribute', 'utils' ], function( Attribute, utils )
 		}
 
 		/**
-		 * Position of the node in the parent element.
+		 * Index of the node in the parent element or null if the node has no parent.
 		 *
-		 * @readonly
-		 * @property {Number} positionInParent
+		 * Throws error if the parent element does not contain this node.
+		 *
+		 * @returns {Number|Null} Index of the node in the parent element or null if the node has not parent.
 		 */
-		get positionInParent() {
+		getIndex() {
 			var pos;
 
-			// No parent or child doesn't exist in parent's children.
-			if ( !this.parent || ( pos = this.parent.getChildIndex( this ) ) == -1 ) {
+			if ( !this.parent ) {
 				return null;
 			}
 
+			// No parent or child doesn't exist in parent's children.
+			if ( ( pos = this.parent.getChildIndex( this ) ) == -1 ) {
+				/**
+				 * The nodes parent does not contain that node.
+				 *
+				 * @error node-not-found-in-parent
+				 * @param {document.Node} node
+				 */
+				throw new CKEditorError(
+					'node-not-found-in-parent: The nodes parent does not contain that node.',
+					{ node: this } );
+			}
+
 			return pos;
 		}
 
@@ -100,9 +113,9 @@ CKEDITOR.define( [ 'document/attribute', 'utils' ], function( Attribute, utils )
 		 * @property {document.Node|null} nextSibling
 		 */
 		get nextSibling() {
-			var pos = this.positionInParent;
+			var index = this.getIndex();
 
-			return ( pos !== null && this.parent.getChild( pos + 1 ) ) || null;
+			return ( index !== null && this.parent.getChild( index + 1 ) ) || null;
 		}
 
 		/**
@@ -112,9 +125,9 @@ CKEDITOR.define( [ 'document/attribute', 'utils' ], function( Attribute, utils )
 		 * @property {document.Node|null} previousSibling
 		 */
 		get previousSibling() {
-			var pos = this.positionInParent;
+			var index = this.getIndex();
 
-			return ( pos !== null && this.parent.getChild( pos - 1 ) ) || null;
+			return ( index !== null && this.parent.getChild( index - 1 ) ) || null;
 		}
 
 		/**
@@ -200,7 +213,7 @@ CKEDITOR.define( [ 'document/attribute', 'utils' ], function( Attribute, utils )
 			var node = this; // jscs:ignore safeContextKeyword
 
 			while ( node.parent ) {
-				path.unshift( node.positionInParent );
+				path.unshift( node.getIndex() );
 				node = node.parent;
 			}
 

+ 2 - 2
packages/ckeditor5-utils/src/document/position.js

@@ -149,7 +149,7 @@ CKEDITOR.define( [ 'utils', 'ckeditorerror' ], function( utils, CKEditorError )
 				throw new CKEditorError( 'position-before-root: You can not make position before root.', { root: node } );
 			}
 
-			return Position.createFromParentAndOffset( node.parent, node.positionInParent );
+			return Position.createFromParentAndOffset( node.parent, node.getIndex() );
 		}
 
 		/**
@@ -169,7 +169,7 @@ CKEDITOR.define( [ 'utils', 'ckeditorerror' ], function( utils, CKEditorError )
 				throw new CKEditorError( 'position-after-root: You can not make position after root.', { root: node } );
 			}
 
-			return Position.createFromParentAndOffset( node.parent, node.positionInParent + 1 );
+			return Position.createFromParentAndOffset( node.parent, node.getIndex() + 1 );
 		}
 	}
 

+ 2 - 2
packages/ckeditor5-utils/src/document/positioniterator.js

@@ -87,7 +87,7 @@ CKEDITOR.define( [
 
 				return formatReturnValue( CHARACTER, nodeAfter );
 			} else {
-				this.position = Position.createFromParentAndOffset( parent.parent, parent.positionInParent + 1 );
+				this.position = Position.createFromParentAndOffset( parent.parent, parent.getIndex() + 1 );
 
 				return formatReturnValue( ELEMENT_LEAVE, this.position.nodeBefore );
 			}
@@ -127,7 +127,7 @@ CKEDITOR.define( [
 
 				return formatReturnValue( CHARACTER, nodeBefore );
 			} else {
-				this.position = Position.createFromParentAndOffset( parent.parent, parent.positionInParent );
+				this.position = Position.createFromParentAndOffset( parent.parent, parent.getIndex() );
 
 				return formatReturnValue( ELEMENT_ENTER, this.position.nodeAfter );
 			}

+ 34 - 15
packages/ckeditor5-utils/tests/document/node.js

@@ -11,11 +11,12 @@ var modules = bender.amd.require(
 	'document/element',
 	'document/character',
 	'document/attribute',
-	'document/nodelist'
+	'document/nodelist',
+	'ckeditorerror'
 );
 
 describe( 'Node', function() {
-	var Element, Character, Attribute, NodeList;
+	var Element, Character, Attribute, NodeList, CKEditorError;
 
 	var root;
 	var one, two, three;
@@ -26,6 +27,7 @@ describe( 'Node', function() {
 		Character = modules[ 'document/character' ];
 		Attribute = modules[ 'document/attribute' ];
 		NodeList = modules[ 'document/nodelist' ];
+		CKEditorError = modules.ckeditorerror;
 
 		charB = new Character( 'b' );
 		charA = new Character( 'a' );
@@ -40,19 +42,6 @@ describe( 'Node', function() {
 	} );
 
 	describe( 'should have a correct property', function() {
-		it( '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( 'depth', function() {
 			expect( root ).to.have.property( 'depth' ).that.equals( 0 );
 
@@ -216,4 +205,34 @@ describe( 'Node', function() {
 			expect( parsedBar.parent ).to.equal( 'foo' );
 		} );
 	} );
+
+	describe( 'getIndex', function() {
+		it( 'should return null if the parent is null', function() {
+			expect( root.getIndex() ).to.be.null;
+		} );
+
+		it( 'should return index in the parent', function() {
+			expect( one.getIndex() ).to.equal( 0 );
+			expect( two.getIndex() ).to.equal( 1 );
+			expect( three.getIndex() ).to.equal( 2 );
+
+			expect( charB.getIndex() ).to.equal( 0 );
+			expect( charA.getIndex() ).to.equal( 1 );
+			expect( img.getIndex() ).to.equal( 2 );
+			expect( charR.getIndex() ).to.equal( 3 );
+		} );
+
+		it( 'should throw an error if parent does not contains element', function() {
+			var f = new Character( 'f' );
+			var bar = new Element( 'bar', [], [] );
+
+			f.parent = bar;
+
+			expect(
+				function() {
+					f.getIndex();
+				}
+			).to.throw( CKEditorError, /node-not-found-in-parent/ );
+		} );
+	} );
 } );