JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / core / dom / node.js
index 6611545..b05b4a7 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -107,24 +107,25 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype,
                {\r
                        var $clone = this.$.cloneNode( includeChildren );\r
 \r
-                       if ( !cloneId )\r
+                       var removeIds = function( node )\r
                        {\r
-                               var removeIds = function( node )\r
-                               {\r
-                                       if ( node.nodeType != CKEDITOR.NODE_ELEMENT )\r
-                                               return;\r
+                               if ( node.nodeType != CKEDITOR.NODE_ELEMENT )\r
+                                       return;\r
 \r
-                                       node.removeAttribute( 'id', false ) ;\r
-                                       node.removeAttribute( '_cke_expando', false ) ;\r
+                               if ( !cloneId )\r
+                                       node.removeAttribute( 'id', false );\r
+                               node.removeAttribute( 'data-cke-expando', false );\r
 \r
+                               if ( includeChildren )\r
+                               {\r
                                        var childs = node.childNodes;\r
-                                       for ( var i=0 ; i < childs.length ; i++ )\r
+                                       for ( var i=0; i < childs.length; i++ )\r
                                                removeIds( childs[ i ] );\r
-                               };\r
+                               }\r
+                       };\r
 \r
-                               // The "id" attribute should never be cloned to avoid duplication.\r
-                               removeIds( $clone );\r
-                       }\r
+                       // The "id" attribute should never be cloned to avoid duplication.\r
+                       removeIds( $clone );\r
 \r
                        return new CKEDITOR.dom.node( $clone );\r
                },\r
@@ -203,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
@@ -243,33 +227,31 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype,
                 */\r
                getDocument : function()\r
                {\r
-                       var document = new CKEDITOR.dom.document( this.$.ownerDocument || this.$.parentNode.ownerDocument );\r
-\r
-                       return (\r
-                       this.getDocument = function()\r
-                               {\r
-                                       return document;\r
-                               })();\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
@@ -505,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
@@ -669,13 +658,13 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype,
                 * @since 3.5\r
                 * @example\r
                 * // For the following HTML:\r
-                * // <div contenteditable="false">Some <b>text</b></div>\r
+                * // &lt;div contenteditable="false"&gt;Some &lt;b&gt;text&lt;/b&gt;&lt;/div&gt;\r
                 *\r
-                * // If "ele" is the above <div>\r
-                * ele.getReadOnlyRoot();  // the <div> element\r
+                * // If "ele" is the above &lt;div&gt;\r
+                * ele.isReadOnly();  // the &lt;div&gt; element\r
                 *\r
-                * // If "ele" is the above <b>\r
-                * ele.getReadOnlyRoot();  // the <div> element\r
+                * // If "ele" is the above &lt;b&gt;\r
+                * ele.isReadOnly();  // the &lt;div&gt; element\r
                 */\r
                isReadOnly : function()\r
                {\r
@@ -684,7 +673,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype,
                        {\r
                                if ( current.type == CKEDITOR.NODE_ELEMENT )\r
                                {\r
-                                       if ( current.is( 'body' ) || current.getCustomData( '_cke_notReadOnly' ) )\r
+                                       if ( current.is( 'body' ) || !!current.data( 'cke-editable' ) )\r
                                                break;\r
 \r
                                        if ( current.getAttribute( 'contentEditable' ) == 'false' )\r