JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2
[ckeditor.git] / _source / core / dom / element.js
index 296b84c..ead6581 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -243,10 +243,13 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                                lastChild = lastChild.getPrevious();\r
                        if ( !lastChild || !lastChild.is || !lastChild.is( 'br' ) )\r
                        {\r
-                               this.append(\r
-                                       CKEDITOR.env.opera ?\r
+                               var bogus = CKEDITOR.env.opera ?\r
                                                this.getDocument().createText('') :\r
-                                               this.getDocument().createElement( 'br' ) );\r
+                                               this.getDocument().createElement( 'br' );\r
+\r
+                               CKEDITOR.env.gecko && bogus.setAttribute( 'type', '_moz' );\r
+\r
+                               this.append( bogus );\r
                        }\r
                },\r
 \r
@@ -256,15 +259,15 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                 * @param {CKEDITOR.dom.element} parent The anscestor element to get broken.\r
                 * @example\r
                 * // Before breaking:\r
-                * //     <b>This <i>is some<span /> sample</i> test text</b>\r
-                * // If "element" is <span /> and "parent" is <i>:\r
-                * //     <b>This <i>is some</i><span /><i> sample</i> test text</b>\r
+                * //     &lt;b&gt;This &lt;i&gt;is some&lt;span /&gt; sample&lt;/i&gt; test text&lt;/b&gt;\r
+                * // If "element" is &lt;span /&gt; and "parent" is &lt;i&gt;:\r
+                * //     &lt;b&gt;This &lt;i&gt;is some&lt;/i&gt;&lt;span /&gt;&lt;i&gt; sample&lt;/i&gt; test text&lt;/b&gt;\r
                 * element.breakParent( parent );\r
                 * @example\r
                 * // Before breaking:\r
-                * //     <b>This <i>is some<span /> sample</i> test text</b>\r
-                * // If "element" is <span /> and "parent" is <b>:\r
-                * //     <b>This <i>is some</i></b><span /><b><i> sample</i> test text</b>\r
+                * //     &lt;b&gt;This &lt;i&gt;is some&lt;span /&gt; sample&lt;/i&gt; test text&lt;/b&gt;\r
+                * // If "element" is &lt;span /&gt; and "parent" is &lt;b&gt;:\r
+                * //     &lt;b&gt;This &lt;i&gt;is some&lt;/i&gt;&lt;/b&gt;&lt;span /&gt;&lt;b&gt;&lt;i&gt; sample&lt;/i&gt; test text&lt;/b&gt;\r
                 * element.breakParent( parent );\r
                 */\r
                breakParent : function( parent )\r
@@ -328,7 +331,9 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                 */\r
                getHtml : function()\r
                {\r
-                       return this.$.innerHTML;\r
+                       var retval = this.$.innerHTML;\r
+                       // Strip <?xml:namespace> tags in IE. (#3341).\r
+                       return CKEDITOR.env.ie ? retval.replace( /<\?[^>]*>/g, '' ) : retval;\r
                },\r
 \r
                getOuterHtml : function()\r
@@ -425,8 +430,16 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                                                        break;\r
 \r
                                                case 'checked':\r
-                                                       return this.$.checked;\r
-                                                       break;\r
+                                               {\r
+                                                       var attr = this.$.attributes.getNamedItem( name ),\r
+                                                               attrValue = attr.specified ? attr.nodeValue     // For value given by parser.\r
+                                                                                                                        : this.$.checked;  // For value created via DOM interface.\r
+\r
+                                                       return attrValue ? 'checked' : null;\r
+                                               }\r
+\r
+                                               case 'hspace':\r
+                                                       return this.$.hspace;\r
 \r
                                                case 'style':\r
                                                        // IE does not return inline styles via getAttribute(). See #2947.\r
@@ -608,7 +621,6 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                        }\r
 \r
                        return (\r
-                       /** @ignore */\r
                        this.getName = function()\r
                                {\r
                                        return nodeName;\r
@@ -627,6 +639,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
 \r
                /**\r
                 * Gets the first child node of this element.\r
+                * @param {Function} evaluator Filtering the result node.\r
                 * @returns {CKEDITOR.dom.node} The first child node or null if not\r
                 *              available.\r
                 * @example\r
@@ -634,10 +647,14 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                 * var first = <b>element.getFirst()</b>;\r
                 * alert( first.getName() );  // "b"\r
                 */\r
-               getFirst : function()\r
+               getFirst : function( evaluator )\r
                {\r
-                       var $ = this.$.firstChild;\r
-                       return $ ? new CKEDITOR.dom.node( $ ) : null;\r
+                       var first = this.$.firstChild,\r
+                               retval = first && new CKEDITOR.dom.node( first );\r
+                       if ( retval && evaluator && !evaluator( retval ) )\r
+                               retval = retval.getNext( evaluator );\r
+\r
+                       return retval;\r
                },\r
 \r
                /**\r
@@ -722,8 +739,8 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                                for ( i = 0 ; i < otherLength ; i++ )\r
                                {\r
                                        attribute = otherAttribs[ i ];\r
-\r
-                                       if ( ( !CKEDITOR.env.ie || ( attribute.specified && attribute.nodeName != '_cke_expando' ) ) && attribute.nodeValue != thisAttribs.getAttribute( attribute.nodeName ) )\r
+                                       if ( attribute.specified && attribute.nodeName != '_cke_expando'\r
+                                                       && attribute.nodeValue != this.getAttribute( attribute.nodeName ) )\r
                                                return false;\r
                                }\r
                        }\r
@@ -739,7 +756,24 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                 */\r
                isVisible : function()\r
                {\r
-                       return this.$.offsetWidth && ( this.$.style.visibility != 'hidden' );\r
+                       var isVisible = !!this.$.offsetHeight && this.getComputedStyle( 'visibility' ) != 'hidden',\r
+                               elementWindow,\r
+                               elementWindowFrame;\r
+\r
+                       // Webkit and Opera report non-zero offsetHeight despite that\r
+                       // element is inside an invisible iframe. (#4542)\r
+                       if ( isVisible && ( CKEDITOR.env.webkit || CKEDITOR.env.opera ) )\r
+                       {\r
+                               elementWindow = this.getWindow();\r
+\r
+                               if ( !elementWindow.equals( CKEDITOR.document.getWindow() )\r
+                                               && ( elementWindowFrame = elementWindow.$.frameElement ) )\r
+                               {\r
+                                       isVisible = new CKEDITOR.dom.element( elementWindowFrame ).isVisible();\r
+                               }\r
+                       }\r
+\r
+                       return isVisible;\r
                },\r
 \r
                /**\r
@@ -957,8 +991,16 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
 \r
                removeAttributes : function ( attributes )\r
                {\r
-                       for ( var i = 0 ; i < attributes.length ; i++ )\r
-                               this.removeAttribute( attributes[ i ] );\r
+                       if ( CKEDITOR.tools.isArray( attributes ) )\r
+                       {\r
+                               for ( var i = 0 ; i < attributes.length ; i++ )\r
+                                       this.removeAttribute( attributes[ i ] );\r
+                       }\r
+                       else\r
+                       {\r
+                               for ( var attr in attributes )\r
+                                       attributes.hasOwnProperty( attr ) && this.removeAttribute( attr );\r
+                       }\r
                },\r
 \r
                /**\r
@@ -971,10 +1013,9 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                 */\r
                removeStyle : function( name )\r
                {\r
+                       this.setStyle( name, '' );\r
                        if ( this.$.style.removeAttribute )\r
                                this.$.style.removeAttribute( CKEDITOR.tools.cssStyleToDomStyle( name ) );\r
-                       else\r
-                               this.setStyle( name, '' );\r
 \r
                        if ( !this.$.style.cssText )\r
                                this.removeAttribute( 'style' );\r
@@ -1306,16 +1347,22 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                        {\r
                                var attribute = attributes[n];\r
 \r
+                               // Lowercase attribute name hard rule is broken for\r
+                               // some attribute on IE, e.g. CHECKED.\r
+                               var attrName = attribute.nodeName.toLowerCase(),\r
+                                       attrValue;\r
+\r
+                               // We can set the type only once, so do it with the proper value, not copying it.\r
+                               if ( attrName in skipAttributes )\r
+                                       continue;\r
+\r
+                               if ( attrName == 'checked' && ( attrValue = this.getAttribute( attrName ) ) )\r
+                                       dest.setAttribute( attrName, attrValue );\r
                                // IE BUG: value attribute is never specified even if it exists.\r
-                               if ( attribute.specified ||\r
-                                 ( CKEDITOR.env.ie && attribute.nodeValue && attribute.nodeName.toLowerCase() == 'value' ) )\r
+                               else if ( attribute.specified ||\r
+                                 ( CKEDITOR.env.ie && attribute.nodeValue && attrName == 'value' ) )\r
                                {\r
-                                       var attrName = attribute.nodeName;\r
-                                       // We can set the type only once, so do it with the proper value, not copying it.\r
-                                       if ( attrName in skipAttributes )\r
-                                               continue;\r
-\r
-                                       var attrValue = this.getAttribute( attrName );\r
+                                       attrValue = this.getAttribute( attrName );\r
                                        if ( attrValue === null )\r
                                                attrValue = attribute.nodeValue;\r
 \r