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