2
* FCKeditor - The text editor for Internet - http://www.fckeditor.net
3
* Copyright (C) 2003-2010 Frederico Caldeira Knabben
7
* Licensed under the terms of any of the following licenses at your
10
* - GNU General Public License Version 2 or later (the "GPL")
11
* http://www.gnu.org/licenses/gpl.html
13
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14
* http://www.gnu.org/licenses/lgpl.html
16
* - Mozilla Public License Version 1.1 or later (the "MPL")
17
* http://www.mozilla.org/MPL/MPL-1.1.html
21
* Active selection functions. (Gecko specific implementation)
24
// Get the selection type (like document.select.type in IE).
25
FCKSelection.GetType = function()
27
// By default set the type to "Text".
30
// Check if the actual selection is a Control (IMG, TABLE, HR, etc...).
33
try { sel = this.GetSelection() ; } catch (e) {}
35
if ( sel && sel.rangeCount == 1 )
37
var range = sel.getRangeAt(0) ;
38
if ( range.startContainer == range.endContainer
39
&& ( range.endOffset - range.startOffset ) == 1
40
&& range.startContainer.nodeType == 1
41
&& FCKListsLib.StyleObjectElements[ range.startContainer.childNodes[ range.startOffset ].nodeName.toLowerCase() ] )
50
// Retrieves the selected element (if any), just in the case that a single
51
// element (object like and image or a table) is selected.
52
FCKSelection.GetSelectedElement = function()
54
var selection = !!FCK.EditorWindow && this.GetSelection() ;
55
if ( !selection || selection.rangeCount < 1 )
58
var range = selection.getRangeAt( 0 ) ;
59
if ( range.startContainer != range.endContainer || range.startContainer.nodeType != 1 || range.startOffset != range.endOffset - 1 )
62
var node = range.startContainer.childNodes[ range.startOffset ] ;
63
if ( node.nodeType != 1 )
69
FCKSelection.GetParentElement = function()
71
if ( this.GetType() == 'Control' )
72
return FCKSelection.GetSelectedElement().parentNode ;
75
var oSel = this.GetSelection() ;
78
// if anchorNode == focusNode, see if the selection is text only or including nodes.
79
// if text only, return the parent node.
80
// if the selection includes DOM nodes, then the anchorNode is the nearest container.
81
if ( oSel.anchorNode && oSel.anchorNode == oSel.focusNode )
83
var oRange = oSel.getRangeAt( 0 ) ;
84
if ( oRange.collapsed || oRange.startContainer.nodeType == 3 )
85
return oSel.anchorNode.parentNode ;
87
return oSel.anchorNode ;
90
// looks like we're having a large selection here. To make the behavior same as IE's TextRange.parentElement(),
91
// we need to find the nearest ancestor node which encapsulates both the beginning and the end of the selection.
92
// TODO: A simpler logic can be found.
93
var anchorPath = new FCKElementPath( oSel.anchorNode ) ;
94
var focusPath = new FCKElementPath( oSel.focusNode ) ;
96
var shallowPath = null ;
97
if ( anchorPath.Elements.length > focusPath.Elements.length )
99
deepPath = anchorPath.Elements ;
100
shallowPath = focusPath.Elements ;
104
deepPath = focusPath.Elements ;
105
shallowPath = anchorPath.Elements ;
108
var deepPathBase = deepPath.length - shallowPath.length ;
109
for( var i = 0 ; i < shallowPath.length ; i++)
111
if ( deepPath[deepPathBase + i] == shallowPath[i])
112
return shallowPath[i];
120
FCKSelection.GetBoundaryParentElement = function( startBoundary )
122
if ( ! FCK.EditorWindow )
124
if ( this.GetType() == 'Control' )
125
return FCKSelection.GetSelectedElement().parentNode ;
128
var oSel = this.GetSelection() ;
129
if ( oSel && oSel.rangeCount > 0 )
131
var range = oSel.getRangeAt( startBoundary ? 0 : ( oSel.rangeCount - 1 ) ) ;
133
var element = startBoundary ? range.startContainer : range.endContainer ;
135
return ( element.nodeType == 1 ? element : element.parentNode ) ;
141
FCKSelection.SelectNode = function( element )
143
var oRange = FCK.EditorDocument.createRange() ;
144
oRange.selectNode( element ) ;
146
var oSel = this.GetSelection() ;
147
oSel.removeAllRanges() ;
148
oSel.addRange( oRange ) ;
151
FCKSelection.Collapse = function( toStart )
153
var oSel = this.GetSelection() ;
155
if ( toStart == null || toStart === true )
156
oSel.collapseToStart() ;
158
oSel.collapseToEnd() ;
161
// The "nodeTagName" parameter must be Upper Case.
162
FCKSelection.HasAncestorNode = function( nodeTagName )
164
var oContainer = this.GetSelectedElement() ;
165
if ( ! oContainer && FCK.EditorWindow )
167
try { oContainer = this.GetSelection().getRangeAt(0).startContainer ; }
173
if ( oContainer.nodeType == 1 && oContainer.nodeName.IEquals( nodeTagName ) ) return true ;
174
oContainer = oContainer.parentNode ;
180
// The "nodeTagName" parameter must be Upper Case.
181
FCKSelection.MoveToAncestorNode = function( nodeTagName )
185
var oContainer = this.GetSelectedElement() ;
187
oContainer = this.GetSelection().getRangeAt(0).startContainer ;
191
if ( oContainer.nodeName.IEquals( nodeTagName ) )
194
oContainer = oContainer.parentNode ;
199
FCKSelection.Delete = function()
201
// Gets the actual selection.
202
var oSel = this.GetSelection() ;
204
// Deletes the actual selection contents.
205
for ( var i = 0 ; i < oSel.rangeCount ; i++ )
207
oSel.getRangeAt(i).deleteContents() ;
214
* Returns the native selection object.
216
FCKSelection.GetSelection = function()
218
return FCK.EditorWindow.getSelection() ;
221
// The following are IE only features (we don't need then in other browsers
223
FCKSelection.Save = function()
225
FCKSelection.Restore = function()
227
FCKSelection.Release = function()