--- /dev/null
+/*\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
+ 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 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.\r
+ * @example\r
+ * // Create a style object for the <b> element.\r
+ * var style = new CKEDITOR.style( { element : 'b' } );\r
+ * var editor = CKEDITOR.instances.editor1;\r
+ * editor.attachStyleStateChange( style, function( state )\r
+ * {\r
+ * if ( state == CKEDITOR.TRISTATE_ON )\r
+ * alert( 'The current state for the B element is ON' );\r
+ * else\r
+ * alert( 'The current state for the B element is OFF' );\r
+ * });\r
+ */\r
+CKEDITOR.editor.prototype.attachStyleStateChange = function( style, callback )\r
+{\r
+ // Try to get the list of attached callbacks.\r
+ var styleStateChangeCallbacks = this._.styleStateChangeCallbacks;\r
+\r
+ // If it doesn't exist, it means this is the first call. So, let's create\r
+ // all the structure to manage the style checks and the callback calls.\r
+ if ( !styleStateChangeCallbacks )\r
+ {\r
+ // Create the callbacks array.\r
+ styleStateChangeCallbacks = this._.styleStateChangeCallbacks = [];\r
+\r
+ // Attach to the selectionChange event, so we can check the styles at\r
+ // that point.\r
+ this.on( 'selectionChange', function( ev )\r
+ {\r
+ // Loop throw all registered callbacks.\r
+ for ( var i = 0 ; i < styleStateChangeCallbacks.length ; i++ )\r
+ {\r
+ var callback = styleStateChangeCallbacks[ i ];\r
+\r
+ // Check the current state for the style defined for that\r
+ // callback.\r
+ var currentState = callback.style.checkActive( ev.data.path ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF;\r
+\r
+ // Call the callback function, passing the current\r
+ // state to it.\r
+ callback.fn.call( this, currentState );\r
+ }\r
+ });\r
+ }\r
+\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
+\r
+CKEDITOR.STYLE_BLOCK = 1;\r
+CKEDITOR.STYLE_INLINE = 2;\r
+CKEDITOR.STYLE_OBJECT = 3;\r
+\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
+ 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
+ varRegex = /#\((.+?)\)/g;\r
+\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
+ if ( variablesValues )\r
+ {\r
+ styleDefinition = CKEDITOR.tools.clone( styleDefinition );\r
+\r
+ replaceVariables( styleDefinition.attributes, variablesValues );\r
+ replaceVariables( styleDefinition.styles, variablesValues );\r
+ }\r
+\r
+ var element = this.element = ( styleDefinition.element || '*' ).toLowerCase();\r
+\r
+ this.type =\r
+ blockElements[ element ] ?\r
+ CKEDITOR.STYLE_BLOCK\r
+ : objectElements[ element ] ?\r
+ CKEDITOR.STYLE_OBJECT\r
+ :\r
+ CKEDITOR.STYLE_INLINE;\r
+\r
+ this._ =\r
+ {\r
+ definition : styleDefinition\r
+ };\r
+ };\r
+\r
+ CKEDITOR.style.prototype =\r
+ {\r
+ apply : function( document )\r
+ {\r
+ applyStyle.call( this, document, false );\r
+ },\r
+\r
+ remove : function( document )\r
+ {\r
+ applyStyle.call( this, document, true );\r
+ },\r
+\r
+ applyToRange : function( range )\r
+ {\r
+ return ( this.applyToRange =\r
+ this.type == CKEDITOR.STYLE_INLINE ?\r
+ 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
+ removeFromRange : function( range )\r
+ {\r
+ 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
+ },\r
+\r
+ applyToObject : function( element )\r
+ {\r
+ setupElement( element, this );\r
+ },\r
+\r
+ /**\r
+ * Get the style state inside an element path. Returns "true" if the\r
+ * element is active in the path.\r
+ */\r
+ checkActive : function( elementPath )\r
+ {\r
+ switch ( this.type )\r
+ {\r
+ 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
+\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
+ }\r
+ 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
+ {\r
+ if ( !element )\r
+ return false;\r
+\r
+ var def = this._.definition,\r
+ attribs;\r
+\r
+ // If the element name is the same as the style name.\r
+ if ( element.getName() == this.element )\r
+ {\r
+ // If no attributes are defined in the element.\r
+ if ( !fullMatch && !element.hasAttributes() )\r
+ return true;\r
+\r
+ attribs = getAttributesForComparison( def );\r
+\r
+ if ( attribs._length )\r
+ {\r
+ for ( var attName in attribs )\r
+ {\r
+ if ( attName == '_length' )\r
+ continue;\r
+\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
+ }\r
+ else if ( fullMatch )\r
+ return false;\r
+ }\r
+ if ( fullMatch )\r
+ return true;\r
+ }\r
+ else\r
+ return true;\r
+ }\r
+\r
+ // Check if the element can be somehow overriden.\r
+ var override = getOverrides( this )[ element.getName() ] ;\r
+ if ( override )\r
+ {\r
+ // If no attributes have been defined, remove the element.\r
+ if ( !( attribs = override.attributes ) )\r
+ return true;\r
+\r
+ for ( var i = 0 ; i < attribs.length ; i++ )\r
+ {\r
+ attName = attribs[i][0];\r
+ var actualAttrValue = element.getAttribute( attName );\r
+ if ( actualAttrValue )\r
+ {\r
+ var attValue = attribs[i][1];\r
+\r
+ // Remove the attribute if:\r
+ // - The override definition value is null;\r
+ // - The override definition value is a string that\r
+ // matches the attribute value exactly.\r
+ // - The override definition value is a regex that\r
+ // has matches in the attribute value.\r
+ if ( attValue === null ||\r
+ ( typeof attValue == 'string' && actualAttrValue == attValue ) ||\r
+ attValue.test( actualAttrValue ) )\r
+ return true;\r
+ }\r
+ }\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
+ // Build the cssText based on the styles definition.\r
+ CKEDITOR.style.getStyleText = function( styleDefinition )\r
+ {\r
+ // If we have already computed it, just return it.\r
+ var stylesDef = styleDefinition._ST;\r
+ if ( stylesDef )\r
+ return stylesDef;\r
+\r
+ stylesDef = styleDefinition.styles;\r
+\r
+ // Builds the StyleText.\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
+ {\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
+\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
+\r
+ if ( range.collapsed )\r
+ {\r
+ // Create the element to be inserted in the DOM.\r
+ var collapsedElement = getElement( this, document );\r
+\r
+ // Insert the empty element into the DOM at the range position.\r
+ range.insertNode( collapsedElement );\r
+\r
+ // Place the selection right inside the empty element.\r
+ range.moveToPosition( collapsedElement, CKEDITOR.POSITION_BEFORE_END );\r
+\r
+ return;\r
+ }\r
+\r
+ var elementName = this.element;\r
+ 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, 1 );\r
+ range.trim();\r
+\r
+ // Get the first node to be processed and the last, which concludes the\r
+ // processing.\r
+ var boundaryNodes = range.createBookmark(),\r
+ firstNode = boundaryNodes.startNode,\r
+ lastNode = boundaryNodes.endNode;\r
+\r
+ var currentNode = firstNode;\r
+\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
+\r
+ if ( currentNode.equals( lastNode ) )\r
+ {\r
+ currentNode = null;\r
+ applyStyle = true;\r
+ }\r
+ else\r
+ {\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.data( '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 ]\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
+ 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\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
+ // In the case of an element node, it will be included\r
+ // only if it is entirely inside the range.\r
+ if ( !styleRange && ( !nodeName || !CKEDITOR.dtd.$removeEmpty[ 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
+ {\r
+ styleRange = new CKEDITOR.dom.range( document );\r
+ styleRange.setStartBefore( currentNode );\r
+ }\r
+\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
+\r
+ // 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, 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
+ {\r
+ includedNode = parentNode;\r
+ }\r
+\r
+ styleRange.setEndAfter( includedNode );\r
+\r
+ }\r
+ }\r
+ else\r
+ applyStyle = true;\r
+ }\r
+ else\r
+ applyStyle = true;\r
+\r
+ // Get the next node to be processed.\r
+ currentNode = currentNode.getNextSourceNode( nodeIsNoStyle || nodeIsReadonly );\r
+ }\r
+\r
+ // Apply the style if we have something to which apply it.\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
+ 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 ( attName in def.attributes )\r
+ {\r
+ if ( removeList.blockedAttrs[ attName ] || !( value = parent.getAttribute( styleName ) ) )\r
+ continue;\r
+\r
+ if ( styleNode.getAttribute( attName ) == value )\r
+ removeList.attrs[ attName ] = 1;\r
+ else\r
+ removeList.blockedAttrs[ attName ] = 1;\r
+ }\r
+\r
+ for ( styleName in def.styles )\r
+ {\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
+ styleRange.extractContents().appendTo( styleNode );\r
+\r
+ // Here we do some cleanup, removing all duplicated\r
+ // elements from the style element.\r
+ removeFromInsideElement( this, styleNode );\r
+\r
+ // Insert it into the range position (it is collapsed after\r
+ // extractContents.\r
+ styleRange.insertNode( styleNode );\r
+\r
+ // Let's merge our new style with its neighbors, if possible.\r
+ styleNode.mergeSiblings();\r
+\r
+ // As the style system breaks text nodes constantly, let's normalize\r
+ // things for performance.\r
+ // With IE, some paragraphs get broken when calling normalize()\r
+ // repeatedly. Also, for IE, we must normalize body, not documentElement.\r
+ // IE is also known for having a "crash effect" with normalize().\r
+ // We should try to normalize with IE too in some way, somewhere.\r
+ 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
+ styleRange = null;\r
+ }\r
+ }\r
+\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
+ {\r
+ /*\r
+ * 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, 1 );\r
+\r
+ var bookmark = range.createBookmark(),\r
+ startNode = bookmark.startNode;\r
+\r
+ if ( range.collapsed )\r
+ {\r
+\r
+ var startPath = new CKEDITOR.dom.elementPath( startNode.getParent() ),\r
+ // The topmost element in elementspatch which we should jump out of.\r
+ boundaryElement;\r
+\r
+\r
+ for ( var i = 0, element ; i < startPath.elements.length\r
+ && ( element = startPath.elements[i] ) ; i++ )\r
+ {\r
+ /*\r
+ * 1. If it's collaped inside text nodes, try to remove the style from the whole element.\r
+ *\r
+ * 2. Otherwise if it's collapsed on element boundaries, moving the selection\r
+ * outside the styles instead of removing the whole tag,\r
+ * also make sure other inner styles were well preserverd.(#3309)\r
+ */\r
+ if ( element == startPath.block || element == startPath.blockLimit )\r
+ break;\r
+\r
+ if ( this.checkElementRemovable( element ) )\r
+ {\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 = isStart ? 'start' : 'end';\r
+ }\r
+ else\r
+ {\r
+ /*\r
+ * Before removing the style node, there may be a sibling to the style node\r
+ * that's exactly the same to the one to be removed. To the user, it makes\r
+ * no difference that they're separate entities in the DOM tree. So, merge\r
+ * them before removal.\r
+ */\r
+ element.mergeSiblings();\r
+ if ( element.getName() == this.element )\r
+ removeFromElement( this, element );\r
+ else\r
+ removeOverrides( element, getOverrides( this )[ element.getName() ] );\r
+ }\r
+ }\r
+ }\r
+\r
+ // Re-create the style tree after/before the boundary element,\r
+ // the replication start from bookmark start node to define the\r
+ // new range.\r
+ if ( boundaryElement )\r
+ {\r
+ var clonedElement = startNode;\r
+ for ( i = 0 ;; i++ )\r
+ {\r
+ var newElement = startPath.elements[ i ];\r
+ if ( newElement.equals( boundaryElement ) )\r
+ break;\r
+ // Avoid copying any matched element.\r
+ else if ( newElement.match )\r
+ continue;\r
+ else\r
+ newElement = newElement.clone();\r
+ newElement.append( clonedElement );\r
+ clonedElement = newElement;\r
+ }\r
+ clonedElement[ boundaryElement.match == 'start' ?\r
+ 'insertBefore' : 'insertAfter' ]( boundaryElement );\r
+ }\r
+ }\r
+ else\r
+ {\r
+ /*\r
+ * Now our range isn't collapsed. Lets walk from the start node to the end\r
+ * node via DFS and remove the styles one-by-one.\r
+ */\r
+ var endNode = bookmark.endNode,\r
+ me = this;\r
+\r
+ /*\r
+ * Find out the style ancestor that needs to be broken down at startNode\r
+ * and endNode.\r
+ */\r
+ function breakNodes()\r
+ {\r
+ var startPath = new CKEDITOR.dom.elementPath( startNode.getParent() ),\r
+ endPath = new CKEDITOR.dom.elementPath( endNode.getParent() ),\r
+ breakStart = null,\r
+ breakEnd = null;\r
+ for ( var i = 0 ; i < startPath.elements.length ; i++ )\r
+ {\r
+ var element = startPath.elements[ i ];\r
+\r
+ if ( element == startPath.block || element == startPath.blockLimit )\r
+ break;\r
+\r
+ if ( me.checkElementRemovable( element ) )\r
+ breakStart = element;\r
+ }\r
+ for ( i = 0 ; i < endPath.elements.length ; i++ )\r
+ {\r
+ element = endPath.elements[ i ];\r
+\r
+ if ( element == endPath.block || element == endPath.blockLimit )\r
+ break;\r
+\r
+ if ( me.checkElementRemovable( element ) )\r
+ breakEnd = element;\r
+ }\r
+\r
+ if ( breakEnd )\r
+ endNode.breakParent( breakEnd );\r
+ if ( breakStart )\r
+ startNode.breakParent( breakStart );\r
+ }\r
+ breakNodes();\r
+\r
+ // Now, do the DFS walk.\r
+ var currentNode = startNode.getNext();\r
+ while ( !currentNode.equals( endNode ) )\r
+ {\r
+ /*\r
+ * Need to get the next node first because removeFromElement() can remove\r
+ * the current node from DOM tree.\r
+ */\r
+ var nextNode = currentNode.getNextSourceNode();\r
+ 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
+ removeFromElement( this, currentNode );\r
+ else\r
+ removeOverrides( currentNode, getOverrides( this )[ currentNode.getName() ] );\r
+\r
+ /*\r
+ * removeFromElement() may have merged the next node with something before\r
+ * the startNode via mergeSiblings(). In that case, the nextNode would\r
+ * contain startNode and we'll have to call breakNodes() again and also\r
+ * reassign the nextNode to something after startNode.\r
+ */\r
+ if ( nextNode.type == CKEDITOR.NODE_ELEMENT && nextNode.contains( startNode ) )\r
+ {\r
+ breakNodes();\r
+ nextNode = startNode.getNext();\r
+ }\r
+ }\r
+ currentNode = nextNode;\r
+ }\r
+ }\r
+\r
+ 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
+ def = style._.definition,\r
+ 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
+ // elements may be merged.\r
+ var bookmark = range.createBookmark( true );\r
+\r
+ 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
+ {\r
+ var newBlock = getElement( this, doc, block );\r
+ replaceBlock( block, newBlock );\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
+ }\r
+\r
+ // Replace the original block with new one, with special treatment\r
+ // for <pre> blocks to make sure content format is well preserved, and merging/splitting adjacent\r
+ // when necessary.(#3188)\r
+ function replaceBlock( block, newBlock )\r
+ {\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
+\r
+ if ( isToPre )\r
+ newBlock = toPre( block, newBlock );\r
+ else if ( isFromPre )\r
+ // Split big <pre> into pieces before start to convert.\r
+ newBlock = fromPres( removeBlock ?\r
+ [ block.getHtml() ] : splitIntoPres( block ), newBlock );\r
+ else\r
+ block.moveChildren( newBlock );\r
+\r
+ newBlock.replace( block );\r
+\r
+ if ( newBlockIsPre )\r
+ {\r
+ // Merge previous <pre> blocks.\r
+ mergePre( newBlock );\r
+ }\r
+ else if ( removeBlock )\r
+ removeNoAttribsElement( newBlock );\r
+ }\r
+\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.getPrevious( nonWhitespaces ) )\r
+ && previousBlock.is\r
+ && previousBlock.is( 'pre') ) )\r
+ return;\r
+\r
+ // Merge the previous <pre> block contents into the current <pre>\r
+ // block.\r
+ //\r
+ // Another thing to be careful here is that currentBlock might contain\r
+ // a '\n' at the beginning, and previousBlock might contain a '\n'\r
+ // towards the end. These new lines are not normally displayed but they\r
+ // become visible after merging.\r
+ var mergedHtml = replace( previousBlock.getHtml(), /\n$/, '' ) + '\n\n' +\r
+ replace( preBlock.getHtml(), /^\n/, '' ) ;\r
+\r
+ // Krugle: IE normalizes innerHTML from <pre>, breaking whitespaces.\r
+ if ( CKEDITOR.env.ie )\r
+ preBlock.$.outerHTML = '<pre>' + mergedHtml + '</pre>';\r
+ else\r
+ preBlock.setHtml( mergedHtml );\r
+\r
+ previousBlock.remove();\r
+ }\r
+\r
+ /**\r
+ * Split into multiple <pre> blocks separated by double line-break.\r
+ * @param preBlock\r
+ */\r
+ function splitIntoPres( preBlock )\r
+ {\r
+ // Exclude the ones at header OR at tail,\r
+ // and ignore bookmark content between them.\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
+ function( match, charBefore, bookmark )\r
+ {\r
+ return charBefore + '</pre>' + bookmark + '<pre>';\r
+ } );\r
+\r
+ var pres = [];\r
+ splitedHtml.replace( /<pre\b.*?>([\s\S]*?)<\/pre>/gi, function( match, preContent ){\r
+ pres.push( preContent );\r
+ } );\r
+ return pres;\r
+ }\r
+\r
+ // Wrapper function of String::replace without considering of head/tail bookmarks nodes.\r
+ function replace( str, regexp, replacement )\r
+ {\r
+ var headBookmark = '',\r
+ tailBookmark = '';\r
+\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
+ return '';\r
+ } );\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;\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
+\r
+ // 1. Trim the first and last line-breaks immediately after and before <pre>,\r
+ // they're not visible.\r
+ blockHtml = blockHtml.replace( /(\r\n|\r)/g, '\n' ) ;\r
+ blockHtml = replace( blockHtml, /^[ \t]*\n/, '' ) ;\r
+ blockHtml = replace( blockHtml, /\n$/, '' ) ;\r
+ // 2. Convert spaces or tabs at the beginning or at the end to \r
+ blockHtml = replace( blockHtml, /^[ \t]+|[ \t]+$/g, function( match, offset, s )\r
+ {\r
+ if ( match.length == 1 ) // one space, preserve it\r
+ return ' ' ;\r
+ else if ( !offset ) // beginning of block\r
+ return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';\r
+ else // end of block\r
+ return ' ' + CKEDITOR.tools.repeat( ' ', match.length - 1 );\r
+ } ) ;\r
+\r
+ // 3. Convert \n to <BR>.\r
+ // 4. Convert contiguous (i.e. non-singular) spaces or tabs to \r
+ blockHtml = blockHtml.replace( /\n/g, '<br>' ) ;\r
+ blockHtml = blockHtml.replace( /[ \t]{2,}/g,\r
+ function ( match )\r
+ {\r
+ return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ' ;\r
+ } ) ;\r
+\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
+\r
+ return docFrag || newBlock;\r
+ }\r
+\r
+ /**\r
+ * Converting from a non-PRE block to a PRE block in formatting operations.\r
+ */\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
+ // 1. Trim head/tail spaces, they're not visible.\r
+ preHtml = replace( preHtml, /(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g, '' );\r
+ // 2. Delete ANSI whitespaces immediately before and after <BR> because\r
+ // they are not visible.\r
+ preHtml = preHtml.replace( /[ \t\r\n]*(<br[^>]*>)[ \t\r\n]*/gi, '$1' );\r
+ // 3. Compress other ANSI whitespaces since they're only visible as one\r
+ // single space previously.\r
+ // 4. Convert to spaces since is no longer needed in <PRE>.\r
+ preHtml = preHtml.replace( /([ \t\n\r]+| )/g, ' ' );\r
+ // 5. Convert any <BR /> to \n. This must not be done earlier because\r
+ // the \n would then get compressed.\r
+ preHtml = preHtml.replace( /<br\b[^>]*>/gi, '\n' );\r
+\r
+ // Krugle: IE normalizes innerHTML to <pre>, breaking whitespaces.\r
+ if ( CKEDITOR.env.ie )\r
+ {\r
+ 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
+ newBlock.setHtml( preHtml );\r
+\r
+ return newBlock;\r
+ }\r
+\r
+ // Removes a style from an element itself, don't care about its subtree.\r
+ function removeFromElement( style, element )\r
+ {\r
+ var def = style._.definition,\r
+ attributes = CKEDITOR.tools.extend( {}, def.attributes, getOverrides( style )[ element.getName() ] ),\r
+ styles = def.styles,\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
+ // Remove definition attributes/style from the elemnt.\r
+ for ( var attName in attributes )\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
+ 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
+ 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
+ function removeFromInsideElement( style, element )\r
+ {\r
+ var def = style._.definition,\r
+ attribs = def.attributes,\r
+ styles = def.styles,\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
+\r
+ // Now remove any other element with different name that is\r
+ // defined to be overriden.\r
+ for ( var overrideElement in overrides )\r
+ {\r
+ if ( overrideElement != style.element )\r
+ {\r
+ innerElements = element.getElementsByTag( overrideElement ) ;\r
+ for ( i = innerElements.count() - 1 ; i >= 0 ; i-- )\r
+ {\r
+ var innerElement = innerElements.getItem( i );\r
+ removeOverrides( innerElement, overrides[ overrideElement ] ) ;\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Remove overriding styles/attributes from the specific element.\r
+ * Note: Remove the element if no attributes remain.\r
+ * @param {Object} element\r
+ * @param {Object} overrides\r
+ */\r
+ function removeOverrides( element, overrides )\r
+ {\r
+ var attributes = overrides && overrides.attributes ;\r
+\r
+ if ( attributes )\r
+ {\r
+ for ( var i = 0 ; i < attributes.length ; i++ )\r
+ {\r
+ var attName = attributes[i][0], actualAttrValue ;\r
+\r
+ if ( ( actualAttrValue = element.getAttribute( attName ) ) )\r
+ {\r
+ var attValue = attributes[i][1] ;\r
+\r
+ // Remove the attribute if:\r
+ // - The override definition value is null ;\r
+ // - The override definition valie is a string that\r
+ // matches the attribute value exactly.\r
+ // - The override definition value is a regex that\r
+ // has matches in the attribute value.\r
+ if ( attValue === null ||\r
+ ( attValue.test && attValue.test( actualAttrValue ) ) ||\r
+ ( typeof attValue == 'string' && actualAttrValue == attValue ) )\r
+ element.removeAttribute( attName ) ;\r
+ }\r
+ }\r
+ }\r
+\r
+ removeNoAttribsElement( element );\r
+ }\r
+\r
+ // If the element has no more attributes, remove it.\r
+ function removeNoAttribsElement( element )\r
+ {\r
+ // If no more attributes remained in the element, remove it,\r
+ // leaving its children.\r
+ if ( !element.hasAttributes() )\r
+ {\r
+ if ( CKEDITOR.dtd.$block[ element.getName() ] )\r
+ {\r
+ var previous = element.getPrevious( nonWhitespaces ),\r
+ next = element.getNext( nonWhitespaces );\r
+\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
+ element.remove( true );\r
+ }\r
+ else\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
+\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
+ }\r
+ }\r
+ }\r
+\r
+ function getElement( style, targetDocument, element )\r
+ {\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
+ elementName = 'span';\r
+\r
+ // 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
+ 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
+ attributes = def.attributes,\r
+ styles = CKEDITOR.style.getStyleText( def );\r
+\r
+ // Assign all defined attributes.\r
+ if ( attributes )\r
+ {\r
+ for ( var att in attributes )\r
+ {\r
+ el.setAttribute( att, attributes[ att ] );\r
+ }\r
+ }\r
+\r
+ // Assign all defined styles.\r
+ if( styles )\r
+ el.setAttribute( 'style', styles );\r
+\r
+ return el;\r
+ }\r
+\r
+ function replaceVariables( list, variablesValues )\r
+ {\r
+ for ( var item in list )\r
+ {\r
+ list[ item ] = list[ item ].replace( varRegex, function( match, varName )\r
+ {\r
+ return variablesValues[ varName ];\r
+ });\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
+ function getAttributesForComparison( styleDefinition )\r
+ {\r
+ // If we have already computed it, just return it.\r
+ var attribs = styleDefinition._AC;\r
+ if ( attribs )\r
+ return attribs;\r
+\r
+ attribs = {};\r
+\r
+ var length = 0;\r
+\r
+ // Loop through all defined attributes.\r
+ var styleAttribs = styleDefinition.attributes;\r
+ if ( styleAttribs )\r
+ {\r
+ for ( var styleAtt in styleAttribs )\r
+ {\r
+ length++;\r
+ attribs[ styleAtt ] = styleAttribs[ styleAtt ];\r
+ }\r
+ }\r
+\r
+ // Includes the style definitions.\r
+ var styleText = CKEDITOR.style.getStyleText( styleDefinition );\r
+ if ( styleText )\r
+ {\r
+ if ( !attribs[ 'style' ] )\r
+ length++;\r
+ attribs[ 'style' ] = styleText;\r
+ }\r
+\r
+ // Appends the "length" information to the object.\r
+ attribs._length = length;\r
+\r
+ // Return it, saving it to the next request.\r
+ return ( styleDefinition._AC = attribs );\r
+ }\r
+\r
+ /**\r
+ * Get the the collection used to compare the elements and attributes,\r
+ * defined in this style overrides, with other element. All information in\r
+ * it is lowercased.\r
+ * @param {CKEDITOR.style} style\r
+ */\r
+ function getOverrides( style )\r
+ {\r
+ if ( style._.overrides )\r
+ return style._.overrides;\r
+\r
+ var overrides = ( style._.overrides = {} ),\r
+ definition = style._.definition.overrides;\r
+\r
+ if ( definition )\r
+ {\r
+ // The override description can be a string, object or array.\r
+ // Internally, well handle arrays only, so transform it if needed.\r
+ if ( !CKEDITOR.tools.isArray( definition ) )\r
+ definition = [ definition ];\r
+\r
+ // Loop through all override definitions.\r
+ for ( var i = 0 ; i < definition.length ; i++ )\r
+ {\r
+ var override = definition[i];\r
+ var elementName;\r
+ var overrideEl;\r
+ var attrs;\r
+\r
+ // If can be a string with the element name.\r
+ if ( typeof override == 'string' )\r
+ elementName = override.toLowerCase();\r
+ // Or an object.\r
+ else\r
+ {\r
+ elementName = override.element ? override.element.toLowerCase() : style.element;\r
+ attrs = override.attributes;\r
+ }\r
+\r
+ // We can have more than one override definition for the same\r
+ // element name, so we attempt to simply append information to\r
+ // it if it already exists.\r
+ overrideEl = overrides[ elementName ] || ( overrides[ elementName ] = {} );\r
+\r
+ if ( attrs )\r
+ {\r
+ // The returning attributes list is an array, because we\r
+ // could have different override definitions for the same\r
+ // attribute name.\r
+ var overrideAttrs = ( overrideEl.attributes = overrideEl.attributes || new Array() );\r
+ for ( var attName in attrs )\r
+ {\r
+ // Each item in the attributes array is also an array,\r
+ // where [0] is the attribute name and [1] is the\r
+ // override value.\r
+ overrideAttrs.push( [ attName.toLowerCase(), attrs[ attName ] ] );\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ 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
+ if ( nativeNormalize !== false )\r
+ {\r
+ // Injects the style in a temporary span object, so the browser parses it,\r
+ // retrieving its final format.\r
+ var temp = new CKEDITOR.dom.element( 'span' );\r
+ temp.setAttribute( 'style', unparsedCssText );\r
+ styleText = temp.getAttribute( 'style' ) || '';\r
+ }\r
+ else\r
+ styleText = unparsedCssText;\r
+\r
+ // Shrinking white-spaces around colon and semi-colon (#4147).\r
+ // Compensate tail semi-colon.\r
+ return styleText.replace( /\s*([;:])\s*/, '$1' )\r
+ .replace( /([^\s;])$/, '$1;')\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
+ // Turn inline style text properties into one hash.\r
+ function parseStyleText( styleText )\r
+ {\r
+ var retval = {};\r
+ styleText\r
+ .replace( /"/g, '"' )\r
+ .replace( /\s*([^ :;]+)\s*:\s*([^;]+)\s*(?=;|$)/g, function( match, name, value )\r
+ {\r
+ retval[ name ] = value;\r
+ } );\r
+ return retval;\r
+ }\r
+\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
+ function applyStyle( document, remove )\r
+ {\r
+ var selection = document.getSelection(),\r
+ ranges = selection.getRanges(),\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
+ selection.selectRanges( ranges );\r
+\r
+ document.removeCustomData( 'doc_processing_style' );\r
+ }\r
+})();\r
+\r
+CKEDITOR.styleCommand = function( style )\r
+{\r
+ this.style = style;\r
+};\r
+\r
+CKEDITOR.styleCommand.prototype.exec = function( editor )\r
+{\r
+ editor.focus();\r
+\r
+ var doc = editor.document;\r
+\r
+ if ( doc )\r
+ {\r
+ if ( this.state == CKEDITOR.TRISTATE_OFF )\r
+ this.style.apply( doc );\r
+ else if ( this.state == CKEDITOR.TRISTATE_ON )\r
+ this.style.remove( doc );\r
+ }\r
+\r
+ 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
+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
+ * 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
+ * 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