JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2
[ckeditor.git] / _source / plugins / styles / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 CKEDITOR.plugins.add( 'styles',\r
7 {\r
8         requires : [ 'selection' ]\r
9 });\r
10 \r
11 /**\r
12  * Registers a function to be called whenever a style changes its state in the\r
13  * editing area. The current state is passed to the function. The possible\r
14  * states are {@link CKEDITOR.TRISTATE_ON} and {@link CKEDITOR.TRISTATE_OFF}.\r
15  * @param {CKEDITOR.style} The style to be watched.\r
16  * @param {Function} The function to be called when the style state changes.\r
17  * @example\r
18  * // Create a style object for the <b> element.\r
19  * var style = new CKEDITOR.style( { element : 'b' } );\r
20  * var editor = CKEDITOR.instances.editor1;\r
21  * editor.attachStyleStateChange( style, function( state )\r
22  *     {\r
23  *         if ( state == CKEDITOR.TRISTATE_ON )\r
24  *             alert( 'The current state for the B element is ON' );\r
25  *         else\r
26  *             alert( 'The current state for the B element is OFF' );\r
27  *     });\r
28  */\r
29 CKEDITOR.editor.prototype.attachStyleStateChange = function( style, callback )\r
30 {\r
31         // Try to get the list of attached callbacks.\r
32         var styleStateChangeCallbacks = this._.styleStateChangeCallbacks;\r
33 \r
34         // If it doesn't exist, it means this is the first call. So, let's create\r
35         // all the structure to manage the style checks and the callback calls.\r
36         if ( !styleStateChangeCallbacks )\r
37         {\r
38                 // Create the callbacks array.\r
39                 styleStateChangeCallbacks = this._.styleStateChangeCallbacks = [];\r
40 \r
41                 // Attach to the selectionChange event, so we can check the styles at\r
42                 // that point.\r
43                 this.on( 'selectionChange', function( ev )\r
44                         {\r
45                                 // Loop throw all registered callbacks.\r
46                                 for ( var i = 0 ; i < styleStateChangeCallbacks.length ; i++ )\r
47                                 {\r
48                                         var callback = styleStateChangeCallbacks[ i ];\r
49 \r
50                                         // Check the current state for the style defined for that\r
51                                         // callback.\r
52                                         var currentState = callback.style.checkActive( ev.data.path ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF;\r
53 \r
54                                         // If the state changed since the last check.\r
55                                         if ( callback.state !== currentState )\r
56                                         {\r
57                                                 // Call the callback function, passing the current\r
58                                                 // state to it.\r
59                                                 callback.fn.call( this, currentState );\r
60 \r
61                                                 // Save the current state, so it can be compared next\r
62                                                 // time.\r
63                                                 callback.state !== currentState;\r
64                                         }\r
65                                 }\r
66                         });\r
67         }\r
68 \r
69         // Save the callback info, so it can be checked on the next occurence of\r
70         // selectionChange.\r
71         styleStateChangeCallbacks.push( { style : style, fn : callback } );\r
72 };\r
73 \r
74 CKEDITOR.STYLE_BLOCK = 1;\r
75 CKEDITOR.STYLE_INLINE = 2;\r
76 CKEDITOR.STYLE_OBJECT = 3;\r
77 \r
78 (function()\r
79 {\r
80         var blockElements       = { address:1,div:1,h1:1,h2:1,h3:1,h4:1,h5:1,h6:1,p:1,pre:1 };\r
81         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
82 \r
83         var semicolonFixRegex = /\s*(?:;\s*|$)/;\r
84 \r
85         CKEDITOR.style = function( styleDefinition, variablesValues )\r
86         {\r
87                 if ( variablesValues )\r
88                 {\r
89                         styleDefinition = CKEDITOR.tools.clone( styleDefinition );\r
90 \r
91                         replaceVariables( styleDefinition.attributes, variablesValues );\r
92                         replaceVariables( styleDefinition.styles, variablesValues );\r
93                 }\r
94 \r
95                 var element = this.element = ( styleDefinition.element || '*' ).toLowerCase();\r
96 \r
97                 this.type =\r
98                         ( element == '#' || blockElements[ element ] ) ?\r
99                                 CKEDITOR.STYLE_BLOCK\r
100                         : objectElements[ element ] ?\r
101                                 CKEDITOR.STYLE_OBJECT\r
102                         :\r
103                                 CKEDITOR.STYLE_INLINE;\r
104 \r
105                 this._ =\r
106                 {\r
107                         definition : styleDefinition\r
108                 };\r
109         };\r
110 \r
111         CKEDITOR.style.prototype =\r
112         {\r
113                 apply : function( document )\r
114                 {\r
115                         applyStyle.call( this, document, false );\r
116                 },\r
117 \r
118                 remove : function( document )\r
119                 {\r
120                         applyStyle.call( this, document, true );\r
121                 },\r
122 \r
123                 applyToRange : function( range )\r
124                 {\r
125                         return ( this.applyToRange =\r
126                                                 this.type == CKEDITOR.STYLE_INLINE ?\r
127                                                         applyInlineStyle\r
128                                                 : this.type == CKEDITOR.STYLE_BLOCK ?\r
129                                                         applyBlockStyle\r
130                                                 : null ).call( this, range );\r
131                 },\r
132 \r
133                 removeFromRange : function( range )\r
134                 {\r
135                         return ( this.removeFromRange =\r
136                                                 this.type == CKEDITOR.STYLE_INLINE ?\r
137                                                         removeInlineStyle\r
138                                                 : null ).call( this, range );\r
139                 },\r
140 \r
141                 applyToObject : function( element )\r
142                 {\r
143                         setupElement( element, this );\r
144                 },\r
145 \r
146                 /**\r
147                  * Get the style state inside an element path. Returns "true" if the\r
148                  * element is active in the path.\r
149                  */\r
150                 checkActive : function( elementPath )\r
151                 {\r
152                         switch ( this.type )\r
153                         {\r
154                                 case CKEDITOR.STYLE_BLOCK :\r
155                                         return this.checkElementRemovable( elementPath.block || elementPath.blockLimit, true );\r
156 \r
157                                 case CKEDITOR.STYLE_INLINE :\r
158 \r
159                                         var elements = elementPath.elements;\r
160 \r
161                                         for ( var i = 0, element ; i < elements.length ; i++ )\r
162                                         {\r
163                                                 element = elements[i];\r
164 \r
165                                                 if ( element == elementPath.block || element == elementPath.blockLimit )\r
166                                                         continue;\r
167 \r
168                                                 if ( this.checkElementRemovable( element, true ) )\r
169                                                         return true;\r
170                                         }\r
171                         }\r
172                         return false;\r
173                 },\r
174 \r
175                 // Checks if an element, or any of its attributes, is removable by the\r
176                 // current style definition.\r
177                 checkElementRemovable : function( element, fullMatch )\r
178                 {\r
179                         if ( !element )\r
180                                 return false;\r
181 \r
182                         var def = this._.definition,\r
183                                 attribs;\r
184 \r
185                         // If the element name is the same as the style name.\r
186                         if ( element.getName() == this.element )\r
187                         {\r
188                                 // If no attributes are defined in the element.\r
189                                 if ( !fullMatch && !element.hasAttributes() )\r
190                                         return true;\r
191 \r
192                                 attribs = getAttributesForComparison( def );\r
193 \r
194                                 if ( attribs._length )\r
195                                 {\r
196                                         for ( var attName in attribs )\r
197                                         {\r
198                                                 if ( attName == '_length' )\r
199                                                         continue;\r
200 \r
201                                                 var elementAttr = element.getAttribute( attName ) || '';\r
202                                                 if ( attribs[ attName ] ==\r
203                                                          ( attName == 'style' ?\r
204                                                            normalizeCssText( elementAttr, false ) : elementAttr  ) )\r
205                                                 {\r
206                                                         if ( !fullMatch )\r
207                                                                 return true;\r
208                                                 }\r
209                                                 else if ( fullMatch )\r
210                                                                 return false;\r
211                                         }\r
212                                         if ( fullMatch )\r
213                                                 return true;\r
214                                 }\r
215                                 else\r
216                                         return true;\r
217                         }\r
218 \r
219                         // Check if the element can be somehow overriden.\r
220                         var override = getOverrides( this )[ element.getName() ] ;\r
221                         if ( override )\r
222                         {\r
223                                 // If no attributes have been defined, remove the element.\r
224                                 if ( !( attribs = override.attributes ) )\r
225                                         return true;\r
226 \r
227                                 for ( var i = 0 ; i < attribs.length ; i++ )\r
228                                 {\r
229                                         attName = attribs[i][0];\r
230                                         var actualAttrValue = element.getAttribute( attName );\r
231                                         if ( actualAttrValue )\r
232                                         {\r
233                                                 var attValue = attribs[i][1];\r
234 \r
235                                                 // Remove the attribute if:\r
236                                                 //    - The override definition value is null;\r
237                                                 //    - The override definition value is a string that\r
238                                                 //      matches the attribute value exactly.\r
239                                                 //    - The override definition value is a regex that\r
240                                                 //      has matches in the attribute value.\r
241                                                 if ( attValue === null ||\r
242                                                                 ( typeof attValue == 'string' && actualAttrValue == attValue ) ||\r
243                                                                 attValue.test( actualAttrValue ) )\r
244                                                         return true;\r
245                                         }\r
246                                 }\r
247                         }\r
248                         return false;\r
249                 }\r
250         };\r
251 \r
252         // Build the cssText based on the styles definition.\r
253         CKEDITOR.style.getStyleText = function( styleDefinition )\r
254         {\r
255                 // If we have already computed it, just return it.\r
256                 var stylesDef = styleDefinition._ST;\r
257                 if ( stylesDef )\r
258                         return stylesDef;\r
259 \r
260                 stylesDef = styleDefinition.styles;\r
261 \r
262                 // Builds the StyleText.\r
263 \r
264                 var stylesText = ( styleDefinition.attributes && styleDefinition.attributes[ 'style' ] ) || '';\r
265 \r
266                 if ( stylesText.length )\r
267                         stylesText = stylesText.replace( semicolonFixRegex, ';' );\r
268 \r
269                 for ( var style in stylesDef )\r
270                         stylesText += ( style + ':' + stylesDef[ style ] ).replace( semicolonFixRegex, ';' );\r
271 \r
272                 // Browsers make some changes to the style when applying them. So, here\r
273                 // we normalize it to the browser format.\r
274                 if ( stylesText.length )\r
275                         stylesText = normalizeCssText( stylesText );\r
276 \r
277                 // Return it, saving it to the next request.\r
278                 return ( styleDefinition._ST = stylesText );\r
279         };\r
280 \r
281         function applyInlineStyle( range )\r
282         {\r
283                 var document = range.document;\r
284 \r
285                 if ( range.collapsed )\r
286                 {\r
287                         // Create the element to be inserted in the DOM.\r
288                         var collapsedElement = getElement( this, document );\r
289 \r
290                         // Insert the empty element into the DOM at the range position.\r
291                         range.insertNode( collapsedElement );\r
292 \r
293                         // Place the selection right inside the empty element.\r
294                         range.moveToPosition( collapsedElement, CKEDITOR.POSITION_BEFORE_END );\r
295 \r
296                         return;\r
297                 }\r
298 \r
299                 var elementName = this.element;\r
300                 var def = this._.definition;\r
301                 var isUnknownElement;\r
302 \r
303                 // Get the DTD definition for the element. Defaults to "span".\r
304                 var dtd = CKEDITOR.dtd[ elementName ] || ( isUnknownElement = true, CKEDITOR.dtd.span );\r
305 \r
306                 // Bookmark the range so we can re-select it after processing.\r
307                 var bookmark = range.createBookmark();\r
308 \r
309                 // Expand the range.\r
310                 range.enlarge( CKEDITOR.ENLARGE_ELEMENT );\r
311                 range.trim();\r
312 \r
313                 // Get the first node to be processed and the last, which concludes the\r
314                 // processing.\r
315                 var boundaryNodes = range.getBoundaryNodes();\r
316                 var firstNode = boundaryNodes.startNode;\r
317                 var lastNode = boundaryNodes.endNode.getNextSourceNode( true );\r
318 \r
319                 // Probably the document end is reached, we need a marker node.\r
320                 if ( !lastNode )\r
321                 {\r
322                                 var marker;\r
323                                 lastNode = marker = document.createText( '' );\r
324                                 lastNode.insertAfter( range.endContainer );\r
325                 }\r
326                 // The detection algorithm below skips the contents inside bookmark nodes, so\r
327                 // we'll need to make sure lastNode isn't the &nbsp; inside a bookmark node.\r
328                 var lastParent = lastNode.getParent();\r
329                 if ( lastParent && lastParent.getAttribute( '_fck_bookmark' ) )\r
330                         lastNode = lastParent;\r
331 \r
332                 if ( lastNode.equals( firstNode ) )\r
333                 {\r
334                         // If the last node is the same as the the first one, we must move\r
335                         // it to the next one, otherwise the first one will not be\r
336                         // processed.\r
337                         lastNode = lastNode.getNextSourceNode( true );\r
338 \r
339                         // It may happen that there are no more nodes after it (the end of\r
340                         // the document), so we must add something there to make our code\r
341                         // simpler.\r
342                         if ( !lastNode )\r
343                         {\r
344                                 lastNode = marker = document.createText( '' );\r
345                                 lastNode.insertAfter( firstNode );\r
346                         }\r
347                 }\r
348 \r
349                 var currentNode = firstNode;\r
350 \r
351                 var styleRange;\r
352 \r
353                 // Indicates that that some useful inline content has been found, so\r
354                 // the style should be applied.\r
355                 var hasContents;\r
356 \r
357                 while ( currentNode )\r
358                 {\r
359                         var applyStyle = false;\r
360 \r
361                         if ( currentNode.equals( lastNode ) )\r
362                         {\r
363                                 currentNode = null;\r
364                                 applyStyle = true;\r
365                         }\r
366                         else\r
367                         {\r
368                                 var nodeType = currentNode.type;\r
369                                 var nodeName = nodeType == CKEDITOR.NODE_ELEMENT ? currentNode.getName() : null;\r
370 \r
371                                 if ( nodeName && currentNode.getAttribute( '_fck_bookmark' ) )\r
372                                 {\r
373                                         currentNode = currentNode.getNextSourceNode( true );\r
374                                         continue;\r
375                                 }\r
376 \r
377                                 // Check if the current node can be a child of the style element.\r
378                                 if ( !nodeName || ( dtd[ nodeName ]\r
379                                         && ( currentNode.getPosition( lastNode ) | CKEDITOR.POSITION_PRECEDING | CKEDITOR.POSITION_IDENTICAL | CKEDITOR.POSITION_IS_CONTAINED ) == ( CKEDITOR.POSITION_PRECEDING + CKEDITOR.POSITION_IDENTICAL + CKEDITOR.POSITION_IS_CONTAINED )\r
380                                         && ( !def.childRule || def.childRule( currentNode ) ) ) )\r
381                                 {\r
382                                         var currentParent = currentNode.getParent();\r
383 \r
384                                         // Check if the style element can be a child of the current\r
385                                         // node parent or if the element is not defined in the DTD.\r
386                                         if ( currentParent\r
387                                                 && ( ( currentParent.getDtd() || CKEDITOR.dtd.span )[ elementName ] || isUnknownElement )\r
388                                                 && ( !def.parentRule || def.parentRule( currentParent ) ) )\r
389                                         {\r
390                                                 // This node will be part of our range, so if it has not\r
391                                                 // been started, place its start right before the node.\r
392                                                 // In the case of an element node, it will be included\r
393                                                 // only if it is entirely inside the range.\r
394                                                 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
395                                                 {\r
396                                                         styleRange = new CKEDITOR.dom.range( document );\r
397                                                         styleRange.setStartBefore( currentNode );\r
398                                                 }\r
399 \r
400                                                 // Non element nodes, or empty elements can be added\r
401                                                 // completely to the range.\r
402                                                 if ( nodeType == CKEDITOR.NODE_TEXT || ( nodeType == CKEDITOR.NODE_ELEMENT && !currentNode.getChildCount() ) )\r
403                                                 {\r
404                                                         var includedNode = currentNode;\r
405                                                         var parentNode;\r
406 \r
407                                                         // This node is about to be included completelly, but,\r
408                                                         // if this is the last node in its parent, we must also\r
409                                                         // check if the parent itself can be added completelly\r
410                                                         // to the range.\r
411                                                         while ( !includedNode.$.nextSibling\r
412                                                                 && ( parentNode = includedNode.getParent(), dtd[ parentNode.getName() ] )\r
413                                                                 && ( parentNode.getPosition( firstNode ) | CKEDITOR.POSITION_FOLLOWING | CKEDITOR.POSITION_IDENTICAL | CKEDITOR.POSITION_IS_CONTAINED ) == ( CKEDITOR.POSITION_FOLLOWING + CKEDITOR.POSITION_IDENTICAL + CKEDITOR.POSITION_IS_CONTAINED )\r
414                                                                 && ( !def.childRule || def.childRule( parentNode ) ) )\r
415                                                         {\r
416                                                                 includedNode = parentNode;\r
417                                                         }\r
418 \r
419                                                         styleRange.setEndAfter( includedNode );\r
420 \r
421                                                         // If the included node still is the last node in its\r
422                                                         // parent, it means that the parent can't be included\r
423                                                         // in this style DTD, so apply the style immediately.\r
424                                                         if ( !includedNode.$.nextSibling )\r
425                                                                 applyStyle = true;\r
426 \r
427                                                         if ( !hasContents )\r
428                                                                 hasContents = ( nodeType != CKEDITOR.NODE_TEXT || (/[^\s\ufeff]/).test( currentNode.getText() ) );\r
429                                                 }\r
430                                         }\r
431                                         else\r
432                                                 applyStyle = true;\r
433                                 }\r
434                                 else\r
435                                         applyStyle = true;\r
436 \r
437                                 // Get the next node to be processed.\r
438                                 currentNode = currentNode.getNextSourceNode();\r
439                         }\r
440 \r
441                         // Apply the style if we have something to which apply it.\r
442                         if ( applyStyle && hasContents && styleRange && !styleRange.collapsed )\r
443                         {\r
444                                 // Build the style element, based on the style object definition.\r
445                                 var styleNode = getElement( this, document );\r
446 \r
447                                 // Get the element that holds the entire range.\r
448                                 var parent = styleRange.getCommonAncestor();\r
449 \r
450                                 // Loop through the parents, removing the redundant attributes\r
451                                 // from the element to be applied.\r
452                                 while ( styleNode && parent )\r
453                                 {\r
454                                         if ( parent.getName() == elementName )\r
455                                         {\r
456                                                 for ( var attName in def.attributes )\r
457                                                 {\r
458                                                         if ( styleNode.getAttribute( attName ) == parent.getAttribute( attName ) )\r
459                                                                 styleNode.removeAttribute( attName );\r
460                                                 }\r
461 \r
462                                                 for ( var styleName in def.styles )\r
463                                                 {\r
464                                                         if ( styleNode.getStyle( styleName ) == parent.getStyle( styleName ) )\r
465                                                                 styleNode.removeStyle( styleName );\r
466                                                 }\r
467 \r
468                                                 if ( !styleNode.hasAttributes() )\r
469                                                 {\r
470                                                         styleNode = null;\r
471                                                         break;\r
472                                                 }\r
473                                         }\r
474 \r
475                                         parent = parent.getParent();\r
476                                 }\r
477 \r
478                                 if ( styleNode )\r
479                                 {\r
480                                         // Move the contents of the range to the style element.\r
481                                         styleRange.extractContents().appendTo( styleNode );\r
482 \r
483                                         // Here we do some cleanup, removing all duplicated\r
484                                         // elements from the style element.\r
485                                         removeFromInsideElement( this, styleNode );\r
486 \r
487                                         // Insert it into the range position (it is collapsed after\r
488                                         // extractContents.\r
489                                         styleRange.insertNode( styleNode );\r
490 \r
491                                         // Let's merge our new style with its neighbors, if possible.\r
492                                         mergeSiblings( styleNode );\r
493 \r
494                                         // As the style system breaks text nodes constantly, let's normalize\r
495                                         // things for performance.\r
496                                         // With IE, some paragraphs get broken when calling normalize()\r
497                                         // repeatedly. Also, for IE, we must normalize body, not documentElement.\r
498                                         // IE is also known for having a "crash effect" with normalize().\r
499                                         // We should try to normalize with IE too in some way, somewhere.\r
500                                         if ( !CKEDITOR.env.ie )\r
501                                                 styleNode.$.normalize();\r
502                                 }\r
503 \r
504                                 // Style applied, let's release the range, so it gets\r
505                                 // re-initialization in the next loop.\r
506                                 styleRange = null;\r
507                         }\r
508                 }\r
509 \r
510                 // Remove the temporary marking node.(#4111)\r
511                 marker && marker.remove();\r
512                 range.moveToBookmark( bookmark );\r
513         }\r
514 \r
515         function removeInlineStyle( range )\r
516         {\r
517                 /*\r
518                  * Make sure our range has included all "collpased" parent inline nodes so\r
519                  * that our operation logic can be simpler.\r
520                  */\r
521                 range.enlarge( CKEDITOR.ENLARGE_ELEMENT );\r
522 \r
523                 var bookmark = range.createBookmark(),\r
524                         startNode = bookmark.startNode;\r
525 \r
526                 if ( range.collapsed )\r
527                 {\r
528 \r
529                         var startPath = new CKEDITOR.dom.elementPath( startNode.getParent() ),\r
530                                 // The topmost element in elementspatch which we should jump out of.\r
531                                 boundaryElement;\r
532 \r
533 \r
534                         for ( var i = 0, element ; i < startPath.elements.length\r
535                                         && ( element = startPath.elements[i] ) ; i++ )\r
536                         {\r
537                                 /*\r
538                                  * 1. If it's collaped inside text nodes, try to remove the style from the whole element.\r
539                                  *\r
540                                  * 2. Otherwise if it's collapsed on element boundaries, moving the selection\r
541                                  *  outside the styles instead of removing the whole tag,\r
542                                  *  also make sure other inner styles were well preserverd.(#3309)\r
543                                  */\r
544                                 if ( element == startPath.block || element == startPath.blockLimit )\r
545                                         break;\r
546 \r
547                                 if ( this.checkElementRemovable( element ) )\r
548                                 {\r
549                                         var endOfElement = range.checkBoundaryOfElement( element, CKEDITOR.END ),\r
550                                                         startOfElement = !endOfElement && range.checkBoundaryOfElement( element, CKEDITOR.START );\r
551                                         if ( startOfElement || endOfElement )\r
552                                         {\r
553                                                 boundaryElement = element;\r
554                                                 boundaryElement.match = startOfElement ? 'start' : 'end';\r
555                                         }\r
556                                         else\r
557                                         {\r
558                                                 /*\r
559                                                  * Before removing the style node, there may be a sibling to the style node\r
560                                                  * that's exactly the same to the one to be removed. To the user, it makes\r
561                                                  * no difference that they're separate entities in the DOM tree. So, merge\r
562                                                  * them before removal.\r
563                                                  */\r
564                                                 mergeSiblings( element );\r
565                                                 removeFromElement( this, element );\r
566 \r
567                                         }\r
568                                 }\r
569                         }\r
570 \r
571                         // Re-create the style tree after/before the boundary element,\r
572                         // the replication start from bookmark start node to define the\r
573                         // new range.\r
574                         if ( boundaryElement )\r
575                         {\r
576                                 var clonedElement = startNode;\r
577                                 for ( i = 0 ;; i++ )\r
578                                 {\r
579                                         var newElement = startPath.elements[ i ];\r
580                                         if ( newElement.equals( boundaryElement ) )\r
581                                                 break;\r
582                                         // Avoid copying any matched element.\r
583                                         else if ( newElement.match )\r
584                                                 continue;\r
585                                         else\r
586                                                 newElement = newElement.clone();\r
587                                         newElement.append( clonedElement );\r
588                                         clonedElement = newElement;\r
589                                 }\r
590                                 clonedElement[ boundaryElement.match == 'start' ?\r
591                                                         'insertBefore' : 'insertAfter' ]( boundaryElement );\r
592                         }\r
593                 }\r
594                 else\r
595                 {\r
596                         /*\r
597                          * Now our range isn't collapsed. Lets walk from the start node to the end\r
598                          * node via DFS and remove the styles one-by-one.\r
599                          */\r
600                         var endNode = bookmark.endNode,\r
601                                 me = this;\r
602 \r
603                         /*\r
604                          * Find out the style ancestor that needs to be broken down at startNode\r
605                          * and endNode.\r
606                          */\r
607                         function breakNodes()\r
608                         {\r
609                                 var startPath = new CKEDITOR.dom.elementPath( startNode.getParent() ),\r
610                                         endPath = new CKEDITOR.dom.elementPath( endNode.getParent() ),\r
611                                         breakStart = null,\r
612                                         breakEnd = null;\r
613                                 for ( var i = 0 ; i < startPath.elements.length ; i++ )\r
614                                 {\r
615                                         var element = startPath.elements[ i ];\r
616 \r
617                                         if ( element == startPath.block || element == startPath.blockLimit )\r
618                                                 break;\r
619 \r
620                                         if ( me.checkElementRemovable( element ) )\r
621                                                 breakStart = element;\r
622                                 }\r
623                                 for ( i = 0 ; i < endPath.elements.length ; i++ )\r
624                                 {\r
625                                         element = endPath.elements[ i ];\r
626 \r
627                                         if ( element == endPath.block || element == endPath.blockLimit )\r
628                                                 break;\r
629 \r
630                                         if ( me.checkElementRemovable( element ) )\r
631                                                 breakEnd = element;\r
632                                 }\r
633 \r
634                                 if ( breakEnd )\r
635                                         endNode.breakParent( breakEnd );\r
636                                 if ( breakStart )\r
637                                         startNode.breakParent( breakStart );\r
638                         }\r
639                         breakNodes();\r
640 \r
641                         // Now, do the DFS walk.\r
642                         var currentNode = startNode.getNext();\r
643                         while ( !currentNode.equals( endNode ) )\r
644                         {\r
645                                 /*\r
646                                  * Need to get the next node first because removeFromElement() can remove\r
647                                  * the current node from DOM tree.\r
648                                  */\r
649                                 var nextNode = currentNode.getNextSourceNode();\r
650                                 if ( currentNode.type == CKEDITOR.NODE_ELEMENT && this.checkElementRemovable( currentNode ) )\r
651                                 {\r
652                                         // Remove style from element or overriding element.\r
653                                         if ( currentNode.getName() == this.element )\r
654                                                 removeFromElement( this, currentNode );\r
655                                         else\r
656                                                 removeOverrides( currentNode, getOverrides( this )[ currentNode.getName() ] );\r
657 \r
658                                         /*\r
659                                          * removeFromElement() may have merged the next node with something before\r
660                                          * the startNode via mergeSiblings(). In that case, the nextNode would\r
661                                          * contain startNode and we'll have to call breakNodes() again and also\r
662                                          * reassign the nextNode to something after startNode.\r
663                                          */\r
664                                         if ( nextNode.type == CKEDITOR.NODE_ELEMENT && nextNode.contains( startNode ) )\r
665                                         {\r
666                                                 breakNodes();\r
667                                                 nextNode = startNode.getNext();\r
668                                         }\r
669                                 }\r
670                                 currentNode = nextNode;\r
671                         }\r
672                 }\r
673 \r
674                 range.moveToBookmark( bookmark );\r
675 }\r
676 \r
677         function applyBlockStyle( range )\r
678         {\r
679                 // Serializible bookmarks is needed here since\r
680                 // elements may be merged.\r
681                 var bookmark = range.createBookmark( true );\r
682 \r
683                 var iterator = range.createIterator();\r
684                 iterator.enforceRealBlocks = true;\r
685 \r
686                 var block;\r
687                 var doc = range.document;\r
688                 var previousPreBlock;\r
689 \r
690                 while ( ( block = iterator.getNextParagraph() ) )               // Only one =\r
691                 {\r
692                         var newBlock = getElement( this, doc );\r
693                         replaceBlock( block, newBlock );\r
694                 }\r
695 \r
696                 range.moveToBookmark( bookmark );\r
697         }\r
698 \r
699         // Replace the original block with new one, with special treatment\r
700         // for <pre> blocks to make sure content format is well preserved, and merging/splitting adjacent\r
701         // when necessary.(#3188)\r
702         function replaceBlock( block, newBlock )\r
703         {\r
704                 var newBlockIsPre       = newBlock.is( 'pre' );\r
705                 var blockIsPre          = block.is( 'pre' );\r
706 \r
707                 var isToPre     = newBlockIsPre && !blockIsPre;\r
708                 var isFromPre   = !newBlockIsPre && blockIsPre;\r
709 \r
710                 if ( isToPre )\r
711                         newBlock = toPre( block, newBlock );\r
712                 else if ( isFromPre )\r
713                         // Split big <pre> into pieces before start to convert.\r
714                         newBlock = fromPres( splitIntoPres( block ), newBlock );\r
715                 else\r
716                         block.moveChildren( newBlock );\r
717 \r
718                 newBlock.replace( block );\r
719 \r
720                 if ( newBlockIsPre )\r
721                 {\r
722                         // Merge previous <pre> blocks.\r
723                         mergePre( newBlock );\r
724                 }\r
725         }\r
726 \r
727         /**\r
728          * Merge a <pre> block with a previous sibling if available.\r
729          */\r
730         function mergePre( preBlock )\r
731         {\r
732                 var previousBlock;\r
733                 if ( !( ( previousBlock = preBlock.getPreviousSourceNode( true, CKEDITOR.NODE_ELEMENT ) )\r
734                                  && previousBlock.is\r
735                                  && previousBlock.is( 'pre') ) )\r
736                         return;\r
737 \r
738                 // Merge the previous <pre> block contents into the current <pre>\r
739                 // block.\r
740                 //\r
741                 // Another thing to be careful here is that currentBlock might contain\r
742                 // a '\n' at the beginning, and previousBlock might contain a '\n'\r
743                 // towards the end. These new lines are not normally displayed but they\r
744                 // become visible after merging.\r
745                 var mergedHtml = replace( previousBlock.getHtml(), /\n$/, '' ) + '\n\n' +\r
746                                 replace( preBlock.getHtml(), /^\n/, '' ) ;\r
747 \r
748                 // Krugle: IE normalizes innerHTML from <pre>, breaking whitespaces.\r
749                 if ( CKEDITOR.env.ie )\r
750                         preBlock.$.outerHTML = '<pre>' + mergedHtml + '</pre>';\r
751                 else\r
752                         preBlock.setHtml( mergedHtml );\r
753 \r
754                 previousBlock.remove();\r
755         }\r
756 \r
757         /**\r
758          * Split into multiple <pre> blocks separated by double line-break.\r
759          * @param preBlock\r
760          */\r
761         function splitIntoPres( preBlock )\r
762         {\r
763                 // Exclude the ones at header OR at tail,\r
764                 // and ignore bookmark content between them.\r
765                 var duoBrRegex = /(\S\s*)\n(?:\s|(<span[^>]+_fck_bookmark.*?\/span>))*\n(?!$)/gi,\r
766                         blockName = preBlock.getName(),\r
767                         splitedHtml = replace( preBlock.getOuterHtml(),\r
768                                 duoBrRegex,\r
769                                 function( match, charBefore, bookmark )\r
770                                 {\r
771                                   return charBefore + '</pre>' + bookmark + '<pre>';\r
772                                 } );\r
773 \r
774                 var pres = [];\r
775                 splitedHtml.replace( /<pre>([\s\S]*?)<\/pre>/gi, function( match, preContent ){\r
776                         pres.push( preContent );\r
777                 } );\r
778                 return pres;\r
779         }\r
780 \r
781         // Wrapper function of String::replace without considering of head/tail bookmarks nodes.\r
782         function replace( str, regexp, replacement )\r
783         {\r
784                 var headBookmark = '',\r
785                         tailBookmark = '';\r
786 \r
787                 str = str.replace( /(^<span[^>]+_fck_bookmark.*?\/span>)|(<span[^>]+_fck_bookmark.*?\/span>$)/gi,\r
788                         function( str, m1, m2 ){\r
789                                         m1 && ( headBookmark = m1 );\r
790                                         m2 && ( tailBookmark = m2 );\r
791                                 return '';\r
792                         } );\r
793                 return headBookmark + str.replace( regexp, replacement ) + tailBookmark;\r
794         }\r
795         /**\r
796          * Converting a list of <pre> into blocks with format well preserved.\r
797          */\r
798         function fromPres( preHtmls, newBlock )\r
799         {\r
800                 var docFrag = new CKEDITOR.dom.documentFragment( newBlock.getDocument() );\r
801                 for ( var i = 0 ; i < preHtmls.length ; i++ )\r
802                 {\r
803                         var blockHtml = preHtmls[ i ];\r
804 \r
805                         // 1. Trim the first and last line-breaks immediately after and before <pre>,\r
806                         // they're not visible.\r
807                          blockHtml =  blockHtml.replace( /(\r\n|\r)/g, '\n' ) ;\r
808                          blockHtml = replace(  blockHtml, /^[ \t]*\n/, '' ) ;\r
809                          blockHtml = replace(  blockHtml, /\n$/, '' ) ;\r
810                         // 2. Convert spaces or tabs at the beginning or at the end to &nbsp;\r
811                          blockHtml = replace(  blockHtml, /^[ \t]+|[ \t]+$/g, function( match, offset, s )\r
812                                         {\r
813                                                 if ( match.length == 1 )        // one space, preserve it\r
814                                                         return '&nbsp;' ;\r
815                                                 else if ( !offset )             // beginning of block\r
816                                                         return CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 ) + ' ';\r
817                                                 else                            // end of block\r
818                                                         return ' ' + CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 );\r
819                                         } ) ;\r
820 \r
821                         // 3. Convert \n to <BR>.\r
822                         // 4. Convert contiguous (i.e. non-singular) spaces or tabs to &nbsp;\r
823                          blockHtml =  blockHtml.replace( /\n/g, '<br>' ) ;\r
824                          blockHtml =  blockHtml.replace( /[ \t]{2,}/g,\r
825                                         function ( match )\r
826                                         {\r
827                                                 return CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 ) + ' ' ;\r
828                                         } ) ;\r
829 \r
830                         var newBlockClone = newBlock.clone();\r
831                         newBlockClone.setHtml(  blockHtml );\r
832                         docFrag.append( newBlockClone );\r
833                 }\r
834                 return docFrag;\r
835         }\r
836 \r
837         /**\r
838          * Converting from a non-PRE block to a PRE block in formatting operations.\r
839          */\r
840         function toPre( block, newBlock )\r
841         {\r
842                 // First trim the block content.\r
843                 var preHtml = block.getHtml();\r
844 \r
845                 // 1. Trim head/tail spaces, they're not visible.\r
846                 preHtml = replace( preHtml, /(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g, '' );\r
847                 // 2. Delete ANSI whitespaces immediately before and after <BR> because\r
848                 //    they are not visible.\r
849                 preHtml = preHtml.replace( /[ \t\r\n]*(<br[^>]*>)[ \t\r\n]*/gi, '$1' );\r
850                 // 3. Compress other ANSI whitespaces since they're only visible as one\r
851                 //    single space previously.\r
852                 // 4. Convert &nbsp; to spaces since &nbsp; is no longer needed in <PRE>.\r
853                 preHtml = preHtml.replace( /([ \t\n\r]+|&nbsp;)/g, ' ' );\r
854                 // 5. Convert any <BR /> to \n. This must not be done earlier because\r
855                 //    the \n would then get compressed.\r
856                 preHtml = preHtml.replace( /<br\b[^>]*>/gi, '\n' );\r
857 \r
858                 // Krugle: IE normalizes innerHTML to <pre>, breaking whitespaces.\r
859                 if ( CKEDITOR.env.ie )\r
860                 {\r
861                         var temp = block.getDocument().createElement( 'div' );\r
862                         temp.append( newBlock );\r
863                         newBlock.$.outerHTML =  '<pre>' + preHtml + '</pre>';\r
864                         newBlock = temp.getFirst().remove();\r
865                 }\r
866                 else\r
867                         newBlock.setHtml( preHtml );\r
868 \r
869                 return newBlock;\r
870         }\r
871 \r
872         // Removes a style from an element itself, don't care about its subtree.\r
873         function removeFromElement( style, element )\r
874         {\r
875                 var def = style._.definition,\r
876                         attributes = def.attributes,\r
877                         styles = def.styles,\r
878                         overrides = getOverrides( style );\r
879 \r
880                 function removeAttrs()\r
881                 {\r
882                         for ( var attName in attributes )\r
883                         {\r
884                                 // The 'class' element value must match (#1318).\r
885                                 if ( attName == 'class' && element.getAttribute( attName ) != attributes[ attName ] )\r
886                                         continue;\r
887                                 element.removeAttribute( attName );\r
888                         }\r
889                 }\r
890 \r
891                 // Remove definition attributes/style from the elemnt.\r
892                 removeAttrs();\r
893                 for ( var styleName in styles )\r
894                         element.removeStyle( styleName );\r
895 \r
896                 // Now remove override styles on the element.\r
897                 attributes = overrides[ element.getName() ];\r
898                 if ( attributes )\r
899                         removeAttrs();\r
900                 removeNoAttribsElement( element );\r
901         }\r
902 \r
903         // Removes a style from inside an element.\r
904         function removeFromInsideElement( style, element )\r
905         {\r
906                 var def = style._.definition,\r
907                         attribs = def.attributes,\r
908                         styles = def.styles,\r
909                         overrides = getOverrides( style );\r
910 \r
911                 var innerElements = element.getElementsByTag( style.element );\r
912 \r
913                 for ( var i = innerElements.count(); --i >= 0 ; )\r
914                         removeFromElement( style,  innerElements.getItem( i ) );\r
915 \r
916                 // Now remove any other element with different name that is\r
917                 // defined to be overriden.\r
918                 for ( var overrideElement in overrides )\r
919                 {\r
920                         if ( overrideElement != style.element )\r
921                         {\r
922                                 innerElements = element.getElementsByTag( overrideElement ) ;\r
923                                 for ( i = innerElements.count() - 1 ; i >= 0 ; i-- )\r
924                                 {\r
925                                         var innerElement = innerElements.getItem( i );\r
926                                         removeOverrides( innerElement, overrides[ overrideElement ] ) ;\r
927                                 }\r
928                         }\r
929                 }\r
930 \r
931         }\r
932 \r
933         /**\r
934          *  Remove overriding styles/attributes from the specific element.\r
935          *  Note: Remove the element if no attributes remain.\r
936          * @param {Object} element\r
937          * @param {Object} overrides\r
938          */\r
939         function removeOverrides( element, overrides )\r
940         {\r
941                 var attributes = overrides && overrides.attributes ;\r
942 \r
943                 if ( attributes )\r
944                 {\r
945                         for ( var i = 0 ; i < attributes.length ; i++ )\r
946                         {\r
947                                 var attName = attributes[i][0], actualAttrValue ;\r
948 \r
949                                 if ( ( actualAttrValue = element.getAttribute( attName ) ) )\r
950                                 {\r
951                                         var attValue = attributes[i][1] ;\r
952 \r
953                                         // Remove the attribute if:\r
954                                         //    - The override definition value is null ;\r
955                                         //    - The override definition valie is a string that\r
956                                         //      matches the attribute value exactly.\r
957                                         //    - The override definition value is a regex that\r
958                                         //      has matches in the attribute value.\r
959                                         if ( attValue === null ||\r
960                                                         ( attValue.test && attValue.test( actualAttrValue ) ) ||\r
961                                                         ( typeof attValue == 'string' && actualAttrValue == attValue ) )\r
962                                                 element.removeAttribute( attName ) ;\r
963                                 }\r
964                         }\r
965                 }\r
966 \r
967                 removeNoAttribsElement( element );\r
968         }\r
969 \r
970         // If the element has no more attributes, remove it.\r
971         function removeNoAttribsElement( element )\r
972         {\r
973                 // If no more attributes remained in the element, remove it,\r
974                 // leaving its children.\r
975                 if ( !element.hasAttributes() )\r
976                 {\r
977                         // Removing elements may open points where merging is possible,\r
978                         // so let's cache the first and last nodes for later checking.\r
979                         var firstChild  = element.getFirst();\r
980                         var lastChild   = element.getLast();\r
981 \r
982                         element.remove( true );\r
983 \r
984                         if ( firstChild )\r
985                         {\r
986                                 // Check the cached nodes for merging.\r
987                                 mergeSiblings( firstChild );\r
988 \r
989                                 if ( lastChild && !firstChild.equals( lastChild ) )\r
990                                         mergeSiblings( lastChild );\r
991                         }\r
992                 }\r
993         }\r
994 \r
995         function mergeSiblings( element )\r
996         {\r
997                 if ( !element || element.type != CKEDITOR.NODE_ELEMENT || !CKEDITOR.dtd.$removeEmpty[ element.getName() ] )\r
998                         return;\r
999 \r
1000                 mergeElements( element, element.getNext(), true );\r
1001                 mergeElements( element, element.getPrevious() );\r
1002         }\r
1003 \r
1004         function mergeElements( element, sibling, isNext )\r
1005         {\r
1006                 if ( sibling && sibling.type == CKEDITOR.NODE_ELEMENT )\r
1007                 {\r
1008                         var hasBookmark = sibling.getAttribute( '_fck_bookmark' );\r
1009 \r
1010                         if ( hasBookmark )\r
1011                                 sibling = isNext ? sibling.getNext() : sibling.getPrevious();\r
1012 \r
1013                         if ( sibling && sibling.type == CKEDITOR.NODE_ELEMENT && element.isIdentical( sibling ) )\r
1014                         {\r
1015                                 // Save the last child to be checked too, to merge things like\r
1016                                 // <b><i></i></b><b><i></i></b> => <b><i></i></b>\r
1017                                 var innerSibling = isNext ? element.getLast() : element.getFirst();\r
1018 \r
1019                                 if ( hasBookmark )\r
1020                                         ( isNext ? sibling.getPrevious() : sibling.getNext() ).move( element, !isNext );\r
1021 \r
1022                                 sibling.moveChildren( element, !isNext );\r
1023                                 sibling.remove();\r
1024 \r
1025                                 // Now check the last inner child (see two comments above).\r
1026                                 if ( innerSibling )\r
1027                                         mergeSiblings( innerSibling );\r
1028                         }\r
1029                 }\r
1030         }\r
1031 \r
1032         function getElement( style, targetDocument )\r
1033         {\r
1034                 var el;\r
1035 \r
1036                 var def = style._.definition;\r
1037 \r
1038                 var elementName = style.element;\r
1039 \r
1040                 // The "*" element name will always be a span for this function.\r
1041                 if ( elementName == '*' )\r
1042                         elementName = 'span';\r
1043 \r
1044                 // Create the element.\r
1045                 el = new CKEDITOR.dom.element( elementName, targetDocument );\r
1046 \r
1047                 return setupElement( el, style );\r
1048         }\r
1049 \r
1050         function setupElement( el, style )\r
1051         {\r
1052                 var def = style._.definition;\r
1053                 var attributes = def.attributes;\r
1054                 var styles = CKEDITOR.style.getStyleText( def );\r
1055 \r
1056                 // Assign all defined attributes.\r
1057                 if ( attributes )\r
1058                 {\r
1059                         for ( var att in attributes )\r
1060                         {\r
1061                                 el.setAttribute( att, attributes[ att ] );\r
1062                         }\r
1063                 }\r
1064 \r
1065                 // Assign all defined styles.\r
1066                 if ( styles )\r
1067                         el.setAttribute( 'style', styles );\r
1068 \r
1069                 return el;\r
1070         }\r
1071 \r
1072         var varRegex = /#\((.+?)\)/g;\r
1073         function replaceVariables( list, variablesValues )\r
1074         {\r
1075                 for ( var item in list )\r
1076                 {\r
1077                         list[ item ] = list[ item ].replace( varRegex, function( match, varName )\r
1078                                 {\r
1079                                         return variablesValues[ varName ];\r
1080                                 });\r
1081                 }\r
1082         }\r
1083 \r
1084 \r
1085         // Returns an object that can be used for style matching comparison.\r
1086         // Attributes names and values are all lowercased, and the styles get\r
1087         // merged with the style attribute.\r
1088         function getAttributesForComparison( styleDefinition )\r
1089         {\r
1090                 // If we have already computed it, just return it.\r
1091                 var attribs = styleDefinition._AC;\r
1092                 if ( attribs )\r
1093                         return attribs;\r
1094 \r
1095                 attribs = {};\r
1096 \r
1097                 var length = 0;\r
1098 \r
1099                 // Loop through all defined attributes.\r
1100                 var styleAttribs = styleDefinition.attributes;\r
1101                 if ( styleAttribs )\r
1102                 {\r
1103                         for ( var styleAtt in styleAttribs )\r
1104                         {\r
1105                                 length++;\r
1106                                 attribs[ styleAtt ] = styleAttribs[ styleAtt ];\r
1107                         }\r
1108                 }\r
1109 \r
1110                 // Includes the style definitions.\r
1111                 var styleText = CKEDITOR.style.getStyleText( styleDefinition );\r
1112                 if ( styleText )\r
1113                 {\r
1114                         if ( !attribs[ 'style' ] )\r
1115                                 length++;\r
1116                         attribs[ 'style' ] = styleText;\r
1117                 }\r
1118 \r
1119                 // Appends the "length" information to the object.\r
1120                 attribs._length = length;\r
1121 \r
1122                 // Return it, saving it to the next request.\r
1123                 return ( styleDefinition._AC = attribs );\r
1124         }\r
1125 \r
1126         /**\r
1127          * Get the the collection used to compare the elements and attributes,\r
1128          * defined in this style overrides, with other element. All information in\r
1129          * it is lowercased.\r
1130          * @param {CKEDITOR.style} style\r
1131          */\r
1132         function getOverrides( style )\r
1133         {\r
1134                 if ( style._.overrides )\r
1135                         return style._.overrides;\r
1136 \r
1137                 var overrides = ( style._.overrides = {} ),\r
1138                         definition = style._.definition.overrides;\r
1139 \r
1140                 if ( definition )\r
1141                 {\r
1142                         // The override description can be a string, object or array.\r
1143                         // Internally, well handle arrays only, so transform it if needed.\r
1144                         if ( !CKEDITOR.tools.isArray( definition ) )\r
1145                                 definition = [ definition ];\r
1146 \r
1147                         // Loop through all override definitions.\r
1148                         for ( var i = 0 ; i < definition.length ; i++ )\r
1149                         {\r
1150                                 var override = definition[i];\r
1151                                 var elementName;\r
1152                                 var overrideEl;\r
1153                                 var attrs;\r
1154 \r
1155                                 // If can be a string with the element name.\r
1156                                 if ( typeof override == 'string' )\r
1157                                         elementName = override.toLowerCase();\r
1158                                 // Or an object.\r
1159                                 else\r
1160                                 {\r
1161                                         elementName = override.element ? override.element.toLowerCase() : style.element;\r
1162                                         attrs = override.attributes;\r
1163                                 }\r
1164 \r
1165                                 // We can have more than one override definition for the same\r
1166                                 // element name, so we attempt to simply append information to\r
1167                                 // it if it already exists.\r
1168                                 overrideEl = overrides[ elementName ] || ( overrides[ elementName ] = {} );\r
1169 \r
1170                                 if ( attrs )\r
1171                                 {\r
1172                                         // The returning attributes list is an array, because we\r
1173                                         // could have different override definitions for the same\r
1174                                         // attribute name.\r
1175                                         var overrideAttrs = ( overrideEl.attributes = overrideEl.attributes || new Array() );\r
1176                                         for ( var attName in attrs )\r
1177                                         {\r
1178                                                 // Each item in the attributes array is also an array,\r
1179                                                 // where [0] is the attribute name and [1] is the\r
1180                                                 // override value.\r
1181                                                 overrideAttrs.push( [ attName.toLowerCase(), attrs[ attName ] ] );\r
1182                                         }\r
1183                                 }\r
1184                         }\r
1185                 }\r
1186 \r
1187                 return overrides;\r
1188         }\r
1189 \r
1190         function normalizeCssText( unparsedCssText, nativeNormalize )\r
1191         {\r
1192                 var styleText;\r
1193                 if ( nativeNormalize !== false )\r
1194                 {\r
1195                         // Injects the style in a temporary span object, so the browser parses it,\r
1196                         // retrieving its final format.\r
1197                         var temp = new CKEDITOR.dom.element( 'span' );\r
1198                         temp.setAttribute( 'style', unparsedCssText );\r
1199                         styleText = temp.getAttribute( 'style' ) || '';\r
1200                 }\r
1201                 else\r
1202                         styleText = unparsedCssText;\r
1203 \r
1204                 // Shrinking white-spaces around colon and semi-colon (#4147).\r
1205                 // Compensate tail semi-colon.\r
1206                 return styleText.replace( /\s*([;:])\s*/, '$1' )\r
1207                                                          .replace( /([^\s;])$/, '$1;')\r
1208                                                          .replace( /,\s+/g, ',' ) // Trimming spaces after comma (e.g. font-family name)(#4107).\r
1209                                                          .toLowerCase();\r
1210         }\r
1211 \r
1212         function applyStyle( document, remove )\r
1213         {\r
1214                 // Get all ranges from the selection.\r
1215                 var selection = document.getSelection();\r
1216                 var ranges = selection.getRanges();\r
1217                 var func = remove ? this.removeFromRange : this.applyToRange;\r
1218 \r
1219                 // Apply the style to the ranges.\r
1220                 for ( var i = 0 ; i < ranges.length ; i++ )\r
1221                         func.call( this, ranges[ i ] );\r
1222 \r
1223                 // Select the ranges again.\r
1224                 selection.selectRanges( ranges );\r
1225         }\r
1226 })();\r
1227 \r
1228 CKEDITOR.styleCommand = function( style )\r
1229 {\r
1230         this.style = style;\r
1231 };\r
1232 \r
1233 CKEDITOR.styleCommand.prototype.exec = function( editor )\r
1234 {\r
1235         editor.focus();\r
1236 \r
1237         var doc = editor.document;\r
1238 \r
1239         if ( doc )\r
1240         {\r
1241                 if ( this.state == CKEDITOR.TRISTATE_OFF )\r
1242                         this.style.apply( doc );\r
1243                 else if ( this.state == CKEDITOR.TRISTATE_ON )\r
1244                         this.style.remove( doc );\r
1245         }\r
1246 \r
1247         return !!doc;\r
1248 };\r
1249 \r
1250 CKEDITOR.stylesSet = new CKEDITOR.resourceManager( '', 'stylesSet' );\r
1251 \r
1252 // Backward compatibility (#5025).\r
1253 CKEDITOR.addStylesSet = CKEDITOR.tools.bind( CKEDITOR.stylesSet.add, CKEDITOR.stylesSet );\r
1254 CKEDITOR.loadStylesSet = function( name, url, callback )\r
1255         {\r
1256                 CKEDITOR.stylesSet.addExternal( name, url, '' );\r
1257                 CKEDITOR.stylesSet.load( name, callback );\r
1258         };\r