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
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
},\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
+ * // <div id="outer"><div id="inner"><p><b>Some text</b></p></div></div>\r
+ * // If node == <b>\r
+ * ascendant = node.getAscendant( 'div' ); // ascendant == <div id="inner">\r
+ * ascendant = node.getAscendant( 'b' ); // ascendant == null\r
+ * ascendant = node.getAscendant( 'b', true ); // ascendant == <b>\r
*/\r
getAscendant : function( name, includeSelf )\r
{\r