JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5
[ckeditor.git] / _source / core / dom / element.js
index 48b0d26..0243dea 100644 (file)
@@ -307,12 +307,16 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
 \r
                /**\r
                 * Moves the selection focus to this element.\r
+                * @param  {Boolean} defer Whether to asynchronously defer the\r
+                *              execution by 100 ms.\r
                 * @example\r
                 * var element = CKEDITOR.document.getById( 'myTextarea' );\r
                 * <b>element.focus()</b>;\r
                 */\r
-               focus : function()\r
+               focus : ( function()\r
                {\r
+                       function exec()\r
+                       {\r
                        // IE throws error if the element is not visible.\r
                        try\r
                        {\r
@@ -320,7 +324,16 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                        }\r
                        catch (e)\r
                        {}\r
-               },\r
+                       }\r
+\r
+                       return function( defer )\r
+                       {\r
+                               if ( defer )\r
+                                       CKEDITOR.tools.setTimeout( exec, 100, this );\r
+                               else\r
+                                       exec.call( this );\r
+                       };\r
+               })(),\r
 \r
                /**\r
                 * Gets the inner HTML of this element.\r
@@ -725,7 +738,10 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                        {\r
                                var attribute = thisAttribs[ i ];\r
 \r
-                               if ( ( !CKEDITOR.env.ie || ( attribute.specified && attribute.nodeName != '_cke_expando' ) ) && attribute.nodeValue != otherElement.getAttribute( attribute.nodeName ) )\r
+                               if ( attribute.nodeName == '_moz_dirty' )\r
+                                       continue;\r
+\r
+                               if ( ( !CKEDITOR.env.ie || ( attribute.specified && attribute.nodeName != 'data-cke-expando' ) ) && attribute.nodeValue != otherElement.getAttribute( attribute.nodeName ) )\r
                                        return false;\r
                        }\r
 \r
@@ -736,7 +752,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                                for ( i = 0 ; i < otherLength ; i++ )\r
                                {\r
                                        attribute = otherAttribs[ i ];\r
-                                       if ( attribute.specified && attribute.nodeName != '_cke_expando'\r
+                                       if ( attribute.specified && attribute.nodeName != 'data-cke-expando'\r
                                                        && attribute.nodeValue != this.getAttribute( attribute.nodeName ) )\r
                                                return false;\r
                                }\r
@@ -786,7 +802,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                        {\r
                                var child = children.getItem( i );\r
 \r
-                               if ( child.type == CKEDITOR.NODE_ELEMENT && child.getAttribute( '_cke_bookmark' ) )\r
+                               if ( child.type == CKEDITOR.NODE_ELEMENT && child.data( 'cke-bookmark' ) )\r
                                        continue;\r
 \r
                                if ( child.type == CKEDITOR.NODE_ELEMENT && !child.isEmptyInlineRemoveable()\r
@@ -831,7 +847,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                                                                        return true;\r
 \r
                                                        // Attributes to be ignored.\r
-                                                       case '_cke_expando' :\r
+                                                       case 'data-cke-expando' :\r
                                                                continue;\r
 \r
                                                        /*jsl:fallthru*/\r
@@ -851,7 +867,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                                                attrsNum = attrs.length;\r
 \r
                                        // The _moz_dirty attribute might get into the element after pasting (#5455)\r
-                                       var execludeAttrs = { _cke_expando : 1, _moz_dirty : 1 };\r
+                                       var execludeAttrs = { 'data-cke-expando' : 1, _moz_dirty : 1 };\r
 \r
                                        return attrsNum > 0 &&\r
                                                ( attrsNum > 2 ||\r
@@ -914,7 +930,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                                        // queuing them to be moved later. (#5567)\r
                                        var pendingNodes = [];\r
 \r
-                                       while ( sibling.getAttribute( '_cke_bookmark' )\r
+                                       while ( sibling.data( 'cke-bookmark' )\r
                                                || sibling.isEmptyInlineRemoveable() )\r
                                        {\r
                                                pendingNodes.push( sibling );\r
@@ -1480,7 +1496,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
 \r
                        // Replace the node.\r
                        this.getParent() && this.$.parentNode.replaceChild( newNode.$, this.$ );\r
-                       newNode.$._cke_expando = this.$._cke_expando;\r
+                       newNode.$[ 'data-cke-expando' ] = this.$[ 'data-cke-expando' ];\r
                        this.$ = newNode.$;\r
                },\r
 \r
@@ -1522,40 +1538,78 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                },\r
 \r
                /**\r
-                *  Update the element's size with box model awareness.\r
-                * @name CKEDITOR.dom.element.setSize\r
-                * @param {String} type [width|height]\r
-                * @param {Number} size The length unit in px.\r
-                * @param isBorderBox Apply the {@param width} and {@param height} based on border box model.\r
+                * Gets element's direction. Supports both CSS 'direction' prop and 'dir' attr.\r
                 */\r
-               setSize : ( function()\r
+               getDirection : function( useComputed )\r
                {\r
-                       var sides = {\r
-                               width : [ "border-left-width", "border-right-width","padding-left", "padding-right" ],\r
-                               height : [ "border-top-width", "border-bottom-width", "padding-top",  "padding-bottom" ]\r
-                       };\r
-\r
-                       return function( type, size, isBorderBox )\r
-                               {\r
-                                       if ( typeof size == 'number' )\r
-                                       {\r
-                                               if ( isBorderBox && !( CKEDITOR.env.ie && CKEDITOR.env.quirks ) )\r
-                                               {\r
-                                                       var     adjustment = 0;\r
-                                                       for ( var i = 0, len = sides[ type ].length; i < len; i++ )\r
-                                                               adjustment += parseInt( this.getComputedStyle( sides [ type ][ i ] ) || 0, 10 ) || 0;\r
-                                                       size -= adjustment;\r
-                                               }\r
-                                               this.setStyle( type, size + 'px' );\r
-                                       }\r
-                               };\r
-               })(),\r
+                       return useComputed ? this.getComputedStyle( 'direction' ) : this.getStyle( 'direction' ) || this.getAttribute( 'dir' );\r
+               },\r
 \r
                /**\r
-                * Gets element's direction. Supports both CSS 'direction' prop and 'dir' attr.\r
+                * Gets, sets and removes custom data to be stored as HTML5 data-* attributes.\r
+                * @name CKEDITOR.dom.element.data\r
+                * @param {String} name The name of the attribute, execluding the 'data-' part.\r
+                * @param {String} [value] The value to set. If set to false, the attribute will be removed.\r
                 */\r
-               getDirection : function( useComputed )\r
+               data : function ( name, value )\r
                {\r
-                       return useComputed ? this.getComputedStyle( 'direction' ) : this.getStyle( 'direction' ) || this.getAttribute( 'dir' );\r
+                       name = 'data-' + name;\r
+                       if ( value === undefined )\r
+                               return this.getAttribute( name );\r
+                       else if ( value === false )\r
+                               this.removeAttribute( name );\r
+                       else\r
+                               this.setAttribute( name, value );\r
                }\r
        });\r
+\r
+( function()\r
+{\r
+       var sides = {\r
+               width : [ "border-left-width", "border-right-width","padding-left", "padding-right" ],\r
+               height : [ "border-top-width", "border-bottom-width", "padding-top",  "padding-bottom" ]\r
+       };\r
+\r
+       function marginAndPaddingSize( type )\r
+       {\r
+               var adjustment = 0;\r
+               for ( var i = 0, len = sides[ type ].length; i < len; i++ )\r
+                       adjustment += parseInt( this.getComputedStyle( sides [ type ][ i ] ) || 0, 10 ) || 0;\r
+               return adjustment;\r
+       }\r
+\r
+       /**\r
+        * Update the element's size with box model awareness.\r
+        * @name CKEDITOR.dom.element.setSize\r
+        * @param {String} type [width|height]\r
+        * @param {Number} size The length unit in px.\r
+        * @param isBorderBox Apply the {@param width} and {@param height} based on border box model.\r
+        */\r
+       CKEDITOR.dom.element.prototype.setSize = function( type, size, isBorderBox )\r
+               {\r
+                       if ( typeof size == 'number' )\r
+                       {\r
+                               if ( isBorderBox && !( CKEDITOR.env.ie && CKEDITOR.env.quirks ) )\r
+                                       size -= marginAndPaddingSize.call( this, type );\r
+\r
+                               this.setStyle( type, size + 'px' );\r
+                       }\r
+               };\r
+\r
+       /**\r
+        * Get the element's size, possibly with box model awareness.\r
+        * @name CKEDITOR.dom.element.getSize\r
+        * @param {String} type [width|height]\r
+        * @param {Boolean} contentSize Get the {@param width} or {@param height} based on border box model.\r
+        */\r
+       CKEDITOR.dom.element.prototype.getSize = function( type, contentSize )\r
+               {\r
+                       var size = Math.max( this.$[ 'offset' + CKEDITOR.tools.capitalize( type )  ],\r
+                               this.$[ 'client' + CKEDITOR.tools.capitalize( type )  ] ) || 0;\r
+\r
+                       if ( contentSize )\r
+                               size -= marginAndPaddingSize.call( this, type );\r
+\r
+                       return size;\r
+               };\r
+})();\r