JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.3
[ckeditor.git] / _source / core / dom / element.js
index ead6581..48b0d26 100644 (file)
@@ -88,7 +88,7 @@ CKEDITOR.dom.element.setMarker = function( database, element, name, value )
 CKEDITOR.dom.element.clearAllMarkers = function( database )\r
 {\r
        for ( var i in database )\r
-               CKEDITOR.dom.element.clearMarkers( database, database[i], true );\r
+               CKEDITOR.dom.element.clearMarkers( database, database[i], 1 );\r
 };\r
 \r
 CKEDITOR.dom.element.clearMarkers = function( database, element, removeFromDatabase )\r
@@ -613,7 +613,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                        // Cache the lowercased name inside a closure.\r
                        var nodeName = this.$.nodeName.toLowerCase();\r
 \r
-                       if ( CKEDITOR.env.ie )\r
+                       if ( CKEDITOR.env.ie && ! ( document.documentMode > 8 ) )\r
                        {\r
                                var scopeName = this.$.scopeName;\r
                                if ( scopeName != 'HTML' )\r
@@ -721,9 +721,6 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                        var thisLength = thisAttribs.length,\r
                                otherLength = otherAttribs.length;\r
 \r
-                       if ( !CKEDITOR.env.ie && thisLength != otherLength )\r
-                               return false;\r
-\r
                        for ( var i = 0 ; i < thisLength ; i++ )\r
                        {\r
                                var attribute = thisAttribs[ i ];\r
@@ -777,6 +774,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( '_cke_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
@@ -825,8 +847,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
@@ -874,6 +904,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( '_cke_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
@@ -1087,11 +1167,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
@@ -1397,7 +1479,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
@@ -1437,5 +1519,43 @@ CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
                                        if ( !event.data.getTarget().hasClass( 'cke_enable_context_menu' ) )\r
                                                event.data.preventDefault();\r
                                } );\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
+               setSize : ( 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
+                       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
+\r
+               /**\r
+                * Gets element's direction. Supports both CSS 'direction' prop and 'dir' attr.\r
+                */\r
+               getDirection : function( useComputed )\r
+               {\r
+                       return useComputed ? this.getComputedStyle( 'direction' ) : this.getStyle( 'direction' ) || this.getAttribute( 'dir' );\r
                }\r
        });\r