JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4b
[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 occurrence 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,th:1,ul:1,dl:1,dt:1,dd:1,form: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                                                 : this.type == CKEDITOR.STYLE_OBJECT ?\r
131                                                         applyObjectStyle\r
132                                                 : null ).call( this, range );\r
133                 },\r
134 \r
135                 removeFromRange : function( range )\r
136                 {\r
137                         return ( this.removeFromRange =\r
138                                                 this.type == CKEDITOR.STYLE_INLINE ?\r
139                                                         removeInlineStyle\r
140                                                 : null ).call( this, range );\r
141                 },\r
142 \r
143                 applyToObject : function( element )\r
144                 {\r
145                         setupElement( element, this );\r
146                 },\r
147 \r
148                 /**\r
149                  * Get the style state inside an element path. Returns "true" if the\r
150                  * element is active in the path.\r
151                  */\r
152                 checkActive : function( elementPath )\r
153                 {\r
154                         switch ( this.type )\r
155                         {\r
156                                 case CKEDITOR.STYLE_BLOCK :\r
157                                         return this.checkElementRemovable( elementPath.block || elementPath.blockLimit, true );\r
158 \r
159                                 case CKEDITOR.STYLE_OBJECT :\r
160                                 case CKEDITOR.STYLE_INLINE :\r
161 \r
162                                         var elements = elementPath.elements;\r
163 \r
164                                         for ( var i = 0, element ; i < elements.length ; i++ )\r
165                                         {\r
166                                                 element = elements[ i ];\r
167 \r
168                                                 if ( this.type == CKEDITOR.STYLE_INLINE\r
169                                                           && ( element == elementPath.block || element == elementPath.blockLimit ) )\r
170                                                         continue;\r
171 \r
172                                                 if( this.type == CKEDITOR.STYLE_OBJECT\r
173                                                          && !( element.getName() in objectElements ) )\r
174                                                                 continue;\r
175 \r
176                                                 if ( this.checkElementRemovable( element, true ) )\r
177                                                         return true;\r
178                                         }\r
179                         }\r
180                         return false;\r
181                 },\r
182 \r
183                 /**\r
184                  * Whether this style can be applied at the element path.\r
185                  * @param elementPath\r
186                  */\r
187                 checkApplicable : function( elementPath )\r
188                 {\r
189                         switch ( this.type )\r
190                         {\r
191                                 case CKEDITOR.STYLE_INLINE :\r
192                                 case CKEDITOR.STYLE_BLOCK :\r
193                                         break;\r
194 \r
195                                 case CKEDITOR.STYLE_OBJECT :\r
196                                         return elementPath.lastElement.getAscendant( this.element, true );\r
197                         }\r
198 \r
199                         return true;\r
200                 },\r
201 \r
202                 // Checks if an element, or any of its attributes, is removable by the\r
203                 // current style definition.\r
204                 checkElementRemovable : function( element, fullMatch )\r
205                 {\r
206                         if ( !element )\r
207                                 return false;\r
208 \r
209                         var def = this._.definition,\r
210                                 attribs;\r
211 \r
212                         // If the element name is the same as the style name.\r
213                         if ( element.getName() == this.element )\r
214                         {\r
215                                 // If no attributes are defined in the element.\r
216                                 if ( !fullMatch && !element.hasAttributes() )\r
217                                         return true;\r
218 \r
219                                 attribs = getAttributesForComparison( def );\r
220 \r
221                                 if ( attribs._length )\r
222                                 {\r
223                                         for ( var attName in attribs )\r
224                                         {\r
225                                                 if ( attName == '_length' )\r
226                                                         continue;\r
227 \r
228                                                 var elementAttr = element.getAttribute( attName ) || '';\r
229 \r
230                                                 // Special treatment for 'style' attribute is required.\r
231                                                 if ( attName == 'style' ?\r
232                                                         compareCssText( attribs[ attName ], normalizeCssText( elementAttr, false ) )\r
233                                                         : attribs[ attName ] == elementAttr  )\r
234                                                 {\r
235                                                         if ( !fullMatch )\r
236                                                                 return true;\r
237                                                 }\r
238                                                 else if ( fullMatch )\r
239                                                                 return false;\r
240                                         }\r
241                                         if ( fullMatch )\r
242                                                 return true;\r
243                                 }\r
244                                 else\r
245                                         return true;\r
246                         }\r
247 \r
248                         // Check if the element can be somehow overriden.\r
249                         var override = getOverrides( this )[ element.getName() ] ;\r
250                         if ( override )\r
251                         {\r
252                                 // If no attributes have been defined, remove the element.\r
253                                 if ( !( attribs = override.attributes ) )\r
254                                         return true;\r
255 \r
256                                 for ( var i = 0 ; i < attribs.length ; i++ )\r
257                                 {\r
258                                         attName = attribs[i][0];\r
259                                         var actualAttrValue = element.getAttribute( attName );\r
260                                         if ( actualAttrValue )\r
261                                         {\r
262                                                 var attValue = attribs[i][1];\r
263 \r
264                                                 // Remove the attribute if:\r
265                                                 //    - The override definition value is null;\r
266                                                 //    - The override definition value is a string that\r
267                                                 //      matches the attribute value exactly.\r
268                                                 //    - The override definition value is a regex that\r
269                                                 //      has matches in the attribute value.\r
270                                                 if ( attValue === null ||\r
271                                                                 ( typeof attValue == 'string' && actualAttrValue == attValue ) ||\r
272                                                                 attValue.test( actualAttrValue ) )\r
273                                                         return true;\r
274                                         }\r
275                                 }\r
276                         }\r
277                         return false;\r
278                 },\r
279 \r
280                 // Builds the preview HTML based on the styles definition.\r
281                 buildPreview : function()\r
282                 {\r
283                         var styleDefinition = this._.definition,\r
284                                 html = [],\r
285                                 elementName = styleDefinition.element;\r
286 \r
287                         // Avoid <bdo> in the preview.\r
288                         if ( elementName == 'bdo' )\r
289                                 elementName = 'span';\r
290 \r
291                         html = [ '<', elementName ];\r
292 \r
293                         // Assign all defined attributes.\r
294                         var attribs     = styleDefinition.attributes;\r
295                         if ( attribs )\r
296                         {\r
297                                 for ( var att in attribs )\r
298                                 {\r
299                                         html.push( ' ', att, '="', attribs[ att ], '"' );\r
300                                 }\r
301                         }\r
302 \r
303                         // Assign the style attribute.\r
304                         var cssStyle = CKEDITOR.style.getStyleText( styleDefinition );\r
305                         if ( cssStyle )\r
306                                 html.push( ' style="', cssStyle, '"' );\r
307 \r
308                         html.push( '>', styleDefinition.name, '</', elementName, '>' );\r
309 \r
310                         return html.join( '' );\r
311                 }\r
312         };\r
313 \r
314         // Build the cssText based on the styles definition.\r
315         CKEDITOR.style.getStyleText = function( styleDefinition )\r
316         {\r
317                 // If we have already computed it, just return it.\r
318                 var stylesDef = styleDefinition._ST;\r
319                 if ( stylesDef )\r
320                         return stylesDef;\r
321 \r
322                 stylesDef = styleDefinition.styles;\r
323 \r
324                 // Builds the StyleText.\r
325                 var stylesText = ( styleDefinition.attributes && styleDefinition.attributes[ 'style' ] ) || '',\r
326                                 specialStylesText = '';\r
327 \r
328                 if ( stylesText.length )\r
329                         stylesText = stylesText.replace( semicolonFixRegex, ';' );\r
330 \r
331                 for ( var style in stylesDef )\r
332                 {\r
333                         var styleVal = stylesDef[ style ],\r
334                                         text = ( style + ':' + styleVal ).replace( semicolonFixRegex, ';' );\r
335 \r
336                         // Some browsers don't support 'inherit' property value, leave them intact. (#5242)\r
337                         if ( styleVal == 'inherit' )\r
338                                 specialStylesText += text;\r
339                         else\r
340                                 stylesText += text;\r
341                 }\r
342 \r
343                 // Browsers make some changes to the style when applying them. So, here\r
344                 // we normalize it to the browser format.\r
345                 if ( stylesText.length )\r
346                         stylesText = normalizeCssText( stylesText );\r
347 \r
348                 stylesText += specialStylesText;\r
349 \r
350                 // Return it, saving it to the next request.\r
351                 return ( styleDefinition._ST = stylesText );\r
352         };\r
353 \r
354         function applyInlineStyle( range )\r
355         {\r
356                 var document = range.document;\r
357 \r
358                 if ( range.collapsed )\r
359                 {\r
360                         // Create the element to be inserted in the DOM.\r
361                         var collapsedElement = getElement( this, document );\r
362 \r
363                         // Insert the empty element into the DOM at the range position.\r
364                         range.insertNode( collapsedElement );\r
365 \r
366                         // Place the selection right inside the empty element.\r
367                         range.moveToPosition( collapsedElement, CKEDITOR.POSITION_BEFORE_END );\r
368 \r
369                         return;\r
370                 }\r
371 \r
372                 var elementName = this.element;\r
373                 var def = this._.definition;\r
374                 var isUnknownElement;\r
375 \r
376                 // Get the DTD definition for the element. Defaults to "span".\r
377                 var dtd = CKEDITOR.dtd[ elementName ] || ( isUnknownElement = true, CKEDITOR.dtd.span );\r
378 \r
379                 // Expand the range.\r
380                 range.enlarge( CKEDITOR.ENLARGE_ELEMENT );\r
381                 range.trim();\r
382 \r
383                 // Get the first node to be processed and the last, which concludes the\r
384                 // processing.\r
385                 var boundaryNodes = range.createBookmark(),\r
386                         firstNode = boundaryNodes.startNode,\r
387                         lastNode = boundaryNodes.endNode;\r
388 \r
389                 var currentNode = firstNode;\r
390 \r
391                 var styleRange;\r
392 \r
393                 while ( currentNode )\r
394                 {\r
395                         var applyStyle = false;\r
396 \r
397                         if ( currentNode.equals( lastNode ) )\r
398                         {\r
399                                 currentNode = null;\r
400                                 applyStyle = true;\r
401                         }\r
402                         else\r
403                         {\r
404                                 var nodeType = currentNode.type;\r
405                                 var nodeName = nodeType == CKEDITOR.NODE_ELEMENT ? currentNode.getName() : null;\r
406 \r
407                                 if ( nodeName && currentNode.getAttribute( '_fck_bookmark' ) )\r
408                                 {\r
409                                         currentNode = currentNode.getNextSourceNode( true );\r
410                                         continue;\r
411                                 }\r
412 \r
413                                 // Check if the current node can be a child of the style element.\r
414                                 if ( !nodeName || ( dtd[ nodeName ]\r
415                                         && ( currentNode.getPosition( lastNode ) | CKEDITOR.POSITION_PRECEDING | CKEDITOR.POSITION_IDENTICAL | CKEDITOR.POSITION_IS_CONTAINED ) == ( CKEDITOR.POSITION_PRECEDING + CKEDITOR.POSITION_IDENTICAL + CKEDITOR.POSITION_IS_CONTAINED )\r
416                                         && ( !def.childRule || def.childRule( currentNode ) ) ) )\r
417                                 {\r
418                                         var currentParent = currentNode.getParent();\r
419 \r
420                                         // Check if the style element can be a child of the current\r
421                                         // node parent or if the element is not defined in the DTD.\r
422                                         if ( currentParent\r
423                                                 && ( ( currentParent.getDtd() || CKEDITOR.dtd.span )[ elementName ] || isUnknownElement )\r
424                                                 && ( !def.parentRule || def.parentRule( currentParent ) ) )\r
425                                         {\r
426                                                 // This node will be part of our range, so if it has not\r
427                                                 // been started, place its start right before the node.\r
428                                                 // In the case of an element node, it will be included\r
429                                                 // only if it is entirely inside the range.\r
430                                                 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
431                                                 {\r
432                                                         styleRange = new CKEDITOR.dom.range( document );\r
433                                                         styleRange.setStartBefore( currentNode );\r
434                                                 }\r
435 \r
436                                                 // Non element nodes, or empty elements can be added\r
437                                                 // completely to the range.\r
438                                                 if ( nodeType == CKEDITOR.NODE_TEXT || ( nodeType == CKEDITOR.NODE_ELEMENT && !currentNode.getChildCount() ) )\r
439                                                 {\r
440                                                         var includedNode = currentNode;\r
441                                                         var parentNode;\r
442 \r
443                                                         // This node is about to be included completelly, but,\r
444                                                         // if this is the last node in its parent, we must also\r
445                                                         // check if the parent itself can be added completelly\r
446                                                         // to the range.\r
447                                                         while ( !includedNode.$.nextSibling\r
448                                                                 && ( parentNode = includedNode.getParent(), dtd[ parentNode.getName() ] )\r
449                                                                 && ( parentNode.getPosition( firstNode ) | CKEDITOR.POSITION_FOLLOWING | CKEDITOR.POSITION_IDENTICAL | CKEDITOR.POSITION_IS_CONTAINED ) == ( CKEDITOR.POSITION_FOLLOWING + CKEDITOR.POSITION_IDENTICAL + CKEDITOR.POSITION_IS_CONTAINED )\r
450                                                                 && ( !def.childRule || def.childRule( parentNode ) ) )\r
451                                                         {\r
452                                                                 includedNode = parentNode;\r
453                                                         }\r
454 \r
455                                                         styleRange.setEndAfter( includedNode );\r
456 \r
457                                                         // If the included node still is the last node in its\r
458                                                         // parent, it means that the parent can't be included\r
459                                                         // in this style DTD, so apply the style immediately.\r
460                                                         if ( !includedNode.$.nextSibling )\r
461                                                                 applyStyle = true;\r
462 \r
463                                                 }\r
464                                         }\r
465                                         else\r
466                                                 applyStyle = true;\r
467                                 }\r
468                                 else\r
469                                         applyStyle = true;\r
470 \r
471                                 // Get the next node to be processed.\r
472                                 currentNode = currentNode.getNextSourceNode();\r
473                         }\r
474 \r
475                         // Apply the style if we have something to which apply it.\r
476                         if ( applyStyle && styleRange && !styleRange.collapsed )\r
477                         {\r
478                                 // Build the style element, based on the style object definition.\r
479                                 var styleNode = getElement( this, document );\r
480 \r
481                                 // Get the element that holds the entire range.\r
482                                 var parent = styleRange.getCommonAncestor();\r
483 \r
484                                 // Loop through the parents, removing the redundant attributes\r
485                                 // from the element to be applied.\r
486                                 while ( styleNode && parent )\r
487                                 {\r
488                                         if ( parent.getName() == elementName )\r
489                                         {\r
490                                                 for ( var attName in def.attributes )\r
491                                                 {\r
492                                                         if ( styleNode.getAttribute( attName ) == parent.getAttribute( attName ) )\r
493                                                                 styleNode.removeAttribute( attName );\r
494                                                 }\r
495 \r
496                                                 for ( var styleName in def.styles )\r
497                                                 {\r
498                                                         if ( styleNode.getStyle( styleName ) == parent.getStyle( styleName ) )\r
499                                                                 styleNode.removeStyle( styleName );\r
500                                                 }\r
501 \r
502                                                 if ( !styleNode.hasAttributes() )\r
503                                                 {\r
504                                                         styleNode = null;\r
505                                                         break;\r
506                                                 }\r
507                                         }\r
508 \r
509                                         parent = parent.getParent();\r
510                                 }\r
511 \r
512                                 if ( styleNode )\r
513                                 {\r
514                                         // Move the contents of the range to the style element.\r
515                                         styleRange.extractContents().appendTo( styleNode );\r
516 \r
517                                         // Here we do some cleanup, removing all duplicated\r
518                                         // elements from the style element.\r
519                                         removeFromInsideElement( this, styleNode );\r
520 \r
521                                         // Insert it into the range position (it is collapsed after\r
522                                         // extractContents.\r
523                                         styleRange.insertNode( styleNode );\r
524 \r
525                                         // Let's merge our new style with its neighbors, if possible.\r
526                                         styleNode.mergeSiblings();\r
527 \r
528                                         // As the style system breaks text nodes constantly, let's normalize\r
529                                         // things for performance.\r
530                                         // With IE, some paragraphs get broken when calling normalize()\r
531                                         // repeatedly. Also, for IE, we must normalize body, not documentElement.\r
532                                         // IE is also known for having a "crash effect" with normalize().\r
533                                         // We should try to normalize with IE too in some way, somewhere.\r
534                                         if ( !CKEDITOR.env.ie )\r
535                                                 styleNode.$.normalize();\r
536                                 }\r
537 \r
538                                 // Style applied, let's release the range, so it gets\r
539                                 // re-initialization in the next loop.\r
540                                 styleRange = null;\r
541                         }\r
542                 }\r
543 \r
544                 // Remove the bookmark nodes.\r
545                 range.moveToBookmark( boundaryNodes );\r
546 \r
547                 // Minimize the result range to exclude empty text nodes. (#5374)\r
548                 range.shrink( CKEDITOR.SHRINK_TEXT );\r
549         }\r
550 \r
551         function removeInlineStyle( range )\r
552         {\r
553                 /*\r
554                  * Make sure our range has included all "collpased" parent inline nodes so\r
555                  * that our operation logic can be simpler.\r
556                  */\r
557                 range.enlarge( CKEDITOR.ENLARGE_ELEMENT );\r
558 \r
559                 var bookmark = range.createBookmark(),\r
560                         startNode = bookmark.startNode;\r
561 \r
562                 if ( range.collapsed )\r
563                 {\r
564 \r
565                         var startPath = new CKEDITOR.dom.elementPath( startNode.getParent() ),\r
566                                 // The topmost element in elementspatch which we should jump out of.\r
567                                 boundaryElement;\r
568 \r
569 \r
570                         for ( var i = 0, element ; i < startPath.elements.length\r
571                                         && ( element = startPath.elements[i] ) ; i++ )\r
572                         {\r
573                                 /*\r
574                                  * 1. If it's collaped inside text nodes, try to remove the style from the whole element.\r
575                                  *\r
576                                  * 2. Otherwise if it's collapsed on element boundaries, moving the selection\r
577                                  *  outside the styles instead of removing the whole tag,\r
578                                  *  also make sure other inner styles were well preserverd.(#3309)\r
579                                  */\r
580                                 if ( element == startPath.block || element == startPath.blockLimit )\r
581                                         break;\r
582 \r
583                                 if ( this.checkElementRemovable( element ) )\r
584                                 {\r
585                                         var isStart;\r
586 \r
587                                         if ( range.collapsed && (\r
588                                                  range.checkBoundaryOfElement( element, CKEDITOR.END ) ||\r
589                                                  ( isStart = range.checkBoundaryOfElement( element, CKEDITOR.START ) ) ) )\r
590                                         {\r
591                                                 boundaryElement = element;\r
592                                                 boundaryElement.match = isStart ? 'start' : 'end';\r
593                                         }\r
594                                         else\r
595                                         {\r
596                                                 /*\r
597                                                  * Before removing the style node, there may be a sibling to the style node\r
598                                                  * that's exactly the same to the one to be removed. To the user, it makes\r
599                                                  * no difference that they're separate entities in the DOM tree. So, merge\r
600                                                  * them before removal.\r
601                                                  */\r
602                                                 element.mergeSiblings();\r
603                                                 removeFromElement( this, element );\r
604 \r
605                                         }\r
606                                 }\r
607                         }\r
608 \r
609                         // Re-create the style tree after/before the boundary element,\r
610                         // the replication start from bookmark start node to define the\r
611                         // new range.\r
612                         if ( boundaryElement )\r
613                         {\r
614                                 var clonedElement = startNode;\r
615                                 for ( i = 0 ;; i++ )\r
616                                 {\r
617                                         var newElement = startPath.elements[ i ];\r
618                                         if ( newElement.equals( boundaryElement ) )\r
619                                                 break;\r
620                                         // Avoid copying any matched element.\r
621                                         else if ( newElement.match )\r
622                                                 continue;\r
623                                         else\r
624                                                 newElement = newElement.clone();\r
625                                         newElement.append( clonedElement );\r
626                                         clonedElement = newElement;\r
627                                 }\r
628                                 clonedElement[ boundaryElement.match == 'start' ?\r
629                                                         'insertBefore' : 'insertAfter' ]( boundaryElement );\r
630                         }\r
631                 }\r
632                 else\r
633                 {\r
634                         /*\r
635                          * Now our range isn't collapsed. Lets walk from the start node to the end\r
636                          * node via DFS and remove the styles one-by-one.\r
637                          */\r
638                         var endNode = bookmark.endNode,\r
639                                 me = this;\r
640 \r
641                         /*\r
642                          * Find out the style ancestor that needs to be broken down at startNode\r
643                          * and endNode.\r
644                          */\r
645                         function breakNodes()\r
646                         {\r
647                                 var startPath = new CKEDITOR.dom.elementPath( startNode.getParent() ),\r
648                                         endPath = new CKEDITOR.dom.elementPath( endNode.getParent() ),\r
649                                         breakStart = null,\r
650                                         breakEnd = null;\r
651                                 for ( var i = 0 ; i < startPath.elements.length ; i++ )\r
652                                 {\r
653                                         var element = startPath.elements[ i ];\r
654 \r
655                                         if ( element == startPath.block || element == startPath.blockLimit )\r
656                                                 break;\r
657 \r
658                                         if ( me.checkElementRemovable( element ) )\r
659                                                 breakStart = element;\r
660                                 }\r
661                                 for ( i = 0 ; i < endPath.elements.length ; i++ )\r
662                                 {\r
663                                         element = endPath.elements[ i ];\r
664 \r
665                                         if ( element == endPath.block || element == endPath.blockLimit )\r
666                                                 break;\r
667 \r
668                                         if ( me.checkElementRemovable( element ) )\r
669                                                 breakEnd = element;\r
670                                 }\r
671 \r
672                                 if ( breakEnd )\r
673                                         endNode.breakParent( breakEnd );\r
674                                 if ( breakStart )\r
675                                         startNode.breakParent( breakStart );\r
676                         }\r
677                         breakNodes();\r
678 \r
679                         // Now, do the DFS walk.\r
680                         var currentNode = startNode.getNext();\r
681                         while ( !currentNode.equals( endNode ) )\r
682                         {\r
683                                 /*\r
684                                  * Need to get the next node first because removeFromElement() can remove\r
685                                  * the current node from DOM tree.\r
686                                  */\r
687                                 var nextNode = currentNode.getNextSourceNode();\r
688                                 if ( currentNode.type == CKEDITOR.NODE_ELEMENT && this.checkElementRemovable( currentNode ) )\r
689                                 {\r
690                                         // Remove style from element or overriding element.\r
691                                         if ( currentNode.getName() == this.element )\r
692                                                 removeFromElement( this, currentNode );\r
693                                         else\r
694                                                 removeOverrides( currentNode, getOverrides( this )[ currentNode.getName() ] );\r
695 \r
696                                         /*\r
697                                          * removeFromElement() may have merged the next node with something before\r
698                                          * the startNode via mergeSiblings(). In that case, the nextNode would\r
699                                          * contain startNode and we'll have to call breakNodes() again and also\r
700                                          * reassign the nextNode to something after startNode.\r
701                                          */\r
702                                         if ( nextNode.type == CKEDITOR.NODE_ELEMENT && nextNode.contains( startNode ) )\r
703                                         {\r
704                                                 breakNodes();\r
705                                                 nextNode = startNode.getNext();\r
706                                         }\r
707                                 }\r
708                                 currentNode = nextNode;\r
709                         }\r
710                 }\r
711 \r
712                 range.moveToBookmark( bookmark );\r
713 }\r
714 \r
715         function applyObjectStyle( range )\r
716         {\r
717                 var root = range.getCommonAncestor( true, true ),\r
718                                 element = root.getAscendant( this.element, true );\r
719                 element && setupElement( element, this );\r
720         }\r
721 \r
722         function applyBlockStyle( range )\r
723         {\r
724                 // Serializible bookmarks is needed here since\r
725                 // elements may be merged.\r
726                 var bookmark = range.createBookmark( true );\r
727 \r
728                 var iterator = range.createIterator();\r
729                 iterator.enforceRealBlocks = true;\r
730 \r
731                 // make recognize <br /> tag as a separator in ENTER_BR mode (#5121)\r
732                 if ( this._.enterMode )\r
733                         iterator.enlargeBr = ( this._.enterMode != CKEDITOR.ENTER_BR );\r
734 \r
735                 var block;\r
736                 var doc = range.document;\r
737                 var previousPreBlock;\r
738 \r
739                 while ( ( block = iterator.getNextParagraph() ) )               // Only one =\r
740                 {\r
741                         var newBlock = getElement( this, doc );\r
742                         replaceBlock( block, newBlock );\r
743                 }\r
744 \r
745                 range.moveToBookmark( bookmark );\r
746         }\r
747 \r
748         // Replace the original block with new one, with special treatment\r
749         // for <pre> blocks to make sure content format is well preserved, and merging/splitting adjacent\r
750         // when necessary.(#3188)\r
751         function replaceBlock( block, newBlock )\r
752         {\r
753                 var newBlockIsPre       = newBlock.is( 'pre' );\r
754                 var blockIsPre          = block.is( 'pre' );\r
755 \r
756                 var isToPre     = newBlockIsPre && !blockIsPre;\r
757                 var isFromPre   = !newBlockIsPre && blockIsPre;\r
758 \r
759                 if ( isToPre )\r
760                         newBlock = toPre( block, newBlock );\r
761                 else if ( isFromPre )\r
762                         // Split big <pre> into pieces before start to convert.\r
763                         newBlock = fromPres( splitIntoPres( block ), newBlock );\r
764                 else\r
765                         block.moveChildren( newBlock );\r
766 \r
767                 newBlock.replace( block );\r
768 \r
769                 if ( newBlockIsPre )\r
770                 {\r
771                         // Merge previous <pre> blocks.\r
772                         mergePre( newBlock );\r
773                 }\r
774         }\r
775 \r
776         var nonWhitespaces = CKEDITOR.dom.walker.whitespaces( true );\r
777         /**\r
778          * Merge a <pre> block with a previous sibling if available.\r
779          */\r
780         function mergePre( preBlock )\r
781         {\r
782                 var previousBlock;\r
783                 if ( !( ( previousBlock = preBlock.getPrevious( nonWhitespaces ) )\r
784                                  && previousBlock.is\r
785                                  && previousBlock.is( 'pre') ) )\r
786                         return;\r
787 \r
788                 // Merge the previous <pre> block contents into the current <pre>\r
789                 // block.\r
790                 //\r
791                 // Another thing to be careful here is that currentBlock might contain\r
792                 // a '\n' at the beginning, and previousBlock might contain a '\n'\r
793                 // towards the end. These new lines are not normally displayed but they\r
794                 // become visible after merging.\r
795                 var mergedHtml = replace( previousBlock.getHtml(), /\n$/, '' ) + '\n\n' +\r
796                                 replace( preBlock.getHtml(), /^\n/, '' ) ;\r
797 \r
798                 // Krugle: IE normalizes innerHTML from <pre>, breaking whitespaces.\r
799                 if ( CKEDITOR.env.ie )\r
800                         preBlock.$.outerHTML = '<pre>' + mergedHtml + '</pre>';\r
801                 else\r
802                         preBlock.setHtml( mergedHtml );\r
803 \r
804                 previousBlock.remove();\r
805         }\r
806 \r
807         /**\r
808          * Split into multiple <pre> blocks separated by double line-break.\r
809          * @param preBlock\r
810          */\r
811         function splitIntoPres( preBlock )\r
812         {\r
813                 // Exclude the ones at header OR at tail,\r
814                 // and ignore bookmark content between them.\r
815                 var duoBrRegex = /(\S\s*)\n(?:\s|(<span[^>]+_fck_bookmark.*?\/span>))*\n(?!$)/gi,\r
816                         blockName = preBlock.getName(),\r
817                         splitedHtml = replace( preBlock.getOuterHtml(),\r
818                                 duoBrRegex,\r
819                                 function( match, charBefore, bookmark )\r
820                                 {\r
821                                   return charBefore + '</pre>' + bookmark + '<pre>';\r
822                                 } );\r
823 \r
824                 var pres = [];\r
825                 splitedHtml.replace( /<pre\b.*?>([\s\S]*?)<\/pre>/gi, function( match, preContent ){\r
826                         pres.push( preContent );\r
827                 } );\r
828                 return pres;\r
829         }\r
830 \r
831         // Wrapper function of String::replace without considering of head/tail bookmarks nodes.\r
832         function replace( str, regexp, replacement )\r
833         {\r
834                 var headBookmark = '',\r
835                         tailBookmark = '';\r
836 \r
837                 str = str.replace( /(^<span[^>]+_fck_bookmark.*?\/span>)|(<span[^>]+_fck_bookmark.*?\/span>$)/gi,\r
838                         function( str, m1, m2 ){\r
839                                         m1 && ( headBookmark = m1 );\r
840                                         m2 && ( tailBookmark = m2 );\r
841                                 return '';\r
842                         } );\r
843                 return headBookmark + str.replace( regexp, replacement ) + tailBookmark;\r
844         }\r
845         /**\r
846          * Converting a list of <pre> into blocks with format well preserved.\r
847          */\r
848         function fromPres( preHtmls, newBlock )\r
849         {\r
850                 var docFrag = new CKEDITOR.dom.documentFragment( newBlock.getDocument() );\r
851                 for ( var i = 0 ; i < preHtmls.length ; i++ )\r
852                 {\r
853                         var blockHtml = preHtmls[ i ];\r
854 \r
855                         // 1. Trim the first and last line-breaks immediately after and before <pre>,\r
856                         // they're not visible.\r
857                          blockHtml =  blockHtml.replace( /(\r\n|\r)/g, '\n' ) ;\r
858                          blockHtml = replace(  blockHtml, /^[ \t]*\n/, '' ) ;\r
859                          blockHtml = replace(  blockHtml, /\n$/, '' ) ;\r
860                         // 2. Convert spaces or tabs at the beginning or at the end to &nbsp;\r
861                          blockHtml = replace(  blockHtml, /^[ \t]+|[ \t]+$/g, function( match, offset, s )\r
862                                         {\r
863                                                 if ( match.length == 1 )        // one space, preserve it\r
864                                                         return '&nbsp;' ;\r
865                                                 else if ( !offset )             // beginning of block\r
866                                                         return CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 ) + ' ';\r
867                                                 else                            // end of block\r
868                                                         return ' ' + CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 );\r
869                                         } ) ;\r
870 \r
871                         // 3. Convert \n to <BR>.\r
872                         // 4. Convert contiguous (i.e. non-singular) spaces or tabs to &nbsp;\r
873                          blockHtml =  blockHtml.replace( /\n/g, '<br>' ) ;\r
874                          blockHtml =  blockHtml.replace( /[ \t]{2,}/g,\r
875                                         function ( match )\r
876                                         {\r
877                                                 return CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 ) + ' ' ;\r
878                                         } ) ;\r
879 \r
880                         var newBlockClone = newBlock.clone();\r
881                         newBlockClone.setHtml(  blockHtml );\r
882                         docFrag.append( newBlockClone );\r
883                 }\r
884                 return docFrag;\r
885         }\r
886 \r
887         /**\r
888          * Converting from a non-PRE block to a PRE block in formatting operations.\r
889          */\r
890         function toPre( block, newBlock )\r
891         {\r
892                 // First trim the block content.\r
893                 var preHtml = block.getHtml();\r
894 \r
895                 // 1. Trim head/tail spaces, they're not visible.\r
896                 preHtml = replace( preHtml, /(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g, '' );\r
897                 // 2. Delete ANSI whitespaces immediately before and after <BR> because\r
898                 //    they are not visible.\r
899                 preHtml = preHtml.replace( /[ \t\r\n]*(<br[^>]*>)[ \t\r\n]*/gi, '$1' );\r
900                 // 3. Compress other ANSI whitespaces since they're only visible as one\r
901                 //    single space previously.\r
902                 // 4. Convert &nbsp; to spaces since &nbsp; is no longer needed in <PRE>.\r
903                 preHtml = preHtml.replace( /([ \t\n\r]+|&nbsp;)/g, ' ' );\r
904                 // 5. Convert any <BR /> to \n. This must not be done earlier because\r
905                 //    the \n would then get compressed.\r
906                 preHtml = preHtml.replace( /<br\b[^>]*>/gi, '\n' );\r
907 \r
908                 // Krugle: IE normalizes innerHTML to <pre>, breaking whitespaces.\r
909                 if ( CKEDITOR.env.ie )\r
910                 {\r
911                         var temp = block.getDocument().createElement( 'div' );\r
912                         temp.append( newBlock );\r
913                         newBlock.$.outerHTML =  '<pre>' + preHtml + '</pre>';\r
914                         newBlock = temp.getFirst().remove();\r
915                 }\r
916                 else\r
917                         newBlock.setHtml( preHtml );\r
918 \r
919                 return newBlock;\r
920         }\r
921 \r
922         // Removes a style from an element itself, don't care about its subtree.\r
923         function removeFromElement( style, element )\r
924         {\r
925                 var def = style._.definition,\r
926                         attributes = CKEDITOR.tools.extend( {}, def.attributes, getOverrides( style )[ element.getName() ] ),\r
927                         styles = def.styles,\r
928                         // If the style is only about the element itself, we have to remove the element.\r
929                         removeEmpty = CKEDITOR.tools.isEmpty( attributes ) && CKEDITOR.tools.isEmpty( styles );\r
930 \r
931                 // Remove definition attributes/style from the elemnt.\r
932                 for ( var attName in attributes )\r
933                 {\r
934                         // The 'class' element value must match (#1318).\r
935                         if ( ( attName == 'class' || style._.definition.fullMatch )\r
936                                 && element.getAttribute( attName ) != normalizeProperty( attName, attributes[ attName ] ) )\r
937                                 continue;\r
938                         removeEmpty = element.hasAttribute( attName );\r
939                         element.removeAttribute( attName );\r
940                 }\r
941 \r
942                 for ( var styleName in styles )\r
943                 {\r
944                         // Full match style insist on having fully equivalence. (#5018)\r
945                         if ( style._.definition.fullMatch\r
946                                 && element.getStyle( styleName ) != normalizeProperty( styleName, styles[ styleName ], true ) )\r
947                                 continue;\r
948 \r
949                         removeEmpty = removeEmpty || !!element.getStyle( styleName );\r
950                         element.removeStyle( styleName );\r
951                 }\r
952 \r
953                 removeEmpty && removeNoAttribsElement( element );\r
954         }\r
955 \r
956         // Removes a style from inside an element.\r
957         function removeFromInsideElement( style, element )\r
958         {\r
959                 var def = style._.definition,\r
960                         attribs = def.attributes,\r
961                         styles = def.styles,\r
962                         overrides = getOverrides( style );\r
963 \r
964                 var innerElements = element.getElementsByTag( style.element );\r
965 \r
966                 for ( var i = innerElements.count(); --i >= 0 ; )\r
967                         removeFromElement( style,  innerElements.getItem( i ) );\r
968 \r
969                 // Now remove any other element with different name that is\r
970                 // defined to be overriden.\r
971                 for ( var overrideElement in overrides )\r
972                 {\r
973                         if ( overrideElement != style.element )\r
974                         {\r
975                                 innerElements = element.getElementsByTag( overrideElement ) ;\r
976                                 for ( i = innerElements.count() - 1 ; i >= 0 ; i-- )\r
977                                 {\r
978                                         var innerElement = innerElements.getItem( i );\r
979                                         removeOverrides( innerElement, overrides[ overrideElement ] ) ;\r
980                                 }\r
981                         }\r
982                 }\r
983 \r
984         }\r
985 \r
986         /**\r
987          *  Remove overriding styles/attributes from the specific element.\r
988          *  Note: Remove the element if no attributes remain.\r
989          * @param {Object} element\r
990          * @param {Object} overrides\r
991          */\r
992         function removeOverrides( element, overrides )\r
993         {\r
994                 var attributes = overrides && overrides.attributes ;\r
995 \r
996                 if ( attributes )\r
997                 {\r
998                         for ( var i = 0 ; i < attributes.length ; i++ )\r
999                         {\r
1000                                 var attName = attributes[i][0], actualAttrValue ;\r
1001 \r
1002                                 if ( ( actualAttrValue = element.getAttribute( attName ) ) )\r
1003                                 {\r
1004                                         var attValue = attributes[i][1] ;\r
1005 \r
1006                                         // Remove the attribute if:\r
1007                                         //    - The override definition value is null ;\r
1008                                         //    - The override definition valie is a string that\r
1009                                         //      matches the attribute value exactly.\r
1010                                         //    - The override definition value is a regex that\r
1011                                         //      has matches in the attribute value.\r
1012                                         if ( attValue === null ||\r
1013                                                         ( attValue.test && attValue.test( actualAttrValue ) ) ||\r
1014                                                         ( typeof attValue == 'string' && actualAttrValue == attValue ) )\r
1015                                                 element.removeAttribute( attName ) ;\r
1016                                 }\r
1017                         }\r
1018                 }\r
1019 \r
1020                 removeNoAttribsElement( element );\r
1021         }\r
1022 \r
1023         // If the element has no more attributes, remove it.\r
1024         function removeNoAttribsElement( element )\r
1025         {\r
1026                 // If no more attributes remained in the element, remove it,\r
1027                 // leaving its children.\r
1028                 if ( !element.hasAttributes() )\r
1029                 {\r
1030                         // Removing elements may open points where merging is possible,\r
1031                         // so let's cache the first and last nodes for later checking.\r
1032                         var firstChild  = element.getFirst();\r
1033                         var lastChild   = element.getLast();\r
1034 \r
1035                         element.remove( true );\r
1036 \r
1037                         if ( firstChild )\r
1038                         {\r
1039                                 // Check the cached nodes for merging.\r
1040                                 firstChild.type == CKEDITOR.NODE_ELEMENT && firstChild.mergeSiblings();\r
1041 \r
1042                                 if ( lastChild && !firstChild.equals( lastChild )\r
1043                                         && lastChild.type == CKEDITOR.NODE_ELEMENT  )\r
1044                                         lastChild.mergeSiblings();\r
1045                         }\r
1046                 }\r
1047         }\r
1048 \r
1049         function getElement( style, targetDocument )\r
1050         {\r
1051                 var el;\r
1052 \r
1053                 var def = style._.definition;\r
1054 \r
1055                 var elementName = style.element;\r
1056 \r
1057                 // The "*" element name will always be a span for this function.\r
1058                 if ( elementName == '*' )\r
1059                         elementName = 'span';\r
1060 \r
1061                 // Create the element.\r
1062                 el = new CKEDITOR.dom.element( elementName, targetDocument );\r
1063 \r
1064                 return setupElement( el, style );\r
1065         }\r
1066 \r
1067         function setupElement( el, style )\r
1068         {\r
1069                 var def = style._.definition;\r
1070                 var attributes = def.attributes;\r
1071                 var styles = CKEDITOR.style.getStyleText( def );\r
1072 \r
1073                 // Assign all defined attributes.\r
1074                 if ( attributes )\r
1075                 {\r
1076                         for ( var att in attributes )\r
1077                         {\r
1078                                 el.setAttribute( att, attributes[ att ] );\r
1079                         }\r
1080                 }\r
1081 \r
1082                 // Assign all defined styles.\r
1083                 if ( styles )\r
1084                         el.setAttribute( 'style', styles );\r
1085 \r
1086                 return el;\r
1087         }\r
1088 \r
1089         var varRegex = /#\((.+?)\)/g;\r
1090         function replaceVariables( list, variablesValues )\r
1091         {\r
1092                 for ( var item in list )\r
1093                 {\r
1094                         list[ item ] = list[ item ].replace( varRegex, function( match, varName )\r
1095                                 {\r
1096                                         return variablesValues[ varName ];\r
1097                                 });\r
1098                 }\r
1099         }\r
1100 \r
1101 \r
1102         // Returns an object that can be used for style matching comparison.\r
1103         // Attributes names and values are all lowercased, and the styles get\r
1104         // merged with the style attribute.\r
1105         function getAttributesForComparison( styleDefinition )\r
1106         {\r
1107                 // If we have already computed it, just return it.\r
1108                 var attribs = styleDefinition._AC;\r
1109                 if ( attribs )\r
1110                         return attribs;\r
1111 \r
1112                 attribs = {};\r
1113 \r
1114                 var length = 0;\r
1115 \r
1116                 // Loop through all defined attributes.\r
1117                 var styleAttribs = styleDefinition.attributes;\r
1118                 if ( styleAttribs )\r
1119                 {\r
1120                         for ( var styleAtt in styleAttribs )\r
1121                         {\r
1122                                 length++;\r
1123                                 attribs[ styleAtt ] = styleAttribs[ styleAtt ];\r
1124                         }\r
1125                 }\r
1126 \r
1127                 // Includes the style definitions.\r
1128                 var styleText = CKEDITOR.style.getStyleText( styleDefinition );\r
1129                 if ( styleText )\r
1130                 {\r
1131                         if ( !attribs[ 'style' ] )\r
1132                                 length++;\r
1133                         attribs[ 'style' ] = styleText;\r
1134                 }\r
1135 \r
1136                 // Appends the "length" information to the object.\r
1137                 attribs._length = length;\r
1138 \r
1139                 // Return it, saving it to the next request.\r
1140                 return ( styleDefinition._AC = attribs );\r
1141         }\r
1142 \r
1143         /**\r
1144          * Get the the collection used to compare the elements and attributes,\r
1145          * defined in this style overrides, with other element. All information in\r
1146          * it is lowercased.\r
1147          * @param {CKEDITOR.style} style\r
1148          */\r
1149         function getOverrides( style )\r
1150         {\r
1151                 if ( style._.overrides )\r
1152                         return style._.overrides;\r
1153 \r
1154                 var overrides = ( style._.overrides = {} ),\r
1155                         definition = style._.definition.overrides;\r
1156 \r
1157                 if ( definition )\r
1158                 {\r
1159                         // The override description can be a string, object or array.\r
1160                         // Internally, well handle arrays only, so transform it if needed.\r
1161                         if ( !CKEDITOR.tools.isArray( definition ) )\r
1162                                 definition = [ definition ];\r
1163 \r
1164                         // Loop through all override definitions.\r
1165                         for ( var i = 0 ; i < definition.length ; i++ )\r
1166                         {\r
1167                                 var override = definition[i];\r
1168                                 var elementName;\r
1169                                 var overrideEl;\r
1170                                 var attrs;\r
1171 \r
1172                                 // If can be a string with the element name.\r
1173                                 if ( typeof override == 'string' )\r
1174                                         elementName = override.toLowerCase();\r
1175                                 // Or an object.\r
1176                                 else\r
1177                                 {\r
1178                                         elementName = override.element ? override.element.toLowerCase() : style.element;\r
1179                                         attrs = override.attributes;\r
1180                                 }\r
1181 \r
1182                                 // We can have more than one override definition for the same\r
1183                                 // element name, so we attempt to simply append information to\r
1184                                 // it if it already exists.\r
1185                                 overrideEl = overrides[ elementName ] || ( overrides[ elementName ] = {} );\r
1186 \r
1187                                 if ( attrs )\r
1188                                 {\r
1189                                         // The returning attributes list is an array, because we\r
1190                                         // could have different override definitions for the same\r
1191                                         // attribute name.\r
1192                                         var overrideAttrs = ( overrideEl.attributes = overrideEl.attributes || new Array() );\r
1193                                         for ( var attName in attrs )\r
1194                                         {\r
1195                                                 // Each item in the attributes array is also an array,\r
1196                                                 // where [0] is the attribute name and [1] is the\r
1197                                                 // override value.\r
1198                                                 overrideAttrs.push( [ attName.toLowerCase(), attrs[ attName ] ] );\r
1199                                         }\r
1200                                 }\r
1201                         }\r
1202                 }\r
1203 \r
1204                 return overrides;\r
1205         }\r
1206 \r
1207         // Make the comparison of attribute value easier by standardizing it.\r
1208         function normalizeProperty( name, value, isStyle )\r
1209         {\r
1210                 var temp = new CKEDITOR.dom.element( 'span' );\r
1211                 temp [ isStyle ? 'setStyle' : 'setAttribute' ]( name, value );\r
1212                 return temp[ isStyle ? 'getStyle' : 'getAttribute' ]( name );\r
1213         }\r
1214 \r
1215         // Make the comparison of style text easier by standardizing it.\r
1216         function normalizeCssText( unparsedCssText, nativeNormalize )\r
1217         {\r
1218                 var styleText;\r
1219                 if ( nativeNormalize !== false )\r
1220                 {\r
1221                         // Injects the style in a temporary span object, so the browser parses it,\r
1222                         // retrieving its final format.\r
1223                         var temp = new CKEDITOR.dom.element( 'span' );\r
1224                         temp.setAttribute( 'style', unparsedCssText );\r
1225                         styleText = temp.getAttribute( 'style' ) || '';\r
1226                 }\r
1227                 else\r
1228                         styleText = unparsedCssText;\r
1229 \r
1230                 // Shrinking white-spaces around colon and semi-colon (#4147).\r
1231                 // Compensate tail semi-colon.\r
1232                 return styleText.replace( /\s*([;:])\s*/, '$1' )\r
1233                                                          .replace( /([^\s;])$/, '$1;')\r
1234                                                          .replace( /,\s+/g, ',' ) // Trimming spaces after comma (e.g. font-family name)(#4107).\r
1235                                                          .toLowerCase();\r
1236         }\r
1237 \r
1238         // Turn inline style text properties into one hash.\r
1239         function parseStyleText( styleText )\r
1240         {\r
1241                 var retval = {};\r
1242                 styleText\r
1243                    .replace( /&quot;/g, '"' )\r
1244                    .replace( /\s*([^ :;]+)\s*:\s*([^;]+)\s*(?=;|$)/g, function( match, name, value )\r
1245                 {\r
1246                         retval[ name ] = value;\r
1247                 } );\r
1248                 return retval;\r
1249         }\r
1250 \r
1251         /**\r
1252          * Compare two bunch of styles, with the speciality that value 'inherit'\r
1253          * is treated as a wildcard which will match any value.\r
1254          * @param {Object|String} source\r
1255          * @param {Object|String} target\r
1256          */\r
1257         function compareCssText( source, target )\r
1258         {\r
1259                 typeof source == 'string' && ( source = parseStyleText( source ) );\r
1260                 typeof target == 'string' && ( target = parseStyleText( target ) );\r
1261                 for( var name in source )\r
1262                 {\r
1263                         if ( !( name in target &&\r
1264                                         ( target[ name ] == source[ name ]\r
1265                                                 || source[ name ] == 'inherit'\r
1266                                                 || target[ name ] == 'inherit' ) ) )\r
1267                         {\r
1268                                 return false;\r
1269                         }\r
1270                 }\r
1271                 return true;\r
1272         }\r
1273 \r
1274         function applyStyle( document, remove )\r
1275         {\r
1276                 var selection = document.getSelection(),\r
1277                         // Bookmark the range so we can re-select it after processing.\r
1278                         bookmarks = selection.createBookmarks(),\r
1279                         ranges = selection.getRanges( true ),\r
1280                         func = remove ? this.removeFromRange : this.applyToRange,\r
1281                         range;\r
1282 \r
1283                 var iterator = ranges.createIterator();\r
1284                 while ( ( range = iterator.getNextRange() ) )\r
1285                         func.call( this, range );\r
1286 \r
1287                 if ( bookmarks.length == 1 && bookmarks[0].collapsed )\r
1288                 {\r
1289                         selection.selectRanges( ranges );\r
1290                         bookmarks[0].startNode.remove();\r
1291                 }\r
1292                 else\r
1293                         selection.selectBookmarks( bookmarks );\r
1294         }\r
1295 })();\r
1296 \r
1297 CKEDITOR.styleCommand = function( style )\r
1298 {\r
1299         this.style = style;\r
1300 };\r
1301 \r
1302 CKEDITOR.styleCommand.prototype.exec = function( editor )\r
1303 {\r
1304         editor.focus();\r
1305 \r
1306         var doc = editor.document;\r
1307 \r
1308         if ( doc )\r
1309         {\r
1310                 if ( this.state == CKEDITOR.TRISTATE_OFF )\r
1311                         this.style.apply( doc );\r
1312                 else if ( this.state == CKEDITOR.TRISTATE_ON )\r
1313                         this.style.remove( doc );\r
1314         }\r
1315 \r
1316         return !!doc;\r
1317 };\r
1318 \r
1319 CKEDITOR.stylesSet = new CKEDITOR.resourceManager( '', 'stylesSet' );\r
1320 \r
1321 // Backward compatibility (#5025).\r
1322 CKEDITOR.addStylesSet = CKEDITOR.tools.bind( CKEDITOR.stylesSet.add, CKEDITOR.stylesSet );\r
1323 CKEDITOR.loadStylesSet = function( name, url, callback )\r
1324         {\r
1325                 CKEDITOR.stylesSet.addExternal( name, url, '' );\r
1326                 CKEDITOR.stylesSet.load( name, callback );\r
1327         };\r
1328 \r
1329 \r
1330 /**\r
1331  * Gets the current styleSet for this instance\r
1332  * @param {Function} The function to be called with the styles data.\r
1333  * @example\r
1334  * editor.getStylesSet( function( stylesDefinitions ) {} );\r
1335  */\r
1336 CKEDITOR.editor.prototype.getStylesSet = function( callback )\r
1337 {\r
1338         if ( !this._.stylesDefinitions )\r
1339         {\r
1340                 var editor = this,\r
1341                         // Respect the backwards compatible definition entry\r
1342                         configStyleSet = editor.config.stylesCombo_stylesSet || editor.config.stylesSet || 'default';\r
1343 \r
1344                 // #5352 Allow to define the styles directly in the config object\r
1345                 if ( configStyleSet instanceof Array )\r
1346                 {\r
1347                         editor._.stylesDefinitions = configStyleSet;\r
1348                         callback( configStyleSet );\r
1349                         return;\r
1350                 }\r
1351 \r
1352                 var     partsStylesSet = configStyleSet.split( ':' ),\r
1353                         styleSetName = partsStylesSet[ 0 ],\r
1354                         externalPath = partsStylesSet[ 1 ],\r
1355                         pluginPath = CKEDITOR.plugins.registered.styles.path;\r
1356 \r
1357                 CKEDITOR.stylesSet.addExternal( styleSetName,\r
1358                                 externalPath ?\r
1359                                         partsStylesSet.slice( 1 ).join( ':' ) :\r
1360                                         pluginPath + 'styles/' + styleSetName + '.js', '' );\r
1361 \r
1362                 CKEDITOR.stylesSet.load( styleSetName, function( stylesSet )\r
1363                         {\r
1364                                 editor._.stylesDefinitions = stylesSet[ styleSetName ];\r
1365                                 callback( editor._.stylesDefinitions );\r
1366                         } ) ;\r
1367         }\r
1368         else\r
1369                 callback( this._.stylesDefinitions );\r
1370 };\r
1371 \r
1372 /**\r
1373  * The "styles definition set" to use in the editor. They will be used in the\r
1374  * styles combo and the Style selector of the div container. <br>\r
1375  * The styles may be defined in the page containing the editor, or can be\r
1376  * loaded on demand from an external file. In the second case, if this setting\r
1377  * contains only a name, the styles definition file will be loaded from the\r
1378  * "styles" folder inside the styles plugin folder.\r
1379  * Otherwise, this setting has the "name:url" syntax, making it\r
1380  * possible to set the URL from which loading the styles file.<br>\r
1381  * Previously this setting was available as config.stylesCombo_stylesSet<br>\r
1382  * @name CKEDITOR.config.stylesSet\r
1383  * @type String|Array\r
1384  * @default 'default'\r
1385  * @since 3.3\r
1386  * @example\r
1387  * // Load from the styles' styles folder (mystyles.js file).\r
1388  * config.stylesSet = 'mystyles';\r
1389  * @example\r
1390  * // Load from a relative URL.\r
1391  * config.stylesSet = 'mystyles:/editorstyles/styles.js';\r
1392  * @example\r
1393  * // Load from a full URL.\r
1394  * config.stylesSet = 'mystyles:http://www.example.com/editorstyles/styles.js';\r
1395  * @example\r
1396  * // Load from a list of definitions.\r
1397  * config.stylesSet = [\r
1398  *  { name : 'Strong Emphasis', element : 'strong' },\r
1399  * { name : 'Emphasis', element : 'em' }, ... ];\r
1400  */\r