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