Вход Регистрация
Файл: fckeditor/editor/_source/internals/fcktools.js
Строк: 786
<?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 ==
 *
 * Utility functions.
 */

var FCKTools = new Object() ;

FCKTools.CreateBogusBR = function( targetDocument )
{
    var 
eBR targetDocument.createElement'br' ) ;
//    eBR.setAttribute( '_moz_editor_bogus_node', 'TRUE' ) ;
    
eBR.setAttribute'type''_moz' ) ;
    return 
eBR ;
}

/**
 * Fixes relative URL entries defined inside CSS styles by appending a prefix
 * to them.
 * @param (String) cssStyles The CSS styles definition possibly containing url()
 *        paths.
 * @param (String) urlFixPrefix The prefix to append to relative URLs.
 */
FCKTools.FixCssUrls = function( urlFixPrefixcssStyles )
{
    if ( !
urlFixPrefix || urlFixPrefix.length == )
        return 
cssStyles ;

    return 
cssStyles.replace( /urls*(([s'"]*)(.*?)([s"']*))/g, function( matchopenerpathcloser )
        {
            if ( /^/|^
w?:/.testpath ) )
                return 
match ;
            else
                return 
'url(' opener urlFixPrefix path closer ')' ;
        } ) ;
}

FCKTools._GetUrlFixedCss = function( cssStylesurlFixPrefix )
{
    var 
match cssStyles.match( /^([^|]+)|([sS]*)/ ) ;

    if ( 
match )
        return 
FCKTools.FixCssUrlsmatch[1], match[2] ) ;
    else
        return 
cssStyles ;
}

/**
 * Appends a <link css> or <style> element to the document.
 * @param (Object) documentElement The DOM document object to which append the
 *        stylesheet.
 * @param (Variant) cssFileOrDef A String pointing to the CSS file URL or an
 *        Array with many CSS file URLs or the CSS definitions for the <style>
 *        element.
 * @return {Array} An array containing all elements created in the target
 *        document. It may include <link> or <style> elements, depending on the
 *        value passed with cssFileOrDef.
 */
FCKTools.AppendStyleSheet = function( domDocumentcssFileOrArrayOrDef )
{
    if ( !
cssFileOrArrayOrDef )
        return [] ;

    if ( 
typeofcssFileOrArrayOrDef ) == 'string' )
    {
        
// Test if the passed argument is an URL.
        
if ( /[\/.][^{}]*$/.testcssFileOrArrayOrDef ) )
        {
            
// The string may have several URLs separated by comma.
            
return this.AppendStyleSheetdomDocumentcssFileOrArrayOrDef.split(',') ) ;
        }
        else
            return [ 
this.AppendStyleStringdomDocumentFCKTools._GetUrlFixedCsscssFileOrArrayOrDef ) ) ] ;
    }
    else
    {
        var 
styles = [] ;
        for ( var 
cssFileOrArrayOrDef.length i++ )
            
styles.pushthis._AppendStyleSheetdomDocumentcssFileOrArrayOrDef[i] ) ) ;
        return 
styles ;
    }
}

FCKTools.GetStyleHtml = (function()
{
    var 
getStyle = function( styleDefmarkTemp )
    {
        if ( 
styleDef.length == )
            return 
'' ;

        var 
temp markTemp ' _fcktemp="true"' '' ;
        return 
'<' 'style type="text/css"' temp '>' styleDef '<' '/style>' ;
    }

    var 
getLink = function( cssFileUrlmarkTemp )
    {
        if ( 
cssFileUrl.length == )
            return 
'' ;

        var 
temp markTemp ' _fcktemp="true"' '' ;
        return 
'<' 'link href="' cssFileUrl '" type="text/css" rel="stylesheet" ' temp '/>' ;
    }

    return function( 
cssFileOrArrayOrDefmarkTemp )
    {
        if ( !
cssFileOrArrayOrDef )
            return 
'' ;

        if ( 
typeofcssFileOrArrayOrDef ) == 'string' )
        {
            
// Test if the passed argument is an URL.
            
if ( /[\/.][^{}]*$/.testcssFileOrArrayOrDef ) )
            {
                
// The string may have several URLs separated by comma.
                
return this.GetStyleHtmlcssFileOrArrayOrDef.split(','), markTemp ) ;
            }
            else
                return 
getStylethis._GetUrlFixedCsscssFileOrArrayOrDef ), markTemp ) ;
        }
        else
        {
            var 
html '' ;

            for ( var 
cssFileOrArrayOrDef.length i++ )
                
html += getLinkcssFileOrArrayOrDef[i], markTemp ) ;

            return 
html ;
        }
    }
})() ;

FCKTools.GetElementDocument = function ( element )
{
    return 
element.ownerDocument || element.document ;
}

// Get the window object where the element is placed in.
FCKTools.GetElementWindow = function( element )
{
    return 
this.GetDocumentWindowthis.GetElementDocumentelement ) ) ;
}

FCKTools.GetDocumentWindow = function( document )
{
    
// With Safari, there is not way to retrieve the window from the document, so we must fix it.
    
if ( FCKBrowserInfo.IsSafari && !document.parentWindow )
        
this.FixDocumentParentWindowwindow.top ) ;

    return 
document.parentWindow || document.defaultView ;
}

/*
    This is a Safari specific function that fix the reference to the parent
    window from the document object.
*/
FCKTools.FixDocumentParentWindow = function( targetWindow )
{
    if ( 
targetWindow.document )
        
targetWindow.document.parentWindow targetWindow ;

    for ( var 
targetWindow.frames.length i++ )
        
FCKTools.FixDocumentParentWindowtargetWindow.frames[i] ) ;
}

FCKTools.HTMLEncode = function( text )
{
    if ( !
text )
        return 
'' ;

    
text text.replace( /&/g'&amp;' ) ;
    
text text.replace( /</g'&lt;' ) ;
    
text text.replace( />/g'&gt;' ) ;

    return 
text ;
}

FCKTools.HTMLDecode = function( text )
{
    if ( !
text )
        return 
'' ;

    
text text.replace( /&gt;/g'>' ) ;
    
text text.replace( /&lt;/g'<' ) ;
    
text text.replace( /&amp;/g'&' ) ;

    return 
text ;
}

FCKTools._ProcessLineBreaksForPMode = function( oEditortextliStatenodestrArray )
{
    var 
closeState ;
    var 
blockStartTag "<p>" ;
    var 
blockEndTag "</p>" ;
    var 
lineBreakTag "<br />" ;
    if ( 
liState )
    {
        
blockStartTag "<li>" ;
        
blockEndTag "</li>" ;
        
closeState ;
    }

    
// Are we currently inside a <p> tag now?
    // If yes, close it at the next double line break.
    
while ( node && node != oEditor.FCK.EditorDocument.body )
    {
        if ( 
node.tagName.toLowerCase() == 'p' )
        {
            
closeState ;
            break;
        }
        
node node.parentNode ;
    }

    for ( var 
text.length i++ )
    {
        var 
text.charAt) ;
        if ( 
== 'r' )
            continue ;

        if ( 
!= 'n' )
        {
            
strArray.push) ;
            continue ;
        }

        
// Now we have encountered a line break.
        // Check if the next character is also a line break.
        
var text.charAt) ;
        if ( 
== 'r' )
        {
            
i++ ;
            
text.charAt) ;
        }
        if ( 
== 'n' )
        {
            
i++ ;    // ignore next character - we have already processed it.
            
if ( closeState )
                
strArray.pushblockEndTag ) ;
            
strArray.pushblockStartTag ) ;
            
closeState ;
        }
        else
            
strArray.pushlineBreakTag ) ;
    }
}

FCKTools._ProcessLineBreaksForDivMode = function( oEditortextliStatenodestrArray )
{
    var 
closeState ;
    var 
blockStartTag "<div>" ;
    var 
blockEndTag "</div>" ;
    if ( 
liState )
    {
        
blockStartTag "<li>" ;
        
blockEndTag "</li>" ;
        
closeState ;
    }

    
// Are we currently inside a <div> tag now?
    // If yes, close it at the next double line break.
    
while ( node && node != oEditor.FCK.EditorDocument.body )
    {
        if ( 
node.tagName.toLowerCase() == 'div' )
        {
            
closeState ;
            break ;
        }
        
node node.parentNode ;
    }

    for ( var 
text.length i++ )
    {
        var 
text.charAt) ;
        if ( 
== 'r' )
            continue ;

        if ( 
!= 'n' )
        {
            
strArray.push) ;
            continue ;
        }

        if ( 
closeState )
        {
            if ( 
strArraystrArray.length ] == blockStartTag )
            {
                
// A div tag must have some contents inside for it to be visible.
                
strArray.push"&nbsp;" ) ;
            }
            
strArray.pushblockEndTag ) ;
        }
        
strArray.pushblockStartTag ) ;
        
closeState ;
    }
    if ( 
closeState )
        
strArray.pushblockEndTag ) ;
}

FCKTools._ProcessLineBreaksForBrMode = function( oEditortextliStatenodestrArray )
{
    var 
closeState ;
    var 
blockStartTag "<br />" ;
    var 
blockEndTag "" ;
    if ( 
liState )
    {
        
blockStartTag "<li>" ;
        
blockEndTag "</li>" ;
        
closeState ;
    }

    for ( var 
text.length i++ )
    {
        var 
text.charAt) ;
        if ( 
== 'r' )
            continue ;

        if ( 
!= 'n' )
        {
            
strArray.push) ;
            continue ;
        }

        if ( 
closeState && blockEndTag.length )
            
strArray.push blockEndTag ) ;
        
strArray.pushblockStartTag ) ;
        
closeState ;
    }
}

FCKTools.ProcessLineBreaks = function( oEditoroConfigtext )
{
    var 
enterMode oConfig.EnterMode.toLowerCase() ;
    var 
strArray = [] ;

    
// Is the caret or selection inside an <li> tag now?
    
var liState ;
    var 
range = new oEditor.FCKDomRangeoEditor.FCK.EditorWindow ) ;
    
range.MoveToSelection() ;
    var 
node range._Range.startContainer ;
    while ( 
node && node.nodeType != )
        
node node.parentNode ;
    if ( 
node && node.tagName.toLowerCase() == 'li' )
        
liState ;

    if ( 
enterMode == 'p' )
        
this._ProcessLineBreaksForPModeoEditortextliStatenodestrArray ) ;
    else if ( 
enterMode == 'div' )
        
this._ProcessLineBreaksForDivModeoEditortextliStatenodestrArray ) ;
    else if ( 
enterMode == 'br' )
        
this._ProcessLineBreaksForBrModeoEditortextliStatenodestrArray ) ;
    return 
strArray.join"" ) ;
}

/**
 * Adds an option to a SELECT element.
 */
FCKTools.AddSelectOption = function( selectElementoptionTextoptionValue )
{
    var 
oOption FCKTools.GetElementDocumentselectElement ).createElement"OPTION" ) ;

    
oOption.text    optionText ;
    
oOption.value    optionValue ;

    
selectElement.options.add(oOption) ;

    return 
oOption ;
}

FCKTools.RunFunction = function( functhisObjectparamsArraytimerWindow )
{
    if ( 
func )
        
this.SetTimeoutfunc0thisObjectparamsArraytimerWindow ) ;
}

FCKTools.SetTimeout = function( funcmillisecondsthisObjectparamsArraytimerWindow )
{
    return ( 
timerWindow || window ).setTimeout(
        function()
        {
            if ( 
paramsArray )
                
func.applythisObject, [].concatparamsArray ) ) ;
            else
                
func.applythisObject ) ;
        },
        
milliseconds ) ;
}

FCKTools.SetInterval = function( funcmillisecondsthisObjectparamsArraytimerWindow )
{
    return ( 
timerWindow || window ).setInterval(
        function()
        {
            
func.applythisObjectparamsArray || [] ) ;
        },
        
milliseconds ) ;
}

FCKTools.ConvertStyleSizeToHtml = function( size )
{
    return 
size.EndsWith'%' ) ? size parseIntsize10 ) ;
}

FCKTools.ConvertHtmlSizeToStyle = function( size )
{
    return 
size.EndsWith'%' ) ? size : ( size 'px' ) ;
}

// START iCM MODIFICATIONS
// Amended to accept a list of one or more ascensor tag names
// Amended to check the element itself before working back up through the parent hierarchy
FCKTools.GetElementAscensor = function( elementascensorTagNames )
{
//    var e = element.parentNode ;
    
var element ;
    var 
lstTags "," ascensorTagNames.toUpperCase() + "," ;

    while ( 
)
    {
        if ( 
lstTags.indexOf"," e.nodeName.toUpperCase() + "," ) != -)
            return 
;

        
e.parentNode ;
    }
    return 
null ;
}
// END iCM MODIFICATIONS

FCKTools.CreateEventListener = function( funcparams )
{
    var 
= function()
    {
        var 
aAllParams = [] ;

        for ( var 
arguments.length i++ )
            
aAllParams.pusharguments[i] ) ;

        
func.applythisaAllParams.concatparams ) ) ;
    }

    return 
;
}

FCKTools.IsStrictMode = function( document )
{
    
// There is no compatMode in Safari, but it seams that it always behave as
    // CSS1Compat, so let's assume it as the default for that browser.
    
return ( 'CSS1Compat' == ( document.compatMode || ( FCKBrowserInfo.IsSafari 'CSS1Compat' null ) ) ) ;
}

// Transforms a "arguments" object to an array.
FCKTools.ArgumentsToArray = function( argsstartIndexmaxLength )
{
    
startIndex startIndex || ;
    
maxLength maxLength || args.length ;

    var 
argsArray = new Array() ;

    for ( var 
startIndex startIndex maxLength && args.length i++ )
        
argsArray.pushargs[i] ) ;

    return 
argsArray ;
}

FCKTools.CloneObject = function( sourceObject )
{
    var 
fCloneCreator = function() {} ;
    
fCloneCreator.prototype sourceObject ;
    return new 
fCloneCreator ;
}

// Appends a bogus <br> at the end of the element, if not yet available.
FCKTools.AppendBogusBr = function( element )
{
    if ( !
element )
        return ;

    var 
eLastChild this.GetLastItemelement.getElementsByTagName('br') ) ;

    if ( !
eLastChild || ( eLastChild.getAttribute'type') != '_moz' && eLastChild.getAttribute'_moz_dirty' ) == null ) )
    {
        var 
doc this.GetElementDocumentelement ) ;

        if ( 
FCKBrowserInfo.IsOpera )
            
element.appendChilddoc.createTextNode('') ) ;
        else
            
element.appendChildthis.CreateBogusBRdoc ) ) ;
    }
}

FCKTools.GetLastItem = function( list )
{
    if ( list.
length )
        return list[ list.
length ] ;

    return 
null ;
}

FCKTools.GetDocumentPosition = function( wnode )
{
    var 
;
    var 
;
    var 
curNode node ;
    var 
prevNode null ;
    var 
curWindow FCKTools.GetElementWindowcurNode ) ;
    while ( 
curNode && !( curWindow == && ( curNode == w.document.body || curNode == w.document.documentElement ) ) )
    {
        
+= curNode.offsetLeft curNode.scrollLeft ;
        
+= curNode.offsetTop curNode.scrollTop ;

        if ( ! 
FCKBrowserInfo.IsOpera )
        {
            var 
scrollNode prevNode ;
            while ( 
scrollNode && scrollNode != curNode )
            {
                
-= scrollNode.scrollLeft ;
                
-= scrollNode.scrollTop ;
                
scrollNode scrollNode.parentNode ;
            }
        }

        
prevNode curNode ;
        if ( 
curNode.offsetParent )
            
curNode curNode.offsetParent ;
        else
        {
            if ( 
curWindow != )
            {
                
curNode curWindow.frameElement ;
                
prevNode null ;
                if ( 
curNode )
                    
curWindow curNode.contentWindow.parent ;
            }
            else
                
curNode null ;
        }
    }

    
// document.body is a special case when it comes to offsetTop and offsetLeft values.
    // 1. It matters if document.body itself is a positioned element;
    // 2. It matters is when we're in IE and the element has no positioned ancestor.
    // Otherwise the values should be ignored.
    
if ( FCKDomTools.GetCurrentElementStylew.document.body'position') != 'static'
            
|| ( FCKBrowserInfo.IsIE && FCKDomTools.GetPositionedAncestornode ) == null ) )
    {
        
+= w.document.body.offsetLeft ;
        
+= w.document.body.offsetTop ;
    }

    return { 
"x" x"y" } ;
}

FCKTools.GetWindowPosition = function( wnode )
{
    var 
pos this.GetDocumentPositionwnode ) ;
    var 
scroll FCKTools.GetScrollPosition) ;
    
pos.-= scroll.;
    
pos.-= scroll.;
    return 
pos ;
}

FCKTools.ProtectFormStyles = function( formNode )
{
    if ( !
formNode || formNode.nodeType != || formNode.tagName.toLowerCase() != 'form' )
        return [] ;
    var 
hijackRecord = [] ;
    var 
hijackNames = [ 'style''className' ] ;
    for ( var 
hijackNames.length i++ )
    {
        var 
name hijackNames[i] ;
        if ( 
formNode.elements.namedItemname ) )
        {
            var 
hijackNode formNode.elements.namedItemname ) ;
            
hijackRecord.push( [ hijackNodehijackNode.nextSibling ] ) ;
            
formNode.removeChildhijackNode ) ;
        }
    }
    return 
hijackRecord ;
}

FCKTools.RestoreFormStyles = function( formNodehijackRecord )
{
    if ( !
formNode || formNode.nodeType != || formNode.tagName.toLowerCase() != 'form' )
        return ;
    if ( 
hijackRecord.length )
    {
        for ( var 
hijackRecord.length >= i-- )
        {
            var 
node hijackRecord[i][0] ;
            var 
sibling hijackRecord[i][1] ;
            if ( 
sibling )
                
formNode.insertBeforenodesibling ) ;
            else
                
formNode.appendChildnode ) ;
        }
    }
}

// Perform a one-step DFS walk.
FCKTools.GetNextNode = function( nodelimitNode )
{
    if ( 
node.firstChild )
        return 
node.firstChild ;
    else if ( 
node.nextSibling )
        return 
node.nextSibling ;
    else
    {
        var 
ancestor node.parentNode ;
        while ( 
ancestor )
        {
            if ( 
ancestor == limitNode )
                return 
null ;
            if ( 
ancestor.nextSibling )
                return 
ancestor.nextSibling ;
            else
                
ancestor ancestor.parentNode ;
        }
    }
    return 
null ;
}

FCKTools.GetNextTextNode = function( textnodelimitNodecheckStop )
{
    
node this.GetNextNodetextnodelimitNode ) ;
    if ( 
checkStop && node && checkStopnode ) )
        return 
null ;
    while ( 
node && node.nodeType != )
    {
        
node this.GetNextNodenodelimitNode ) ;
        if ( 
checkStop && node && checkStopnode ) )
            return 
null ;
    }
    return 
node ;
}

/**
 * Merge all objects passed by argument into a single object.
 */
FCKTools.Merge = function()
{
    var 
args arguments ;
    var 
args[0] ;

    for ( var 
args.length i++ )
    {
        var 
arg args[i] ;
        for ( var 
p in arg )
            
o[p] = arg[p] ;
    }

    return 
;
}

/**
 * Check if the passed argument is a real Array. It may not working when
 * calling it cross windows.
 */
FCKTools.IsArray = function( it )
{
    return ( 
it instanceof Array ) ;
}

/**
 * Appends a "length" property to an object, containing the number of
 * properties available on it, excluded the append property itself.
 */
FCKTools.AppendLengthProperty = function( targetObjectpropertyName )
{
    var 
counter ;

    for ( var 
n in targetObject )
        
counter++ ;

    return 
targetObjectpropertyName || 'length' ] = counter ;
}

/**
 * Gets the browser parsed version of a css text (style attribute value). On
 * some cases, the browser makes changes to the css text, returning a different
 * value. For example, hexadecimal colors get transformed to rgb().
 */
FCKTools.NormalizeCssText = function( unparsedCssText )
{
    
// Injects the style in a temporary span object, so the browser parses it,
    // retrieving its final format.
    
var tempSpan document.createElement'span' ) ;
    
tempSpan.style.cssText unparsedCssText ;
    return 
tempSpan.style.cssText ;
}

/**
 * Binding the "this" reference to an object for a function.
 */
FCKTools.Bind = function( subjectfunc )
{
  return function(){ return 
func.applysubjectarguments ) ; } ;
}

/**
 * Retrieve the correct "empty iframe" URL for the current browser, which
 * causes the minimum fuzz (e.g. security warnings in HTTPS, DNS error in
 * IE5.5, etc.) for that browser, making the iframe ready to DOM use whithout
 * having to loading an external file.
 */
FCKTools.GetVoidUrl = function()
{
    if ( 
FCK_IS_CUSTOM_DOMAIN )
        return 
"javascript: void( function(){" +
            
"document.open();" +
            
"document.write('<html><head><title></title></head><body></body></html>');" +
            
"document.domain = '" FCK_RUNTIME_DOMAIN "';" +
            
"document.close();" +
            
"}() ) ;";

    if ( 
FCKBrowserInfo.IsIE )
    {
        if ( 
FCKBrowserInfo.IsIE7 || !FCKBrowserInfo.IsIE6 )
            return 
"" ;                    // IE7+ / IE5.5
        
else
            return 
"javascript: '';" ;    // IE6+
    
}

    return 
"javascript: void(0);" ;        // All other browsers.
}

FCKTools.ResetStyles = function( element )
{
    
element.style.cssText 'margin:0;' +
        
'padding:0;' +
        
'border:0;' +
        
'background-color:transparent;' +
        
'background-image:none;' ;
}
?>
Онлайн: 3
Реклама