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