2
* FCKeditor - The text editor for Internet - http://www.fckeditor.net
3
* Copyright (C) 2003-2007 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
* Utility functions to work with the DOM.
26
MoveChildren : function( source, target )
28
if ( source == target )
32
while ( (eChild = source.firstChild) )
33
target.appendChild( source.removeChild( eChild ) ) ;
36
// Remove blank spaces from the beginning and the end of the contents of a node.
37
TrimNode : function( node, ignoreEndBRs )
39
this.LTrimNode( node ) ;
40
this.RTrimNode( node, ignoreEndBRs ) ;
43
LTrimNode : function( node )
47
while ( (eChildNode = node.firstChild) )
49
if ( eChildNode.nodeType == 3 )
51
var sTrimmed = eChildNode.nodeValue.LTrim() ;
52
var iOriginalLength = eChildNode.nodeValue.length ;
54
if ( sTrimmed.length == 0 )
56
node.removeChild( eChildNode ) ;
59
else if ( sTrimmed.length < iOriginalLength )
61
eChildNode.splitText( iOriginalLength - sTrimmed.length ) ;
62
node.removeChild( node.firstChild ) ;
69
RTrimNode : function( node, ignoreEndBRs )
73
while ( (eChildNode = node.lastChild) )
75
switch ( eChildNode.nodeType )
78
if ( eChildNode.nodeName.toUpperCase() == 'BR' && ( ignoreEndBRs || eChildNode.getAttribute( 'type', 2 ) == '_moz' ) )
80
node.removeChild( eChildNode ) ;
86
var sTrimmed = eChildNode.nodeValue.RTrim() ;
87
var iOriginalLength = eChildNode.nodeValue.length ;
89
if ( sTrimmed.length == 0 )
91
// If the trimmed text node is empty, just remove it.
93
// Use "eChildNode.parentNode" instead of "node" to avoid IE bug (#81).
94
eChildNode.parentNode.removeChild( eChildNode ) ;
97
else if ( sTrimmed.length < iOriginalLength )
99
// If the trimmed text lenght is less than the original
100
// lenght, strip all spaces from the end by splitting
101
// the text and removing the resulting useless node.
103
eChildNode.splitText( sTrimmed.length ) ;
104
// Use "node.lastChild.parentNode" instead of "node" to avoid IE bug (#81).
105
node.lastChild.parentNode.removeChild( node.lastChild ) ;
112
RemoveNode : function( node, excludeChildren )
114
if ( excludeChildren )
116
// Move all children before the node.
118
while ( (eChild = node.firstChild) )
119
node.parentNode.insertBefore( node.removeChild( eChild ), node ) ;
122
return node.parentNode.removeChild( node ) ;
125
GetFirstChild : function( node, childNames )
127
// If childNames is a string, transform it in a Array.
128
if ( typeof ( childNames ) == 'string' )
129
childNames = [ childNames ] ;
131
var eChild = node.firstChild ;
134
if ( eChild.nodeType == 1 && eChild.tagName.Equals.apply( eChild.tagName, childNames ) )
137
eChild = eChild.nextSibling ;
143
GetLastChild : function( node, childNames )
145
// If childNames is a string, transform it in a Array.
146
if ( typeof ( childNames ) == 'string' )
147
childNames = [ childNames ] ;
149
var eChild = node.lastChild ;
152
if ( eChild.nodeType == 1 && ( !childNames || eChild.tagName.Equals( childNames ) ) )
155
eChild = eChild.previousSibling ;
162
* Gets the previous element (nodeType=1) in the source order. Returns
163
* "null" If no element is found.
164
* @param {Object} currentNode The node to start searching from.
165
* @param {Boolean} ignoreSpaceTextOnly Sets how text nodes will be
166
* handled. If set to "true", only white spaces text nodes
167
* will be ignored, while non white space text nodes will stop
168
* the search, returning null. If "false" or ommitted, all
169
* text nodes are ignored.
170
* @param {string[]} stopSearchElements An array of element names that
171
* will cause the search to stop when found, returning null.
172
* May be ommitted (or null).
173
* @param {string[]} ignoreElements An array of element names that
174
* must be ignored during the search.
176
GetPreviousSourceElement : function( currentNode, ignoreSpaceTextOnly, stopSearchElements, ignoreElements )
181
if ( stopSearchElements && currentNode.nodeType == 1 && currentNode.nodeName.IEquals( stopSearchElements ) )
184
if ( currentNode.previousSibling )
185
currentNode = currentNode.previousSibling ;
187
return this.GetPreviousSourceElement( currentNode.parentNode, ignoreSpaceTextOnly, stopSearchElements, ignoreElements ) ;
189
while ( currentNode )
191
if ( currentNode.nodeType == 1 )
193
if ( stopSearchElements && currentNode.nodeName.IEquals( stopSearchElements ) )
196
if ( !ignoreElements || !currentNode.nodeName.IEquals( ignoreElements ) )
199
else if ( ignoreSpaceTextOnly && currentNode.nodeType == 3 && currentNode.nodeValue.RTrim().length > 0 )
202
if ( currentNode.lastChild )
203
currentNode = currentNode.lastChild ;
205
return this.GetPreviousSourceElement( currentNode, ignoreSpaceTextOnly, stopSearchElements, ignoreElements ) ;
212
* Gets the next element (nodeType=1) in the source order. Returns
213
* "null" If no element is found.
214
* @param {Object} currentNode The node to start searching from.
215
* @param {Boolean} ignoreSpaceTextOnly Sets how text nodes will be
216
* handled. If set to "true", only white spaces text nodes
217
* will be ignored, while non white space text nodes will stop
218
* the search, returning null. If "false" or ommitted, all
219
* text nodes are ignored.
220
* @param {string[]} stopSearchElements An array of element names that
221
* will cause the search to stop when found, returning null.
222
* May be ommitted (or null).
223
* @param {string[]} ignoreElements An array of element names that
224
* must be ignored during the search.
226
GetNextSourceElement : function( currentNode, ignoreSpaceTextOnly, stopSearchElements, ignoreElements )
231
if ( currentNode.nextSibling )
232
currentNode = currentNode.nextSibling ;
234
return this.GetNextSourceElement( currentNode.parentNode, ignoreSpaceTextOnly, stopSearchElements, ignoreElements ) ;
236
while ( currentNode )
238
if ( currentNode.nodeType == 1 )
240
if ( stopSearchElements && currentNode.nodeName.IEquals( stopSearchElements ) )
243
if ( !ignoreElements || !currentNode.nodeName.IEquals( ignoreElements ) )
246
else if ( ignoreSpaceTextOnly && currentNode.nodeType == 3 && currentNode.nodeValue.RTrim().length > 0 )
249
if ( currentNode.firstChild )
250
currentNode = currentNode.firstChild ;
252
return this.GetNextSourceElement( currentNode, ignoreSpaceTextOnly, stopSearchElements, ignoreElements ) ;
258
// Inserts a element after a existing one.
259
InsertAfterNode : function( existingNode, newNode )
261
return existingNode.parentNode.insertBefore( newNode, existingNode.nextSibling ) ;
264
GetParents : function( node )
266
var parents = new Array() ;
270
parents.splice( 0, 0, node ) ;
271
node = node.parentNode ;
277
GetIndexOf : function( node )
279
var currentNode = node.parentNode ? node.parentNode.firstChild : null ;
280
var currentIndex = -1 ;
282
while ( currentNode )
286
if ( currentNode == node )
287
return currentIndex ;
289
currentNode = currentNode.nextSibling ;
b'\\ No newline at end of file'