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 * Class for working with a selection range, much like the W3C DOM Range, but
22 * it is not intended to be an implementation of the W3C interface.
23 * (Gecko Implementation)
24 */
25
26FCKDomRange.prototype.MoveToSelection = function()
27{
28	this.Release( true ) ;
29
30	var oSel = this.Window.getSelection() ;
31
32	if ( oSel && oSel.rangeCount > 0 )
33	{
34		this._Range = FCKW3CRange.CreateFromRange( this.Window.document, oSel.getRangeAt(0) ) ;
35		this._UpdateElementInfo() ;
36	}
37	else
38		this.MoveToElementStart( this.Window.document.body ) ;
39}
40
41FCKDomRange.prototype.Select = function()
42{
43	var oRange = this._Range ;
44	if ( oRange )
45	{
46		var startContainer = oRange.startContainer ;
47
48		// If we have a collapsed range, inside an empty element, we must add
49		// something to it, otherwise the caret will not be visible.
50		if ( oRange.collapsed && startContainer.nodeType == 1 && startContainer.childNodes.length == 0 )
51			startContainer.appendChild( oRange._Document.createTextNode('') ) ;
52
53		var oDocRange = this.Window.document.createRange() ;
54		oDocRange.setStart( startContainer, oRange.startOffset ) ;
55
56		try
57		{
58			oDocRange.setEnd( oRange.endContainer, oRange.endOffset ) ;
59		}
60		catch ( e )
61		{
62			// There is a bug in Firefox implementation (it would be too easy
63			// otherwise). The new start can't be after the end (W3C says it can).
64			// So, let's create a new range and collapse it to the desired point.
65			if ( e.toString().Contains( 'NS_ERROR_ILLEGAL_VALUE' ) )
66			{
67				oRange.collapse( true ) ;
68				oDocRange.setEnd( oRange.endContainer, oRange.endOffset ) ;
69			}
70			else
71				throw( e ) ;
72		}
73
74		var oSel = this.Window.getSelection() ;
75		oSel.removeAllRanges() ;
76
77		// We must add a clone otherwise Firefox will have rendering issues.
78		oSel.addRange( oDocRange ) ;
79	}
80}
81
82// Not compatible with bookmark created with CreateBookmark2.
83// The bookmark nodes will be deleted from the document.
84FCKDomRange.prototype.SelectBookmark = function( bookmark )
85{
86	var domRange = this.Window.document.createRange() ;
87
88	var startNode	= this.GetBookmarkNode( bookmark, true ) ;
89	var endNode		= this.GetBookmarkNode( bookmark, false ) ;
90
91	domRange.setStart( startNode.parentNode, FCKDomTools.GetIndexOf( startNode ) ) ;
92	FCKDomTools.RemoveNode( startNode ) ;
93
94	if ( endNode )
95	{
96		domRange.setEnd( endNode.parentNode, FCKDomTools.GetIndexOf( endNode ) ) ;
97		FCKDomTools.RemoveNode( endNode ) ;
98	}
99
100	var selection = this.Window.getSelection() ;
101	selection.removeAllRanges() ;
102	selection.addRange( domRange ) ;
103}