PageRenderTime 54ms CodeModel.GetById 13ms app.highlight 35ms RepoModel.GetById 1ms app.codeStats 1ms

/source/Plug-in/fck/editor/_source/commandclasses/fckblockquotecommand.js

http://prosporous.googlecode.com/
JavaScript | 250 lines | 192 code | 25 blank | 33 comment | 62 complexity | aaa3f5fc9b49f12b15406144bcbccc2d MD5 | raw file
  1/*
  2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
  3 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
  4 *
  5 * == BEGIN LICENSE ==
  6 *
  7 * Licensed under the terms of any of the following licenses at your
  8 * choice:
  9 *
 10 *  - GNU General Public License Version 2 or later (the "GPL")
 11 *    http://www.gnu.org/licenses/gpl.html
 12 *
 13 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 14 *    http://www.gnu.org/licenses/lgpl.html
 15 *
 16 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 17 *    http://www.mozilla.org/MPL/MPL-1.1.html
 18 *
 19 * == END LICENSE ==
 20 *
 21 * FCKBlockQuoteCommand Class: adds or removes blockquote tags.
 22 */
 23
 24var FCKBlockQuoteCommand = function()
 25{
 26}
 27
 28FCKBlockQuoteCommand.prototype = 
 29{
 30	Execute : function()
 31	{
 32		FCKUndo.SaveUndoStep() ;
 33
 34		var state = this.GetState() ;
 35
 36		var range = new FCKDomRange( FCK.EditorWindow ) ;
 37		range.MoveToSelection() ;
 38
 39		var bookmark = range.CreateBookmark() ;
 40
 41		// Kludge for #1592: if the bookmark nodes are in the beginning of
 42		// blockquote, then move them to the nearest block element in the
 43		// blockquote.
 44		if ( FCKBrowserInfo.IsIE )
 45		{
 46			var bStart	= range.GetBookmarkNode( bookmark, true ) ;
 47			var bEnd	= range.GetBookmarkNode( bookmark, false ) ;
 48			
 49			var cursor ;
 50			
 51			if ( bStart 
 52					&& bStart.parentNode.nodeName.IEquals( 'blockquote' )
 53					&& !bStart.previousSibling )
 54			{
 55				cursor = bStart ;
 56				while ( ( cursor = cursor.nextSibling ) )
 57				{
 58					if ( FCKListsLib.BlockElements[ cursor.nodeName.toLowerCase() ] )
 59						FCKDomTools.MoveNode( bStart, cursor, true ) ;
 60				}
 61			}
 62
 63			if ( bEnd 
 64					&& bEnd.parentNode.nodeName.IEquals( 'blockquote' )
 65					&& !bEnd.previousSibling )
 66			{
 67				cursor = bEnd ;
 68				while ( ( cursor = cursor.nextSibling ) )
 69				{
 70					if ( FCKListsLib.BlockElements[ cursor.nodeName.toLowerCase() ] )
 71					{
 72						if ( cursor.firstChild == bStart )
 73							FCKDomTools.InsertAfterNode( bStart, bEnd ) ;
 74						else
 75							FCKDomTools.MoveNode( bEnd, cursor, true ) ;
 76					}
 77				}
 78			}
 79		}
 80
 81		var iterator = new FCKDomRangeIterator( range ) ;
 82		var block ;
 83
 84		if ( state == FCK_TRISTATE_OFF )
 85		{
 86			iterator.EnforceRealBlocks = true ;
 87			var paragraphs = [] ;
 88			while ( ( block = iterator.GetNextParagraph() ) )
 89				paragraphs.push( block ) ;
 90
 91			// If no paragraphs, create one from the current selection position.
 92			if ( paragraphs.length < 1 )
 93			{
 94				para = range.Window.document.createElement( FCKConfig.EnterMode.IEquals( 'p' ) ? 'p' : 'div' ) ;
 95				range.InsertNode( para ) ;
 96				para.appendChild( range.Window.document.createTextNode( '\ufeff' ) ) ;
 97				range.MoveToBookmark( bookmark ) ;
 98				range.MoveToNodeContents( para ) ;
 99				range.Collapse( true ) ;
100				bookmark = range.CreateBookmark() ;
101				paragraphs.push( para ) ;
102			}
103
104			// Make sure all paragraphs have the same parent.
105			var commonParent = paragraphs[0].parentNode ;
106			var tmp = [] ;
107			for ( var i = 0 ; i < paragraphs.length ; i++ )
108			{
109				block = paragraphs[i] ;
110				commonParent = FCKDomTools.GetCommonParents( block.parentNode, commonParent ).pop() ;
111			}
112			var lastBlock = null ;
113			while ( paragraphs.length > 0 )
114			{
115				block = paragraphs.shift() ;
116				while ( block.parentNode != commonParent )
117					block = block.parentNode ;
118				if ( block != lastBlock )
119					tmp.push( block ) ;
120				lastBlock = block ;
121			}
122
123			// If any of the selected blocks is a blockquote, remove it to prevent nested blockquotes.
124			while ( tmp.length > 0 )
125			{
126				block = tmp.shift() ;
127				if ( block.nodeName.IEquals( 'blockquote' ) )
128				{
129					var docFrag = block.ownerDocument.createDocumentFragment() ;
130					while ( block.firstChild )
131					{
132						docFrag.appendChild( block.removeChild( block.firstChild ) ) ;
133						paragraphs.push( docFrag.lastChild ) ;
134					}
135					block.parentNode.replaceChild( docFrag, block ) ;
136				}
137				else
138					paragraphs.push( block ) ;
139			}
140
141			// Now we have all the blocks to be included in a new blockquote node.
142			var bqBlock = range.Window.document.createElement( 'blockquote' ) ;
143			commonParent.insertBefore( bqBlock, paragraphs[0] ) ;
144			while ( paragraphs.length > 0 )
145			{
146				block = paragraphs.shift() ;
147				bqBlock.appendChild( block ) ;
148			}
149		}
150		else if ( state == FCK_TRISTATE_ON )
151		{
152			var moveOutNodes = [] ;
153			while ( ( block = iterator.GetNextParagraph() ) )
154			{
155				var bqParent = null ;
156				var bqChild = null ;
157				while ( block.parentNode )
158				{
159					if ( block.parentNode.nodeName.IEquals( 'blockquote' ) )
160					{
161						bqParent = block.parentNode ;
162						bqChild = block ;
163						break ;
164					}
165					block = block.parentNode ;
166				}
167
168				if ( bqParent && bqChild )
169					moveOutNodes.push( bqChild ) ;
170			}
171
172			var movedNodes = [] ;
173			while ( moveOutNodes.length > 0 )
174			{
175				var node = moveOutNodes.shift() ;
176				var bqBlock = node.parentNode ;
177
178				// If the node is located at the beginning or the end, just take it out without splitting.
179				// Otherwise, split the blockquote node and move the paragraph in between the two blockquote nodes.
180				if ( node == node.parentNode.firstChild )
181				{
182					bqBlock.parentNode.insertBefore( bqBlock.removeChild( node ), bqBlock ) ;
183					if ( ! bqBlock.firstChild )
184						bqBlock.parentNode.removeChild( bqBlock ) ;
185				}
186				else if ( node == node.parentNode.lastChild )
187				{
188					bqBlock.parentNode.insertBefore( bqBlock.removeChild( node ), bqBlock.nextSibling ) ;
189					if ( ! bqBlock.firstChild )
190						bqBlock.parentNode.removeChild( bqBlock ) ;
191				}
192				else
193					FCKDomTools.BreakParent( node, node.parentNode, range ) ;
194
195				movedNodes.push( node ) ;
196			}
197
198			if ( FCKConfig.EnterMode.IEquals( 'br' ) )
199			{
200				while ( movedNodes.length )
201				{
202					var node = movedNodes.shift() ;
203					var firstTime = true ;
204					if ( node.nodeName.IEquals( 'div' ) )
205					{
206						var docFrag = node.ownerDocument.createDocumentFragment() ;
207						var needBeginBr = firstTime && node.previousSibling && 
208							!FCKListsLib.BlockBoundaries[node.previousSibling.nodeName.toLowerCase()] ;
209						if ( firstTime && needBeginBr )
210							docFrag.appendChild( node.ownerDocument.createElement( 'br' ) ) ;
211						var needEndBr = node.nextSibling && 
212							!FCKListsLib.BlockBoundaries[node.nextSibling.nodeName.toLowerCase()] ;
213						while ( node.firstChild )
214							docFrag.appendChild( node.removeChild( node.firstChild ) ) ;
215						if ( needEndBr )
216							docFrag.appendChild( node.ownerDocument.createElement( 'br' ) ) ;
217						node.parentNode.replaceChild( docFrag, node ) ;
218						firstTime = false ;
219					}
220				}
221			}
222		}
223		range.MoveToBookmark( bookmark ) ;
224		range.Select() ;
225
226		FCK.Focus() ;
227		FCK.Events.FireEvent( 'OnSelectionChange' ) ;
228	},
229
230	GetState : function()
231	{
232		// Disabled if not WYSIWYG.
233		if ( FCK.EditMode != FCK_EDITMODE_WYSIWYG || ! FCK.EditorWindow )
234			return FCK_TRISTATE_DISABLED ;
235
236		var path = new FCKElementPath( FCKSelection.GetBoundaryParentElement( true ) ) ;
237		var firstBlock = path.Block || path.BlockLimit ;
238
239		if ( !firstBlock || firstBlock.nodeName.toLowerCase() == 'body' )
240			return FCK_TRISTATE_OFF ;
241
242		// See if the first block has a blockquote parent.
243		for ( var i = 0 ; i < path.Elements.length ; i++ )
244		{
245			if ( path.Elements[i].nodeName.IEquals( 'blockquote' ) )
246				return FCK_TRISTATE_ON ;
247		}
248		return FCK_TRISTATE_OFF ;
249	}
250} ;