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