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