Вход Регистрация
Файл: fckeditor/editor/_source/classes/fckw3crange.js
Строк: 684
<?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 ==
 *
 * This class partially implements the W3C DOM Range for browser that don't
 * support the standards (like IE):
 * http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html
 */

var FCKW3CRange = function( parentDocument )
{
    
this._Document parentDocument ;

    
this.startContainer    null ;
    
this.startOffset    null ;
    
this.endContainer    null ;
    
this.endOffset        null ;
    
this.collapsed        true ;
}

FCKW3CRange.CreateRange = function( parentDocument )
{
    
// We could opt to use the Range implementation of the browsers. The problem
    // is that every browser have different bugs on their implementations,
    // mostly related to different interpretations of the W3C specifications.
    // So, for now, let's use our implementation and pray for browsers fixings
    // soon. Otherwise will go crazy on trying to find out workarounds.
    /*
    // Get the browser implementation of the range, if available.
    if ( parentDocument.createRange )
    {
        var range = parentDocument.createRange() ;
        if ( typeof( range.startContainer ) != 'undefined' )
            return range ;
    }
    */
    
return new FCKW3CRangeparentDocument ) ;
}

FCKW3CRange.CreateFromRange = function( parentDocumentsourceRange )
{
    var 
range FCKW3CRange.CreateRangeparentDocument ) ;
    
range.setStartsourceRange.startContainersourceRange.startOffset ) ;
    
range.setEndsourceRange.endContainersourceRange.endOffset ) ;
    return 
range ;
}

FCKW3CRange.prototype =
{

    
_UpdateCollapsed : function()
    {
      
this.collapsed = ( this.startContainer == this.endContainer && this.startOffset == this.endOffset ) ;
    },

    
// W3C requires a check for the new position. If it is after the end
    // boundary, the range should be collapsed to the new start. It seams we
    // will not need this check for our use of this class so we can ignore it for now.
    
setStart : function( refNodeoffset )
    {
        
this.startContainer    refNode ;
        
this.startOffset    offset ;

        if ( !
this.endContainer )
        {
            
this.endContainer    refNode ;
            
this.endOffset        offset ;
        }

        
this._UpdateCollapsed() ;
    },

    
// W3C requires a check for the new position. If it is before the start
    // boundary, the range should be collapsed to the new end. It seams we
    // will not need this check for our use of this class so we can ignore it for now.
    
setEnd : function( refNodeoffset )
    {
        
this.endContainer    refNode ;
        
this.endOffset        offset ;

        if ( !
this.startContainer )
        {
            
this.startContainer    refNode ;
            
this.startOffset    offset ;
        }

        
this._UpdateCollapsed() ;
    },

    
setStartAfter : function( refNode )
    {
        
this.setStartrefNode.parentNodeFCKDomTools.GetIndexOfrefNode ) + ) ;
    },

    
setStartBefore : function( refNode )
    {
        
this.setStartrefNode.parentNodeFCKDomTools.GetIndexOfrefNode ) ) ;
    },

    
setEndAfter : function( refNode )
    {
        
this.setEndrefNode.parentNodeFCKDomTools.GetIndexOfrefNode ) + ) ;
    },

    
setEndBefore : function( refNode )
    {
        
this.setEndrefNode.parentNodeFCKDomTools.GetIndexOfrefNode ) ) ;
    },

    
collapse : function( toStart )
    {
        if ( 
toStart )
        {
            
this.endContainer    this.startContainer ;
            
this.endOffset        this.startOffset ;
        }
        else
        {
            
this.startContainer    this.endContainer ;
            
this.startOffset    this.endOffset ;
        }

        
this.collapsed true ;
    },

    
selectNodeContents : function( refNode )
    {
        
this.setStartrefNode) ;
        
this.setEndrefNoderefNode.nodeType == refNode.data.length refNode.childNodes.length ) ;
    },

    
insertNode : function( newNode )
    {
        var 
startContainer this.startContainer ;
        var 
startOffset this.startOffset ;

        
// If we are in a text node.
        
if ( startContainer.nodeType == )
        {
            
startContainer.splitTextstartOffset ) ;

            
// Check if it is necessary to update the end boundary.
            
if ( startContainer == this.endContainer )
                
this.setEndstartContainer.nextSiblingthis.endOffset this.startOffset ) ;

            
// Insert the new node it after the text node.
            
FCKDomTools.InsertAfterNodestartContainernewNode ) ;

            return ;
        }
        else
        {
            
// Simply insert the new node before the current start node.
            
startContainer.insertBeforenewNodestartContainer.childNodesstartOffset ] || null ) ;

            
// Check if it is necessary to update the end boundary.
            
if ( startContainer == this.endContainer )
            {
                
this.endOffset++ ;
                
this.collapsed false ;
            }
        }
    },

    
deleteContents : function()
    {
        if ( 
this.collapsed )
            return ;

        
this._ExecContentsAction) ;
    },

    
extractContents : function()
    {
        var 
docFrag = new FCKDocumentFragmentthis._Document ) ;

        if ( !
this.collapsed )
            
this._ExecContentsAction1docFrag ) ;

        return 
docFrag ;
    },

    
// The selection may be lost when cloning (due to the splitText() call).
    
cloneContents : function()
    {
        var 
docFrag = new FCKDocumentFragmentthis._Document ) ;

        if ( !
this.collapsed )
            
this._ExecContentsAction2docFrag ) ;

        return 
docFrag ;
    },

    
_ExecContentsAction : function( actiondocFrag )
    {
        var 
startNode    this.startContainer ;
        var 
endNode        this.endContainer ;

        var 
startOffset    this.startOffset ;
        var 
endOffset    this.endOffset ;

        var 
removeStartNode    false ;
        var 
removeEndNode    false ;

        
// Check the start and end nodes and make the necessary removals or changes.

        // Start from the end, otherwise DOM mutations (splitText) made in the
        // start boundary may interfere on the results here.

        // For text containers, we must simply split the node and point to the
        // second part. The removal will be handled by the rest of the code .
        
if ( endNode.nodeType == )
            
endNode endNode.splitTextendOffset ) ;
        else
        {
            
// If the end container has children and the offset is pointing
            // to a child, then we should start from it.
            
if ( endNode.childNodes.length )
            {
                
// If the offset points after the last node.
                
if ( endOffset endNode.childNodes.length )
                {
                    
// Let's create a temporary node and mark it for removal.
                    
endNode FCKDomTools.InsertAfterNodeendNode.lastChildthis._Document.createTextNode('') ) ;
                    
removeEndNode true ;
                }
                else
                    
endNode endNode.childNodesendOffset ] ;
            }
        }

        
// For text containers, we must simply split the node. The removal will
        // be handled by the rest of the code .
        
if ( startNode.nodeType == )
        {
            
startNode.splitTextstartOffset ) ;

            
// In cases the end node is the same as the start node, the above
            // splitting will also split the end, so me must move the end to
            // the second part of the split.
            
if ( startNode == endNode )
                
endNode startNode.nextSibling ;
        }
        else
        {
            
// If the start container has children and the offset is pointing
            // to a child, then we should start from its previous sibling.

            // If the offset points to the first node, we don't have a
            // sibling, so let's use the first one, but mark it for removal.
            
if ( startOffset == )
            {
                
// Let's create a temporary node and mark it for removal.
                
startNode startNode.insertBeforethis._Document.createTextNode(''), startNode.firstChild ) ;
                
removeStartNode true ;
            }
            else if ( 
startOffset startNode.childNodes.length )
            {
                
// Let's create a temporary node and mark it for removal.
                
startNode startNode.appendChildthis._Document.createTextNode('') ) ;
                
removeStartNode true ;
            }
            else
                
startNode startNode.childNodesstartOffset ].previousSibling ;
        }

        
// Get the parent nodes tree for the start and end boundaries.
        
var startParents    FCKDomTools.GetParentsstartNode ) ;
        var 
endParents        FCKDomTools.GetParentsendNode ) ;

        
// Compare them, to find the top most siblings.
        
var itopStarttopEnd ;

        for ( 
startParents.length i++ )
        {
            
topStart    startParents[i] ;
            
topEnd        endParents[i] ;

            
// The compared nodes will match until we find the top most
            // siblings (different nodes that have the same parent).
            // "i" will hold the index in the parents array for the top
            // most element.
            
if ( topStart != topEnd )
                break ;
        }

        var clone, 
levelStartNodelevelClonecurrentNodecurrentSibling ;

        if ( 
docFrag )
            clone = 
docFrag.RootNode ;

        
// Remove all successive sibling nodes for every node in the
        // startParents tree.
        
for ( var startParents.length j++ )
        {
            
levelStartNode startParents[j] ;

            
// For Extract and Clone, we must clone this level.
            
if ( clone && levelStartNode != startNode )        // action = 0 = Delete
                
levelClone = clone.appendChildlevelStartNode.cloneNodelevelStartNode == startNode ) ) ;

            
currentNode levelStartNode.nextSibling ;

            while( 
currentNode )
            {
                
// Stop processing when the current node matches a node in the
                // endParents tree or if it is the endNode.
                
if ( currentNode == endParents[j] || currentNode == endNode )
                    break ;

                
// Cache the next sibling.
                
currentSibling currentNode.nextSibling ;

                
// If cloning, just clone it.
                
if ( action == )    // 2 = Clone
                    
clone.appendChildcurrentNode.cloneNodetrue ) ) ;
                else
                {
                    
// Both Delete and Extract will remove the node.
                    
currentNode.parentNode.removeChildcurrentNode ) ;

                    
// When Extracting, move the removed node to the docFrag.
                    
if ( action == )    // 1 = Extract
                        
clone.appendChildcurrentNode ) ;
                }

                
currentNode currentSibling ;
            }

            if ( clone )
                clone = 
levelClone ;
        }

        if ( 
docFrag )
            clone = 
docFrag.RootNode ;

        
// Remove all previous sibling nodes for every node in the
        // endParents tree.
        
for ( var endParents.length k++ )
        {
            
levelStartNode endParents[k] ;

            
// For Extract and Clone, we must clone this level.
            
if ( action && levelStartNode != endNode )        // action = 0 = Delete
                
levelClone = clone.appendChildlevelStartNode.cloneNodelevelStartNode == endNode ) ) ;

            
// The processing of siblings may have already been done by the parent.
            
if ( !startParents[k] || levelStartNode.parentNode != startParents[k].parentNode )
            {
                
currentNode levelStartNode.previousSibling ;

                while( 
currentNode )
                {
                    
// Stop processing when the current node matches a node in the
                    // startParents tree or if it is the startNode.
                    
if ( currentNode == startParents[k] || currentNode == startNode )
                        break ;

                    
// Cache the next sibling.
                    
currentSibling currentNode.previousSibling ;

                    
// If cloning, just clone it.
                    
if ( action == )    // 2 = Clone
                        
clone.insertBeforecurrentNode.cloneNodetrue ), clone.firstChild ) ;
                    else
                    {
                        
// Both Delete and Extract will remove the node.
                        
currentNode.parentNode.removeChildcurrentNode ) ;

                        
// When Extracting, mode the removed node to the docFrag.
                        
if ( action == )    // 1 = Extract
                            
clone.insertBeforecurrentNode, clone.firstChild ) ;
                    }

                    
currentNode currentSibling ;
                }
            }

            if ( clone )
                clone = 
levelClone ;
        }

        if ( 
action == )        // 2 = Clone.
        
{
            
// No changes in the DOM should be done, so fix the split text (if any).

            
var startTextNode this.startContainer ;
            if ( 
startTextNode.nodeType == )
            {
                
startTextNode.data += startTextNode.nextSibling.data ;
                
startTextNode.parentNode.removeChildstartTextNode.nextSibling ) ;
            }

            var 
endTextNode this.endContainer ;
            if ( 
endTextNode.nodeType == && endTextNode.nextSibling )
            {
                
endTextNode.data += endTextNode.nextSibling.data ;
                
endTextNode.parentNode.removeChildendTextNode.nextSibling ) ;
            }
        }
        else
        {
            
// Collapse the range.

            // If a node has been partially selected, collapse the range between
            // topStart and topEnd. Otherwise, simply collapse it to the start. (W3C specs).
            
if ( topStart && topEnd && ( startNode.parentNode != topStart.parentNode || endNode.parentNode != topEnd.parentNode ) )
            {
                var 
endIndex FCKDomTools.GetIndexOftopEnd ) ;

                
// If the start node is to be removed, we must correct the
                // index to reflect the removal.
                
if ( removeStartNode && topEnd.parentNode == startNode.parentNode )
                    
endIndex-- ;

                
this.setStarttopEnd.parentNodeendIndex ) ;
            }

            
// Collapse it to the start.
            
this.collapsetrue ) ;
        }

        
// Cleanup any marked node.
        
if( removeStartNode )
            
startNode.parentNode.removeChildstartNode ) ;

        if( 
removeEndNode && endNode.parentNode )
            
endNode.parentNode.removeChildendNode ) ;
    },

    
cloneRange : function()
    {
        return 
FCKW3CRange.CreateFromRangethis._Documentthis ) ;
    }
} ;
?>
Онлайн: 0
Реклама