import rt 3.8.7
[freeside.git] / rt / share / html / NoAuth / RichText / FCKeditor / editor / _source / classes / fckdomrange_gecko.js
1 /*\r
2  * FCKeditor - The text editor for Internet - http://www.fckeditor.net\r
3  * Copyright (C) 2003-2009 Frederico Caldeira Knabben\r
4  *\r
5  * == BEGIN LICENSE ==\r
6  *\r
7  * Licensed under the terms of any of the following licenses at your\r
8  * choice:\r
9  *\r
10  *  - GNU General Public License Version 2 or later (the "GPL")\r
11  *    http://www.gnu.org/licenses/gpl.html\r
12  *\r
13  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")\r
14  *    http://www.gnu.org/licenses/lgpl.html\r
15  *\r
16  *  - Mozilla Public License Version 1.1 or later (the "MPL")\r
17  *    http://www.mozilla.org/MPL/MPL-1.1.html\r
18  *\r
19  * == END LICENSE ==\r
20  *\r
21  * Class for working with a selection range, much like the W3C DOM Range, but\r
22  * it is not intended to be an implementation of the W3C interface.\r
23  * (Gecko Implementation)\r
24  */\r
25 \r
26 FCKDomRange.prototype.MoveToSelection = function()\r
27 {\r
28         this.Release( true ) ;\r
29 \r
30         var oSel = this.Window.getSelection() ;\r
31 \r
32         if ( oSel && oSel.rangeCount > 0 )\r
33         {\r
34                 this._Range = FCKW3CRange.CreateFromRange( this.Window.document, oSel.getRangeAt(0) ) ;\r
35                 this._UpdateElementInfo() ;\r
36         }\r
37         else\r
38                 if ( this.Window.document )\r
39                         this.MoveToElementStart( this.Window.document.body ) ;\r
40 }\r
41 \r
42 FCKDomRange.prototype.Select = function()\r
43 {\r
44         var oRange = this._Range ;\r
45         if ( oRange )\r
46         {\r
47                 var startContainer = oRange.startContainer ;\r
48 \r
49                 // If we have a collapsed range, inside an empty element, we must add\r
50                 // something to it, otherwise the caret will not be visible.\r
51                 if ( oRange.collapsed && startContainer.nodeType == 1 && startContainer.childNodes.length == 0 )\r
52                         startContainer.appendChild( oRange._Document.createTextNode('') ) ;\r
53 \r
54                 var oDocRange = this.Window.document.createRange() ;\r
55                 oDocRange.setStart( startContainer, oRange.startOffset ) ;\r
56 \r
57                 try\r
58                 {\r
59                         oDocRange.setEnd( oRange.endContainer, oRange.endOffset ) ;\r
60                 }\r
61                 catch ( e )\r
62                 {\r
63                         // There is a bug in Firefox implementation (it would be too easy\r
64                         // otherwise). The new start can't be after the end (W3C says it can).\r
65                         // So, let's create a new range and collapse it to the desired point.\r
66                         if ( e.toString().Contains( 'NS_ERROR_ILLEGAL_VALUE' ) )\r
67                         {\r
68                                 oRange.collapse( true ) ;\r
69                                 oDocRange.setEnd( oRange.endContainer, oRange.endOffset ) ;\r
70                         }\r
71                         else\r
72                                 throw( e ) ;\r
73                 }\r
74 \r
75                 var oSel = this.Window.getSelection() ;\r
76                 oSel.removeAllRanges() ;\r
77 \r
78                 // We must add a clone otherwise Firefox will have rendering issues.\r
79                 oSel.addRange( oDocRange ) ;\r
80         }\r
81 }\r
82 \r
83 // Not compatible with bookmark created with CreateBookmark2.\r
84 // The bookmark nodes will be deleted from the document.\r
85 FCKDomRange.prototype.SelectBookmark = function( bookmark )\r
86 {\r
87         var domRange = this.Window.document.createRange() ;\r
88 \r
89         var startNode   = this.GetBookmarkNode( bookmark, true ) ;\r
90         var endNode             = this.GetBookmarkNode( bookmark, false ) ;\r
91 \r
92         domRange.setStart( startNode.parentNode, FCKDomTools.GetIndexOf( startNode ) ) ;\r
93         FCKDomTools.RemoveNode( startNode ) ;\r
94 \r
95         if ( endNode )\r
96         {\r
97                 domRange.setEnd( endNode.parentNode, FCKDomTools.GetIndexOf( endNode ) ) ;\r
98                 FCKDomTools.RemoveNode( endNode ) ;\r
99         }\r
100 \r
101         var selection = this.Window.getSelection() ;\r
102         selection.removeAllRanges() ;\r
103         selection.addRange( domRange ) ;\r
104 }\r