JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.3.2
[ckeditor.git] / _source / core / dom / element.js
index 534d6e1..b2f9bcc 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
@@ -616,7 +621,6 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                        }\r
 \r
                        return (\r
-                       /** @ignore */\r
                        this.getName = function()\r
                                {\r
                                        return nodeName;\r
@@ -773,6 +777,31 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                },\r
 \r
                /**\r
+                * Whether it's an empty inline elements which has no visual impact when removed.\r
+                */\r
+               isEmptyInlineRemoveable : function()\r
+               {\r
+                       if ( !CKEDITOR.dtd.$removeEmpty[ this.getName() ] )\r
+                               return false;\r
+\r
+                       var children = this.getChildren();\r
+                       for ( var i = 0, count = children.count(); i < count; i++ )\r
+                       {\r
+                               var child = children.getItem( i );\r
+\r
+                               if ( child.type == CKEDITOR.NODE_ELEMENT && child.getAttribute( '_fck_bookmark' ) )\r
+                                       continue;\r
+\r
+                               if ( child.type == CKEDITOR.NODE_ELEMENT && !child.isEmptyInlineRemoveable()\r
+                                       || child.type == CKEDITOR.NODE_TEXT && CKEDITOR.tools.trim( child.getText() ) )\r
+                               {\r
+                                       return false;\r
+                               }\r
+                       }\r
+                       return true;\r
+               },\r
+\r
+               /**\r
                 * Indicates that the element has defined attributes.\r
                 * @returns {Boolean} True if the element has attributes.\r
                 * @example\r
@@ -821,8 +850,16 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                        :\r
                                function()\r
                                {\r
-                                       var attributes = this.$.attributes;\r
-                                       return ( attributes.length > 1 || ( attributes.length == 1 && attributes[0].nodeName != '_cke_expando' ) );\r
+                                       var attrs = this.$.attributes,\r
+                                               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
+\r
+                                       return attrsNum > 0 &&\r
+                                               ( attrsNum > 2 ||\r
+                                                       !execludeAttrs[ attrs[0].nodeName ] ||\r
+                                                       ( attrsNum == 2 && !execludeAttrs[ attrs[1].nodeName ] ) );\r
                                },\r
 \r
                /**\r
@@ -870,6 +907,56 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                        }\r
                },\r
 \r
+               mergeSiblings : ( function()\r
+               {\r
+                       function mergeElements( element, sibling, isNext )\r
+                       {\r
+                               if ( sibling && sibling.type == CKEDITOR.NODE_ELEMENT )\r
+                               {\r
+                                       // Jumping over bookmark nodes and empty inline elements, e.g. <b><i></i></b>,\r
+                                       // queuing them to be moved later. (#5567)\r
+                                       var pendingNodes = [];\r
+\r
+                                       while ( sibling.getAttribute( '_fck_bookmark' )\r
+                                               || sibling.isEmptyInlineRemoveable() )\r
+                                       {\r
+                                               pendingNodes.push( sibling );\r
+                                               sibling = isNext ? sibling.getNext() : sibling.getPrevious();\r
+                                               if ( !sibling || sibling.type != CKEDITOR.NODE_ELEMENT )\r
+                                                       return;\r
+                                       }\r
+\r
+                                       if ( element.isIdentical( sibling ) )\r
+                                       {\r
+                                               // Save the last child to be checked too, to merge things like\r
+                                               // <b><i></i></b><b><i></i></b> => <b><i></i></b>\r
+                                               var innerSibling = isNext ? element.getLast() : element.getFirst();\r
+\r
+                                               // Move pending nodes first into the target element.\r
+                                               while( pendingNodes.length )\r
+                                                       pendingNodes.shift().move( element, !isNext );\r
+\r
+                                               sibling.moveChildren( element, !isNext );\r
+                                               sibling.remove();\r
+\r
+                                               // Now check the last inner child (see two comments above).\r
+                                               if ( innerSibling && innerSibling.type == CKEDITOR.NODE_ELEMENT )\r
+                                                       innerSibling.mergeSiblings();\r
+                                       }\r
+                               }\r
+                       }\r
+\r
+                       return function()\r
+                               {\r
+                                       // Merge empty links and anchors also. (#5567)\r
+                                       if ( !( CKEDITOR.dtd.$removeEmpty[ this.getName() ] || this.is( 'a' ) ) )\r
+                                               return;\r
+\r
+                                       mergeElements( this, this.getNext(), true );\r
+                                       mergeElements( this, this.getPrevious() );\r
+                               };\r
+               } )(),\r
+\r
                /**\r
                 * Shows this element (display it).\r
                 * @example\r
@@ -987,8 +1074,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
@@ -1075,11 +1170,13 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                                function()\r
                                {\r
                                        this.$.style.MozUserSelect = 'none';\r
+                                       this.on( 'dragstart', function (evt) { evt.data.preventDefault(); } );\r
                                }\r
                        : CKEDITOR.env.webkit ?\r
                                function()\r
                                {\r
                                        this.$.style.KhtmlUserSelect = 'none';\r
+                                       this.on( 'dragstart', function (evt) { evt.data.preventDefault(); } );\r
                                }\r
                        :\r
                                function()\r
@@ -1344,7 +1441,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                                if ( attrName in skipAttributes )\r
                                        continue;\r
 \r
-                               if( attrName == 'checked' && ( attrValue = this.getAttribute( attrName ) ) )\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
                                else if ( attribute.specified ||\r
@@ -1385,7 +1482,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                        this.moveChildren( newNode );\r
 \r
                        // Replace the node.\r
-                       this.$.parentNode.replaceChild( newNode.$, this.$ );\r
+                       this.getParent() && this.$.parentNode.replaceChild( newNode.$, this.$ );\r
                        newNode.$._cke_expando = this.$._cke_expando;\r
                        this.$ = newNode.$;\r
                },\r