JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / core / dom / node.js
index ef41e06..b05b4a7 100644 (file)
@@ -204,29 +204,12 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype,
                        while ( node && node != $documentElement )\r
                        {\r
                                var parentNode = node.parentNode;\r
-                               var currentIndex = -1;\r
 \r
                                if ( parentNode )\r
                                {\r
-                                       for ( var i = 0 ; i < parentNode.childNodes.length ; i++ )\r
-                                       {\r
-                                               var candidate = parentNode.childNodes[i];\r
-\r
-                                               if ( normalized &&\r
-                                                               candidate.nodeType == 3 &&\r
-                                                               candidate.previousSibling &&\r
-                                                               candidate.previousSibling.nodeType == 3 )\r
-                                               {\r
-                                                       continue;\r
-                                               }\r
-\r
-                                               currentIndex++;\r
-\r
-                                               if ( candidate == node )\r
-                                                       break;\r
-                                       }\r
-\r
-                                       address.unshift( currentIndex );\r
+                                       // Get the node index. For performance, call getIndex\r
+                                       // directly, instead of creating a new node object.\r
+                                       address.unshift( this.getIndex.call( { $ : node }, normalized ) );\r
                                }\r
 \r
                                node = parentNode;\r
@@ -247,24 +230,28 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype,
                        return new CKEDITOR.dom.document( this.$.ownerDocument || this.$.parentNode.ownerDocument );\r
                },\r
 \r
-               getIndex : function()\r
+               getIndex : function( normalized )\r
                {\r
-                       var $ = this.$;\r
+                       // Attention: getAddress depends on this.$\r
 \r
-                       var currentNode = $.parentNode && $.parentNode.firstChild;\r
-                       var currentIndex = -1;\r
+                       var current = this.$,\r
+                               index = 0;\r
 \r
-                       while ( currentNode )\r
+                       while ( ( current = current.previousSibling ) )\r
                        {\r
-                               currentIndex++;\r
-\r
-                               if ( currentNode == $ )\r
-                                       return currentIndex;\r
+                               // When normalizing, do not count it if this is an\r
+                               // empty text node or if it's a text node following another one.\r
+                               if ( normalized && current.nodeType == 3 &&\r
+                                        ( !current.nodeValue.length ||\r
+                                          ( current.previousSibling && current.previousSibling.nodeType == 3 ) ) )\r
+                               {\r
+                                       continue;\r
+                               }\r
 \r
-                               currentNode = currentNode.nextSibling;\r
+                               index++;\r
                        }\r
 \r
-                       return -1;\r
+                       return index;\r
                },\r
 \r
                getNextSourceNode : function( startFromSibling, nodeType, guard )\r
@@ -500,11 +487,18 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype,
                },\r
 \r
                /**\r
-                * Gets the closes ancestor node of a specified node name.\r
-                * @param {String} name Node name of ancestor node.\r
-                * @param {Boolean} includeSelf (Optional) Whether to include the current\r
-                * node in the calculation or not.\r
-                * @returns {CKEDITOR.dom.node} Ancestor node.\r
+                * Gets the closest ancestor node of this node, specified by its node name.\r
+                * @param {String} name The node name of the ancestor node to search.\r
+                * @param {Boolean} [includeSelf] Whether to include the current\r
+                * node in the search.\r
+                * @returns {CKEDITOR.dom.node} The located ancestor node or null if not found.\r
+                * @example\r
+                * // Suppose we have the following HTML:\r
+                * // &lt;div id="outer"&gt;&lt;div id="inner"&gt;&lt;p&gt;&lt;b&gt;Some text&lt;/b&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;\r
+                * // If node == &lt;b&gt;\r
+                * ascendant = node.getAscendant( 'div' );      // ascendant == &lt;div id="inner"&gt\r
+                * ascendant = node.getAscendant( 'b' );        // ascendant == null\r
+                * ascendant = node.getAscendant( 'b', true );  // ascendant == &lt;b&gt;\r
                 */\r
                getAscendant : function( name, includeSelf )\r
                {\r