summaryrefslogtreecommitdiff
path: root/rt/share/html/NoAuth/RichText/FCKeditor/editor/_source/internals/fckselection_ie.js
blob: 7dc180fb0bc4cfc2f6f7964c4e13d52d8ed107d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2003-2009 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 ==
 *
 * Active selection functions. (IE specific implementation)
 */

// Get the selection type.
FCKSelection.GetType = function()
{
	// It is possible that we can still get a text range object even when type=='None' is returned by IE.
	// So we'd better check the object returned by createRange() rather than by looking at the type.
	try
	{
		var ieType = FCKSelection.GetSelection().type ;
		if ( ieType == 'Control' || ieType == 'Text' )
			return ieType ;

		if ( this.GetSelection().createRange().parentElement )
			return 'Text' ;
	}
	catch(e)
	{
		// Nothing to do, it will return None properly.
	}

	return 'None' ;
} ;

// Retrieves the selected element (if any), just in the case that a single
// element (object like and image or a table) is selected.
FCKSelection.GetSelectedElement = function()
{
	if ( this.GetType() == 'Control' )
	{
		var oRange = this.GetSelection().createRange() ;

		if ( oRange && oRange.item )
			return this.GetSelection().createRange().item(0) ;
	}
	return null ;
} ;

FCKSelection.GetParentElement = function()
{
	switch ( this.GetType() )
	{
		case 'Control' :
			var el = FCKSelection.GetSelectedElement() ;
			return el ? el.parentElement : null ;

		case 'None' :
			return null ;

		default :
			return this.GetSelection().createRange().parentElement() ;
	}
} ;

FCKSelection.GetBoundaryParentElement = function( startBoundary )
{
	switch ( this.GetType() )
	{
		case 'Control' :
			var el = FCKSelection.GetSelectedElement() ;
			return el ? el.parentElement : null ;

		case 'None' :
			return null ;

		default :
			var doc = FCK.EditorDocument ;

			var range = doc.selection.createRange() ;
			range.collapse( startBoundary !== false ) ;

			var el = range.parentElement() ;

			// It may happen that range is comming from outside "doc", so we
			// must check it (#1204).
			return FCKTools.GetElementDocument( el ) == doc ? el : null ;
	}
} ;

FCKSelection.SelectNode = function( node )
{
	FCK.Focus() ;
	this.GetSelection().empty() ;
	var oRange ;
	try
	{
		// Try to select the node as a control.
		oRange = FCK.EditorDocument.body.createControlRange() ;
		oRange.addElement( node ) ;
	}
	catch(e)
	{
		// If failed, select it as a text range.
		oRange = FCK.EditorDocument.body.createTextRange() ;
		oRange.moveToElementText( node ) ;
	}

	oRange.select() ;
} ;

FCKSelection.Collapse = function( toStart )
{
	FCK.Focus() ;
	if ( this.GetType() == 'Text' )
	{
		var oRange = this.GetSelection().createRange() ;
		oRange.collapse( toStart == null || toStart === true ) ;
		oRange.select() ;
	}
} ;

// The "nodeTagName" parameter must be Upper Case.
FCKSelection.HasAncestorNode = function( nodeTagName )
{
	var oContainer ;

	if ( this.GetSelection().type == "Control" )
	{
		oContainer = this.GetSelectedElement() ;
	}
	else
	{
		var oRange  = this.GetSelection().createRange() ;
		oContainer = oRange.parentElement() ;
	}

	while ( oContainer )
	{
		if ( oContainer.nodeName.IEquals( nodeTagName ) ) return true ;
		oContainer = oContainer.parentNode ;
	}

	return false ;
} ;

// The "nodeTagName" parameter must be UPPER CASE.
// It can be also an array of names
FCKSelection.MoveToAncestorNode = function( nodeTagName )
{
	var oNode, oRange ;

	if ( ! FCK.EditorDocument )
		return null ;

	if ( this.GetSelection().type == "Control" )
	{
		oRange = this.GetSelection().createRange() ;
		for ( i = 0 ; i < oRange.length ; i++ )
		{
			if (oRange(i).parentNode)
			{
				oNode = oRange(i).parentNode ;
				break ;
			}
		}
	}
	else
	{
		oRange  = this.GetSelection().createRange() ;
		oNode = oRange.parentElement() ;
	}

	while ( oNode && !oNode.nodeName.Equals( nodeTagName ) )
		oNode = oNode.parentNode ;

	return oNode ;
} ;

FCKSelection.Delete = function()
{
	// Gets the actual selection.
	var oSel = this.GetSelection() ;

	// Deletes the actual selection contents.
	if ( oSel.type.toLowerCase() != "none" )
	{
		oSel.clear() ;
	}

	return oSel ;
} ;

/**
 * Returns the native selection object.
 */
FCKSelection.GetSelection = function()
{
	this.Restore() ;
	return FCK.EditorDocument.selection ;
}

FCKSelection.Save = function( lock )
{
	var editorDocument = FCK.EditorDocument ;

	if ( !editorDocument )
		return ;

	// Avoid saving again a selection while a dialog is open #2616
	if ( this.locked )
		return ;
	this.locked = !!lock ;

	var selection = editorDocument.selection ;
	var range ;

	if ( selection )
	{
		// The call might fail if the document doesn't have the focus (#1801),
		// but we don't want to modify the current selection (#2495) with a call to FCK.Focus() ;
		try {
			range = selection.createRange() ;
		}
		catch(e) {}

		// Ensure that the range comes from the editor document.
		if ( range )
		{
			if ( range.parentElement && FCKTools.GetElementDocument( range.parentElement() ) != editorDocument )
				range = null ;
			else if ( range.item && FCKTools.GetElementDocument( range.item(0) )!= editorDocument )
				range = null ;
		}
	}

	this.SelectionData = range ;
}

FCKSelection._GetSelectionDocument = function( selection )
{
	var range = selection.createRange() ;
	if ( !range )
		return null;
	else if ( range.item )
		return FCKTools.GetElementDocument( range.item( 0 ) ) ;
	else
		return FCKTools.GetElementDocument( range.parentElement() ) ;
}

FCKSelection.Restore = function()
{
	if ( this.SelectionData )
	{
		FCK.IsSelectionChangeLocked = true ;

		try
		{
			// Don't repeat the restore process if the editor document is already selected.
			if ( String( this._GetSelectionDocument( FCK.EditorDocument.selection ).body.contentEditable ) == 'true' )
			{
				FCK.IsSelectionChangeLocked = false ;
				return ;
			}
			this.SelectionData.select() ;
		}
		catch ( e ) {}

		FCK.IsSelectionChangeLocked = false ;
	}
}

FCKSelection.Release = function()
{
	this.locked = false ;
	delete this.SelectionData ;
}