Вход Регистрация
Файл: fckeditor/editor/_source/classes/fckstyle.js
Строк: 1885
<?php

/*
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2003-2008 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 *
 * FCKStyle Class: contains a style definition, and all methods to work with
 * the style in a document.
 */

/**
 * @param {Object} styleDesc A "style descriptor" object, containing the raw
 * style definition in the following format:
 *        '<style name>' : {
 *            Element : '<element name>',
 *            Attributes : {
 *                '<att name>' : '<att value>',
 *                ...
 *            },
 *            Styles : {
 *                '<style name>' : '<style value>',
 *                ...
 *            },
 *            Overrides : '<element name>'|{
 *                Element : '<element name>',
 *                Attributes : {
 *                    '<att name>' : '<att value>'|/<att regex>/
 *                },
 *                Styles : {
 *                    '<style name>' : '<style value>'|/<style regex>/
 *                },
 *            }
 *        }
 */
var FCKStyle = function( styleDesc )
{
    
this.Element = ( styleDesc.Element || 'span' ).toLowerCase() ;
    
this._StyleDesc styleDesc ;
}

FCKStyle.prototype =
{
    
/**
     * Get the style type, based on its element name:
     *        - FCK_STYLE_BLOCK  (0): Block Style
     *        - FCK_STYLE_INLINE (1): Inline Style
     *        - FCK_STYLE_OBJECT (2): Object Style
     */
    
GetType : function()
    {
        var 
type this.GetType_$ ;

        if ( 
type != undefined )
            return 
type ;

        var 
elementName this.Element ;

        if ( 
elementName == '#' || FCKListsLib.StyleBlockElementselementName ] )
            
type FCK_STYLE_BLOCK ;
        else if ( 
FCKListsLib.StyleObjectElementselementName ] )
            
type FCK_STYLE_OBJECT ;
        else
            
type FCK_STYLE_INLINE ;

        return ( 
this.GetType_$ = type ) ;
    },

    
/**
     * Apply the style to the current selection.
     */
    
ApplyToSelection : function( targetWindow )
    {
        
// Create a range for the current selection.
        
var range = new FCKDomRangetargetWindow ) ;
        
range.MoveToSelection() ;

        
this.ApplyToRangerangetrue ) ;
    },

    
/**
     * Apply the style to a FCKDomRange.
     */
    
ApplyToRange : function( rangeselectItupdateRange )
    {
        
// ApplyToRange is not valid for FCK_STYLE_OBJECT types.
        // Use ApplyToObject instead.

        
switch ( this.GetType() )
        {
            case 
FCK_STYLE_BLOCK :
                
this.ApplyToRange this._ApplyBlockStyle ;
                break ;
            case 
FCK_STYLE_INLINE :
                
this.ApplyToRange this._ApplyInlineStyle ;
                break ;
            default :
                return ;
        }

        
this.ApplyToRangerangeselectItupdateRange ) ;
    },

    
/**
     * Apply the style to an object. Valid for FCK_STYLE_BLOCK types only.
     */
    
ApplyToObject : function( objectElement )
    {
        if ( !
objectElement )
            return ;

        
this.BuildElementnullobjectElement ) ;
    },

    
/**
     * Remove the style from the current selection.
     */
    
RemoveFromSelection : function( targetWindow )
    {
        
// Create a range for the current selection.
        
var range = new FCKDomRangetargetWindow ) ;
        
range.MoveToSelection() ;

        
this.RemoveFromRangerangetrue ) ;
    },

    
/**
     * Remove the style from a FCKDomRange. Block type styles will have no
     * effect.
     */
    
RemoveFromRange : function( rangeselectItupdateRange )
    {
        var 
bookmark ;

        
// Create the attribute list to be used later for element comparisons.
        
var styleAttribs this._GetAttribsForComparison() ;
        var 
styleOverrides this._GetOverridesForComparison() ;

        
// If collapsed, we are removing all conflicting styles from the range
        // parent tree.
        
if ( range.CheckIsCollapsed() )
        {
            
// Bookmark the range so we can re-select it after processing.
            
var bookmark range.CreateBookmarktrue ) ;

            
// Let's start from the bookmark <span> parent.
            
var bookmarkStart range.GetBookmarkNodebookmarktrue ) ;

            var 
path = new FCKElementPathbookmarkStart.parentNode ) ;

            
// While looping through the path, we'll be saving references to
            // parent elements if the range is in one of their boundaries. In
            // this way, we are able to create a copy of those elements when
            // removing a style if the range is in a boundary limit (see #1270).
            
var boundaryElements = [] ;

            
// Check if the range is in the boundary limits of an element
            // (related to #1270).
            
var isBoundaryRight = !FCKDomTools.GetNextSiblingbookmarkStart ) ;
            var 
isBoundary isBoundaryRight || !FCKDomTools.GetPreviousSiblingbookmarkStart ) ;

            
// This is the last element to be removed in the boundary situation
            // described at #1270.
            
var lastBoundaryElement ;
            var 
boundaryLimitIndex = -;

            for ( var 
path.Elements.length i++ )
            {
                var 
pathElement path.Elements[i] ;
                if ( 
this.CheckElementRemovablepathElement ) )
                {
                    if ( 
isBoundary
                        
&& !FCKDomTools.CheckIsEmptyElementpathElement,
                                function( 
el )
                                {
                                    return ( 
el != bookmarkStart ) ;
                                } )
                        )
                    {
                        
lastBoundaryElement pathElement ;

                        
// We'll be continuously including elements in the
                        // boundaryElements array, but only those added before
                        // setting lastBoundaryElement must be used later, so
                        // let's mark the current index here.
                        
boundaryLimitIndex boundaryElements.length ;
                    }
                    else
                    {
                        var 
pathElementName pathElement.nodeName.toLowerCase() ;

                        if ( 
pathElementName == this.Element )
                        {
                            
// Remove any attribute that conflict with this style, no
                            // matter their values.
                            
for ( var att in styleAttribs )
                            {
                                if ( 
FCKDomTools.HasAttributepathElementatt ) )
                                {
                                    switch ( 
att )
                                    {
                                        case 
'style' :
                                            
this._RemoveStylesFromElementpathElement ) ;
                                            break ;

                                        case 
'class' :
                                            
// The 'class' element value must match (#1318).
                                            
if ( FCKDomTools.GetAttributeValuepathElementatt ) != this.GetFinalAttributeValueatt ) )
                                                continue ;

                                            
/*jsl:fallthru*/

                                        
default :
                                            
FCKDomTools.RemoveAttributepathElementatt ) ;
                                    }
                                }
                            }
                        }

                        
// Remove overrides defined to the same element name.
                        
this._RemoveOverridespathElementstyleOverridespathElementName ] ) ;

                        
// Remove the element if no more attributes are available and it's an inline style element
                        
if ( this.GetType() == FCK_STYLE_INLINE)
                            
this._RemoveNoAttribElementpathElement ) ;
                    }
                }
                else if ( 
isBoundary )
                    
boundaryElements.pushpathElement ) ;

                
// Check if we are still in a boundary (at the same side).
                
isBoundary isBoundary && ( ( isBoundaryRight && !FCKDomTools.GetNextSiblingpathElement ) ) || ( !isBoundaryRight && !FCKDomTools.GetPreviousSiblingpathElement ) ) ) ;

                
// If we are in an element that is not anymore a boundary, or
                // we are at the last element, let's move things outside the
                // boundary (if available).
                
if ( lastBoundaryElement && ( !isBoundary || ( == path.Elements.length ) ) )
                {
                    
// Remove the bookmark node from the DOM.
                    
var currentElement FCKDomTools.RemoveNodebookmarkStart ) ;

                    
// Build the collapsed group of elements that are not
                    // removed by this style, but share the boundary.
                    // (see comment 1 and 2 at #1270)
                    
for ( var <= boundaryLimitIndex j++ )
                    {
                        var 
newElement FCKDomTools.CloneElementboundaryElements[j] ) ;
                        
newElement.appendChildcurrentElement ) ;
                        
currentElement newElement ;
                    }

                    
// Re-insert the bookmark node (and the collapsed elements)
                    // in the DOM, in the new position next to the styled element.
                    
if ( isBoundaryRight )
                        
FCKDomTools.InsertAfterNodelastBoundaryElementcurrentElement ) ;
                    else
                        
lastBoundaryElement.parentNode.insertBeforecurrentElementlastBoundaryElement ) ;

                    
isBoundary false ;
                    
lastBoundaryElement null ;
                }
            }

                
// Re-select the original range.
            
if ( selectIt )
                
range.SelectBookmarkbookmark ) ;

            if ( 
updateRange )
                
range.MoveToBookmarkbookmark ) ;

            return ;
        }

        
// Expand the range, if inside inline element boundaries.
        
range.Expand'inline_elements' ) ;

        
// Bookmark the range so we can re-select it after processing.
        
bookmark range.CreateBookmarktrue ) ;

        
// The style will be applied within the bookmark boundaries.
        
var startNode    range.GetBookmarkNodebookmarktrue ) ;
        var 
endNode        range.GetBookmarkNodebookmarkfalse ) ;

        
range.Releasetrue ) ;

        
// We need to check the selection boundaries (bookmark spans) to break
        // the code in a way that we can properly remove partially selected nodes.
        // For example, removing a <b> style from
        //        <b>This is [some text</b> to show <b>the] problem</b>
        // ... where [ and ] represent the selection, must result:
        //        <b>This is </b>[some text to show the]<b> problem</b>
        // The strategy is simple, we just break the partial nodes before the
        // removal logic, having something that could be represented this way:
        //        <b>This is </b>[<b>some text</b> to show <b>the</b>]<b> problem</b>

        // Let's start checking the start boundary.
        
var path = new FCKElementPathstartNode ) ;
        var 
pathElements path.Elements ;
        var 
pathElement ;

        for ( var 
pathElements.length i++ )
        {
            
pathElement pathElements[i] ;

            if ( 
pathElement == path.Block || pathElement == path.BlockLimit )
                break ;

            
// If this element can be removed (even partially).
            
if ( this.CheckElementRemovablepathElement ) )
                
FCKDomTools.BreakParentstartNodepathElementrange ) ;
        }

        
// Now the end boundary.
        
path = new FCKElementPathendNode ) ;
        
pathElements path.Elements ;

        for ( var 
pathElements.length i++ )
        {
            
pathElement pathElements[i] ;

            if ( 
pathElement == path.Block || pathElement == path.BlockLimit )
                break ;

            
elementName pathElement.nodeName.toLowerCase() ;

            
// If this element can be removed (even partially).
            
if ( this.CheckElementRemovablepathElement ) )
                
FCKDomTools.BreakParentendNodepathElementrange ) ;
        }

        
// Navigate through all nodes between the bookmarks.
        
var currentNode FCKDomTools.GetNextSourceNodestartNodetrue ) ;

        while ( 
currentNode )
        {
            
// Cache the next node to be processed. Do it now, because
            // currentNode may be removed.
            
var nextNode FCKDomTools.GetNextSourceNodecurrentNode ) ;

            
// Remove elements nodes that match with this style rules.
            
if ( currentNode.nodeType == )
            {
                var 
elementName currentNode.nodeName.toLowerCase() ;

                var 
mayRemove = ( elementName == this.Element ) ;
                if ( 
mayRemove )
                {
                    
// Remove any attribute that conflict with this style, no matter
                    // their values.
                    
for ( var att in styleAttribs )
                    {
                        if ( 
FCKDomTools.HasAttributecurrentNodeatt ) )
                        {
                            switch ( 
att )
                            {
                                case 
'style' :
                                    
this._RemoveStylesFromElementcurrentNode ) ;
                                    break ;

                                case 
'class' :
                                    
// The 'class' element value must match (#1318).
                                    
if ( FCKDomTools.GetAttributeValuecurrentNodeatt ) != this.GetFinalAttributeValueatt ) )
                                        continue ;

                                    
/*jsl:fallthru*/

                                
default :
                                    
FCKDomTools.RemoveAttributecurrentNodeatt ) ;
                            }
                        }
                    }
                }
                else
                    
mayRemove = !!styleOverrideselementName ] ;

                if ( 
mayRemove )
                {
                    
// Remove overrides defined to the same element name.
                    
this._RemoveOverridescurrentNodestyleOverrideselementName ] ) ;

                    
// Remove the element if no more attributes are available.
                    
this._RemoveNoAttribElementcurrentNode ) ;
                }
            }

            
// If we have reached the end of the selection, stop looping.
            
if ( nextNode == endNode )
                break ;

            
currentNode nextNode ;
        }

        
this._FixBookmarkStartstartNode ) ;

        
// Re-select the original range.
        
if ( selectIt )
            
range.SelectBookmarkbookmark ) ;

        if ( 
updateRange )
            
range.MoveToBookmarkbookmark ) ;
    },

    
/**
     * Checks if an element, or any of its attributes, is removable by the
     * current style definition.
     */
    
CheckElementRemovable : function( elementfullMatch )
    {
        if ( !
element )
            return 
false ;

        var 
elementName element.nodeName.toLowerCase() ;

        
// If the element name is the same as the style name.
        
if ( elementName == this.Element )
        {
            
// If no attributes are defined in the element.
            
if ( !fullMatch && !FCKDomTools.HasAttributeselement ) )
                return 
true ;

            
// If any attribute conflicts with the style attributes.
            
var attribs this._GetAttribsForComparison() ;
            var 
allMatched = ( attribs._length == ) ;
            for ( var 
att in attribs )
            {
                if ( 
att == '_length' )
                    continue ;

                if ( 
this._CompareAttributeValuesattFCKDomTools.GetAttributeValueelementatt ), ( this.GetFinalAttributeValueatt ) || '' ) ) )
                {
                    
allMatched true ;
                    if ( !
fullMatch )
                        break ;
                }
                else
                {
                    
allMatched false ;
                    if ( 
fullMatch )
                        return 
false ;
                }
            }
            if ( 
allMatched )
                return 
true ;
        }

        
// Check if the element can be somehow overriden.
        
var override this._GetOverridesForComparison()[ elementName ] ;
        if ( 
override )
        {
            
// If no attributes have been defined, remove the element.
            
if ( !( attribs override.Attributes ) ) // Only one "="
                
return true ;

            for ( var 
attribs.length i++ )
            {
                var 
attName attribs[i][0] ;
                if ( 
FCKDomTools.HasAttributeelementattName ) )
                {
                    var 
attValue attribs[i][1] ;

                    
// Remove the attribute if:
                    //    - The override definition value is null ;
                    //    - The override definition valie is a string that
                    //      matches the attribute value exactly.
                    //    - The override definition value is a regex that
                    //      has matches in the attribute value.
                    
if ( attValue == null ||
                            ( 
typeof attValue == 'string' && FCKDomTools.GetAttributeValueelementattName ) == attValue ) ||
                            
attValue.testFCKDomTools.GetAttributeValueelementattName ) ) )
                        return 
true ;
                }
            }
        }

        return 
false ;
    },

    
/**
     * Get the style state for an element path. Returns "true" if the element
     * is active in the path.
     */
    
CheckActive : function( elementPath )
    {
        switch ( 
this.GetType() )
        {
            case 
FCK_STYLE_BLOCK :
                return 
this.CheckElementRemovableelementPath.Block || elementPath.BlockLimittrue ) ;

            case 
FCK_STYLE_INLINE :

                var 
elements elementPath.Elements ;

                for ( var 
elements.length i++ )
                {
                    var 
element elements[i] ;

                    if ( 
element == elementPath.Block || element == elementPath.BlockLimit )
                        continue ;

                    if ( 
this.CheckElementRemovableelementtrue ) )
                        return 
true ;
                }
        }
        return 
false ;
    },

    
/**
     * Removes an inline style from inside an element tree. The element node
     * itself is not checked or removed, only the child tree inside of it.
     */
    
RemoveFromElement : function( element )
    {
        var 
attribs this._GetAttribsForComparison() ;
        var 
overrides this._GetOverridesForComparison() ;

        
// Get all elements with the same name.
        
var innerElements element.getElementsByTagNamethis.Element ) ;

        for ( var 
innerElements.length >= i-- )
        {
            var 
innerElement innerElements[i] ;

            
// Remove any attribute that conflict with this style, no matter
            // their values.
            
for ( var att in attribs )
            {
                if ( 
FCKDomTools.HasAttributeinnerElementatt ) )
                {
                    switch ( 
att )
                    {
                        case 
'style' :
                            
this._RemoveStylesFromElementinnerElement ) ;
                            break ;

                        case 
'class' :
                            
// The 'class' element value must match (#1318).
                            
if ( FCKDomTools.GetAttributeValueinnerElementatt ) != this.GetFinalAttributeValueatt ) )
                                continue ;

                            
/*jsl:fallthru*/

                        
default :
                            
FCKDomTools.RemoveAttributeinnerElementatt ) ;
                    }
                }
            }

            
// Remove overrides defined to the same element name.
            
this._RemoveOverridesinnerElementoverridesthis.Element ] ) ;

            
// Remove the element if no more attributes are available.
            
this._RemoveNoAttribElementinnerElement ) ;
        }

        
// Now remove any other element with different name that is
        // defined to be overriden.
        
for ( var overrideElement in overrides )
        {
            if ( 
overrideElement != this.Element )
            {
                
// Get all elements.
                
innerElements element.getElementsByTagNameoverrideElement ) ;

                for ( var 
innerElements.length >= i-- )
                {
                    var 
innerElement innerElements[i] ;
                    
this._RemoveOverridesinnerElementoverridesoverrideElement ] ) ;
                    
this._RemoveNoAttribElementinnerElement ) ;
                }
            }
        }
    },

    
_RemoveStylesFromElement : function( element )
    {
        var 
elementStyle element.style.cssText ;
        var 
pattern this.GetFinalStyleValue() ;

        if ( 
elementStyle.length && pattern.length == )
            return ;

        
pattern '(^|;)\s*(' +
            
pattern.replace( /s*([^ ]+):.*?(;|$)/g'$1|' ).replace( /|$/, '' ) +
            
'):[^;]+' ;

        var 
regex = new RegExppattern'gi' ) ;

        
elementStyle elementStyle.replaceregex'' ).Trim() ;

        if ( 
elementStyle.length == || elementStyle == ';' )
            
FCKDomTools.RemoveAttributeelement'style' ) ;
        else
            
element.style.cssText elementStyle.replaceregex'' ) ;
    },

    
/**
     * Remove all attributes that are defined to be overriden,
     */
    
_RemoveOverrides : function( elementoverride )
    {
        var 
attributes override && override.Attributes ;

        if ( 
attributes )
        {
            for ( var 
attributes.length i++ )
            {
                var 
attName attributes[i][0] ;

                if ( 
FCKDomTools.HasAttributeelementattName ) )
                {
                    var 
attValue    attributes[i][1] ;

                    
// Remove the attribute if:
                    //    - The override definition value is null ;
                    //    - The override definition valie is a string that
                    //      matches the attribute value exactly.
                    //    - The override definition value is a regex that
                    //      has matches in the attribute value.
                    
if ( attValue == null ||
                            ( 
attValue.test && attValue.testFCKDomTools.GetAttributeValueelementattName ) ) ) ||
                            ( 
typeof attValue == 'string' && FCKDomTools.GetAttributeValueelementattName ) == attValue ) )
                        
FCKDomTools.RemoveAttributeelementattName ) ;
                }
            }
        }
    },

    
/**
     * If the element has no more attributes, remove it.
     */
    
_RemoveNoAttribElement : function( element )
    {
        
// If no more attributes remained in the element, remove it,
        // leaving its children.
        
if ( !FCKDomTools.HasAttributeselement ) )
        {
            
// Removing elements may open points where merging is possible,
            // so let's cache the first and last nodes for later checking.
            
var firstChild    element.firstChild ;
            var 
lastChild    element.lastChild ;

            
FCKDomTools.RemoveNodeelementtrue ) ;

            
// Check the cached nodes for merging.
            
this._MergeSiblingsfirstChild ) ;

            if ( 
firstChild != lastChild )
                
this._MergeSiblingslastChild ) ;
        }
    },

    
/**
     * Creates a DOM element for this style object.
     */
    
BuildElement : function( targetDocelement )
    {
        
// Create the element.
        
var el element || targetDoc.createElementthis.Element ) ;

        
// Assign all defined attributes.
        
var attribs    this._StyleDesc.Attributes ;
        var 
attValue ;
        if ( 
attribs )
        {
            for ( var 
att in attribs )
            {
                
attValue this.GetFinalAttributeValueatt ) ;

                if ( 
att.toLowerCase() == 'class' )
                    
el.className attValue ;
                else
                    
el.setAttributeattattValue ) ;
            }
        }

        
// Assign the style attribute.
        
if ( this._GetStyleText().length )
            
el.style.cssText this.GetFinalStyleValue() ;

        return 
el ;
    },

    
_CompareAttributeValues : function( attNamevalueAvalueB )
    {
        if ( 
attName == 'style' && valueA && valueB )
        {
            
valueA valueA.replace( /;$/, '' ).toLowerCase() ;
            
valueB valueB.replace( /;$/, '' ).toLowerCase() ;
        }

        
// Return true if they match or if valueA is null and valueB is an empty string
        
return ( valueA == valueB || ( ( valueA === null || valueA === '' ) && ( valueB === null || valueB === '' ) ) )
    },

    
GetFinalAttributeValue : function( attName )
    {
        var 
attValue this._StyleDesc.Attributes ;
        var 
attValue attValue attValueattName ] : null ;

        if ( !
attValue && attName == 'style' )
            return 
this.GetFinalStyleValue() ;

        if ( 
attValue && this._Variables )
            
// Using custom Replace() to guarantee the correct scope.
            
attValue attValue.ReplaceFCKRegexLib.StyleVariableAttNamethis._GetVariableReplacethis ) ;

        return 
attValue ;
    },

    
GetFinalStyleValue : function()
    {
        var 
attValue this._GetStyleText() ;

        if ( 
attValue.length && this._Variables )
        {
            
// Using custom Replace() to guarantee the correct scope.
            
attValue attValue.ReplaceFCKRegexLib.StyleVariableAttNamethis._GetVariableReplacethis ) ;
            
attValue FCKTools.NormalizeCssTextattValue ) ;
        }

        return 
attValue ;
    },

    
_GetVariableReplace : function()
    {
        
// The second group in the regex is the variable name.
        
return this._Variablesarguments[2] ] || arguments[0] ;
    },

    
/**
     * Set the value of a variable attribute or style, to be used when
     * appliying the style.
     */
    
SetVariable : function( namevalue )
    {
        var 
variables this._Variables ;

        if ( !
variables )
            
variables this._Variables = {} ;

        
this._Variablesname ] = value ;
    },

    
/**
     * Converting from a PRE block to a non-PRE block in formatting operations.
     */
    
_FromPre : function( docblocknewBlock )
    {
        var 
innerHTML block.innerHTML ;

        
// Trim the first and last linebreaks immediately after and before <pre>, </pre>,
        // if they exist.
        // This is done because the linebreaks are not rendered.
        
innerHTML innerHTML.replace( /(rn|r)/g'n' ) ;
        
innerHTML innerHTML.replace( /^[ t]*n/, '' ) ;
        
innerHTML innerHTML.replace( /n$/, '' ) ;

        
// 1. Convert spaces or tabs at the beginning or at the end to &nbsp;
        
innerHTML innerHTML.replace( /^[ t]+|[ t]+$/g, function( matchoffset)
                {
                    if ( 
match.length == )    // one space, preserve it
                        
return '&nbsp;' ;
                    else if ( 
offset == )        // beginning of block
                        
return new Array( match.length ).join'&nbsp;' ) + ' ' ;
                    else                
// end of block
                        
return ' ' + new Array( match.length ).join'&nbsp;' ) ;
                } ) ;

        
// 2. Convert n to <BR>.
        // 3. Convert contiguous (i.e. non-singular) spaces or tabs to &nbsp;
        
var htmlIterator = new FCKHtmlIteratorinnerHTML ) ;
        var 
results = [] ;
        
htmlIterator.Each( function( isTagvalue )
            {
                if ( !
isTag )
                {
                    
value value.replace( /n/g'<br>' ) ;
                    
value value.replace( /[ t]{2,}/g,
                            function ( 
match )
                            {
                                return new Array( 
match.length ).join'&nbsp;' ) + ' ' ;
                            } ) ;
                }
                
results.pushvalue ) ;
            } ) ;
        
newBlock.innerHTML results.join'' ) ;
        return 
newBlock ;
    },

    
/**
     * Converting from a non-PRE block to a PRE block in formatting operations.
     */
    
_ToPre : function( docblocknewBlock )
    {
        
// Handle converting from a regular block to a <pre> block.
        
var innerHTML block.innerHTML.Trim() ;

        
// 1. Delete ANSI whitespaces immediately before and after <BR> because
        //    they are not visible.
        // 2. Mark down any <BR /> nodes here so they can be turned into n in
        //    the next step and avoid being compressed.
        
innerHTML innerHTML.replace( /[ trn]*(<br[^>]*>)[ trn]*/gi'<br />' ) ;

        
// 3. Compress other ANSI whitespaces since they're only visible as one
        //    single space previously.
        // 4. Convert &nbsp; to spaces since &nbsp; is no longer needed in <PRE>.
        // 5. Convert any <BR /> to n. This must not be done earlier because
        //    the n would then get compressed.
        
var htmlIterator = new FCKHtmlIteratorinnerHTML ) ;
        var 
results = [] ;
        
htmlIterator.Each( function( isTagvalue )
            {
                if ( !
isTag )
                    
value value.replace( /([ tnr]+|&nbsp;)/g' ' ) ;
                else if ( 
isTag && value == '<br />' )
                    
value 'n' ;
                
results.pushvalue ) ;
            } ) ;

        
// Assigning innerHTML to <PRE> in IE causes all linebreaks to be
        // reduced to spaces.
        // Assigning outerHTML to <PRE> in IE doesn't work if the <PRE> isn't
        // contained in another node since the node reference is changed after
        // outerHTML assignment.
        // So, we need some hacks to workaround IE bugs here.
        
if ( FCKBrowserInfo.IsIE )
        {
            var 
temp doc.createElement'div' ) ;
            
temp.appendChildnewBlock ) ;
            
newBlock.outerHTML '<pre>n' results.join'' ) + '</pre>' ;
            
newBlock temp.removeChildtemp.firstChild ) ;
        }
        else
            
newBlock.innerHTML results.join'' ) ;

        return 
newBlock ;
    },

    
/**
     * Merge a <pre> block with a previous <pre> block, if available.
     */
    
_CheckAndMergePre : function( previousBlockpreBlock )
    {
        
// Check if the previous block and the current block are next
        // to each other.
        
if ( previousBlock != FCKDomTools.GetPreviousSourceElementpreBlocktrue ) )
            return ;

        
// Merge the previous <pre> block contents into the current <pre>
        // block.
        //
        // Another thing to be careful here is that currentBlock might contain
        // a 'n' at the beginning, and previousBlock might contain a 'n'
        // towards the end. These new lines are not normally displayed but they
        // become visible after merging.
        
var innerHTML previousBlock.innerHTML.replace( /n$/, '' ) + 'nn' +
                
preBlock.innerHTML.replace( /^n/, '' ) ;

        
// Buggy IE normalizes innerHTML from <pre>, breaking whitespaces.
        
if ( FCKBrowserInfo.IsIE )
            
preBlock.outerHTML '<pre>' innerHTML '</pre>' ;
        else
            
preBlock.innerHTML innerHTML ;

        
// Remove the previous <pre> block.
        //
        // The preBlock must not be moved or deleted from the DOM tree. This
        // guarantees the FCKDomRangeIterator in _ApplyBlockStyle would not
        // get lost at the next iteration.
        
FCKDomTools.RemoveNodepreviousBlock ) ;
    },

    
_CheckAndSplitPre : function( newBlock )
    {
        var 
lastNewBlock ;

        var 
cursor newBlock.firstChild ;

        
// We are not splitting <br><br> at the beginning of the block, so
        // we'll start from the second child.
        
cursor cursor && cursor.nextSibling ;

        while ( 
cursor )
        {
            var 
next cursor.nextSibling ;

            
// If we have two <BR>s, and they're not at the beginning or the end,
            // then we'll split up the contents following them into another block.
            // Stop processing if we are at the last child couple.
            
if ( next && next.nextSibling && cursor.nodeName.IEquals'br' ) && next.nodeName.IEquals'br' ) )
            {
                
// Remove the first <br>.
                
FCKDomTools.RemoveNodecursor ) ;

                
// Move to the node after the second <br>.
                
cursor next.nextSibling ;

                
// Remove the second <br>.
                
FCKDomTools.RemoveNodenext ) ;

                
// Create the block that will hold the child nodes from now on.
                
lastNewBlock FCKDomTools.InsertAfterNodelastNewBlock || newBlockFCKDomTools.CloneElementnewBlock ) ) ;

                continue ;
            }

            
// If we split it, then start moving the nodes to the new block.
            
if ( lastNewBlock )
            {
                
cursor cursor.previousSibling ;
                
FCKDomTools.MoveNode(cursor.nextSiblinglastNewBlock ) ;
            }

            
cursor cursor.nextSibling ;
        }
    },

    
/**
     * Apply an inline style to a FCKDomRange.
     *
     * TODO
     *    - Implement the "#" style handling.
     *    - Properly handle block containers like <div> and <blockquote>.
     */
    
_ApplyBlockStyle : function( rangeselectItupdateRange )
    {
        
// Bookmark the range so we can re-select it after processing.
        
var bookmark ;

        if ( 
selectIt )
            
bookmark range.CreateBookmark() ;

        var 
iterator = new FCKDomRangeIteratorrange ) ;
        
iterator.EnforceRealBlocks true ;

        var 
block ;
        var 
doc range.Window.document ;
        var 
previousPreBlock ;

        while( ( 
block iterator.GetNextParagraph() ) )        // Only one =
        
{
            
// Create the new node right before the current one.
            
var newBlock this.BuildElementdoc ) ;

            
// Check if we are changing from/to <pre>.
            
var newBlockIsPre    newBlock.nodeName.IEquals'pre' ) ;
            var 
blockIsPre        block.nodeName.IEquals'pre' ) ;

            var 
toPre    newBlockIsPre && !blockIsPre ;
            var 
fromPre    = !newBlockIsPre && blockIsPre ;

            
// Move everything from the current node to the new one.
            
if ( toPre )
                
newBlock this._ToPredocblocknewBlock ) ;
            else if ( 
fromPre )
                
newBlock this._FromPredocblocknewBlock ) ;
            else    
// Convering from a regular block to another regular block.
                
FCKDomTools.MoveChildrenblocknewBlock ) ;

            
// Replace the current block.
            
block.parentNode.insertBeforenewBlockblock ) ;
            
FCKDomTools.RemoveNodeblock ) ;

            
// Complete other tasks after inserting the node in the DOM.
            
if ( newBlockIsPre )
            {
                if ( 
previousPreBlock )
                    
this._CheckAndMergePrepreviousPreBlocknewBlock ) ;    // Merge successive <pre> blocks.
                
previousPreBlock newBlock ;
            }
            else if ( 
fromPre )
                
this._CheckAndSplitPrenewBlock ) ;    // Split <br><br> in successive <pre>s.
        
}

        
// Re-select the original range.
        
if ( selectIt )
            
range.SelectBookmarkbookmark ) ;

        if ( 
updateRange )
            
range.MoveToBookmarkbookmark ) ;
    },

    
/**
     * Apply an inline style to a FCKDomRange.
     *
     * TODO
     *    - Merge elements, when applying styles to similar elements that enclose
     *    the entire selection, outputing:
     *        <span style="color: #ff0000; background-color: #ffffff">XYZ</span>
     *    instead of:
     *        <span style="color: #ff0000;"><span style="background-color: #ffffff">XYZ</span></span>
     */
    
_ApplyInlineStyle : function( rangeselectItupdateRange )
    {
        var 
doc range.Window.document ;

        if ( 
range.CheckIsCollapsed() )
        {
            
// Create the element to be inserted in the DOM.
            
var collapsedElement this.BuildElementdoc ) ;
            
range.InsertNodecollapsedElement ) ;
            
range.MoveToPositioncollapsedElement) ;
            
range.Select() ;

            return ;
        }

        
// The general idea here is navigating through all nodes inside the
        // current selection, working on distinct range blocks, defined by the
        // DTD compatibility between the style element and the nodes inside the
        // ranges.
        //
        // For example, suppose we have the following selection (where [ and ]
        // are the boundaries), and we apply a <b> style there:
        //
        //        <p>Here we [have <b>some</b> text.<p>
        //        <p>And some here] here.</p>
        //
        // Two different ranges will be detected:
        //
        //        "have <b>some</b> text."
        //        "And some here"
        //
        // Both ranges will be extracted, moved to a <b> element, and
        // re-inserted, resulting in the following output:
        //
        //        <p>Here we [<b>have some text.</b><p>
        //        <p><b>And some here</b>] here.</p>
        //
        // Note that the <b> element at <b>some</b> is also removed because it
        // is not needed anymore.

        
var elementName this.Element ;

        
// Get the DTD definition for the element. Defaults to "span".
        
var elementDTD FCK.DTDelementName ] || FCK.DTD.span ;

        
// Create the attribute list to be used later for element comparisons.
        
var styleAttribs this._GetAttribsForComparison() ;
        var 
styleNode ;

        
// Expand the range, if inside inline element boundaries.
        
range.Expand'inline_elements' ) ;

        
// Bookmark the range so we can re-select it after processing.
        
var bookmark range.CreateBookmarktrue ) ;

        
// The style will be applied within the bookmark boundaries.
        
var startNode    range.GetBookmarkNodebookmarktrue ) ;
        var 
endNode        range.GetBookmarkNodebookmarkfalse ) ;

        
// We'll be reusing the range to apply the styles. So, release it here
        // to indicate that it has not been initialized.
        
range.Releasetrue ) ;

        
// Let's start the nodes lookup from the node right after the bookmark
        // span.
        
var currentNode FCKDomTools.GetNextSourceNodestartNodetrue ) ;

        while ( 
currentNode )
        {
            var 
applyStyle false ;

            var 
nodeType currentNode.nodeType ;
            var 
nodeName nodeType == currentNode.nodeName.toLowerCase() : null ;

            
// Check if the current node can be a child of the style element.
            
if ( !nodeName || elementDTDnodeName ] )
            {
                
// Check if the style element can be a child of the current
                // node parent or if the element is not defined in the DTD.
                
if ( ( FCK.DTDcurrentNode.parentNode.nodeName.toLowerCase() ] || FCK.DTD.span )[ elementName ] || !FCK.DTDelementName ] )
                {
                    
// This node will be part of our range, so if it has not
                    // been started, place its start right before the node.
                    
if ( !range.CheckHasRange() )
                        
range.SetStartcurrentNode) ;

                    
// Non element nodes, or empty elements can be added
                    // completely to the range.
                    
if ( nodeType != || currentNode.childNodes.length == )
                    {
                        var 
includedNode currentNode ;
                        var 
parentNode includedNode.parentNode ;

                        
// This node is about to be included completelly, but,
                        // if this is the last node in its parent, we must also
                        // check if the parent itself can be added completelly
                        // to the range.
                        
while ( includedNode == parentNode.lastChild
                            
&& elementDTDparentNode.nodeName.toLowerCase() ] )
                        {
                            
includedNode parentNode ;
                        }

                        
range.SetEndincludedNode) ;

                        
// If the included node is the last node in its parent
                        // and its parent can't be inside the style node, apply
                        // the style immediately.
                        
if ( includedNode == includedNode.parentNode.lastChild && !elementDTDincludedNode.parentNode.nodeName.toLowerCase() ] )
                            
applyStyle true ;
                    }
                    else
                    {
                        
// Element nodes will not be added directly. We need to
                        // check their children because the selection could end
                        // inside the node, so let's place the range end right
                        // before the element.
                        
range.SetEndcurrentNode) ;
                    }
                }
                else
                    
applyStyle true ;
            }
            else
                
applyStyle true ;

            
// Get the next node to be processed.
            
currentNode FCKDomTools.GetNextSourceNodecurrentNode ) ;

            
// If we have reached the end of the selection, just apply the
            // style ot the range, and stop looping.
            
if ( currentNode == endNode )
            {
                
currentNode null ;
                
applyStyle true ;
            }

            
// Apply the style if we have something to which apply it.
            
if ( applyStyle && range.CheckHasRange() && !range.CheckIsCollapsed() )
            {
                
// Build the style element, based on the style object definition.
                
styleNode this.BuildElementdoc ) ;

                
// Move the contents of the range to the style element.
                
range.ExtractContents().AppendTostyleNode ) ;

                
// If it is not empty.
                
if ( styleNode.innerHTML.RTrim().length )
                {
                    
// Insert it in the range position (it is collapsed after
                    // ExtractContents.
                    
range.InsertNodestyleNode ) ;

                    
// Here we do some cleanup, removing all duplicated
                    // elements from the style element.
                    
this.RemoveFromElementstyleNode ) ;

                    
// Let's merge our new style with its neighbors, if possible.
                    
this._MergeSiblingsstyleNodethis._GetAttribsForComparison() ) ;

                    
// As the style system breaks text nodes constantly, let's normalize
                    // things for performance.
                    // With IE, some paragraphs get broken when calling normalize()
                    // repeatedly. Also, for IE, we must normalize body, not documentElement.
                    // IE is also known for having a "crash effect" with normalize().
                    // We should try to normalize with IE too in some way, somewhere.
                    
if ( !FCKBrowserInfo.IsIE )
                        
styleNode.normalize() ;
                }

                
// Style applied, let's release the range, so it gets marked to
                // re-initialization in the next loop.
                
range.Releasetrue ) ;
            }
        }

        
this._FixBookmarkStartstartNode ) ;

        
// Re-select the original range.
        
if ( selectIt )
            
range.SelectBookmarkbookmark ) ;

        if ( 
updateRange )
            
range.MoveToBookmarkbookmark ) ;
    },

    
_FixBookmarkStart : function( startNode )
    {
        
// After appliying or removing an inline style, the start boundary of
        // the selection must be placed inside all inline elements it is
        // bordering.
        
var startSibling ;
        while ( ( 
startSibling startNode.nextSibling ) )    // Only one "=".
        
{
            if ( 
startSibling.nodeType == 1
                
&& FCKListsLib.InlineNonEmptyElementsstartSibling.nodeName.toLowerCase() ] )
            {
                
// If it is an empty inline element, we can safely remove it.
                
if ( !startSibling.firstChild )
                    
FCKDomTools.RemoveNodestartSibling ) ;
                else
                    
FCKDomTools.MoveNodestartNodestartSiblingtrue ) ;
                continue ;
            }

            
// Empty text nodes can be safely removed to not disturb.
            
if ( startSibling.nodeType == && startSibling.length == )
            {
                
FCKDomTools.RemoveNodestartSibling ) ;
                continue ;
            }

            break ;
        }
    },

    
/**
     * Merge an element with its similar siblings.
     * "attribs" is and object computed with _CreateAttribsForComparison.
     */
    
_MergeSiblings : function( elementattribs )
    {
        if ( !
element || element.nodeType != || !FCKListsLib.InlineNonEmptyElementselement.nodeName.toLowerCase() ] )
            return ;

        
this._MergeNextSiblingelementattribs ) ;
        
this._MergePreviousSiblingelementattribs ) ;
    },

    
/**
     * Merge an element with its similar siblings after it.
     * "attribs" is and object computed with _CreateAttribsForComparison.
     */
    
_MergeNextSibling : function( elementattribs )
    {
        
// Check the next sibling.
        
var sibling element.nextSibling ;

        
// Check if the next sibling is a bookmark element. In this case, jump it.
        
var hasBookmark = ( sibling && sibling.nodeType == && sibling.getAttribute'_fck_bookmark' ) ) ;
        if ( 
hasBookmark )
            
sibling sibling.nextSibling ;

        if ( 
sibling && sibling.nodeType == && sibling.nodeName == element.nodeName )
        {
            if ( !
attribs )
                
attribs this._CreateElementAttribsForComparisonelement ) ;

            if ( 
this._CheckAttributesMatchsiblingattribs ) )
            {
                
// Save the last child to be checked too (to merge things like <b><i></i></b><b><i></i></b>).
                
var innerSibling element.lastChild ;

                if ( 
hasBookmark )
                    
FCKDomTools.MoveNodeelement.nextSiblingelement ) ;

                
// Move contents from the sibling.
                
FCKDomTools.MoveChildrensiblingelement ) ;
                
FCKDomTools.RemoveNodesibling ) ;

                
// Now check the last inner child (see two comments above).
                
if ( innerSibling )
                    
this._MergeNextSiblinginnerSibling ) ;
            }
        }
    },

    
/**
     * Merge an element with its similar siblings before it.
     * "attribs" is and object computed with _CreateAttribsForComparison.
     */
    
_MergePreviousSibling : function( elementattribs )
    {
        
// Check the previous sibling.
        
var sibling element.previousSibling ;

        
// Check if the previous sibling is a bookmark element. In this case, jump it.
        
var hasBookmark = ( sibling && sibling.nodeType == && sibling.getAttribute'_fck_bookmark' ) ) ;
        if ( 
hasBookmark )
            
sibling sibling.previousSibling ;

        if ( 
sibling && sibling.nodeType == && sibling.nodeName == element.nodeName )
        {
            if ( !
attribs )
                
attribs this._CreateElementAttribsForComparisonelement ) ;

            if ( 
this._CheckAttributesMatchsiblingattribs ) )
            {
                
// Save the first child to be checked too (to merge things like <b><i></i></b><b><i></i></b>).
                
var innerSibling element.firstChild ;

                if ( 
hasBookmark )
                    
FCKDomTools.MoveNodeelement.previousSiblingelementtrue ) ;

                
// Move contents to the sibling.
                
FCKDomTools.MoveChildrensiblingelementtrue ) ;
                
FCKDomTools.RemoveNodesibling ) ;

                
// Now check the first inner child (see two comments above).
                
if ( innerSibling )
                    
this._MergePreviousSiblinginnerSibling ) ;
            }
        }
    },

    
/**
     * Build the cssText based on the styles definition.
     */
    
_GetStyleText : function()
    {
        var 
stylesDef this._StyleDesc.Styles ;

        
// Builds the StyleText.
        
var stylesText = ( this._StyleDesc.Attributes this._StyleDesc.Attributes['style'] || '' '' ) ;

        if ( 
stylesText.length )
            
stylesText += ';' ;

        for ( var 
style in stylesDef )
            
stylesText += style ':' stylesDef[style] + ';' ;

        
// Browsers make some changes to the style when applying them. So, here
        // we normalize it to the browser format. We'll not do that if there
        // are variables inside the style.
        
if ( stylesText.length && !( /#(/.test( stylesText ) ) )
        
{
            
stylesText FCKTools.NormalizeCssTextstylesText ) ;
        }

        return (
this._GetStyleText = function() { return stylesText ; })() ;
    },

    
/**
     * Get the the collection used to compare the attributes defined in this
     * style with attributes in an element. All information in it is lowercased.
     */
    
_GetAttribsForComparison : function()
    {
        
// If we have already computed it, just return it.
        
var attribs this._GetAttribsForComparison_$ ;
        if ( 
attribs )
            return 
attribs ;

        
attribs = new Object() ;

        
// Loop through all defined attributes.
        
var styleAttribs this._StyleDesc.Attributes ;
        if ( 
styleAttribs )
        {
            for ( var 
styleAtt in styleAttribs )
            {
                
attribsstyleAtt.toLowerCase() ] = styleAttribsstyleAtt ].toLowerCase() ;
            }
        }

        
// Includes the style definitions.
        
if ( this._GetStyleText().length )
        {
            
attribs['style'] = this._GetStyleText().toLowerCase() ;
        }

        
// Appends the "length" information to the object.
        
FCKTools.AppendLengthPropertyattribs'_length' ) ;

        
// Return it, saving it to the next request.
        
return ( this._GetAttribsForComparison_$ = attribs ) ;
    },

    
/**
     * Get the the collection used to compare the elements and attributes,
     * defined in this style overrides, with other element. All information in
     * it is lowercased.
     */
    
_GetOverridesForComparison : function()
    {
        
// If we have already computed it, just return it.
        
var overrides this._GetOverridesForComparison_$ ;
        if ( 
overrides )
            return 
overrides ;

        
overrides = new Object() ;

        var 
overridesDesc this._StyleDesc.Overrides ;

        if ( 
overridesDesc )
        {
            
// The override description can be a string, object or array.
            // Internally, well handle arrays only, so transform it if needed.
            
if ( !FCKTools.IsArrayoverridesDesc ) )
                
overridesDesc = [ overridesDesc ] ;

            
// Loop through all override definitions.
            
for ( var overridesDesc.length i++ )
            {
                var 
override overridesDesc[i] ;
                var 
elementName ;
                var 
overrideEl ;
                var 
attrs ;

                
// If can be a string with the element name.
                
if ( typeof override == 'string' )
                    
elementName override.toLowerCase() ;
                
// Or an object.
                
else
                {
                    
elementName override.Element override.Element.toLowerCase() : this.Element ;
                    
attrs override.Attributes ;
                }

                
// We can have more than one override definition for the same
                // element name, so we attempt to simply append information to
                // it if it already exists.
                
overrideEl overrideselementName ] || ( overrideselementName ] = {} ) ;

                if ( 
attrs )
                {
                    
// The returning attributes list is an array, because we
                    // could have different override definitions for the same
                    // attribute name.
                    
var overrideAttrs = ( overrideEl.Attributes overrideEl.Attributes || new Array() ) ;
                    for ( var 
attName in attrs )
                    {
                        
// Each item in the attributes array is also an array,
                        // where [0] is the attribute name and [1] is the
                        // override value.
                        
overrideAttrs.push( [ attName.toLowerCase(), attrsattName ] ] ) ;
                    }
                }
            }
        }

        return ( 
this._GetOverridesForComparison_$ = overrides ) ;
    },

    
/*
     * Create and object containing all attributes specified in an element,
     * added by a "_length" property. All values are lowercased.
     */
    
_CreateElementAttribsForComparison : function( element )
    {
        var 
attribs = new Object() ;
        var 
attribsCount ;

        for ( var 
element.attributes.length i++ )
        {
            var 
att element.attributes[i] ;

            if ( 
att.specified )
            {
                
attribsatt.nodeName.toLowerCase() ] = FCKDomTools.GetAttributeValueelementatt ).toLowerCase() ;
                
attribsCount++ ;
            }
        }

        
attribs._length attribsCount ;

        return 
attribs ;
    },

    
/**
     * Checks is the element attributes have a perfect match with the style
     * attributes.
     */
    
_CheckAttributesMatch : function( elementstyleAttribs )
    {
        
// Loop through all specified attributes. The same number of
        // attributes must be found and their values must match to
        // declare them as equal.

        
var elementAttrbs element.attributes ;
        var 
matchCount ;

        for ( var 
elementAttrbs.length i++ )
        {
            var 
att elementAttrbs[i] ;
            if ( 
att.specified )
            {
                var 
attName att.nodeName.toLowerCase() ;
                var 
styleAtt styleAttribsattName ] ;

                
// The attribute is not defined in the style.
                
if ( !styleAtt )
                    break ;

                
// The values are different.
                
if ( styleAtt != FCKDomTools.GetAttributeValueelementatt ).toLowerCase() )
                    break ;

                
matchCount++ ;
            }
        }

        return ( 
matchCount == styleAttribs._length ) ;
    }
} ;
?>
Онлайн: 1
Реклама