2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
6 CKEDITOR.plugins.add( 'styles',
\r
8 requires : [ 'selection' ]
\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
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
23 * if ( state == CKEDITOR.TRISTATE_ON )
\r
24 * alert( 'The current state for the B element is ON' );
\r
26 * alert( 'The current state for the B element is OFF' );
\r
29 CKEDITOR.editor.prototype.attachStyleStateChange = function( style, callback )
\r
31 // Try to get the list of attached callbacks.
\r
32 var styleStateChangeCallbacks = this._.styleStateChangeCallbacks;
\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
38 // Create the callbacks array.
\r
39 styleStateChangeCallbacks = this._.styleStateChangeCallbacks = [];
\r
41 // Attach to the selectionChange event, so we can check the styles at
\r
43 this.on( 'selectionChange', function( ev )
\r
45 // Loop throw all registered callbacks.
\r
46 for ( var i = 0 ; i < styleStateChangeCallbacks.length ; i++ )
\r
48 var callback = styleStateChangeCallbacks[ i ];
\r
50 // Check the current state for the style defined for that
\r
52 var currentState = callback.style.checkActive( ev.data.path ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF;
\r
54 // If the state changed since the last check.
\r
55 if ( callback.state !== currentState )
\r
57 // Call the callback function, passing the current
\r
59 callback.fn.call( this, currentState );
\r
61 // Save the current state, so it can be compared next
\r
63 callback.state !== currentState;
\r
69 // Save the callback info, so it can be checked on the next occurence of
\r
71 styleStateChangeCallbacks.push( { style : style, fn : callback } );
\r
74 CKEDITOR.STYLE_BLOCK = 1;
\r
75 CKEDITOR.STYLE_INLINE = 2;
\r
76 CKEDITOR.STYLE_OBJECT = 3;
\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,ul:1 };
\r
83 var semicolonFixRegex = /\s*(?:;\s*|$)/;
\r
85 CKEDITOR.style = function( styleDefinition, variablesValues )
\r
87 if ( variablesValues )
\r
89 styleDefinition = CKEDITOR.tools.clone( styleDefinition );
\r
91 replaceVariables( styleDefinition.attributes, variablesValues );
\r
92 replaceVariables( styleDefinition.styles, variablesValues );
\r
95 var element = this.element = ( styleDefinition.element || '*' ).toLowerCase();
\r
98 ( element == '#' || blockElements[ element ] ) ?
\r
99 CKEDITOR.STYLE_BLOCK
\r
100 : objectElements[ element ] ?
\r
101 CKEDITOR.STYLE_OBJECT
\r
103 CKEDITOR.STYLE_INLINE;
\r
107 definition : styleDefinition
\r
111 CKEDITOR.style.prototype =
\r
113 apply : function( document )
\r
115 applyStyle.call( this, document, false );
\r
118 remove : function( document )
\r
120 applyStyle.call( this, document, true );
\r
123 applyToRange : function( range )
\r
125 return ( this.applyToRange =
\r
126 this.type == CKEDITOR.STYLE_INLINE ?
\r
128 : this.type == CKEDITOR.STYLE_BLOCK ?
\r
130 : null ).call( this, range );
\r
133 removeFromRange : function( range )
\r
135 return ( this.removeFromRange =
\r
136 this.type == CKEDITOR.STYLE_INLINE ?
\r
138 : null ).call( this, range );
\r
141 applyToObject : function( element )
\r
143 setupElement( element, this );
\r
147 * Get the style state inside an element path. Returns "true" if the
\r
148 * element is active in the path.
\r
150 checkActive : function( elementPath )
\r
152 switch ( this.type )
\r
154 case CKEDITOR.STYLE_BLOCK :
\r
155 return this.checkElementRemovable( elementPath.block || elementPath.blockLimit, true );
\r
157 case CKEDITOR.STYLE_INLINE :
\r
159 var elements = elementPath.elements;
\r
161 for ( var i = 0, element ; i < elements.length ; i++ )
\r
163 element = elements[i];
\r
165 if ( element == elementPath.block || element == elementPath.blockLimit )
\r
168 if ( this.checkElementRemovable( element, true ) )
\r
175 // Checks if an element, or any of its attributes, is removable by the
\r
176 // current style definition.
\r
177 checkElementRemovable : function( element, fullMatch )
\r
182 var def = this._.definition,
\r
185 // If the element name is the same as the style name.
\r
186 if ( element.getName() == this.element )
\r
188 // If no attributes are defined in the element.
\r
189 if ( !fullMatch && !element.hasAttributes() )
\r
192 attribs = getAttributesForComparison( def );
\r
194 if ( attribs._length )
\r
196 for ( var attName in attribs )
\r
198 if ( attName == '_length' )
\r
201 var elementAttr = element.getAttribute( attName ) || '';
\r
202 if ( attribs[ attName ] ==
\r
203 ( attName == 'style' ?
\r
204 normalizeCssText( elementAttr, false ) : elementAttr ) )
\r
209 else if ( fullMatch )
\r
219 // Check if the element can be somehow overriden.
\r
220 var override = getOverrides( this )[ element.getName() ] ;
\r
223 // If no attributes have been defined, remove the element.
\r
224 if ( !( attribs = override.attributes ) )
\r
227 for ( var i = 0 ; i < attribs.length ; i++ )
\r
229 attName = attribs[i][0];
\r
230 var actualAttrValue = element.getAttribute( attName );
\r
231 if ( actualAttrValue )
\r
233 var attValue = attribs[i][1];
\r
235 // Remove the attribute if:
\r
236 // - The override definition value is null;
\r
237 // - The override definition value is a string that
\r
238 // matches the attribute value exactly.
\r
239 // - The override definition value is a regex that
\r
240 // has matches in the attribute value.
\r
241 if ( attValue === null ||
\r
242 ( typeof attValue == 'string' && actualAttrValue == attValue ) ||
\r
243 attValue.test( actualAttrValue ) )
\r
252 // Build the cssText based on the styles definition.
\r
253 CKEDITOR.style.getStyleText = function( styleDefinition )
\r
255 // If we have already computed it, just return it.
\r
256 var stylesDef = styleDefinition._ST;
\r
260 stylesDef = styleDefinition.styles;
\r
262 // Builds the StyleText.
\r
264 var stylesText = ( styleDefinition.attributes && styleDefinition.attributes[ 'style' ] ) || '';
\r
266 if ( stylesText.length )
\r
267 stylesText = stylesText.replace( semicolonFixRegex, ';' );
\r
269 for ( var style in stylesDef )
\r
270 stylesText += ( style + ':' + stylesDef[ style ] ).replace( semicolonFixRegex, ';' );
\r
272 // Browsers make some changes to the style when applying them. So, here
\r
273 // we normalize it to the browser format.
\r
274 if ( stylesText.length )
\r
275 stylesText = normalizeCssText( stylesText );
\r
277 // Return it, saving it to the next request.
\r
278 return ( styleDefinition._ST = stylesText );
\r
281 function applyInlineStyle( range )
\r
283 var document = range.document;
\r
285 if ( range.collapsed )
\r
287 // Create the element to be inserted in the DOM.
\r
288 var collapsedElement = getElement( this, document );
\r
290 // Insert the empty element into the DOM at the range position.
\r
291 range.insertNode( collapsedElement );
\r
293 // Place the selection right inside the empty element.
\r
294 range.moveToPosition( collapsedElement, CKEDITOR.POSITION_BEFORE_END );
\r
299 var elementName = this.element;
\r
300 var def = this._.definition;
\r
301 var isUnknownElement;
\r
303 // Get the DTD definition for the element. Defaults to "span".
\r
304 var dtd = CKEDITOR.dtd[ elementName ] || ( isUnknownElement = true, CKEDITOR.dtd.span );
\r
306 // Bookmark the range so we can re-select it after processing.
\r
307 var bookmark = range.createBookmark();
\r
309 // Expand the range.
\r
310 range.enlarge( CKEDITOR.ENLARGE_ELEMENT );
\r
313 // Get the first node to be processed and the last, which concludes the
\r
315 var boundaryNodes = range.getBoundaryNodes();
\r
316 var firstNode = boundaryNodes.startNode;
\r
317 var lastNode = boundaryNodes.endNode.getNextSourceNode( true );
\r
319 // Probably the document end is reached, we need a marker node.
\r
323 lastNode = marker = document.createText( '' );
\r
324 lastNode.insertAfter( range.endContainer );
\r
326 // The detection algorithm below skips the contents inside bookmark nodes, so
\r
327 // we'll need to make sure lastNode isn't the inside a bookmark node.
\r
328 var lastParent = lastNode.getParent();
\r
329 if ( lastParent && lastParent.getAttribute( '_fck_bookmark' ) )
\r
330 lastNode = lastParent;
\r
332 if ( lastNode.equals( firstNode ) )
\r
334 // If the last node is the same as the the first one, we must move
\r
335 // it to the next one, otherwise the first one will not be
\r
337 lastNode = lastNode.getNextSourceNode( true );
\r
339 // It may happen that there are no more nodes after it (the end of
\r
340 // the document), so we must add something there to make our code
\r
344 lastNode = marker = document.createText( '' );
\r
345 lastNode.insertAfter( firstNode );
\r
349 var currentNode = firstNode;
\r
353 // Indicates that that some useful inline content has been found, so
\r
354 // the style should be applied.
\r
357 while ( currentNode )
\r
359 var applyStyle = false;
\r
361 if ( currentNode.equals( lastNode ) )
\r
363 currentNode = null;
\r
368 var nodeType = currentNode.type;
\r
369 var nodeName = nodeType == CKEDITOR.NODE_ELEMENT ? currentNode.getName() : null;
\r
371 if ( nodeName && currentNode.getAttribute( '_fck_bookmark' ) )
\r
373 currentNode = currentNode.getNextSourceNode( true );
\r
377 // Check if the current node can be a child of the style element.
\r
378 if ( !nodeName || ( dtd[ nodeName ]
\r
379 && ( currentNode.getPosition( lastNode ) | CKEDITOR.POSITION_PRECEDING | CKEDITOR.POSITION_IDENTICAL | CKEDITOR.POSITION_IS_CONTAINED ) == ( CKEDITOR.POSITION_PRECEDING + CKEDITOR.POSITION_IDENTICAL + CKEDITOR.POSITION_IS_CONTAINED )
\r
380 && ( !def.childRule || def.childRule( currentNode ) ) ) )
\r
382 var currentParent = currentNode.getParent();
\r
384 // Check if the style element can be a child of the current
\r
385 // node parent or if the element is not defined in the DTD.
\r
387 && ( ( currentParent.getDtd() || CKEDITOR.dtd.span )[ elementName ] || isUnknownElement )
\r
388 && ( !def.parentRule || def.parentRule( currentParent ) ) )
\r
390 // This node will be part of our range, so if it has not
\r
391 // been started, place its start right before the node.
\r
392 // In the case of an element node, it will be included
\r
393 // only if it is entirely inside the range.
\r
394 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
396 styleRange = new CKEDITOR.dom.range( document );
\r
397 styleRange.setStartBefore( currentNode );
\r
400 // Non element nodes, or empty elements can be added
\r
401 // completely to the range.
\r
402 if ( nodeType == CKEDITOR.NODE_TEXT || ( nodeType == CKEDITOR.NODE_ELEMENT && !currentNode.getChildCount() ) )
\r
404 var includedNode = currentNode;
\r
407 // This node is about to be included completelly, but,
\r
408 // if this is the last node in its parent, we must also
\r
409 // check if the parent itself can be added completelly
\r
411 while ( !includedNode.$.nextSibling
\r
412 && ( parentNode = includedNode.getParent(), dtd[ parentNode.getName() ] )
\r
413 && ( parentNode.getPosition( firstNode ) | CKEDITOR.POSITION_FOLLOWING | CKEDITOR.POSITION_IDENTICAL | CKEDITOR.POSITION_IS_CONTAINED ) == ( CKEDITOR.POSITION_FOLLOWING + CKEDITOR.POSITION_IDENTICAL + CKEDITOR.POSITION_IS_CONTAINED )
\r
414 && ( !def.childRule || def.childRule( parentNode ) ) )
\r
416 includedNode = parentNode;
\r
419 styleRange.setEndAfter( includedNode );
\r
421 // If the included node still is the last node in its
\r
422 // parent, it means that the parent can't be included
\r
423 // in this style DTD, so apply the style immediately.
\r
424 if ( !includedNode.$.nextSibling )
\r
427 if ( !hasContents )
\r
428 hasContents = ( nodeType != CKEDITOR.NODE_TEXT || (/[^\s\ufeff]/).test( currentNode.getText() ) );
\r
437 // Get the next node to be processed.
\r
438 currentNode = currentNode.getNextSourceNode();
\r
441 // Apply the style if we have something to which apply it.
\r
442 if ( applyStyle && hasContents && styleRange && !styleRange.collapsed )
\r
444 // Build the style element, based on the style object definition.
\r
445 var styleNode = getElement( this, document );
\r
447 // Get the element that holds the entire range.
\r
448 var parent = styleRange.getCommonAncestor();
\r
450 // Loop through the parents, removing the redundant attributes
\r
451 // from the element to be applied.
\r
452 while ( styleNode && parent )
\r
454 if ( parent.getName() == elementName )
\r
456 for ( var attName in def.attributes )
\r
458 if ( styleNode.getAttribute( attName ) == parent.getAttribute( attName ) )
\r
459 styleNode.removeAttribute( attName );
\r
462 for ( var styleName in def.styles )
\r
464 if ( styleNode.getStyle( styleName ) == parent.getStyle( styleName ) )
\r
465 styleNode.removeStyle( styleName );
\r
468 if ( !styleNode.hasAttributes() )
\r
475 parent = parent.getParent();
\r
480 // Move the contents of the range to the style element.
\r
481 styleRange.extractContents().appendTo( styleNode );
\r
483 // Here we do some cleanup, removing all duplicated
\r
484 // elements from the style element.
\r
485 removeFromInsideElement( this, styleNode );
\r
487 // Insert it into the range position (it is collapsed after
\r
488 // extractContents.
\r
489 styleRange.insertNode( styleNode );
\r
491 // Let's merge our new style with its neighbors, if possible.
\r
492 mergeSiblings( styleNode );
\r
494 // As the style system breaks text nodes constantly, let's normalize
\r
495 // things for performance.
\r
496 // With IE, some paragraphs get broken when calling normalize()
\r
497 // repeatedly. Also, for IE, we must normalize body, not documentElement.
\r
498 // IE is also known for having a "crash effect" with normalize().
\r
499 // We should try to normalize with IE too in some way, somewhere.
\r
500 if ( !CKEDITOR.env.ie )
\r
501 styleNode.$.normalize();
\r
504 // Style applied, let's release the range, so it gets
\r
505 // re-initialization in the next loop.
\r
510 // Remove the temporary marking node.(#4111)
\r
511 marker && marker.remove();
\r
512 range.moveToBookmark( bookmark );
\r
515 function removeInlineStyle( range )
\r
518 * Make sure our range has included all "collpased" parent inline nodes so
\r
519 * that our operation logic can be simpler.
\r
521 range.enlarge( CKEDITOR.ENLARGE_ELEMENT );
\r
523 var bookmark = range.createBookmark(),
\r
524 startNode = bookmark.startNode;
\r
526 if ( range.collapsed )
\r
529 var startPath = new CKEDITOR.dom.elementPath( startNode.getParent() ),
\r
530 // The topmost element in elementspatch which we should jump out of.
\r
534 for ( var i = 0, element ; i < startPath.elements.length
\r
535 && ( element = startPath.elements[i] ) ; i++ )
\r
538 * 1. If it's collaped inside text nodes, try to remove the style from the whole element.
\r
540 * 2. Otherwise if it's collapsed on element boundaries, moving the selection
\r
541 * outside the styles instead of removing the whole tag,
\r
542 * also make sure other inner styles were well preserverd.(#3309)
\r
544 if ( element == startPath.block || element == startPath.blockLimit )
\r
547 if ( this.checkElementRemovable( element ) )
\r
549 var endOfElement = range.checkBoundaryOfElement( element, CKEDITOR.END ),
\r
550 startOfElement = !endOfElement && range.checkBoundaryOfElement( element, CKEDITOR.START );
\r
551 if ( startOfElement || endOfElement )
\r
553 boundaryElement = element;
\r
554 boundaryElement.match = startOfElement ? 'start' : 'end';
\r
559 * Before removing the style node, there may be a sibling to the style node
\r
560 * that's exactly the same to the one to be removed. To the user, it makes
\r
561 * no difference that they're separate entities in the DOM tree. So, merge
\r
562 * them before removal.
\r
564 mergeSiblings( element );
\r
565 removeFromElement( this, element );
\r
571 // Re-create the style tree after/before the boundary element,
\r
572 // the replication start from bookmark start node to define the
\r
574 if ( boundaryElement )
\r
576 var clonedElement = startNode;
\r
577 for ( i = 0 ;; i++ )
\r
579 var newElement = startPath.elements[ i ];
\r
580 if ( newElement.equals( boundaryElement ) )
\r
582 // Avoid copying any matched element.
\r
583 else if ( newElement.match )
\r
586 newElement = newElement.clone();
\r
587 newElement.append( clonedElement );
\r
588 clonedElement = newElement;
\r
590 clonedElement[ boundaryElement.match == 'start' ?
\r
591 'insertBefore' : 'insertAfter' ]( boundaryElement );
\r
597 * Now our range isn't collapsed. Lets walk from the start node to the end
\r
598 * node via DFS and remove the styles one-by-one.
\r
600 var endNode = bookmark.endNode,
\r
604 * Find out the style ancestor that needs to be broken down at startNode
\r
607 function breakNodes()
\r
609 var startPath = new CKEDITOR.dom.elementPath( startNode.getParent() ),
\r
610 endPath = new CKEDITOR.dom.elementPath( endNode.getParent() ),
\r
613 for ( var i = 0 ; i < startPath.elements.length ; i++ )
\r
615 var element = startPath.elements[ i ];
\r
617 if ( element == startPath.block || element == startPath.blockLimit )
\r
620 if ( me.checkElementRemovable( element ) )
\r
621 breakStart = element;
\r
623 for ( i = 0 ; i < endPath.elements.length ; i++ )
\r
625 element = endPath.elements[ i ];
\r
627 if ( element == endPath.block || element == endPath.blockLimit )
\r
630 if ( me.checkElementRemovable( element ) )
\r
631 breakEnd = element;
\r
635 endNode.breakParent( breakEnd );
\r
637 startNode.breakParent( breakStart );
\r
641 // Now, do the DFS walk.
\r
642 var currentNode = startNode.getNext();
\r
643 while ( !currentNode.equals( endNode ) )
\r
646 * Need to get the next node first because removeFromElement() can remove
\r
647 * the current node from DOM tree.
\r
649 var nextNode = currentNode.getNextSourceNode();
\r
650 if ( currentNode.type == CKEDITOR.NODE_ELEMENT && this.checkElementRemovable( currentNode ) )
\r
652 // Remove style from element or overriding element.
\r
653 if ( currentNode.getName() == this.element )
\r
654 removeFromElement( this, currentNode );
\r
656 removeOverrides( currentNode, getOverrides( this )[ currentNode.getName() ] );
\r
659 * removeFromElement() may have merged the next node with something before
\r
660 * the startNode via mergeSiblings(). In that case, the nextNode would
\r
661 * contain startNode and we'll have to call breakNodes() again and also
\r
662 * reassign the nextNode to something after startNode.
\r
664 if ( nextNode.type == CKEDITOR.NODE_ELEMENT && nextNode.contains( startNode ) )
\r
667 nextNode = startNode.getNext();
\r
670 currentNode = nextNode;
\r
674 range.moveToBookmark( bookmark );
\r
677 function applyBlockStyle( range )
\r
679 // Serializible bookmarks is needed here since
\r
680 // elements may be merged.
\r
681 var bookmark = range.createBookmark( true );
\r
683 var iterator = range.createIterator();
\r
684 iterator.enforceRealBlocks = true;
\r
687 var doc = range.document;
\r
688 var previousPreBlock;
\r
690 while ( ( block = iterator.getNextParagraph() ) ) // Only one =
\r
692 var newBlock = getElement( this, doc );
\r
693 replaceBlock( block, newBlock );
\r
696 range.moveToBookmark( bookmark );
\r
699 // Replace the original block with new one, with special treatment
\r
700 // for <pre> blocks to make sure content format is well preserved, and merging/splitting adjacent
\r
701 // when necessary.(#3188)
\r
702 function replaceBlock( block, newBlock )
\r
704 var newBlockIsPre = newBlock.is( 'pre' );
\r
705 var blockIsPre = block.is( 'pre' );
\r
707 var isToPre = newBlockIsPre && !blockIsPre;
\r
708 var isFromPre = !newBlockIsPre && blockIsPre;
\r
711 newBlock = toPre( block, newBlock );
\r
712 else if ( isFromPre )
\r
713 // Split big <pre> into pieces before start to convert.
\r
714 newBlock = fromPres( splitIntoPres( block ), newBlock );
\r
716 block.moveChildren( newBlock );
\r
718 newBlock.replace( block );
\r
720 if ( newBlockIsPre )
\r
722 // Merge previous <pre> blocks.
\r
723 mergePre( newBlock );
\r
728 * Merge a <pre> block with a previous sibling if available.
\r
730 function mergePre( preBlock )
\r
733 if ( !( ( previousBlock = preBlock.getPreviousSourceNode( true, CKEDITOR.NODE_ELEMENT ) )
\r
734 && previousBlock.is
\r
735 && previousBlock.is( 'pre') ) )
\r
738 // Merge the previous <pre> block contents into the current <pre>
\r
741 // Another thing to be careful here is that currentBlock might contain
\r
742 // a '\n' at the beginning, and previousBlock might contain a '\n'
\r
743 // towards the end. These new lines are not normally displayed but they
\r
744 // become visible after merging.
\r
745 var mergedHtml = replace( previousBlock.getHtml(), /\n$/, '' ) + '\n\n' +
\r
746 replace( preBlock.getHtml(), /^\n/, '' ) ;
\r
748 // Krugle: IE normalizes innerHTML from <pre>, breaking whitespaces.
\r
749 if ( CKEDITOR.env.ie )
\r
750 preBlock.$.outerHTML = '<pre>' + mergedHtml + '</pre>';
\r
752 preBlock.setHtml( mergedHtml );
\r
754 previousBlock.remove();
\r
758 * Split into multiple <pre> blocks separated by double line-break.
\r
761 function splitIntoPres( preBlock )
\r
763 // Exclude the ones at header OR at tail,
\r
764 // and ignore bookmark content between them.
\r
765 var duoBrRegex = /(\S\s*)\n(?:\s|(<span[^>]+_fck_bookmark.*?\/span>))*\n(?!$)/gi,
\r
766 blockName = preBlock.getName(),
\r
767 splitedHtml = replace( preBlock.getOuterHtml(),
\r
769 function( match, charBefore, bookmark )
\r
771 return charBefore + '</pre>' + bookmark + '<pre>';
\r
775 splitedHtml.replace( /<pre>([\s\S]*?)<\/pre>/gi, function( match, preContent ){
\r
776 pres.push( preContent );
\r
781 // Wrapper function of String::replace without considering of head/tail bookmarks nodes.
\r
782 function replace( str, regexp, replacement )
\r
784 var headBookmark = '',
\r
787 str = str.replace( /(^<span[^>]+_fck_bookmark.*?\/span>)|(<span[^>]+_fck_bookmark.*?\/span>$)/gi,
\r
788 function( str, m1, m2 ){
\r
789 m1 && ( headBookmark = m1 );
\r
790 m2 && ( tailBookmark = m2 );
\r
793 return headBookmark + str.replace( regexp, replacement ) + tailBookmark;
\r
796 * Converting a list of <pre> into blocks with format well preserved.
\r
798 function fromPres( preHtmls, newBlock )
\r
800 var docFrag = new CKEDITOR.dom.documentFragment( newBlock.getDocument() );
\r
801 for ( var i = 0 ; i < preHtmls.length ; i++ )
\r
803 var blockHtml = preHtmls[ i ];
\r
805 // 1. Trim the first and last line-breaks immediately after and before <pre>,
\r
806 // they're not visible.
\r
807 blockHtml = blockHtml.replace( /(\r\n|\r)/g, '\n' ) ;
\r
808 blockHtml = replace( blockHtml, /^[ \t]*\n/, '' ) ;
\r
809 blockHtml = replace( blockHtml, /\n$/, '' ) ;
\r
810 // 2. Convert spaces or tabs at the beginning or at the end to
\r
811 blockHtml = replace( blockHtml, /^[ \t]+|[ \t]+$/g, function( match, offset, s )
\r
813 if ( match.length == 1 ) // one space, preserve it
\r
815 else if ( !offset ) // beginning of block
\r
816 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
\r
817 else // end of block
\r
818 return ' ' + CKEDITOR.tools.repeat( ' ', match.length - 1 );
\r
821 // 3. Convert \n to <BR>.
\r
822 // 4. Convert contiguous (i.e. non-singular) spaces or tabs to
\r
823 blockHtml = blockHtml.replace( /\n/g, '<br>' ) ;
\r
824 blockHtml = blockHtml.replace( /[ \t]{2,}/g,
\r
827 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ' ;
\r
830 var newBlockClone = newBlock.clone();
\r
831 newBlockClone.setHtml( blockHtml );
\r
832 docFrag.append( newBlockClone );
\r
838 * Converting from a non-PRE block to a PRE block in formatting operations.
\r
840 function toPre( block, newBlock )
\r
842 // First trim the block content.
\r
843 var preHtml = block.getHtml();
\r
845 // 1. Trim head/tail spaces, they're not visible.
\r
846 preHtml = replace( preHtml, /(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g, '' );
\r
847 // 2. Delete ANSI whitespaces immediately before and after <BR> because
\r
848 // they are not visible.
\r
849 preHtml = preHtml.replace( /[ \t\r\n]*(<br[^>]*>)[ \t\r\n]*/gi, '$1' );
\r
850 // 3. Compress other ANSI whitespaces since they're only visible as one
\r
851 // single space previously.
\r
852 // 4. Convert to spaces since is no longer needed in <PRE>.
\r
853 preHtml = preHtml.replace( /([ \t\n\r]+| )/g, ' ' );
\r
854 // 5. Convert any <BR /> to \n. This must not be done earlier because
\r
855 // the \n would then get compressed.
\r
856 preHtml = preHtml.replace( /<br\b[^>]*>/gi, '\n' );
\r
858 // Krugle: IE normalizes innerHTML to <pre>, breaking whitespaces.
\r
859 if ( CKEDITOR.env.ie )
\r
861 var temp = block.getDocument().createElement( 'div' );
\r
862 temp.append( newBlock );
\r
863 newBlock.$.outerHTML = '<pre>' + preHtml + '</pre>';
\r
864 newBlock = temp.getFirst().remove();
\r
867 newBlock.setHtml( preHtml );
\r
872 // Removes a style from an element itself, don't care about its subtree.
\r
873 function removeFromElement( style, element )
\r
875 var def = style._.definition,
\r
876 attributes = def.attributes,
\r
877 styles = def.styles,
\r
878 overrides = getOverrides( style );
\r
880 function removeAttrs()
\r
882 for ( var attName in attributes )
\r
884 // The 'class' element value must match (#1318).
\r
885 if ( attName == 'class' && element.getAttribute( attName ) != attributes[ attName ] )
\r
887 element.removeAttribute( attName );
\r
891 // Remove definition attributes/style from the elemnt.
\r
893 for ( var styleName in styles )
\r
894 element.removeStyle( styleName );
\r
896 // Now remove override styles on the element.
\r
897 attributes = overrides[ element.getName() ];
\r
900 removeNoAttribsElement( element );
\r
903 // Removes a style from inside an element.
\r
904 function removeFromInsideElement( style, element )
\r
906 var def = style._.definition,
\r
907 attribs = def.attributes,
\r
908 styles = def.styles,
\r
909 overrides = getOverrides( style );
\r
911 var innerElements = element.getElementsByTag( style.element );
\r
913 for ( var i = innerElements.count(); --i >= 0 ; )
\r
914 removeFromElement( style, innerElements.getItem( i ) );
\r
916 // Now remove any other element with different name that is
\r
917 // defined to be overriden.
\r
918 for ( var overrideElement in overrides )
\r
920 if ( overrideElement != style.element )
\r
922 innerElements = element.getElementsByTag( overrideElement ) ;
\r
923 for ( i = innerElements.count() - 1 ; i >= 0 ; i-- )
\r
925 var innerElement = innerElements.getItem( i );
\r
926 removeOverrides( innerElement, overrides[ overrideElement ] ) ;
\r
934 * Remove overriding styles/attributes from the specific element.
\r
935 * Note: Remove the element if no attributes remain.
\r
936 * @param {Object} element
\r
937 * @param {Object} overrides
\r
939 function removeOverrides( element, overrides )
\r
941 var attributes = overrides && overrides.attributes ;
\r
945 for ( var i = 0 ; i < attributes.length ; i++ )
\r
947 var attName = attributes[i][0], actualAttrValue ;
\r
949 if ( ( actualAttrValue = element.getAttribute( attName ) ) )
\r
951 var attValue = attributes[i][1] ;
\r
953 // Remove the attribute if:
\r
954 // - The override definition value is null ;
\r
955 // - The override definition valie is a string that
\r
956 // matches the attribute value exactly.
\r
957 // - The override definition value is a regex that
\r
958 // has matches in the attribute value.
\r
959 if ( attValue === null ||
\r
960 ( attValue.test && attValue.test( actualAttrValue ) ) ||
\r
961 ( typeof attValue == 'string' && actualAttrValue == attValue ) )
\r
962 element.removeAttribute( attName ) ;
\r
967 removeNoAttribsElement( element );
\r
970 // If the element has no more attributes, remove it.
\r
971 function removeNoAttribsElement( element )
\r
973 // If no more attributes remained in the element, remove it,
\r
974 // leaving its children.
\r
975 if ( !element.hasAttributes() )
\r
977 // Removing elements may open points where merging is possible,
\r
978 // so let's cache the first and last nodes for later checking.
\r
979 var firstChild = element.getFirst();
\r
980 var lastChild = element.getLast();
\r
982 element.remove( true );
\r
986 // Check the cached nodes for merging.
\r
987 mergeSiblings( firstChild );
\r
989 if ( lastChild && !firstChild.equals( lastChild ) )
\r
990 mergeSiblings( lastChild );
\r
995 function mergeSiblings( element )
\r
997 if ( !element || element.type != CKEDITOR.NODE_ELEMENT || !CKEDITOR.dtd.$removeEmpty[ element.getName() ] )
\r
1000 mergeElements( element, element.getNext(), true );
\r
1001 mergeElements( element, element.getPrevious() );
\r
1004 function mergeElements( element, sibling, isNext )
\r
1006 if ( sibling && sibling.type == CKEDITOR.NODE_ELEMENT )
\r
1008 var hasBookmark = sibling.getAttribute( '_fck_bookmark' );
\r
1010 if ( hasBookmark )
\r
1011 sibling = isNext ? sibling.getNext() : sibling.getPrevious();
\r
1013 if ( sibling && sibling.type == CKEDITOR.NODE_ELEMENT && element.isIdentical( sibling ) )
\r
1015 // Save the last child to be checked too, to merge things like
\r
1016 // <b><i></i></b><b><i></i></b> => <b><i></i></b>
\r
1017 var innerSibling = isNext ? element.getLast() : element.getFirst();
\r
1019 if ( hasBookmark )
\r
1020 ( isNext ? sibling.getPrevious() : sibling.getNext() ).move( element, !isNext );
\r
1022 sibling.moveChildren( element, !isNext );
\r
1025 // Now check the last inner child (see two comments above).
\r
1026 if ( innerSibling )
\r
1027 mergeSiblings( innerSibling );
\r
1032 function getElement( style, targetDocument )
\r
1036 var def = style._.definition;
\r
1038 var elementName = style.element;
\r
1040 // The "*" element name will always be a span for this function.
\r
1041 if ( elementName == '*' )
\r
1042 elementName = 'span';
\r
1044 // Create the element.
\r
1045 el = new CKEDITOR.dom.element( elementName, targetDocument );
\r
1047 return setupElement( el, style );
\r
1050 function setupElement( el, style )
\r
1052 var def = style._.definition;
\r
1053 var attributes = def.attributes;
\r
1054 var styles = CKEDITOR.style.getStyleText( def );
\r
1056 // Assign all defined attributes.
\r
1059 for ( var att in attributes )
\r
1061 el.setAttribute( att, attributes[ att ] );
\r
1065 // Assign all defined styles.
\r
1067 el.setAttribute( 'style', styles );
\r
1072 var varRegex = /#\((.+?)\)/g;
\r
1073 function replaceVariables( list, variablesValues )
\r
1075 for ( var item in list )
\r
1077 list[ item ] = list[ item ].replace( varRegex, function( match, varName )
\r
1079 return variablesValues[ varName ];
\r
1085 // Returns an object that can be used for style matching comparison.
\r
1086 // Attributes names and values are all lowercased, and the styles get
\r
1087 // merged with the style attribute.
\r
1088 function getAttributesForComparison( styleDefinition )
\r
1090 // If we have already computed it, just return it.
\r
1091 var attribs = styleDefinition._AC;
\r
1099 // Loop through all defined attributes.
\r
1100 var styleAttribs = styleDefinition.attributes;
\r
1101 if ( styleAttribs )
\r
1103 for ( var styleAtt in styleAttribs )
\r
1106 attribs[ styleAtt ] = styleAttribs[ styleAtt ];
\r
1110 // Includes the style definitions.
\r
1111 var styleText = CKEDITOR.style.getStyleText( styleDefinition );
\r
1114 if ( !attribs[ 'style' ] )
\r
1116 attribs[ 'style' ] = styleText;
\r
1119 // Appends the "length" information to the object.
\r
1120 attribs._length = length;
\r
1122 // Return it, saving it to the next request.
\r
1123 return ( styleDefinition._AC = attribs );
\r
1127 * Get the the collection used to compare the elements and attributes,
\r
1128 * defined in this style overrides, with other element. All information in
\r
1129 * it is lowercased.
\r
1130 * @param {CKEDITOR.style} style
\r
1132 function getOverrides( style )
\r
1134 if ( style._.overrides )
\r
1135 return style._.overrides;
\r
1137 var overrides = ( style._.overrides = {} ),
\r
1138 definition = style._.definition.overrides;
\r
1142 // The override description can be a string, object or array.
\r
1143 // Internally, well handle arrays only, so transform it if needed.
\r
1144 if ( !CKEDITOR.tools.isArray( definition ) )
\r
1145 definition = [ definition ];
\r
1147 // Loop through all override definitions.
\r
1148 for ( var i = 0 ; i < definition.length ; i++ )
\r
1150 var override = definition[i];
\r
1155 // If can be a string with the element name.
\r
1156 if ( typeof override == 'string' )
\r
1157 elementName = override.toLowerCase();
\r
1161 elementName = override.element ? override.element.toLowerCase() : style.element;
\r
1162 attrs = override.attributes;
\r
1165 // We can have more than one override definition for the same
\r
1166 // element name, so we attempt to simply append information to
\r
1167 // it if it already exists.
\r
1168 overrideEl = overrides[ elementName ] || ( overrides[ elementName ] = {} );
\r
1172 // The returning attributes list is an array, because we
\r
1173 // could have different override definitions for the same
\r
1174 // attribute name.
\r
1175 var overrideAttrs = ( overrideEl.attributes = overrideEl.attributes || new Array() );
\r
1176 for ( var attName in attrs )
\r
1178 // Each item in the attributes array is also an array,
\r
1179 // where [0] is the attribute name and [1] is the
\r
1180 // override value.
\r
1181 overrideAttrs.push( [ attName.toLowerCase(), attrs[ attName ] ] );
\r
1190 function normalizeCssText( unparsedCssText, nativeNormalize )
\r
1193 if ( nativeNormalize !== false )
\r
1195 // Injects the style in a temporary span object, so the browser parses it,
\r
1196 // retrieving its final format.
\r
1197 var temp = new CKEDITOR.dom.element( 'span' );
\r
1198 temp.setAttribute( 'style', unparsedCssText );
\r
1199 styleText = temp.getAttribute( 'style' ) || '';
\r
1202 styleText = unparsedCssText;
\r
1204 // Shrinking white-spaces around colon and semi-colon (#4147).
\r
1205 // Compensate tail semi-colon.
\r
1206 return styleText.replace( /\s*([;:])\s*/, '$1' )
\r
1207 .replace( /([^\s;])$/, '$1;')
\r
1208 .replace( /,\s+/g, ',' ) // Trimming spaces after comma (e.g. font-family name)(#4107).
\r
1212 function applyStyle( document, remove )
\r
1214 // Get all ranges from the selection.
\r
1215 var selection = document.getSelection();
\r
1216 var ranges = selection.getRanges();
\r
1217 var func = remove ? this.removeFromRange : this.applyToRange;
\r
1219 // Apply the style to the ranges.
\r
1220 for ( var i = 0 ; i < ranges.length ; i++ )
\r
1221 func.call( this, ranges[ i ] );
\r
1223 // Select the ranges again.
\r
1224 selection.selectRanges( ranges );
\r
1228 CKEDITOR.styleCommand = function( style )
\r
1230 this.style = style;
\r
1233 CKEDITOR.styleCommand.prototype.exec = function( editor )
\r
1237 var doc = editor.document;
\r
1241 if ( this.state == CKEDITOR.TRISTATE_OFF )
\r
1242 this.style.apply( doc );
\r
1243 else if ( this.state == CKEDITOR.TRISTATE_ON )
\r
1244 this.style.remove( doc );
\r
1250 CKEDITOR.stylesSet = new CKEDITOR.resourceManager( '', 'stylesSet' );
\r
1252 // Backward compatibility (#5025).
\r
1253 CKEDITOR.addStylesSet = CKEDITOR.tools.bind( CKEDITOR.stylesSet.add, CKEDITOR.stylesSet );
\r
1254 CKEDITOR.loadStylesSet = function( name, url, callback )
\r
1256 CKEDITOR.stylesSet.addExternal( name, url, '' );
\r
1257 CKEDITOR.stylesSet.load( name, callback );
\r