8
0
Pārlūkot izejas kodu

nextSibling and previousSibling use positionInParent.

Piotr Jasiun 10 gadi atpakaļ
vecāks
revīzija
c60c531b08
1 mainītis faili ar 12 papildinājumiem un 15 dzēšanām
  1. 12 15
      packages/ckeditor5-utils/src/document/node.js

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

@@ -44,7 +44,14 @@ CKEDITOR.define( function() {
 		 * @property {Number} positionInParent
 		 */
 		get positionInParent() {
-			return this.parent ? this.parent.children.indexOf( this ) : null;
+			var pos;
+
+			// No parent or child doesn't exist in parent's children.
+			if ( !this.parent || ( pos = this.parent.children.indexOf( this ) ) == -1 ) {
+				return null;
+			}
+
+			return pos;
 		}
 
 		/**
@@ -73,14 +80,9 @@ CKEDITOR.define( function() {
 		 * @property {document.Node|Null} nextSibling
 		 */
 		get nextSibling() {
-			var i;
+			var pos = this.positionInParent;
 
-			// No parent or child doesn't exist in parent's children.
-			if ( !this.parent || ( i = this.parent.children.indexOf( this ) ) == -1 ) {
-				return null;
-			}
-
-			return this.parent.children[ i + 1 ] || null;
+			return ( pos !== null && this.parent.children[ pos + 1 ] ) || null;
 		}
 
 		/**
@@ -90,14 +92,9 @@ CKEDITOR.define( function() {
 		 * @property {document.Node|Null} previousSibling
 		 */
 		get previousSibling() {
-			var i;
-
-			// No parent or child doesn't exist in parent's children.
-			if ( !this.parent || ( i = this.parent.children.indexOf( this ) ) == -1 ) {
-				return null;
-			}
+			var pos = this.positionInParent;
 
-			return this.parent.children[ i - 1 ] || null;
+			return ( pos !== null && this.parent.children[ pos - 1 ] ) || null;
 		}
 	}