X-Git-Url: https://jasonwoof.com/gitweb/?p=ckeditor.git;a=blobdiff_plain;f=_source%2Fcore%2Fdom%2Fnode.js;h=b05b4a7aa6fb0cd880b4c82b9500c0825da34de6;hp=ef41e06bafb09cd617ac9aac2c28cfc6f6e50cee;hb=4e90e78dc97789709ee7404359a5517540c27553;hpb=8f6c203fdaa543c3bca40baea6ae4ddcdf1a77f5 diff --git a/_source/core/dom/node.js b/_source/core/dom/node.js index ef41e06..b05b4a7 100644 --- a/_source/core/dom/node.js +++ b/_source/core/dom/node.js @@ -204,29 +204,12 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, while ( node && node != $documentElement ) { var parentNode = node.parentNode; - var currentIndex = -1; if ( parentNode ) { - for ( var i = 0 ; i < parentNode.childNodes.length ; i++ ) - { - var candidate = parentNode.childNodes[i]; - - if ( normalized && - candidate.nodeType == 3 && - candidate.previousSibling && - candidate.previousSibling.nodeType == 3 ) - { - continue; - } - - currentIndex++; - - if ( candidate == node ) - break; - } - - address.unshift( currentIndex ); + // Get the node index. For performance, call getIndex + // directly, instead of creating a new node object. + address.unshift( this.getIndex.call( { $ : node }, normalized ) ); } node = parentNode; @@ -247,24 +230,28 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, return new CKEDITOR.dom.document( this.$.ownerDocument || this.$.parentNode.ownerDocument ); }, - getIndex : function() + getIndex : function( normalized ) { - var $ = this.$; + // Attention: getAddress depends on this.$ - var currentNode = $.parentNode && $.parentNode.firstChild; - var currentIndex = -1; + var current = this.$, + index = 0; - while ( currentNode ) + while ( ( current = current.previousSibling ) ) { - currentIndex++; - - if ( currentNode == $ ) - return currentIndex; + // When normalizing, do not count it if this is an + // empty text node or if it's a text node following another one. + if ( normalized && current.nodeType == 3 && + ( !current.nodeValue.length || + ( current.previousSibling && current.previousSibling.nodeType == 3 ) ) ) + { + continue; + } - currentNode = currentNode.nextSibling; + index++; } - return -1; + return index; }, getNextSourceNode : function( startFromSibling, nodeType, guard ) @@ -500,11 +487,18 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, }, /** - * Gets the closes ancestor node of a specified node name. - * @param {String} name Node name of ancestor node. - * @param {Boolean} includeSelf (Optional) Whether to include the current - * node in the calculation or not. - * @returns {CKEDITOR.dom.node} Ancestor node. + * Gets the closest ancestor node of this node, specified by its node name. + * @param {String} name The node name of the ancestor node to search. + * @param {Boolean} [includeSelf] Whether to include the current + * node in the search. + * @returns {CKEDITOR.dom.node} The located ancestor node or null if not found. + * @example + * // Suppose we have the following HTML: + * // <div id="outer"><div id="inner"><p><b>Some text</b></p></div></div> + * // If node == <b> + * ascendant = node.getAscendant( 'div' ); // ascendant == <div id="inner"> + * ascendant = node.getAscendant( 'b' ); // ascendant == null + * ascendant = node.getAscendant( 'b', true ); // ascendant == <b> */ getAscendant : function( name, includeSelf ) {