Вход Регистрация
Файл: fckeditor/editor/_source/commandclasses/fckblockquotecommand.js
Строк: 267
<?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 ==
 *
 * FCKBlockQuoteCommand Class: adds or removes blockquote tags.
 */

var FCKBlockQuoteCommand = function()
{
}

FCKBlockQuoteCommand.prototype =
{
    
Execute : function()
    {
        
FCKUndo.SaveUndoStep() ;

        var 
state this.GetState() ;

        var 
range = new FCKDomRangeFCK.EditorWindow ) ;
        
range.MoveToSelection() ;

        var 
bookmark range.CreateBookmark() ;

        
// Kludge for #1592: if the bookmark nodes are in the beginning of
        // blockquote, then move them to the nearest block element in the
        // blockquote.
        
if ( FCKBrowserInfo.IsIE )
        {
            var 
bStart    range.GetBookmarkNodebookmarktrue ) ;
            var 
bEnd    range.GetBookmarkNodebookmarkfalse ) ;

            var 
cursor ;

            if ( 
bStart
                    
&& bStart.parentNode.nodeName.IEquals'blockquote' )
                    && !
bStart.previousSibling )
            {
                
cursor bStart ;
                while ( ( 
cursor cursor.nextSibling ) )
                {
                    if ( 
FCKListsLib.BlockElementscursor.nodeName.toLowerCase() ] )
                        
FCKDomTools.MoveNodebStartcursortrue ) ;
                }
            }

            if ( 
bEnd
                    
&& bEnd.parentNode.nodeName.IEquals'blockquote' )
                    && !
bEnd.previousSibling )
            {
                
cursor bEnd ;
                while ( ( 
cursor cursor.nextSibling ) )
                {
                    if ( 
FCKListsLib.BlockElementscursor.nodeName.toLowerCase() ] )
                    {
                        if ( 
cursor.firstChild == bStart )
                            
FCKDomTools.InsertAfterNodebStartbEnd ) ;
                        else
                            
FCKDomTools.MoveNodebEndcursortrue ) ;
                    }
                }
            }
        }

        var 
iterator = new FCKDomRangeIteratorrange ) ;
        var 
block ;

        if ( 
state == FCK_TRISTATE_OFF )
        {
            
iterator.EnforceRealBlocks true ;
            var 
paragraphs = [] ;
            while ( ( 
block iterator.GetNextParagraph() ) )
                
paragraphs.pushblock ) ;

            
// If no paragraphs, create one from the current selection position.
            
if ( paragraphs.length )
            {
                
para range.Window.document.createElementFCKConfig.EnterMode.IEquals'p' ) ? 'p' 'div' ) ;
                
range.InsertNodepara ) ;
                
para.appendChildrange.Window.document.createTextNode'ufeff' ) ) ;
                
range.MoveToBookmarkbookmark ) ;
                
range.MoveToNodeContentspara ) ;
                
range.Collapsetrue ) ;
                
bookmark range.CreateBookmark() ;
                
paragraphs.pushpara ) ;
            }

            
// Make sure all paragraphs have the same parent.
            
var commonParent paragraphs[0].parentNode ;
            var 
tmp = [] ;
            for ( var 
paragraphs.length i++ )
            {
                
block paragraphs[i] ;
                
commonParent FCKDomTools.GetCommonParentsblock.parentNodecommonParent ).pop() ;
            }
            var 
lastBlock null ;
            while ( 
paragraphs.length )
            {
                
block paragraphs.shift() ;
                while ( 
block.parentNode != commonParent )
                    
block block.parentNode ;
                if ( 
block != lastBlock )
                    
tmp.pushblock ) ;
                
lastBlock block ;
            }

            
// If any of the selected blocks is a blockquote, remove it to prevent nested blockquotes.
            
while ( tmp.length )
            {
                
block tmp.shift() ;
                if ( 
block.nodeName.IEquals'blockquote' ) )
                {
                    var 
docFrag FCKTools.GetElementDocumentblock ).createDocumentFragment() ;
                    while ( 
block.firstChild )
                    {
                        
docFrag.appendChildblock.removeChildblock.firstChild ) ) ;
                        
paragraphs.pushdocFrag.lastChild ) ;
                    }
                    
block.parentNode.replaceChilddocFragblock ) ;
                }
                else
                    
paragraphs.pushblock ) ;
            }

            
// Now we have all the blocks to be included in a new blockquote node.
            
var bqBlock range.Window.document.createElement'blockquote' ) ;
            
commonParent.insertBeforebqBlockparagraphs[0] ) ;
            while ( 
paragraphs.length )
            {
                
block paragraphs.shift() ;
                
bqBlock.appendChildblock ) ;
            }
        }
        else if ( 
state == FCK_TRISTATE_ON )
        {
            var 
moveOutNodes = [] ;
            while ( ( 
block iterator.GetNextParagraph() ) )
            {
                var 
bqParent null ;
                var 
bqChild null ;
                while ( 
block.parentNode )
                {
                    if ( 
block.parentNode.nodeName.IEquals'blockquote' ) )
                    {
                        
bqParent block.parentNode ;
                        
bqChild block ;
                        break ;
                    }
                    
block block.parentNode ;
                }

                if ( 
bqParent && bqChild )
                    
moveOutNodes.pushbqChild ) ;
            }

            var 
movedNodes = [] ;
            while ( 
moveOutNodes.length )
            {
                var 
node moveOutNodes.shift() ;
                var 
bqBlock node.parentNode ;

                
// If the node is located at the beginning or the end, just take it out without splitting.
                // Otherwise, split the blockquote node and move the paragraph in between the two blockquote nodes.
                
if ( node == node.parentNode.firstChild )
                {
                    
bqBlock.parentNode.insertBeforebqBlock.removeChildnode ), bqBlock ) ;
                    if ( ! 
bqBlock.firstChild )
                        
bqBlock.parentNode.removeChildbqBlock ) ;
                }
                else if ( 
node == node.parentNode.lastChild )
                {
                    
bqBlock.parentNode.insertBeforebqBlock.removeChildnode ), bqBlock.nextSibling ) ;
                    if ( ! 
bqBlock.firstChild )
                        
bqBlock.parentNode.removeChildbqBlock ) ;
                }
                else
                    
FCKDomTools.BreakParentnodenode.parentNoderange ) ;

                
movedNodes.pushnode ) ;
            }

            if ( 
FCKConfig.EnterMode.IEquals'br' ) )
            {
                while ( 
movedNodes.length )
                {
                    var 
node movedNodes.shift() ;
                    var 
firstTime true ;
                    if ( 
node.nodeName.IEquals'div' ) )
                    {
                        var 
docFrag FCKTools.GetElementDocumentnode ).createDocumentFragment() ;
                        var 
needBeginBr firstTime && node.previousSibling &&
                            !
FCKListsLib.BlockBoundaries[node.previousSibling.nodeName.toLowerCase()] ;
                        if ( 
firstTime && needBeginBr )
                            
docFrag.appendChildFCKTools.GetElementDocumentnode ).createElement'br' ) ) ;
                        var 
needEndBr node.nextSibling &&
                            !
FCKListsLib.BlockBoundaries[node.nextSibling.nodeName.toLowerCase()] ;
                        while ( 
node.firstChild )
                            
docFrag.appendChildnode.removeChildnode.firstChild ) ) ;
                        if ( 
needEndBr )
                            
docFrag.appendChildFCKTools.GetElementDocumentnode ).createElement'br' ) ) ;
                        
node.parentNode.replaceChilddocFragnode ) ;
                        
firstTime false ;
                    }
                }
            }
        }
        
range.MoveToBookmarkbookmark ) ;
        
range.Select() ;

        
FCK.Focus() ;
        
FCK.Events.FireEvent'OnSelectionChange' ) ;
    },

    
GetState : function()
    {
        
// Disabled if not WYSIWYG.
        
if ( FCK.EditMode != FCK_EDITMODE_WYSIWYG || ! FCK.EditorWindow )
            return 
FCK_TRISTATE_DISABLED ;

        var 
path = new FCKElementPathFCKSelection.GetBoundaryParentElementtrue ) ) ;
        var 
firstBlock path.Block || path.BlockLimit ;

        if ( !
firstBlock || firstBlock.nodeName.toLowerCase() == 'body' )
            return 
FCK_TRISTATE_OFF ;

        
// See if the first block has a blockquote parent.
        
for ( var path.Elements.length i++ )
        {
            if ( 
path.Elements[i].nodeName.IEquals'blockquote' ) )
                return 
FCK_TRISTATE_ON ;
        }
        return 
FCK_TRISTATE_OFF ;
    }
} ;
?>
Онлайн: 0
Реклама