JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.1
[ckeditor.git] / _source / plugins / styles / plugin.js
index 37126f1..0850a66 100644 (file)
@@ -1,19 +1,29 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
 CKEDITOR.plugins.add( 'styles',\r
 {\r
-       requires : [ 'selection' ]\r
+       requires : [ 'selection' ],\r
+       init : function( editor )\r
+       {\r
+               // This doesn't look like correct, but it's the safest way to proper\r
+               // pass the disableReadonlyStyling configuration to the style system\r
+               // without having to change any method signature in the API. (#6103)\r
+               editor.on( 'contentDom', function()\r
+                       {\r
+                               editor.document.setCustomData( 'cke_includeReadonly', !editor.config.disableReadonlyStyling );\r
+                       });\r
+       }\r
 });\r
 \r
 /**\r
- * Registers a function to be called whenever a style changes its state in the\r
+ * Registers a function to be called whenever the selection position changes 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} style The style to be watched.\r
- * @param {Function} callback The function to be called when the style state changes.\r
+ * @param {Function} callback The function to be called.\r
  * @example\r
  * // Create a style object for the <b> element.\r
  * var style = new CKEDITOR.style( { element : 'b' } );\r
@@ -51,17 +61,9 @@ CKEDITOR.editor.prototype.attachStyleStateChange = function( style, callback )
                                        // callback.\r
                                        var currentState = callback.style.checkActive( ev.data.path ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF;\r
 \r
-                                       // If the state changed since the last check.\r
-                                       if ( callback.state !== currentState )\r
-                                       {\r
-                                               // Call the callback function, passing the current\r
-                                               // state to it.\r
-                                               callback.fn.call( this, currentState );\r
-\r
-                                               // Save the current state, so it can be compared next\r
-                                               // time.\r
-                                               callback.state = currentState;\r
-                                       }\r
+                                       // Call the callback function, passing the current\r
+                                       // state to it.\r
+                                       callback.fn.call( this, currentState );\r
                                }\r
                        });\r
        }\r
@@ -77,10 +79,14 @@ CKEDITOR.STYLE_OBJECT = 3;
 \r
 (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,th:1,ul:1,dl:1,dt:1,dd:1,form:1};\r
+       var blockElements       = { address:1,div:1,h1:1,h2:1,h3:1,h4:1,h5:1,h6:1,p:1,pre:1,section:1,header:1,footer:1,nav:1,article:1,aside:1,figure:1,dialog:1,hgroup:1,time:1,meter:1,menu:1,command:1,keygen:1,output:1,progress:1,details:1,datagrid:1,datalist:1 },\r
+               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,audio:1,video:1 };\r
+\r
+       var semicolonFixRegex = /\s*(?:;\s*|$)/,\r
+               varRegex = /#\((.+?)\)/g;\r
 \r
-       var semicolonFixRegex = /\s*(?:;\s*|$)/;\r
+       var notBookmark = CKEDITOR.dom.walker.bookmark( 0, 1 ),\r
+               nonWhitespaces = CKEDITOR.dom.walker.whitespaces( 1 );\r
 \r
        CKEDITOR.style = function( styleDefinition, variablesValues )\r
        {\r
@@ -92,16 +98,22 @@ CKEDITOR.STYLE_OBJECT = 3;
                        replaceVariables( styleDefinition.styles, variablesValues );\r
                }\r
 \r
-               var element = this.element = ( styleDefinition.element || '*' ).toLowerCase();\r
+               var element = this.element = styleDefinition.element ?\r
+                               ( typeof styleDefinition.element == 'string' ? styleDefinition.element.toLowerCase() : styleDefinition.element )\r
+                               : '*';\r
 \r
                this.type =\r
-                       ( element == '#' || blockElements[ element ] ) ?\r
+                       blockElements[ element ] ?\r
                                CKEDITOR.STYLE_BLOCK\r
                        : objectElements[ element ] ?\r
                                CKEDITOR.STYLE_OBJECT\r
                        :\r
                                CKEDITOR.STYLE_INLINE;\r
 \r
+               // If the 'element' property is an object with a set of possible element, it will be applied like an object style: only to existing elements\r
+               if ( typeof this.element == 'object' )\r
+                       this.type = CKEDITOR.STYLE_OBJECT;\r
+\r
                this._ =\r
                {\r
                        definition : styleDefinition\r
@@ -137,6 +149,8 @@ CKEDITOR.STYLE_OBJECT = 3;
                        return ( this.removeFromRange =\r
                                                this.type == CKEDITOR.STYLE_INLINE ?\r
                                                        removeInlineStyle\r
+                                               : this.type == CKEDITOR.STYLE_BLOCK ?\r
+                                                       removeBlockStyle\r
                                                : this.type == CKEDITOR.STYLE_OBJECT ?\r
                                                        removeObjectStyle\r
                                                : null ).call( this, range );\r
@@ -171,9 +185,12 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                          && ( element == elementPath.block || element == elementPath.blockLimit ) )\r
                                                        continue;\r
 \r
-                                               if( this.type == CKEDITOR.STYLE_OBJECT\r
-                                                        && !( element.getName() in objectElements ) )\r
+                                               if( this.type == CKEDITOR.STYLE_OBJECT )\r
+                                               {\r
+                                                       var name = element.getName();\r
+                                                       if ( !( typeof this.element == 'string' ? name == this.element : name in this.element ) )\r
                                                                continue;\r
+                                               }\r
 \r
                                                if ( this.checkElementRemovable( element, true ) )\r
                                                        return true;\r
@@ -205,14 +222,15 @@ CKEDITOR.STYLE_OBJECT = 3;
                // current style definition.\r
                checkElementRemovable : function( element, fullMatch )\r
                {\r
-                       if ( !element )\r
+                       if ( !element || element.isReadOnly() )\r
                                return false;\r
 \r
                        var def = this._.definition,\r
-                               attribs;\r
+                               attribs,\r
+                               name = element.getName();\r
 \r
                        // If the element name is the same as the style name.\r
-                       if ( element.getName() == this.element )\r
+                       if ( typeof this.element == 'string' ? name == this.element : name in this.element )\r
                        {\r
                                // If no attributes are defined in the element.\r
                                if ( !fullMatch && !element.hasAttributes() )\r
@@ -280,7 +298,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                },\r
 \r
                // Builds the preview HTML based on the styles definition.\r
-               buildPreview : function()\r
+               buildPreview : function( label )\r
                {\r
                        var styleDefinition = this._.definition,\r
                                html = [],\r
@@ -307,7 +325,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                        if ( cssStyle )\r
                                html.push( ' style="', cssStyle, '"' );\r
 \r
-                       html.push( '>', styleDefinition.name, '</', elementName, '>' );\r
+                       html.push( '>', ( label || styleDefinition.name ), '</', elementName, '>' );\r
 \r
                        return html.join( '' );\r
                }\r
@@ -353,6 +371,35 @@ CKEDITOR.STYLE_OBJECT = 3;
                return ( styleDefinition._ST = stylesText );\r
        };\r
 \r
+       // Gets the parent element which blocks the styling for an element. This\r
+       // can be done through read-only elements (contenteditable=false) or\r
+       // elements with the "data-nostyle" attribute.\r
+       function getUnstylableParent( element )\r
+       {\r
+               var unstylable,\r
+                       editable;\r
+\r
+               while ( ( element = element.getParent() ) )\r
+               {\r
+                       if ( element.getName() == 'body' )\r
+                               break;\r
+\r
+                       if ( element.getAttribute( 'data-nostyle' ) )\r
+                               unstylable = element;\r
+                       else if ( !editable )\r
+                       {\r
+                               var contentEditable = element.getAttribute( 'contentEditable' );\r
+\r
+                               if ( contentEditable == 'false' )\r
+                                       unstylable = element;\r
+                               else if ( contentEditable == 'true' )\r
+                                       editable = 1;\r
+                       }\r
+               }\r
+\r
+               return unstylable;\r
+       }\r
+\r
        function applyInlineStyle( range )\r
        {\r
                var document = range.document;\r
@@ -375,11 +422,19 @@ CKEDITOR.STYLE_OBJECT = 3;
                var def = this._.definition;\r
                var isUnknownElement;\r
 \r
+               // Indicates that fully selected read-only elements are to be included in the styling range.\r
+               var includeReadonly = def.includeReadonly;\r
+\r
+               // If the read-only inclusion is not available in the definition, try\r
+               // to get it from the document data.\r
+               if ( includeReadonly == undefined )\r
+                       includeReadonly = document.getCustomData( 'cke_includeReadonly' );\r
+\r
                // Get the DTD definition for the element. Defaults to "span".\r
                var dtd = CKEDITOR.dtd[ elementName ] || ( isUnknownElement = true, CKEDITOR.dtd.span );\r
 \r
                // Expand the range.\r
-               range.enlarge( CKEDITOR.ENLARGE_ELEMENT );\r
+               range.enlarge( CKEDITOR.ENLARGE_ELEMENT, 1 );\r
                range.trim();\r
 \r
                // Get the first node to be processed and the last, which concludes the\r
@@ -392,6 +447,24 @@ CKEDITOR.STYLE_OBJECT = 3;
 \r
                var styleRange;\r
 \r
+               // Check if the boundaries are inside non stylable elements.\r
+               var firstUnstylable = getUnstylableParent( firstNode ),\r
+                       lastUnstylable = getUnstylableParent( lastNode );\r
+\r
+               // If the first element can't be styled, we'll start processing right\r
+               // after its unstylable root.\r
+               if ( firstUnstylable )\r
+                       currentNode = firstUnstylable.getNextSourceNode( true );\r
+\r
+               // If the last element can't be styled, we'll stop processing on its\r
+               // unstylable root.\r
+               if ( lastUnstylable )\r
+                       lastNode = lastUnstylable;\r
+\r
+               // Do nothing if the current node now follows the last node to be processed.\r
+               if ( currentNode.getPosition( lastNode ) == CKEDITOR.POSITION_FOLLOWING )\r
+                       currentNode = 0;\r
+\r
                while ( currentNode )\r
                {\r
                        var applyStyle = false;\r
@@ -405,8 +478,10 @@ CKEDITOR.STYLE_OBJECT = 3;
                        {\r
                                var nodeType = currentNode.type;\r
                                var nodeName = nodeType == CKEDITOR.NODE_ELEMENT ? currentNode.getName() : null;\r
+                               var nodeIsReadonly = nodeName && ( currentNode.getAttribute( 'contentEditable' ) == 'false' );\r
+                               var nodeIsNoStyle = nodeName && currentNode.getAttribute( 'data-nostyle' );\r
 \r
-                               if ( nodeName && currentNode.getAttribute( '_cke_bookmark' ) )\r
+                               if ( nodeName && currentNode.data( 'cke-bookmark' ) )\r
                                {\r
                                        currentNode = currentNode.getNextSourceNode( true );\r
                                        continue;\r
@@ -414,6 +489,8 @@ CKEDITOR.STYLE_OBJECT = 3;
 \r
                                // Check if the current node can be a child of the style element.\r
                                if ( !nodeName || ( dtd[ nodeName ]\r
+                                       && !nodeIsNoStyle\r
+                                       && ( !nodeIsReadonly || includeReadonly )\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
@@ -435,9 +512,9 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                        styleRange.setStartBefore( currentNode );\r
                                                }\r
 \r
-                                               // Non element nodes, or empty elements can be added\r
-                                               // completely to the range.\r
-                                               if ( nodeType == CKEDITOR.NODE_TEXT || ( nodeType == CKEDITOR.NODE_ELEMENT && !currentNode.getChildCount() ) )\r
+                                               // Non element nodes, readonly elements, or empty\r
+                                               // elements can be added completely to the range.\r
+                                               if ( nodeType == CKEDITOR.NODE_TEXT || nodeIsReadonly || ( nodeType == CKEDITOR.NODE_ELEMENT && !currentNode.getChildCount() ) )\r
                                                {\r
                                                        var includedNode = currentNode;\r
                                                        var parentNode;\r
@@ -445,8 +522,8 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                        // This node is about to be included completelly, but,\r
                                                        // if this is the last node in its parent, we must also\r
                                                        // check if the parent itself can be added completelly\r
-                                                       // to the range.\r
-                                                       while ( !includedNode.$.nextSibling\r
+                                                       // to the range, otherwise apply the style immediately.\r
+                                                       while ( ( applyStyle = !includedNode.getNext( notBookmark ) )\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
                                                                && ( !def.childRule || def.childRule( parentNode ) ) )\r
@@ -456,12 +533,6 @@ CKEDITOR.STYLE_OBJECT = 3;
 \r
                                                        styleRange.setEndAfter( includedNode );\r
 \r
-                                                       // If the included node still is the last node in its\r
-                                                       // parent, it means that the parent can't be included\r
-                                                       // in this style DTD, so apply the style immediately.\r
-                                                       if ( !includedNode.$.nextSibling )\r
-                                                               applyStyle = true;\r
-\r
                                                }\r
                                        }\r
                                        else\r
@@ -471,7 +542,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                                        applyStyle = true;\r
 \r
                                // Get the next node to be processed.\r
-                               currentNode = currentNode.getNextSourceNode();\r
+                               currentNode = currentNode.getNextSourceNode( nodeIsNoStyle || nodeIsReadonly );\r
                        }\r
 \r
                        // Apply the style if we have something to which apply it.\r
@@ -590,7 +661,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                 * Make sure our range has included all "collpased" parent inline nodes so\r
                 * that our operation logic can be simpler.\r
                 */\r
-               range.enlarge( CKEDITOR.ENLARGE_ELEMENT );\r
+               range.enlarge( CKEDITOR.ENLARGE_ELEMENT, 1 );\r
 \r
                var bookmark = range.createBookmark(),\r
                        startNode = bookmark.startNode;\r
@@ -636,8 +707,10 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                 * them before removal.\r
                                                 */\r
                                                element.mergeSiblings();\r
-                                               removeFromElement( this, element );\r
-\r
+                                               if ( element.getName() == this.element )\r
+                                                       removeFromElement( this, element );\r
+                                               else\r
+                                                       removeOverrides( element, getOverrides( this )[ element.getName() ] );\r
                                        }\r
                                }\r
                        }\r
@@ -746,26 +819,26 @@ CKEDITOR.STYLE_OBJECT = 3;
                }\r
 \r
                range.moveToBookmark( bookmark );\r
-}\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
+                       element = root.getAscendant( this.element, true );\r
+               element && !element.isReadOnly() && 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
+                       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 style = this,\r
+                       def = style._.definition,\r
+                       attributes = def.attributes;\r
                var styles = CKEDITOR.style.getStyleText( def );\r
 \r
                // Remove all defined attributes.\r
@@ -809,8 +882,44 @@ CKEDITOR.STYLE_OBJECT = 3;
 \r
                while ( ( block = iterator.getNextParagraph() ) )               // Only one =\r
                {\r
-                       var newBlock = getElement( this, doc, block );\r
-                       replaceBlock( block, newBlock );\r
+                       if ( !block.isReadOnly() )\r
+                       {\r
+                               var newBlock = getElement( this, doc, block );\r
+                               replaceBlock( block, newBlock );\r
+                       }\r
+               }\r
+\r
+               range.moveToBookmark( bookmark );\r
+       }\r
+\r
+       function removeBlockStyle( range )\r
+       {\r
+               // Serializible bookmarks is needed here since\r
+               // elements may be merged.\r
+               var bookmark = range.createBookmark( 1 );\r
+\r
+               var iterator = range.createIterator();\r
+               iterator.enforceRealBlocks = true;\r
+               iterator.enlargeBr = this._.enterMode != CKEDITOR.ENTER_BR;\r
+\r
+               var block;\r
+               while ( ( block = iterator.getNextParagraph() ) )\r
+               {\r
+                       if ( this.checkElementRemovable( block ) )\r
+                       {\r
+                               // <pre> get special treatment.\r
+                               if ( block.is( 'pre' ) )\r
+                               {\r
+                                       var newBlock = this._.enterMode == CKEDITOR.ENTER_BR ?\r
+                                                               null : range.document.createElement(\r
+                                                                       this._.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' );\r
+\r
+                                       newBlock && block.copyAttributes( newBlock );\r
+                                       replaceBlock( block, newBlock );\r
+                               }\r
+                               else\r
+                                        removeFromElement( this, block, 1 );\r
+                       }\r
                }\r
 \r
                range.moveToBookmark( bookmark );\r
@@ -821,8 +930,17 @@ CKEDITOR.STYLE_OBJECT = 3;
        // when necessary.(#3188)\r
        function replaceBlock( block, newBlock )\r
        {\r
-               var newBlockIsPre       = newBlock.is( 'pre' );\r
-               var blockIsPre          = block.is( 'pre' );\r
+               // Block is to be removed, create a temp element to\r
+               // save contents.\r
+               var removeBlock = !newBlock;\r
+               if ( removeBlock )\r
+               {\r
+                       newBlock = block.getDocument().createElement( 'div' );\r
+                       block.copyAttributes( newBlock );\r
+               }\r
+\r
+               var newBlockIsPre       = newBlock && newBlock.is( 'pre' );\r
+               var blockIsPre  = block.is( 'pre' );\r
 \r
                var isToPre     = newBlockIsPre && !blockIsPre;\r
                var isFromPre   = !newBlockIsPre && blockIsPre;\r
@@ -831,7 +949,8 @@ CKEDITOR.STYLE_OBJECT = 3;
                        newBlock = toPre( block, newBlock );\r
                else if ( isFromPre )\r
                        // Split big <pre> into pieces before start to convert.\r
-                       newBlock = fromPres( splitIntoPres( block ), newBlock );\r
+                       newBlock = fromPres( removeBlock ?\r
+                                               [ block.getHtml() ] : splitIntoPres( block ), newBlock );\r
                else\r
                        block.moveChildren( newBlock );\r
 \r
@@ -842,9 +961,10 @@ CKEDITOR.STYLE_OBJECT = 3;
                        // Merge previous <pre> blocks.\r
                        mergePre( newBlock );\r
                }\r
+               else if ( removeBlock )\r
+                       removeNoAttribsElement( newBlock );\r
        }\r
 \r
-       var nonWhitespaces = CKEDITOR.dom.walker.whitespaces( true );\r
        /**\r
         * Merge a <pre> block with a previous sibling if available.\r
         */\r
@@ -883,7 +1003,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[^>]+_cke_bookmark.*?\/span>))*\n(?!$)/gi,\r
+               var duoBrRegex = /(\S\s*)\n(?:\s|(<span[^>]+data-cke-bookmark.*?\/span>))*\n(?!$)/gi,\r
                        blockName = preBlock.getName(),\r
                        splitedHtml = replace( preBlock.getOuterHtml(),\r
                                duoBrRegex,\r
@@ -905,7 +1025,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                var headBookmark = '',\r
                        tailBookmark = '';\r
 \r
-               str = str.replace( /(^<span[^>]+_cke_bookmark.*?\/span>)|(<span[^>]+_cke_bookmark.*?\/span>$)/gi,\r
+               str = str.replace( /(^<span[^>]+data-cke-bookmark.*?\/span>)|(<span[^>]+data-cke-bookmark.*?\/span>$)/gi,\r
                        function( str, m1, m2 ){\r
                                        m1 && ( headBookmark = m1 );\r
                                        m2 && ( tailBookmark = m2 );\r
@@ -913,12 +1033,16 @@ CKEDITOR.STYLE_OBJECT = 3;
                        } );\r
                return headBookmark + str.replace( regexp, replacement ) + tailBookmark;\r
        }\r
+\r
        /**\r
         * Converting a list of <pre> into blocks with format well preserved.\r
         */\r
        function fromPres( preHtmls, newBlock )\r
        {\r
-               var docFrag = new CKEDITOR.dom.documentFragment( newBlock.getDocument() );\r
+               var docFrag;\r
+               if ( preHtmls.length > 1 )\r
+                       docFrag = new CKEDITOR.dom.documentFragment( newBlock.getDocument() );\r
+\r
                for ( var i = 0 ; i < preHtmls.length ; i++ )\r
                {\r
                        var blockHtml = preHtmls[ i ];\r
@@ -948,11 +1072,17 @@ CKEDITOR.STYLE_OBJECT = 3;
                                                return CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 ) + ' ' ;\r
                                        } ) ;\r
 \r
-                       var newBlockClone = newBlock.clone();\r
-                       newBlockClone.setHtml(  blockHtml );\r
-                       docFrag.append( newBlockClone );\r
+                       if ( docFrag )\r
+                       {\r
+                               var newBlockClone = newBlock.clone();\r
+                               newBlockClone.setHtml(  blockHtml );\r
+                               docFrag.append( newBlockClone );\r
+                       }\r
+                       else\r
+                               newBlock.setHtml( blockHtml );\r
                }\r
-               return docFrag;\r
+\r
+               return docFrag || newBlock;\r
        }\r
 \r
        /**\r
@@ -960,6 +1090,9 @@ CKEDITOR.STYLE_OBJECT = 3;
         */\r
        function toPre( block, newBlock )\r
        {\r
+               var bogus = block.getBogus();\r
+               bogus && bogus.remove();\r
+\r
                // First trim the block content.\r
                var preHtml = block.getHtml();\r
 \r
@@ -982,6 +1115,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                        var temp = block.getDocument().createElement( 'div' );\r
                        temp.append( newBlock );\r
                        newBlock.$.outerHTML =  '<pre>' + preHtml + '</pre>';\r
+                       newBlock.copyAttributes( temp.getFirst() );\r
                        newBlock = temp.getFirst().remove();\r
                }\r
                else\r
@@ -1021,7 +1155,12 @@ CKEDITOR.STYLE_OBJECT = 3;
                        element.removeStyle( styleName );\r
                }\r
 \r
-               removeEmpty && removeNoAttribsElement( element );\r
+               if ( removeEmpty )\r
+               {\r
+                       !CKEDITOR.dtd.$block[ element.getName() ] || style._.enterMode == CKEDITOR.ENTER_BR && !element.hasAttributes() ?\r
+                               removeNoAttribsElement( element ) :\r
+                               element.renameNode( style._.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' );\r
+               }\r
        }\r
 \r
        // Removes a style from inside an element.\r
@@ -1030,9 +1169,8 @@ CKEDITOR.STYLE_OBJECT = 3;
                var def = style._.definition,\r
                        attribs = def.attributes,\r
                        styles = def.styles,\r
-                       overrides = getOverrides( style );\r
-\r
-               var innerElements = element.getElementsByTag( style.element );\r
+                       overrides = getOverrides( style ),\r
+                       innerElements = element.getElementsByTag( style.element );\r
 \r
                for ( var i = innerElements.count(); --i >= 0 ; )\r
                        removeFromElement( style,  innerElements.getItem( i ) );\r
@@ -1051,7 +1189,6 @@ CKEDITOR.STYLE_OBJECT = 3;
                                }\r
                        }\r
                }\r
-\r
        }\r
 \r
        /**\r
@@ -1098,32 +1235,46 @@ CKEDITOR.STYLE_OBJECT = 3;
                // leaving its children.\r
                if ( !element.hasAttributes() )\r
                {\r
-                       // Removing elements may open points where merging is possible,\r
-                       // so let's cache the first and last nodes for later checking.\r
-                       var firstChild  = element.getFirst();\r
-                       var lastChild   = element.getLast();\r
+                       if ( CKEDITOR.dtd.$block[ element.getName() ] )\r
+                       {\r
+                               var previous = element.getPrevious( nonWhitespaces ),\r
+                                               next = element.getNext( nonWhitespaces );\r
 \r
-                       element.remove( true );\r
+                               if ( previous && ( previous.type == CKEDITOR.NODE_TEXT || !previous.isBlockBoundary( { br : 1 } ) ) )\r
+                                       element.append( 'br', 1 );\r
+                               if ( next && ( next.type == CKEDITOR.NODE_TEXT || !next.isBlockBoundary( { br : 1 } ) ) )\r
+                                       element.append( 'br' );\r
 \r
-                       if ( firstChild )\r
+                               element.remove( true );\r
+                       }\r
+                       else\r
                        {\r
-                               // Check the cached nodes for merging.\r
-                               firstChild.type == CKEDITOR.NODE_ELEMENT && firstChild.mergeSiblings();\r
+                               // Removing elements may open points where merging is possible,\r
+                               // so let's cache the first and last nodes for later checking.\r
+                               var firstChild = element.getFirst();\r
+                               var lastChild = element.getLast();\r
+\r
+                               element.remove( true );\r
+\r
+                               if ( firstChild )\r
+                               {\r
+                                       // Check the cached nodes for merging.\r
+                                       firstChild.type == CKEDITOR.NODE_ELEMENT && firstChild.mergeSiblings();\r
+\r
+                                       if ( lastChild && !firstChild.equals( lastChild )\r
+                                                       && lastChild.type == CKEDITOR.NODE_ELEMENT )\r
+                                               lastChild.mergeSiblings();\r
+                               }\r
 \r
-                               if ( lastChild && !firstChild.equals( lastChild )\r
-                                       && lastChild.type == CKEDITOR.NODE_ELEMENT  )\r
-                                       lastChild.mergeSiblings();\r
                        }\r
                }\r
        }\r
 \r
        function getElement( style, targetDocument, element )\r
        {\r
-               var el;\r
-\r
-               var def = style._.definition;\r
-\r
-               var elementName = style.element;\r
+               var el,\r
+                       def = style._.definition,\r
+                       elementName = style.element;\r
 \r
                // The "*" element name will always be a span for this function.\r
                if ( elementName == '*' )\r
@@ -1136,14 +1287,22 @@ CKEDITOR.STYLE_OBJECT = 3;
                if ( element )\r
                        element.copyAttributes( el );\r
 \r
-               return setupElement( el, style );\r
+               el = setupElement( el, style );\r
+\r
+               // Avoid ID duplication.\r
+               if ( targetDocument.getCustomData( 'doc_processing_style' ) && el.hasAttribute( 'id' ) )\r
+                       el.removeAttribute( 'id' );\r
+               else\r
+                       targetDocument.setCustomData( 'doc_processing_style', 1 );\r
+\r
+               return el;\r
        }\r
 \r
        function setupElement( el, style )\r
        {\r
-               var def = style._.definition;\r
-               var attributes = def.attributes;\r
-               var styles = CKEDITOR.style.getStyleText( def );\r
+               var def = style._.definition,\r
+                       attributes = def.attributes,\r
+                       styles = CKEDITOR.style.getStyleText( def );\r
 \r
                // Assign all defined attributes.\r
                if ( attributes )\r
@@ -1161,7 +1320,6 @@ CKEDITOR.STYLE_OBJECT = 3;
                return el;\r
        }\r
 \r
-       var varRegex = /#\((.+?)\)/g;\r
        function replaceVariables( list, variablesValues )\r
        {\r
                for ( var item in list )\r
@@ -1173,7 +1331,6 @@ CKEDITOR.STYLE_OBJECT = 3;
                }\r
        }\r
 \r
-\r
        // Returns an object that can be used for style matching comparison.\r
        // Attributes names and values are all lowercased, and the styles get\r
        // merged with the style attribute.\r
@@ -1302,6 +1459,16 @@ CKEDITOR.STYLE_OBJECT = 3;
                else\r
                        styleText = unparsedCssText;\r
 \r
+               // Normalize font-family property, ignore quotes and being case insensitive. (#7322)\r
+               // http://www.w3.org/TR/css3-fonts/#font-family-the-font-family-property\r
+               styleText = styleText.replace( /(font-family:)(.*?)(?=;|$)/, function ( match, prop, val )\r
+               {\r
+                       var names = val.split( ',' );\r
+                       for ( var i = 0; i < names.length; i++ )\r
+                               names[ i ] = CKEDITOR.tools.trim( names[ i ].replace( /["']/g, '' ) );\r
+                       return prop + names.join( ',' );\r
+               });\r
+\r
                // Shrinking white-spaces around colon and semi-colon (#4147).\r
                // Compensate tail semi-colon.\r
                return styleText.replace( /\s*([;:])\s*/, '$1' )\r
@@ -1355,7 +1522,7 @@ CKEDITOR.STYLE_OBJECT = 3;
                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
+                       ranges = selection.getRanges(),\r
                        func = remove ? this.removeFromRange : this.applyToRange,\r
                        range;\r
 \r
@@ -1363,13 +1530,15 @@ CKEDITOR.STYLE_OBJECT = 3;
                while ( ( range = iterator.getNextRange() ) )\r
                        func.call( this, range );\r
 \r
-               if ( bookmarks.length == 1 && bookmarks[0].collapsed )\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
+               document.removeCustomData( 'doc_processing_style' );\r
        }\r
 })();\r
 \r
@@ -1395,6 +1564,37 @@ CKEDITOR.styleCommand.prototype.exec = function( editor )
        return !!doc;\r
 };\r
 \r
+/**\r
+ * Manages styles registration and loading. See also {@link CKEDITOR.config.stylesSet}.\r
+ * @namespace\r
+ * @augments CKEDITOR.resourceManager\r
+ * @constructor\r
+ * @since 3.2\r
+ * @example\r
+ * // The set of styles for the <b>Styles</b> combo\r
+ * CKEDITOR.stylesSet.add( 'default',\r
+ * [\r
+ *     // Block Styles\r
+ *     { name : 'Blue Title'           , element : 'h3', styles : { 'color' : 'Blue' } },\r
+ *     { name : 'Red Title'            , element : 'h3', styles : { 'color' : 'Red' } },\r
+ *\r
+ *     // Inline Styles\r
+ *     { name : 'Marker: Yellow'       , element : 'span', styles : { 'background-color' : 'Yellow' } },\r
+ *     { name : 'Marker: Green'        , element : 'span', styles : { 'background-color' : 'Lime' } },\r
+ *\r
+ *     // Object Styles\r
+ *     {\r
+ *             name : 'Image on Left',\r
+ *             element : 'img',\r
+ *             attributes :\r
+ *             {\r
+ *                     'style' : 'padding: 5px; margin-right: 5px',\r
+ *                     'border' : '2',\r
+ *                     'align' : 'left'\r
+ *             }\r
+ *     }\r
+ * ]);\r
+ */\r
 CKEDITOR.stylesSet = new CKEDITOR.resourceManager( '', 'stylesSet' );\r
 \r
 // Backward compatibility (#5025).\r
@@ -1449,6 +1649,23 @@ CKEDITOR.editor.prototype.getStylesSet = function( callback )
 };\r
 \r
 /**\r
+ * Indicates that fully selected read-only elements will be included when\r
+ * applying the style (for inline styles only).\r
+ * @name CKEDITOR.style.includeReadonly\r
+ * @type Boolean\r
+ * @default false\r
+ * @since 3.5\r
+ */\r
+\r
+ /**\r
+  * Disables inline styling on read-only elements.\r
+  * @name CKEDITOR.config.disableReadonlyStyling\r
+  * @type Boolean\r
+  * @default false\r
+  * @since 3.5\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