1 | /* |
---|
2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net |
---|
3 | * Copyright (C) 2003-2009 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 | * Active selection functions. (IE specific implementation) |
---|
22 | */ |
---|
23 | |
---|
24 | // Get the selection type. |
---|
25 | FCKSelection.GetType = function() |
---|
26 | { |
---|
27 | // It is possible that we can still get a text range object even when type=='None' is returned by IE. |
---|
28 | // So we'd better check the object returned by createRange() rather than by looking at the type. |
---|
29 | try |
---|
30 | { |
---|
31 | var ieType = FCKSelection.GetSelection().type ; |
---|
32 | if ( ieType == 'Control' || ieType == 'Text' ) |
---|
33 | return ieType ; |
---|
34 | |
---|
35 | if ( this.GetSelection().createRange().parentElement ) |
---|
36 | return 'Text' ; |
---|
37 | } |
---|
38 | catch(e) |
---|
39 | { |
---|
40 | // Nothing to do, it will return None properly. |
---|
41 | } |
---|
42 | |
---|
43 | return 'None' ; |
---|
44 | } ; |
---|
45 | |
---|
46 | // Retrieves the selected element (if any), just in the case that a single |
---|
47 | // element (object like and image or a table) is selected. |
---|
48 | FCKSelection.GetSelectedElement = function() |
---|
49 | { |
---|
50 | if ( this.GetType() == 'Control' ) |
---|
51 | { |
---|
52 | var oRange = this.GetSelection().createRange() ; |
---|
53 | |
---|
54 | if ( oRange && oRange.item ) |
---|
55 | return this.GetSelection().createRange().item(0) ; |
---|
56 | } |
---|
57 | return null ; |
---|
58 | } ; |
---|
59 | |
---|
60 | FCKSelection.GetParentElement = function() |
---|
61 | { |
---|
62 | switch ( this.GetType() ) |
---|
63 | { |
---|
64 | case 'Control' : |
---|
65 | var el = FCKSelection.GetSelectedElement() ; |
---|
66 | return el ? el.parentElement : null ; |
---|
67 | |
---|
68 | case 'None' : |
---|
69 | return null ; |
---|
70 | |
---|
71 | default : |
---|
72 | return this.GetSelection().createRange().parentElement() ; |
---|
73 | } |
---|
74 | } ; |
---|
75 | |
---|
76 | FCKSelection.GetBoundaryParentElement = function( startBoundary ) |
---|
77 | { |
---|
78 | switch ( this.GetType() ) |
---|
79 | { |
---|
80 | case 'Control' : |
---|
81 | var el = FCKSelection.GetSelectedElement() ; |
---|
82 | return el ? el.parentElement : null ; |
---|
83 | |
---|
84 | case 'None' : |
---|
85 | return null ; |
---|
86 | |
---|
87 | default : |
---|
88 | var doc = FCK.EditorDocument ; |
---|
89 | |
---|
90 | var range = doc.selection.createRange() ; |
---|
91 | range.collapse( startBoundary !== false ) ; |
---|
92 | |
---|
93 | var el = range.parentElement() ; |
---|
94 | |
---|
95 | // It may happen that range is comming from outside "doc", so we |
---|
96 | // must check it (#1204). |
---|
97 | return FCKTools.GetElementDocument( el ) == doc ? el : null ; |
---|
98 | } |
---|
99 | } ; |
---|
100 | |
---|
101 | FCKSelection.SelectNode = function( node ) |
---|
102 | { |
---|
103 | FCK.Focus() ; |
---|
104 | this.GetSelection().empty() ; |
---|
105 | var oRange ; |
---|
106 | try |
---|
107 | { |
---|
108 | // Try to select the node as a control. |
---|
109 | oRange = FCK.EditorDocument.body.createControlRange() ; |
---|
110 | oRange.addElement( node ) ; |
---|
111 | } |
---|
112 | catch(e) |
---|
113 | { |
---|
114 | // If failed, select it as a text range. |
---|
115 | oRange = FCK.EditorDocument.body.createTextRange() ; |
---|
116 | oRange.moveToElementText( node ) ; |
---|
117 | } |
---|
118 | |
---|
119 | oRange.select() ; |
---|
120 | } ; |
---|
121 | |
---|
122 | FCKSelection.Collapse = function( toStart ) |
---|
123 | { |
---|
124 | FCK.Focus() ; |
---|
125 | if ( this.GetType() == 'Text' ) |
---|
126 | { |
---|
127 | var oRange = this.GetSelection().createRange() ; |
---|
128 | oRange.collapse( toStart == null || toStart === true ) ; |
---|
129 | oRange.select() ; |
---|
130 | } |
---|
131 | } ; |
---|
132 | |
---|
133 | // The "nodeTagName" parameter must be Upper Case. |
---|
134 | FCKSelection.HasAncestorNode = function( nodeTagName ) |
---|
135 | { |
---|
136 | var oContainer ; |
---|
137 | |
---|
138 | if ( this.GetSelection().type == "Control" ) |
---|
139 | { |
---|
140 | oContainer = this.GetSelectedElement() ; |
---|
141 | } |
---|
142 | else |
---|
143 | { |
---|
144 | var oRange = this.GetSelection().createRange() ; |
---|
145 | oContainer = oRange.parentElement() ; |
---|
146 | } |
---|
147 | |
---|
148 | while ( oContainer ) |
---|
149 | { |
---|
150 | if ( oContainer.nodeName.IEquals( nodeTagName ) ) return true ; |
---|
151 | oContainer = oContainer.parentNode ; |
---|
152 | } |
---|
153 | |
---|
154 | return false ; |
---|
155 | } ; |
---|
156 | |
---|
157 | // The "nodeTagName" parameter must be UPPER CASE. |
---|
158 | // It can be also an array of names |
---|
159 | FCKSelection.MoveToAncestorNode = function( nodeTagName ) |
---|
160 | { |
---|
161 | var oNode, oRange ; |
---|
162 | |
---|
163 | if ( ! FCK.EditorDocument ) |
---|
164 | return null ; |
---|
165 | |
---|
166 | if ( this.GetSelection().type == "Control" ) |
---|
167 | { |
---|
168 | oRange = this.GetSelection().createRange() ; |
---|
169 | for ( i = 0 ; i < oRange.length ; i++ ) |
---|
170 | { |
---|
171 | if (oRange(i).parentNode) |
---|
172 | { |
---|
173 | oNode = oRange(i).parentNode ; |
---|
174 | break ; |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | else |
---|
179 | { |
---|
180 | oRange = this.GetSelection().createRange() ; |
---|
181 | oNode = oRange.parentElement() ; |
---|
182 | } |
---|
183 | |
---|
184 | while ( oNode && !oNode.nodeName.Equals( nodeTagName ) ) |
---|
185 | oNode = oNode.parentNode ; |
---|
186 | |
---|
187 | return oNode ; |
---|
188 | } ; |
---|
189 | |
---|
190 | FCKSelection.Delete = function() |
---|
191 | { |
---|
192 | // Gets the actual selection. |
---|
193 | var oSel = this.GetSelection() ; |
---|
194 | |
---|
195 | // Deletes the actual selection contents. |
---|
196 | if ( oSel.type.toLowerCase() != "none" ) |
---|
197 | { |
---|
198 | oSel.clear() ; |
---|
199 | } |
---|
200 | |
---|
201 | return oSel ; |
---|
202 | } ; |
---|
203 | |
---|
204 | /** |
---|
205 | * Returns the native selection object. |
---|
206 | */ |
---|
207 | FCKSelection.GetSelection = function() |
---|
208 | { |
---|
209 | this.Restore() ; |
---|
210 | return FCK.EditorDocument.selection ; |
---|
211 | } |
---|
212 | |
---|
213 | FCKSelection.Save = function( lock ) |
---|
214 | { |
---|
215 | var editorDocument = FCK.EditorDocument ; |
---|
216 | |
---|
217 | if ( !editorDocument ) |
---|
218 | return ; |
---|
219 | |
---|
220 | // Avoid saving again a selection while a dialog is open #2616 |
---|
221 | if ( this.locked ) |
---|
222 | return ; |
---|
223 | this.locked = !!lock ; |
---|
224 | |
---|
225 | var selection = editorDocument.selection ; |
---|
226 | var range ; |
---|
227 | |
---|
228 | if ( selection ) |
---|
229 | { |
---|
230 | // The call might fail if the document doesn't have the focus (#1801), |
---|
231 | // but we don't want to modify the current selection (#2495) with a call to FCK.Focus() ; |
---|
232 | try { |
---|
233 | range = selection.createRange() ; |
---|
234 | } |
---|
235 | catch(e) {} |
---|
236 | |
---|
237 | // Ensure that the range comes from the editor document. |
---|
238 | if ( range ) |
---|
239 | { |
---|
240 | if ( range.parentElement && FCKTools.GetElementDocument( range.parentElement() ) != editorDocument ) |
---|
241 | range = null ; |
---|
242 | else if ( range.item && FCKTools.GetElementDocument( range.item(0) )!= editorDocument ) |
---|
243 | range = null ; |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | this.SelectionData = range ; |
---|
248 | } |
---|
249 | |
---|
250 | FCKSelection._GetSelectionDocument = function( selection ) |
---|
251 | { |
---|
252 | var range = selection.createRange() ; |
---|
253 | if ( !range ) |
---|
254 | return null; |
---|
255 | else if ( range.item ) |
---|
256 | return FCKTools.GetElementDocument( range.item( 0 ) ) ; |
---|
257 | else |
---|
258 | return FCKTools.GetElementDocument( range.parentElement() ) ; |
---|
259 | } |
---|
260 | |
---|
261 | FCKSelection.Restore = function() |
---|
262 | { |
---|
263 | if ( this.SelectionData ) |
---|
264 | { |
---|
265 | FCK.IsSelectionChangeLocked = true ; |
---|
266 | |
---|
267 | try |
---|
268 | { |
---|
269 | // Don't repeat the restore process if the editor document is already selected. |
---|
270 | if ( String( this._GetSelectionDocument( FCK.EditorDocument.selection ).body.contentEditable ) == 'true' ) |
---|
271 | { |
---|
272 | FCK.IsSelectionChangeLocked = false ; |
---|
273 | return ; |
---|
274 | } |
---|
275 | this.SelectionData.select() ; |
---|
276 | } |
---|
277 | catch ( e ) {} |
---|
278 | |
---|
279 | FCK.IsSelectionChangeLocked = false ; |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | FCKSelection.Release = function() |
---|
284 | { |
---|
285 | this.locked = false ; |
---|
286 | delete this.SelectionData ; |
---|
287 | } |
---|