JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.3
[ckeditor.git] / _source / plugins / styles / plugin.js
index 2e82127..37126f1 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
@@ -12,8 +12,8 @@ CKEDITOR.plugins.add( 'styles',
  * Registers a function to be called whenever a style changes its state in the\r
  * editing area. The current state is passed to the function. The possible\r
  * states are {@link CKEDITOR.TRISTATE_ON} and {@link CKEDITOR.TRISTATE_OFF}.\r
- * @param {CKEDITOR.style} The style to be watched.\r
- * @param {Function} The function to be called when the style state changes.\r
+ * @param {CKEDITOR.style} style The style to be watched.\r
+ * @param {Function} callback The function to be called when the style state changes.\r
  * @example\r
  * // Create a style object for the <b> element.\r
  * var style = new CKEDITOR.style( { element : 'b' } );\r
@@ -60,13 +60,13 @@ CKEDITOR.editor.prototype.attachStyleStateChange = function( style, callback )
 \r
                                                // Save the current state, so it can be compared next\r
                                                // time.\r
-                                               callback.state !== currentState;\r
+                                               callback.state = currentState;\r
                                        }\r
                                }\r
                        });\r
        }\r
 \r
-       // Save the callback info, so it can be checked on the next occurence of\r
+       // Save the callback info, so it can be checked on the next occurrence of\r
        // selectionChange.\r
        styleStateChangeCallbacks.push( { style : style, fn : callback } );\r
 };\r
@@ -78,7 +78,7 @@ CKEDITOR.STYLE_OBJECT = 3;
 (function()\r
 {\r
        var blockElements       = { address:1,div:1,h1:1,h2:1,h3:1,h4:1,h5:1,h6:1,p:1,pre:1 };\r
-       var objectElements      = { a:1,embed:1,hr:1,img:1,li:1,object:1,ol:1,table:1,td:1,tr:1,ul:1 };\r
+       var objectElements      = { a:1,embed:1,hr:1,img:1,li:1,object:1,ol:1,table:1,td:1,tr:1,th:1,ul:1,dl:1,dt:1,dd:1,form:1};\r
 \r
        var semicolonFixRegex = /\s*(?:;\s*|$)/;\r
 \r
@@ -127,6 +127,8 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                        applyInlineStyle\r
                                                : this.type == CKEDITOR.STYLE_BLOCK ?\r
                                                        applyBlockStyle\r
+                                               : this.type == CKEDITOR.STYLE_OBJECT ?\r
+                                                       applyObjectStyle\r
                                                : null ).call( this, range );\r
                },\r
 \r
@@ -135,6 +137,8 @@ CKEDITOR.STYLE_OBJECT = 3;
                        return ( this.removeFromRange =\r
                                                this.type == CKEDITOR.STYLE_INLINE ?\r
                                                        removeInlineStyle\r
+                                               : this.type == CKEDITOR.STYLE_OBJECT ?\r
+                                                       removeObjectStyle\r
                                                : null ).call( this, range );\r
                },\r
 \r
@@ -154,17 +158,23 @@ CKEDITOR.STYLE_OBJECT = 3;
                                case CKEDITOR.STYLE_BLOCK :\r
                                        return this.checkElementRemovable( elementPath.block || elementPath.blockLimit, true );\r
 \r
+                               case CKEDITOR.STYLE_OBJECT :\r
                                case CKEDITOR.STYLE_INLINE :\r
 \r
                                        var elements = elementPath.elements;\r
 \r
                                        for ( var i = 0, element ; i < elements.length ; i++ )\r
                                        {\r
-                                               element = elements[i];\r
+                                               element = elements[ i ];\r
 \r
-                                               if ( element == elementPath.block || element == elementPath.blockLimit )\r
+                                               if ( this.type == CKEDITOR.STYLE_INLINE\r
+                                                         && ( element == elementPath.block || element == elementPath.blockLimit ) )\r
                                                        continue;\r
 \r
+                                               if( this.type == CKEDITOR.STYLE_OBJECT\r
+                                                        && !( element.getName() in objectElements ) )\r
+                                                               continue;\r
+\r
                                                if ( this.checkElementRemovable( element, true ) )\r
                                                        return true;\r
                                        }\r
@@ -172,6 +182,25 @@ CKEDITOR.STYLE_OBJECT = 3;
                        return false;\r
                },\r
 \r
+               /**\r
+                * Whether this style can be applied at the element path.\r
+                * @param elementPath\r
+                */\r
+               checkApplicable : function( elementPath )\r
+               {\r
+                       switch ( this.type )\r
+                       {\r
+                               case CKEDITOR.STYLE_INLINE :\r
+                               case CKEDITOR.STYLE_BLOCK :\r
+                                       break;\r
+\r
+                               case CKEDITOR.STYLE_OBJECT :\r
+                                       return elementPath.lastElement.getAscendant( this.element, true );\r
+                       }\r
+\r
+                       return true;\r
+               },\r
+\r
                // Checks if an element, or any of its attributes, is removable by the\r
                // current style definition.\r
                checkElementRemovable : function( element, fullMatch )\r
@@ -198,10 +227,12 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                if ( attName == '_length' )\r
                                                        continue;\r
 \r
-                                               var elementAttr = element.getAttribute( attName );\r
-                                               if ( attribs[attName] ==\r
-                                                        ( attName == 'style' ?\r
-                                                          normalizeCssText( elementAttr, false ) : elementAttr  ) )\r
+                                               var elementAttr = element.getAttribute( attName ) || '';\r
+\r
+                                               // Special treatment for 'style' attribute is required.\r
+                                               if ( attName == 'style' ?\r
+                                                       compareCssText( attribs[ attName ], normalizeCssText( elementAttr, false ) )\r
+                                                       : attribs[ attName ] == elementAttr  )\r
                                                {\r
                                                        if ( !fullMatch )\r
                                                                return true;\r
@@ -209,7 +240,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                else if ( fullMatch )\r
                                                                return false;\r
                                        }\r
-                                       if( fullMatch )\r
+                                       if ( fullMatch )\r
                                                return true;\r
                                }\r
                                else\r
@@ -246,6 +277,39 @@ CKEDITOR.STYLE_OBJECT = 3;
                                }\r
                        }\r
                        return false;\r
+               },\r
+\r
+               // Builds the preview HTML based on the styles definition.\r
+               buildPreview : function()\r
+               {\r
+                       var styleDefinition = this._.definition,\r
+                               html = [],\r
+                               elementName = styleDefinition.element;\r
+\r
+                       // Avoid <bdo> in the preview.\r
+                       if ( elementName == 'bdo' )\r
+                               elementName = 'span';\r
+\r
+                       html = [ '<', elementName ];\r
+\r
+                       // Assign all defined attributes.\r
+                       var attribs     = styleDefinition.attributes;\r
+                       if ( attribs )\r
+                       {\r
+                               for ( var att in attribs )\r
+                               {\r
+                                       html.push( ' ', att, '="', attribs[ att ], '"' );\r
+                               }\r
+                       }\r
+\r
+                       // Assign the style attribute.\r
+                       var cssStyle = CKEDITOR.style.getStyleText( styleDefinition );\r
+                       if ( cssStyle )\r
+                               html.push( ' style="', cssStyle, '"' );\r
+\r
+                       html.push( '>', styleDefinition.name, '</', elementName, '>' );\r
+\r
+                       return html.join( '' );\r
                }\r
        };\r
 \r
@@ -260,20 +324,31 @@ CKEDITOR.STYLE_OBJECT = 3;
                stylesDef = styleDefinition.styles;\r
 \r
                // Builds the StyleText.\r
-\r
-               var stylesText = ( styleDefinition.attributes && styleDefinition.attributes[ 'style' ] ) || '';\r
+               var stylesText = ( styleDefinition.attributes && styleDefinition.attributes[ 'style' ] ) || '',\r
+                               specialStylesText = '';\r
 \r
                if ( stylesText.length )\r
                        stylesText = stylesText.replace( semicolonFixRegex, ';' );\r
 \r
                for ( var style in stylesDef )\r
-                       stylesText += ( style + ':' + stylesDef[ style ] ).replace( semicolonFixRegex, ';' );\r
+               {\r
+                       var styleVal = stylesDef[ style ],\r
+                                       text = ( style + ':' + styleVal ).replace( semicolonFixRegex, ';' );\r
+\r
+                       // Some browsers don't support 'inherit' property value, leave them intact. (#5242)\r
+                       if ( styleVal == 'inherit' )\r
+                               specialStylesText += text;\r
+                       else\r
+                               stylesText += text;\r
+               }\r
 \r
                // Browsers make some changes to the style when applying them. So, here\r
                // we normalize it to the browser format.\r
                if ( stylesText.length )\r
                        stylesText = normalizeCssText( stylesText );\r
 \r
+               stylesText += specialStylesText;\r
+\r
                // Return it, saving it to the next request.\r
                return ( styleDefinition._ST = stylesText );\r
        };\r
@@ -303,57 +378,20 @@ CKEDITOR.STYLE_OBJECT = 3;
                // Get the DTD definition for the element. Defaults to "span".\r
                var dtd = CKEDITOR.dtd[ elementName ] || ( isUnknownElement = true, CKEDITOR.dtd.span );\r
 \r
-               // Bookmark the range so we can re-select it after processing.\r
-               var bookmark = range.createBookmark();\r
-\r
                // Expand the range.\r
                range.enlarge( CKEDITOR.ENLARGE_ELEMENT );\r
                range.trim();\r
 \r
                // Get the first node to be processed and the last, which concludes the\r
                // processing.\r
-               var boundaryNodes = range.getBoundaryNodes();\r
-               var firstNode = boundaryNodes.startNode;\r
-               var lastNode = boundaryNodes.endNode.getNextSourceNode( true );\r
-\r
-               // Probably the document end is reached, we need a marker node.\r
-               if ( !lastNode )\r
-               {\r
-                               var marker;\r
-                               lastNode = marker = document.createText( '' );\r
-                               lastNode.insertAfter( range.endContainer );\r
-               }\r
-               // The detection algorithm below skips the contents inside bookmark nodes, so\r
-               // we'll need to make sure lastNode isn't the &nbsp; inside a bookmark node.\r
-               var lastParent = lastNode.getParent();\r
-               if ( lastParent && lastParent.getAttribute( '_fck_bookmark' ) )\r
-                       lastNode = lastParent;\r
-\r
-               if ( lastNode.equals( firstNode ) )\r
-               {\r
-                       // If the last node is the same as the the first one, we must move\r
-                       // it to the next one, otherwise the first one will not be\r
-                       // processed.\r
-                       lastNode = lastNode.getNextSourceNode( true );\r
-\r
-                       // It may happen that there are no more nodes after it (the end of\r
-                       // the document), so we must add something there to make our code\r
-                       // simpler.\r
-                       if ( !lastNode )\r
-                       {\r
-                               lastNode = marker = document.createText( '' );\r
-                               lastNode.insertAfter( firstNode );\r
-                       }\r
-               }\r
+               var boundaryNodes = range.createBookmark(),\r
+                       firstNode = boundaryNodes.startNode,\r
+                       lastNode = boundaryNodes.endNode;\r
 \r
                var currentNode = firstNode;\r
 \r
                var styleRange;\r
 \r
-               // Indicates that that some useful inline content has been found, so\r
-               // the style should be applied.\r
-               var hasContents;\r
-\r
                while ( currentNode )\r
                {\r
                        var applyStyle = false;\r
@@ -368,20 +406,24 @@ CKEDITOR.STYLE_OBJECT = 3;
                                var nodeType = currentNode.type;\r
                                var nodeName = nodeType == CKEDITOR.NODE_ELEMENT ? currentNode.getName() : null;\r
 \r
-                               if ( nodeName && currentNode.getAttribute( '_fck_bookmark' ) )\r
+                               if ( nodeName && currentNode.getAttribute( '_cke_bookmark' ) )\r
                                {\r
                                        currentNode = currentNode.getNextSourceNode( true );\r
                                        continue;\r
                                }\r
 \r
                                // Check if the current node can be a child of the style element.\r
-                               if ( !nodeName || ( dtd[ nodeName ] && ( currentNode.getPosition( lastNode ) | CKEDITOR.POSITION_PRECEDING | CKEDITOR.POSITION_IDENTICAL | CKEDITOR.POSITION_IS_CONTAINED ) == ( CKEDITOR.POSITION_PRECEDING + CKEDITOR.POSITION_IDENTICAL + CKEDITOR.POSITION_IS_CONTAINED ) ) )\r
+                               if ( !nodeName || ( dtd[ nodeName ]\r
+                                       && ( currentNode.getPosition( lastNode ) | CKEDITOR.POSITION_PRECEDING | CKEDITOR.POSITION_IDENTICAL | CKEDITOR.POSITION_IS_CONTAINED ) == ( CKEDITOR.POSITION_PRECEDING + CKEDITOR.POSITION_IDENTICAL + CKEDITOR.POSITION_IS_CONTAINED )\r
+                                       && ( !def.childRule || def.childRule( currentNode ) ) ) )\r
                                {\r
                                        var currentParent = currentNode.getParent();\r
 \r
                                        // Check if the style element can be a child of the current\r
                                        // node parent or if the element is not defined in the DTD.\r
-                                       if ( currentParent && ( ( currentParent.getDtd() || CKEDITOR.dtd.span )[ elementName ] || isUnknownElement ) )\r
+                                       if ( currentParent\r
+                                               && ( ( currentParent.getDtd() || CKEDITOR.dtd.span )[ elementName ] || isUnknownElement )\r
+                                               && ( !def.parentRule || def.parentRule( currentParent ) ) )\r
                                        {\r
                                                // This node will be part of our range, so if it has not\r
                                                // been started, place its start right before the node.\r
@@ -406,7 +448,8 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                        // to the range.\r
                                                        while ( !includedNode.$.nextSibling\r
                                                                && ( parentNode = includedNode.getParent(), dtd[ parentNode.getName() ] )\r
-                                                               && ( parentNode.getPosition( firstNode ) | CKEDITOR.POSITION_FOLLOWING | CKEDITOR.POSITION_IDENTICAL | CKEDITOR.POSITION_IS_CONTAINED ) == ( CKEDITOR.POSITION_FOLLOWING + CKEDITOR.POSITION_IDENTICAL + CKEDITOR.POSITION_IS_CONTAINED ) )\r
+                                                               && ( parentNode.getPosition( firstNode ) | CKEDITOR.POSITION_FOLLOWING | CKEDITOR.POSITION_IDENTICAL | CKEDITOR.POSITION_IS_CONTAINED ) == ( CKEDITOR.POSITION_FOLLOWING + CKEDITOR.POSITION_IDENTICAL + CKEDITOR.POSITION_IS_CONTAINED )\r
+                                                               && ( !def.childRule || def.childRule( parentNode ) ) )\r
                                                        {\r
                                                                includedNode = parentNode;\r
                                                        }\r
@@ -419,8 +462,6 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                        if ( !includedNode.$.nextSibling )\r
                                                                applyStyle = true;\r
 \r
-                                                       if ( !hasContents )\r
-                                                               hasContents = ( nodeType != CKEDITOR.NODE_TEXT || (/[^\s\ufeff]/).test( currentNode.getText() ) );\r
                                                }\r
                                        }\r
                                        else\r
@@ -434,42 +475,67 @@ CKEDITOR.STYLE_OBJECT = 3;
                        }\r
 \r
                        // Apply the style if we have something to which apply it.\r
-                       if ( applyStyle && hasContents && styleRange && !styleRange.collapsed )\r
+                       if ( applyStyle && styleRange && !styleRange.collapsed )\r
                        {\r
                                // Build the style element, based on the style object definition.\r
-                               var styleNode = getElement( this, document );\r
+                               var styleNode = getElement( this, document ),\r
+                                       styleHasAttrs = styleNode.hasAttributes();\r
 \r
                                // Get the element that holds the entire range.\r
                                var parent = styleRange.getCommonAncestor();\r
 \r
+                               var removeList = {\r
+                                       styles : {},\r
+                                       attrs : {},\r
+                                       // Styles cannot be removed.\r
+                                       blockedStyles : {},\r
+                                       // Attrs cannot be removed.\r
+                                       blockedAttrs : {}\r
+                               };\r
+\r
+                               var attName, styleName, value;\r
+\r
                                // Loop through the parents, removing the redundant attributes\r
                                // from the element to be applied.\r
                                while ( styleNode && parent )\r
                                {\r
                                        if ( parent.getName() == elementName )\r
                                        {\r
-                                               for ( var attName in def.attributes )\r
+                                               for ( attName in def.attributes )\r
                                                {\r
-                                                       if ( styleNode.getAttribute( attName ) == parent.getAttribute( attName ) )\r
-                                                               styleNode.removeAttribute( attName );\r
-                                               }\r
+                                                       if ( removeList.blockedAttrs[ attName ] || !( value = parent.getAttribute( styleName ) ) )\r
+                                                               continue;\r
 \r
-                                               for ( var styleName in def.styles )\r
-                                               {\r
-                                                       if ( styleNode.getStyle( styleName ) == parent.getStyle( styleName ) )\r
-                                                               styleNode.removeStyle( styleName );\r
+                                                       if ( styleNode.getAttribute( attName ) == value )\r
+                                                               removeList.attrs[ attName ] = 1;\r
+                                                       else\r
+                                                               removeList.blockedAttrs[ attName ] = 1;\r
                                                }\r
 \r
-                                               if ( !styleNode.hasAttributes() )\r
+                                               for ( styleName in def.styles )\r
                                                {\r
-                                                       styleNode = null;\r
-                                                       break;\r
+                                                       if ( removeList.blockedStyles[ styleName ] || !( value = parent.getStyle( styleName ) ) )\r
+                                                               continue;\r
+\r
+                                                       if ( styleNode.getStyle( styleName ) == value )\r
+                                                               removeList.styles[ styleName ] = 1;\r
+                                                       else\r
+                                                               removeList.blockedStyles[ styleName ] = 1;\r
                                                }\r
                                        }\r
 \r
                                        parent = parent.getParent();\r
                                }\r
 \r
+                               for ( attName in removeList.attrs )\r
+                                       styleNode.removeAttribute( attName );\r
+\r
+                               for ( styleName in removeList.styles )\r
+                                       styleNode.removeStyle( styleName );\r
+\r
+                               if ( styleHasAttrs && !styleNode.hasAttributes() )\r
+                                       styleNode = null;\r
+\r
                                if ( styleNode )\r
                                {\r
                                        // Move the contents of the range to the style element.\r
@@ -484,7 +550,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                                        styleRange.insertNode( styleNode );\r
 \r
                                        // Let's merge our new style with its neighbors, if possible.\r
-                                       mergeSiblings( styleNode );\r
+                                       styleNode.mergeSiblings();\r
 \r
                                        // As the style system breaks text nodes constantly, let's normalize\r
                                        // things for performance.\r
@@ -495,6 +561,15 @@ CKEDITOR.STYLE_OBJECT = 3;
                                        if ( !CKEDITOR.env.ie )\r
                                                styleNode.$.normalize();\r
                                }\r
+                               // Style already inherit from parents, left just to clear up any internal overrides. (#5931)\r
+                               else\r
+                               {\r
+                                       styleNode = new CKEDITOR.dom.element( 'span' );\r
+                                       styleRange.extractContents().appendTo( styleNode );\r
+                                       styleRange.insertNode( styleNode );\r
+                                       removeFromInsideElement( this, styleNode );\r
+                                       styleNode.remove( true );\r
+                               }\r
 \r
                                // Style applied, let's release the range, so it gets\r
                                // re-initialization in the next loop.\r
@@ -502,9 +577,11 @@ CKEDITOR.STYLE_OBJECT = 3;
                        }\r
                }\r
 \r
-               // Remove the temporary marking node.(#4111)\r
-               marker && marker.remove();\r
-               range.moveToBookmark( bookmark );\r
+               // Remove the bookmark nodes.\r
+               range.moveToBookmark( boundaryNodes );\r
+\r
+               // Minimize the result range to exclude empty text nodes. (#5374)\r
+               range.shrink( CKEDITOR.SHRINK_TEXT );\r
        }\r
 \r
        function removeInlineStyle( range )\r
@@ -541,12 +618,14 @@ CKEDITOR.STYLE_OBJECT = 3;
 \r
                                if ( this.checkElementRemovable( element ) )\r
                                {\r
-                                       var endOfElement = range.checkBoundaryOfElement( element, CKEDITOR.END ),\r
-                                                       startOfElement = !endOfElement && range.checkBoundaryOfElement( element, CKEDITOR.START );\r
-                                       if ( startOfElement || endOfElement )\r
+                                       var isStart;\r
+\r
+                                       if ( range.collapsed && (\r
+                                                range.checkBoundaryOfElement( element, CKEDITOR.END ) ||\r
+                                                ( isStart = range.checkBoundaryOfElement( element, CKEDITOR.START ) ) ) )\r
                                        {\r
                                                boundaryElement = element;\r
-                                               boundaryElement.match = startOfElement ? 'start' : 'end';\r
+                                               boundaryElement.match = isStart ? 'start' : 'end';\r
                                        }\r
                                        else\r
                                        {\r
@@ -556,7 +635,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                 * no difference that they're separate entities in the DOM tree. So, merge\r
                                                 * them before removal.\r
                                                 */\r
-                                               mergeSiblings( element );\r
+                                               element.mergeSiblings();\r
                                                removeFromElement( this, element );\r
 \r
                                        }\r
@@ -575,7 +654,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                                        if ( newElement.equals( boundaryElement ) )\r
                                                break;\r
                                        // Avoid copying any matched element.\r
-                                       else if( newElement.match )\r
+                                       else if ( newElement.match )\r
                                                continue;\r
                                        else\r
                                                newElement = newElement.clone();\r
@@ -645,7 +724,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                                if ( currentNode.type == CKEDITOR.NODE_ELEMENT && this.checkElementRemovable( currentNode ) )\r
                                {\r
                                        // Remove style from element or overriding element.\r
-                                       if( currentNode.getName() == this.element )\r
+                                       if ( currentNode.getName() == this.element )\r
                                                removeFromElement( this, currentNode );\r
                                        else\r
                                                removeOverrides( currentNode, getOverrides( this )[ currentNode.getName() ] );\r
@@ -669,6 +748,48 @@ CKEDITOR.STYLE_OBJECT = 3;
                range.moveToBookmark( bookmark );\r
 }\r
 \r
+       function applyObjectStyle( range )\r
+       {\r
+               var root = range.getCommonAncestor( true, true ),\r
+                               element = root.getAscendant( this.element, true );\r
+               element && setupElement( element, this );\r
+       }\r
+\r
+       function removeObjectStyle( range )\r
+       {\r
+               var root = range.getCommonAncestor( true, true ),\r
+                               element = root.getAscendant( this.element, true );\r
+\r
+               if ( !element )\r
+                       return;\r
+\r
+               var style = this;\r
+               var def = style._.definition;\r
+               var attributes = def.attributes;\r
+               var styles = CKEDITOR.style.getStyleText( def );\r
+\r
+               // Remove all defined attributes.\r
+               if ( attributes )\r
+               {\r
+                       for ( var att in attributes )\r
+                       {\r
+                               element.removeAttribute( att, attributes[ att ] );\r
+                       }\r
+               }\r
+\r
+               // Assign all defined styles.\r
+               if ( def.styles )\r
+               {\r
+                       for ( var i in def.styles )\r
+                       {\r
+                               if ( !def.styles.hasOwnProperty( i ) )\r
+                                       continue;\r
+\r
+                               element.removeStyle( i );\r
+                       }\r
+               }\r
+       }\r
+\r
        function applyBlockStyle( range )\r
        {\r
                // Serializible bookmarks is needed here since\r
@@ -678,13 +799,17 @@ CKEDITOR.STYLE_OBJECT = 3;
                var iterator = range.createIterator();\r
                iterator.enforceRealBlocks = true;\r
 \r
+               // make recognize <br /> tag as a separator in ENTER_BR mode (#5121)\r
+               if ( this._.enterMode )\r
+                       iterator.enlargeBr = ( this._.enterMode != CKEDITOR.ENTER_BR );\r
+\r
                var block;\r
                var doc = range.document;\r
                var previousPreBlock;\r
 \r
-               while( ( block = iterator.getNextParagraph() ) )                // Only one =\r
+               while ( ( block = iterator.getNextParagraph() ) )               // Only one =\r
                {\r
-                       var newBlock = getElement( this, doc );\r
+                       var newBlock = getElement( this, doc, block );\r
                        replaceBlock( block, newBlock );\r
                }\r
 \r
@@ -719,13 +844,14 @@ CKEDITOR.STYLE_OBJECT = 3;
                }\r
        }\r
 \r
+       var nonWhitespaces = CKEDITOR.dom.walker.whitespaces( true );\r
        /**\r
         * Merge a <pre> block with a previous sibling if available.\r
         */\r
        function mergePre( preBlock )\r
        {\r
                var previousBlock;\r
-               if ( !( ( previousBlock = preBlock.getPreviousSourceNode( true, CKEDITOR.NODE_ELEMENT ) )\r
+               if ( !( ( previousBlock = preBlock.getPrevious( nonWhitespaces ) )\r
                                 && previousBlock.is\r
                                 && previousBlock.is( 'pre') ) )\r
                        return;\r
@@ -757,7 +883,7 @@ CKEDITOR.STYLE_OBJECT = 3;
        {\r
                // Exclude the ones at header OR at tail,\r
                // and ignore bookmark content between them.\r
-               var duoBrRegex = /(\S\s*)\n(?:\s|(<span[^>]+_fck_bookmark.*?\/span>))*\n(?!$)/gi,\r
+               var duoBrRegex = /(\S\s*)\n(?:\s|(<span[^>]+_cke_bookmark.*?\/span>))*\n(?!$)/gi,\r
                        blockName = preBlock.getName(),\r
                        splitedHtml = replace( preBlock.getOuterHtml(),\r
                                duoBrRegex,\r
@@ -767,7 +893,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                                } );\r
 \r
                var pres = [];\r
-               splitedHtml.replace( /<pre>([\s\S]*?)<\/pre>/gi, function( match, preContent ){\r
+               splitedHtml.replace( /<pre\b.*?>([\s\S]*?)<\/pre>/gi, function( match, preContent ){\r
                        pres.push( preContent );\r
                } );\r
                return pres;\r
@@ -779,7 +905,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                var headBookmark = '',\r
                        tailBookmark = '';\r
 \r
-               str = str.replace( /(^<span[^>]+_fck_bookmark.*?\/span>)|(<span[^>]+_fck_bookmark.*?\/span>$)/gi,\r
+               str = str.replace( /(^<span[^>]+_cke_bookmark.*?\/span>)|(<span[^>]+_cke_bookmark.*?\/span>$)/gi,\r
                        function( str, m1, m2 ){\r
                                        m1 && ( headBookmark = m1 );\r
                                        m2 && ( tailBookmark = m2 );\r
@@ -868,31 +994,34 @@ CKEDITOR.STYLE_OBJECT = 3;
        function removeFromElement( style, element )\r
        {\r
                var def = style._.definition,\r
-                       attributes = def.attributes,\r
+                       attributes = CKEDITOR.tools.extend( {}, def.attributes, getOverrides( style )[ element.getName() ] ),\r
                        styles = def.styles,\r
-                       overrides = getOverrides( style );\r
+                       // If the style is only about the element itself, we have to remove the element.\r
+                       removeEmpty = CKEDITOR.tools.isEmpty( attributes ) && CKEDITOR.tools.isEmpty( styles );\r
 \r
-               function removeAttrs()\r
+               // Remove definition attributes/style from the elemnt.\r
+               for ( var attName in attributes )\r
                {\r
-                       for ( var attName in attributes )\r
-                       {\r
-                               // The 'class' element value must match (#1318).\r
-                               if ( attName == 'class' && element.getAttribute( attName ) != attributes[ attName ] )\r
-                                       continue;\r
-                               element.removeAttribute( attName );\r
-                       }\r
+                       // The 'class' element value must match (#1318).\r
+                       if ( ( attName == 'class' || style._.definition.fullMatch )\r
+                               && element.getAttribute( attName ) != normalizeProperty( attName, attributes[ attName ] ) )\r
+                               continue;\r
+                       removeEmpty = element.hasAttribute( attName );\r
+                       element.removeAttribute( attName );\r
                }\r
 \r
-               // Remove definition attributes/style from the elemnt.\r
-               removeAttrs();\r
                for ( var styleName in styles )\r
+               {\r
+                       // Full match style insist on having fully equivalence. (#5018)\r
+                       if ( style._.definition.fullMatch\r
+                               && element.getStyle( styleName ) != normalizeProperty( styleName, styles[ styleName ], true ) )\r
+                               continue;\r
+\r
+                       removeEmpty = removeEmpty || !!element.getStyle( styleName );\r
                        element.removeStyle( styleName );\r
+               }\r
 \r
-               // Now remove override styles on the element.\r
-               attributes = overrides[ element.getName() ];\r
-               if( attributes )\r
-                       removeAttrs();\r
-               removeNoAttribsElement( element );\r
+               removeEmpty && removeNoAttribsElement( element );\r
        }\r
 \r
        // Removes a style from inside an element.\r
@@ -979,52 +1108,16 @@ CKEDITOR.STYLE_OBJECT = 3;
                        if ( firstChild )\r
                        {\r
                                // Check the cached nodes for merging.\r
-                               mergeSiblings( firstChild );\r
-\r
-                               if ( lastChild && !firstChild.equals( lastChild ) )\r
-                                       mergeSiblings( lastChild );\r
-                       }\r
-               }\r
-       }\r
-\r
-       function mergeSiblings( element )\r
-       {\r
-               if ( !element || element.type != CKEDITOR.NODE_ELEMENT || !CKEDITOR.dtd.$removeEmpty[ element.getName() ] )\r
-                       return;\r
-\r
-               mergeElements( element, element.getNext(), true );\r
-               mergeElements( element, element.getPrevious() );\r
-       }\r
-\r
-       function mergeElements( element, sibling, isNext )\r
-       {\r
-               if ( sibling && sibling.type == CKEDITOR.NODE_ELEMENT )\r
-               {\r
-                       var hasBookmark = sibling.getAttribute( '_fck_bookmark' );\r
-\r
-                       if ( hasBookmark )\r
-                               sibling = isNext ? sibling.getNext() : sibling.getPrevious();\r
-\r
-                       if ( sibling && sibling.type == CKEDITOR.NODE_ELEMENT && 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
-                               if ( hasBookmark )\r
-                                       ( isNext ? sibling.getPrevious() : sibling.getNext() ).move( element, !isNext );\r
+                               firstChild.type == CKEDITOR.NODE_ELEMENT && firstChild.mergeSiblings();\r
 \r
-                               sibling.moveChildren( element, !isNext );\r
-                               sibling.remove();\r
-\r
-                               // Now check the last inner child (see two comments above).\r
-                               if ( innerSibling )\r
-                                       mergeSiblings( innerSibling );\r
+                               if ( lastChild && !firstChild.equals( lastChild )\r
+                                       && lastChild.type == CKEDITOR.NODE_ELEMENT  )\r
+                                       lastChild.mergeSiblings();\r
                        }\r
                }\r
        }\r
 \r
-       function getElement( style, targetDocument )\r
+       function getElement( style, targetDocument, element )\r
        {\r
                var el;\r
 \r
@@ -1039,6 +1132,10 @@ CKEDITOR.STYLE_OBJECT = 3;
                // Create the element.\r
                el = new CKEDITOR.dom.element( elementName, targetDocument );\r
 \r
+               // #6226: attributes should be copied before the new ones are applied\r
+               if ( element )\r
+                       element.copyAttributes( el );\r
+\r
                return setupElement( el, style );\r
        }\r
 \r
@@ -1058,7 +1155,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                }\r
 \r
                // Assign all defined styles.\r
-               if ( styles )\r
+               if( styles )\r
                        el.setAttribute( 'style', styles );\r
 \r
                return el;\r
@@ -1126,7 +1223,7 @@ CKEDITOR.STYLE_OBJECT = 3;
         */\r
        function getOverrides( style )\r
        {\r
-               if( style._.overrides )\r
+               if ( style._.overrides )\r
                        return style._.overrides;\r
 \r
                var overrides = ( style._.overrides = {} ),\r
@@ -1182,6 +1279,15 @@ CKEDITOR.STYLE_OBJECT = 3;
                return overrides;\r
        }\r
 \r
+       // Make the comparison of attribute value easier by standardizing it.\r
+       function normalizeProperty( name, value, isStyle )\r
+       {\r
+               var temp = new CKEDITOR.dom.element( 'span' );\r
+               temp [ isStyle ? 'setStyle' : 'setAttribute' ]( name, value );\r
+               return temp[ isStyle ? 'getStyle' : 'getAttribute' ]( name );\r
+       }\r
+\r
+       // Make the comparison of style text easier by standardizing it.\r
        function normalizeCssText( unparsedCssText, nativeNormalize )\r
        {\r
                var styleText;\r
@@ -1191,7 +1297,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                        // retrieving its final format.\r
                        var temp = new CKEDITOR.dom.element( 'span' );\r
                        temp.setAttribute( 'style', unparsedCssText );\r
-                       styleText = temp.getAttribute( 'style' );\r
+                       styleText = temp.getAttribute( 'style' ) || '';\r
                }\r
                else\r
                        styleText = unparsedCssText;\r
@@ -1200,23 +1306,70 @@ CKEDITOR.STYLE_OBJECT = 3;
                // Compensate tail semi-colon.\r
                return styleText.replace( /\s*([;:])\s*/, '$1' )\r
                                                         .replace( /([^\s;])$/, '$1;')\r
-                                                        .replace( /,\s+/g, ',' ) // Trimming spaces after comma (e.g. font-family name)(#4107).\r
+                                                       // Trimming spaces after comma(#4107),\r
+                                                       // remove quotations(#6403),\r
+                                                       // mostly for differences on "font-family".\r
+                                                        .replace( /,\s+/g, ',' )\r
+                                                        .replace( /\"/g,'' )\r
                                                         .toLowerCase();\r
        }\r
 \r
-       function applyStyle( document, remove )\r
+       // Turn inline style text properties into one hash.\r
+       function parseStyleText( styleText )\r
        {\r
-               // Get all ranges from the selection.\r
-               var selection = document.getSelection();\r
-               var ranges = selection.getRanges();\r
-               var func = remove ? this.removeFromRange : this.applyToRange;\r
+               var retval = {};\r
+               styleText\r
+                  .replace( /&quot;/g, '"' )\r
+                  .replace( /\s*([^ :;]+)\s*:\s*([^;]+)\s*(?=;|$)/g, function( match, name, value )\r
+               {\r
+                       retval[ name ] = value;\r
+               } );\r
+               return retval;\r
+       }\r
 \r
-               // Apply the style to the ranges.\r
-               for ( var i = 0 ; i < ranges.length ; i++ )\r
-                       func.call( this, ranges[ i ] );\r
+       /**\r
+        * Compare two bunch of styles, with the speciality that value 'inherit'\r
+        * is treated as a wildcard which will match any value.\r
+        * @param {Object|String} source\r
+        * @param {Object|String} target\r
+        */\r
+       function compareCssText( source, target )\r
+       {\r
+               typeof source == 'string' && ( source = parseStyleText( source ) );\r
+               typeof target == 'string' && ( target = parseStyleText( target ) );\r
+               for( var name in source )\r
+               {\r
+                       if ( !( name in target &&\r
+                                       ( target[ name ] == source[ name ]\r
+                                               || source[ name ] == 'inherit'\r
+                                               || target[ name ] == 'inherit' ) ) )\r
+                       {\r
+                               return false;\r
+                       }\r
+               }\r
+               return true;\r
+       }\r
 \r
-               // Select the ranges again.\r
-               selection.selectRanges( ranges );\r
+       function applyStyle( document, remove )\r
+       {\r
+               var selection = document.getSelection(),\r
+                       // Bookmark the range so we can re-select it after processing.\r
+                       bookmarks = selection.createBookmarks( 1 ),\r
+                       ranges = selection.getRanges( 1 ),\r
+                       func = remove ? this.removeFromRange : this.applyToRange,\r
+                       range;\r
+\r
+               var iterator = ranges.createIterator();\r
+               while ( ( range = iterator.getNextRange() ) )\r
+                       func.call( this, range );\r
+\r
+               if ( bookmarks.length == 1 && bookmarks[0].collapsed )\r
+               {\r
+                       selection.selectRanges( ranges );\r
+                       document.getById( bookmarks[ 0 ].startNode ).remove();\r
+               }\r
+               else\r
+                       selection.selectBookmarks( bookmarks );\r
        }\r
 })();\r
 \r
@@ -1241,3 +1394,86 @@ CKEDITOR.styleCommand.prototype.exec = function( editor )
 \r
        return !!doc;\r
 };\r
+\r
+CKEDITOR.stylesSet = new CKEDITOR.resourceManager( '', 'stylesSet' );\r
+\r
+// Backward compatibility (#5025).\r
+CKEDITOR.addStylesSet = CKEDITOR.tools.bind( CKEDITOR.stylesSet.add, CKEDITOR.stylesSet );\r
+CKEDITOR.loadStylesSet = function( name, url, callback )\r
+       {\r
+               CKEDITOR.stylesSet.addExternal( name, url, '' );\r
+               CKEDITOR.stylesSet.load( name, callback );\r
+       };\r
+\r
+\r
+/**\r
+ * Gets the current styleSet for this instance\r
+ * @param {Function} callback The function to be called with the styles data.\r
+ * @example\r
+ * editor.getStylesSet( function( stylesDefinitions ) {} );\r
+ */\r
+CKEDITOR.editor.prototype.getStylesSet = function( callback )\r
+{\r
+       if ( !this._.stylesDefinitions )\r
+       {\r
+               var editor = this,\r
+                       // Respect the backwards compatible definition entry\r
+                       configStyleSet = editor.config.stylesCombo_stylesSet || editor.config.stylesSet || 'default';\r
+\r
+               // #5352 Allow to define the styles directly in the config object\r
+               if ( configStyleSet instanceof Array )\r
+               {\r
+                       editor._.stylesDefinitions = configStyleSet;\r
+                       callback( configStyleSet );\r
+                       return;\r
+               }\r
+\r
+               var     partsStylesSet = configStyleSet.split( ':' ),\r
+                       styleSetName = partsStylesSet[ 0 ],\r
+                       externalPath = partsStylesSet[ 1 ],\r
+                       pluginPath = CKEDITOR.plugins.registered.styles.path;\r
+\r
+               CKEDITOR.stylesSet.addExternal( styleSetName,\r
+                               externalPath ?\r
+                                       partsStylesSet.slice( 1 ).join( ':' ) :\r
+                                       pluginPath + 'styles/' + styleSetName + '.js', '' );\r
+\r
+               CKEDITOR.stylesSet.load( styleSetName, function( stylesSet )\r
+                       {\r
+                               editor._.stylesDefinitions = stylesSet[ styleSetName ];\r
+                               callback( editor._.stylesDefinitions );\r
+                       } ) ;\r
+       }\r
+       else\r
+               callback( this._.stylesDefinitions );\r
+};\r
+\r
+/**\r
+ * The "styles definition set" to use in the editor. They will be used in the\r
+ * styles combo and the Style selector of the div container. <br>\r
+ * The styles may be defined in the page containing the editor, or can be\r
+ * loaded on demand from an external file. In the second case, if this setting\r
+ * contains only a name, the styles definition file will be loaded from the\r
+ * "styles" folder inside the styles plugin folder.\r
+ * Otherwise, this setting has the "name:url" syntax, making it\r
+ * possible to set the URL from which loading the styles file.<br>\r
+ * Previously this setting was available as config.stylesCombo_stylesSet<br>\r
+ * @name CKEDITOR.config.stylesSet\r
+ * @type String|Array\r
+ * @default 'default'\r
+ * @since 3.3\r
+ * @example\r
+ * // Load from the styles' styles folder (mystyles.js file).\r
+ * config.stylesSet = 'mystyles';\r
+ * @example\r
+ * // Load from a relative URL.\r
+ * config.stylesSet = 'mystyles:/editorstyles/styles.js';\r
+ * @example\r
+ * // Load from a full URL.\r
+ * config.stylesSet = 'mystyles:http://www.example.com/editorstyles/styles.js';\r
+ * @example\r
+ * // Load from a list of definitions.\r
+ * config.stylesSet = [\r
+ *  { name : 'Strong Emphasis', element : 'strong' },\r
+ * { name : 'Emphasis', element : 'em' }, ... ];\r
+ */\r