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. (Gecko specific implementation)
|
---|
22 | */
|
---|
23 |
|
---|
24 | // Get the selection type (like document.select.type in IE).
|
---|
25 | FCKSelection.GetType = function()
|
---|
26 | {
|
---|
27 | // By default set the type to "Text".
|
---|
28 | var type = 'Text' ;
|
---|
29 |
|
---|
30 | // Check if the actual selection is a Control (IMG, TABLE, HR, etc...).
|
---|
31 |
|
---|
32 | var sel ;
|
---|
33 | try { sel = this.GetSelection() ; } catch (e) {}
|
---|
34 |
|
---|
35 | if ( sel && sel.rangeCount == 1 )
|
---|
36 | {
|
---|
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() ] )
|
---|
42 | {
|
---|
43 | type = 'Control' ;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | return type ;
|
---|
48 | }
|
---|
49 |
|
---|
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()
|
---|
53 | {
|
---|
54 | var selection = !!FCK.EditorWindow && this.GetSelection() ;
|
---|
55 | if ( !selection || selection.rangeCount < 1 )
|
---|
56 | return null ;
|
---|
57 |
|
---|
58 | var range = selection.getRangeAt( 0 ) ;
|
---|
59 | if ( range.startContainer != range.endContainer || range.startContainer.nodeType != 1 || range.startOffset != range.endOffset - 1 )
|
---|
60 | return null ;
|
---|
61 |
|
---|
62 | var node = range.startContainer.childNodes[ range.startOffset ] ;
|
---|
63 | if ( node.nodeType != 1 )
|
---|
64 | return null ;
|
---|
65 |
|
---|
66 | return node ;
|
---|
67 | }
|
---|
68 |
|
---|
69 | FCKSelection.GetParentElement = function()
|
---|
70 | {
|
---|
71 | if ( this.GetType() == 'Control' )
|
---|
72 | return FCKSelection.GetSelectedElement().parentNode ;
|
---|
73 | else
|
---|
74 | {
|
---|
75 | var oSel = this.GetSelection() ;
|
---|
76 | if ( oSel )
|
---|
77 | {
|
---|
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 )
|
---|
82 | {
|
---|
83 | var oRange = oSel.getRangeAt( 0 ) ;
|
---|
84 | if ( oRange.collapsed || oRange.startContainer.nodeType == 3 )
|
---|
85 | return oSel.anchorNode.parentNode ;
|
---|
86 | else
|
---|
87 | return oSel.anchorNode ;
|
---|
88 | }
|
---|
89 |
|
---|
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 ) ;
|
---|
95 | var deepPath = null ;
|
---|
96 | var shallowPath = null ;
|
---|
97 | if ( anchorPath.Elements.length > focusPath.Elements.length )
|
---|
98 | {
|
---|
99 | deepPath = anchorPath.Elements ;
|
---|
100 | shallowPath = focusPath.Elements ;
|
---|
101 | }
|
---|
102 | else
|
---|
103 | {
|
---|
104 | deepPath = focusPath.Elements ;
|
---|
105 | shallowPath = anchorPath.Elements ;
|
---|
106 | }
|
---|
107 |
|
---|
108 | var deepPathBase = deepPath.length - shallowPath.length ;
|
---|
109 | for( var i = 0 ; i < shallowPath.length ; i++)
|
---|
110 | {
|
---|
111 | if ( deepPath[deepPathBase + i] == shallowPath[i])
|
---|
112 | return shallowPath[i];
|
---|
113 | }
|
---|
114 | return null ;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | return null ;
|
---|
118 | }
|
---|
119 |
|
---|
120 | FCKSelection.GetBoundaryParentElement = function( startBoundary )
|
---|
121 | {
|
---|
122 | if ( ! FCK.EditorWindow )
|
---|
123 | return null ;
|
---|
124 | if ( this.GetType() == 'Control' )
|
---|
125 | return FCKSelection.GetSelectedElement().parentNode ;
|
---|
126 | else
|
---|
127 | {
|
---|
128 | var oSel = this.GetSelection() ;
|
---|
129 | if ( oSel && oSel.rangeCount > 0 )
|
---|
130 | {
|
---|
131 | var range = oSel.getRangeAt( startBoundary ? 0 : ( oSel.rangeCount - 1 ) ) ;
|
---|
132 |
|
---|
133 | var element = startBoundary ? range.startContainer : range.endContainer ;
|
---|
134 |
|
---|
135 | return ( element.nodeType == 1 ? element : element.parentNode ) ;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | return null ;
|
---|
139 | }
|
---|
140 |
|
---|
141 | FCKSelection.SelectNode = function( element )
|
---|
142 | {
|
---|
143 | var oRange = FCK.EditorDocument.createRange() ;
|
---|
144 | oRange.selectNode( element ) ;
|
---|
145 |
|
---|
146 | var oSel = this.GetSelection() ;
|
---|
147 | oSel.removeAllRanges() ;
|
---|
148 | oSel.addRange( oRange ) ;
|
---|
149 | }
|
---|
150 |
|
---|
151 | FCKSelection.Collapse = function( toStart )
|
---|
152 | {
|
---|
153 | var oSel = this.GetSelection() ;
|
---|
154 |
|
---|
155 | if ( toStart == null || toStart === true )
|
---|
156 | oSel.collapseToStart() ;
|
---|
157 | else
|
---|
158 | oSel.collapseToEnd() ;
|
---|
159 | }
|
---|
160 |
|
---|
161 | // The "nodeTagName" parameter must be Upper Case.
|
---|
162 | FCKSelection.HasAncestorNode = function( nodeTagName )
|
---|
163 | {
|
---|
164 | var oContainer = this.GetSelectedElement() ;
|
---|
165 | if ( ! oContainer && FCK.EditorWindow )
|
---|
166 | {
|
---|
167 | try { oContainer = this.GetSelection().getRangeAt(0).startContainer ; }
|
---|
168 | catch(e){}
|
---|
169 | }
|
---|
170 |
|
---|
171 | while ( oContainer )
|
---|
172 | {
|
---|
173 | if ( oContainer.nodeType == 1 && oContainer.nodeName.IEquals( nodeTagName ) ) return true ;
|
---|
174 | oContainer = oContainer.parentNode ;
|
---|
175 | }
|
---|
176 |
|
---|
177 | return false ;
|
---|
178 | }
|
---|
179 |
|
---|
180 | // The "nodeTagName" parameter must be Upper Case.
|
---|
181 | FCKSelection.MoveToAncestorNode = function( nodeTagName )
|
---|
182 | {
|
---|
183 | var oNode ;
|
---|
184 |
|
---|
185 | var oContainer = this.GetSelectedElement() ;
|
---|
186 | if ( ! oContainer )
|
---|
187 | oContainer = this.GetSelection().getRangeAt(0).startContainer ;
|
---|
188 |
|
---|
189 | while ( oContainer )
|
---|
190 | {
|
---|
191 | if ( oContainer.nodeName.IEquals( nodeTagName ) )
|
---|
192 | return oContainer ;
|
---|
193 |
|
---|
194 | oContainer = oContainer.parentNode ;
|
---|
195 | }
|
---|
196 | return null ;
|
---|
197 | }
|
---|
198 |
|
---|
199 | FCKSelection.Delete = function()
|
---|
200 | {
|
---|
201 | // Gets the actual selection.
|
---|
202 | var oSel = this.GetSelection() ;
|
---|
203 |
|
---|
204 | // Deletes the actual selection contents.
|
---|
205 | for ( var i = 0 ; i < oSel.rangeCount ; i++ )
|
---|
206 | {
|
---|
207 | oSel.getRangeAt(i).deleteContents() ;
|
---|
208 | }
|
---|
209 |
|
---|
210 | return oSel ;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Returns the native selection object.
|
---|
215 | */
|
---|
216 | FCKSelection.GetSelection = function()
|
---|
217 | {
|
---|
218 | return FCK.EditorWindow.getSelection() ;
|
---|
219 | }
|
---|
220 |
|
---|
221 | // The following are IE only features (we don't need then in other browsers
|
---|
222 | // currently).
|
---|
223 | FCKSelection.Save = function()
|
---|
224 | {}
|
---|
225 | FCKSelection.Restore = function()
|
---|
226 | {}
|
---|
227 | FCKSelection.Release = function()
|
---|
228 | {}
|
---|