JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / core / dom / node.js
index a4188dc..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
@@ -25,6 +25,10 @@ CKEDITOR.dom.node = function( domNode )
        {\r
                switch ( domNode.nodeType )\r
                {\r
+                       // Safari don't consider document as element node type. (#3389)\r
+                       case CKEDITOR.NODE_DOCUMENT :\r
+                               return new CKEDITOR.dom.document( domNode );\r
+\r
                        case CKEDITOR.NODE_ELEMENT :\r
                                return new CKEDITOR.dom.element( domNode );\r
 \r
@@ -49,6 +53,13 @@ CKEDITOR.dom.node.prototype = new CKEDITOR.dom.domObject();
 CKEDITOR.NODE_ELEMENT = 1;\r
 \r
 /**\r
+ * Document node type.\r
+ * @constant\r
+ * @example\r
+ */\r
+CKEDITOR.NODE_DOCUMENT = 9;\r
+\r
+/**\r
  * Text node type.\r
  * @constant\r
  * @example\r
@@ -96,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
@@ -192,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
@@ -232,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
@@ -494,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
@@ -646,6 +646,45 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype,
                                        child.parentNode.removeChild( child ) ;\r
                                }\r
                        }\r
+               },\r
+\r
+               /**\r
+                * Checks is this node is read-only (should not be changed). It\r
+                * additionaly returns the element, if any, which defines the read-only\r
+                * state of this node. It may be the node itself or any of its parent\r
+                * nodes.\r
+                * @returns {CKEDITOR.dom.element|Boolean} An element containing\r
+                *              read-only attributes or "false" if none is found.\r
+                * @since 3.5\r
+                * @example\r
+                * // For the following HTML:\r
+                * // &lt;div contenteditable="false"&gt;Some &lt;b&gt;text&lt;/b&gt;&lt;/div&gt;\r
+                *\r
+                * // If "ele" is the above &lt;div&gt;\r
+                * ele.isReadOnly();  // the &lt;div&gt; element\r
+                *\r
+                * // If "ele" is the above &lt;b&gt;\r
+                * ele.isReadOnly();  // the &lt;div&gt; element\r
+                */\r
+               isReadOnly : function()\r
+               {\r
+                       var current = this;\r
+                       while( current )\r
+                       {\r
+                               if ( current.type == CKEDITOR.NODE_ELEMENT )\r
+                               {\r
+                                       if ( current.is( 'body' ) || !!current.data( 'cke-editable' ) )\r
+                                               break;\r
+\r
+                                       if ( current.getAttribute( 'contentEditable' ) == 'false' )\r
+                                               return current;\r
+                                       else if ( current.getAttribute( 'contentEditable' ) == 'true' )\r
+                                               break;\r
+                               }\r
+                               current = current.getParent();\r
+                       }\r
+\r
+                       return false;\r
                }\r
        }\r
 );\r