2
* FCKeditor - The text editor for internet
3
* Copyright (C) 2003-2005 Frederico Caldeira Knabben
5
* Licensed under the terms of the GNU Lesser General Public License:
6
* http://www.opensource.org/licenses/lgpl-license.php
8
* For further information visit:
9
* http://www.fckeditor.net/
11
* "Support Open Source software. What about a donation today?"
13
* File Name: fcktools_gecko.js
14
* Utility functions. (Gecko version).
17
* Frederico Caldeira Knabben (fredck@fckeditor.net)
20
// Constant for the Gecko Bogus Node.
21
var GECKO_BOGUS = '<br _moz_editor_bogus_node="TRUE">' ;
23
// Appends a CSS file to a document.
24
FCKTools.AppendStyleSheet = function( documentElement, cssFileUrl )
26
var e = documentElement.createElement( 'LINK' ) ;
27
e.rel = 'stylesheet' ;
30
documentElement.getElementsByTagName("HEAD")[0].appendChild( e ) ;
34
// Removes all attributes and values from the element.
35
FCKTools.ClearElementAttributes = function( element )
37
// Loop throw all attributes in the element
38
for ( var i = 0 ; i < element.attributes.length ; i++ )
40
// Remove the element by name.
41
element.removeAttribute( element.attributes[i].name, 0 ) ; // 0 : Case Insensitive
45
// Returns an Array of strings with all defined in the elements inside another element.
46
FCKTools.GetAllChildrenIds = function( parentElement )
48
// Create the array that will hold all Ids.
49
var aIds = new Array() ;
51
// Define a recursive function that search for the Ids.
52
var fGetIds = function( parent )
54
for ( var i = 0 ; i < parent.childNodes.length ; i++ )
56
var sId = parent.childNodes[i].id ;
58
// Check if the Id is defined for the element.
59
if ( sId && sId.length > 0 ) aIds[ aIds.length ] = sId ;
62
fGetIds( parent.childNodes[i] ) ;
66
// Start the recursive calls.
67
fGetIds( parentElement ) ;
72
FCKTools.RemoveOuterTags = function( e )
74
var oFragment = e.ownerDocument.createDocumentFragment() ;
76
for ( var i = 0 ; i < e.childNodes.length ; i++ )
77
oFragment.appendChild( e.childNodes[i] ) ;
79
e.parentNode.replaceChild( oFragment, e ) ;
82
FCKTools.CreateXmlObject = function( object )
87
return new XMLHttpRequest() ;
89
return document.implementation.createDocument( '', '', null ) ;
94
FCKTools.DisableSelection = function( element )
96
element.style.MozUserSelect = 'none' ; // Gecko only.
97
// element.style.userSelect = 'none' ; // CSS3 (not supported yet).
100
// START iCM Modifications
102
// Starting at the specified node, find the first inline node of the sequence
103
// For example, assume we have the following elements : <p>Some text <span>some more text</span> and <a href="href">some link</a> yet some more text</p>
104
// If the "some link" text node is the one specified, then the "Some text" text node will be the first inline node returned.
105
FCKTools.GetFirstInlineNode = function( oNode )
107
if ( FCKRegexLib.BlockElements.test( oNode.nodeName ) )
109
else if ( oNode.previousSibling && !FCKRegexLib.BlockElements.test( oNode.previousSibling.nodeName ) )
110
return FCKTools.GetFirstInlineNode( oNode.previousSibling ) ;
111
else if ( oNode.parentNode && !FCKRegexLib.BlockElements.test( oNode.parentNode.nodeName ) && oNode.parentNode.nodeName.toUpperCase() != "BODY" )
112
return FCKTools.GetFirstInlineNode( oNode.parentNode ) ;
117
// Starting at the specified node, find the last inline node of the sequence
118
// For example, assume we have the following elements : <p>Some text <span>some more text</span> and <a href="href">some link</a> yet some more text</p>
119
// If the "some link" text node is the one specified, then the " yet some more text" text node will be the last inline node returned.
120
FCKTools.GetLastInlineNode = function( oNode )
122
if ( FCKRegexLib.BlockElements.test( oNode.nodeName ) )
124
else if ( oNode.nextSibling && !FCKRegexLib.BlockElements.test( oNode.nextSibling.nodeName ) )
125
return FCKTools.GetLastInlineNode( oNode.nextSibling ) ;
126
else if ( oNode.parentNode && !FCKRegexLib.BlockElements.test( oNode.parentNode.nodeName ) && oNode.parentNode.nodeName.toUpperCase() != "BODY" )
127
return FCKTools.GetLastInlineNode( oNode.parentNode ) ;
133
// Split the supplied parent at the specified child and (optionally) offset.
134
// Ensure that enclosing block elements are created where missing but that existing
135
// block elements (table for example) don't get incorrectly nested.
136
FCKTools.SplitNode = function( oParentBlockNode, oChildNode, nOffset )
138
if ( typeof nOffset == "undefined" ) nOffset = 0 ;
140
var oFragment = FCK.EditorDocument.createDocumentFragment() ;
141
var oRange = FCK.EditorDocument.createRange() ;
143
if ( FCKRegexLib.ListElements.test( oParentBlockNode.nodeName ) )
145
// Treat OL/UL parents differently as want to split at the specified
146
// child LI node to create to OL/UL lists.
147
oStartNode = oParentBlockNode.firstChild ;
148
oEndNode = oParentBlockNode.lastChild ;
152
// Locate the inline nodes adjacent to the specified child node so that these can
154
oStartNode = FCKTools.GetFirstInlineNode( oChildNode ) ;
155
oEndNode = FCKTools.GetLastInlineNode( oChildNode ) ;
158
// Create a new tag which holds the content of the affected node(s) located before (but not including) the child node and offset
159
if ( FCKRegexLib.BlockElements.test( oStartNode.nodeName ) && !FCKRegexLib.ListElements.test( oParentBlockNode.nodeName ) )
161
// First element of the bunch is already a block element so we don't want to wrap it with a new block element.
162
// Just use this first node provided it is not the same as the last node (to prevent duplication), otherwise
163
// create a new empty P element.
164
if ( oStartNode != oEndNode )
166
oBlockNode1 = oStartNode.cloneNode( true ) ;
170
oBlockNode1 = FCK.EditorDocument.createElement( "P" ) ;
171
oBlockNode1.innerHTML = GECKO_BOGUS ;
173
if ( !FCKRegexLib.SpecialBlockElements.test( oParentBlockNode.nodeName ) )
174
FCKTools.SetElementAttributes( oBlockNode1, oParentBlockNode.attributes ) ; // Transfer across any class attributes, etc
179
// First element of the bunch is not a block element (or it is a LI element which is a special case).
180
// So ensure all of the inline nodes before the selection are wrapped with a suitable block element.
181
var oBlockNode1 = FCK.EditorDocument.createElement( FCKRegexLib.SpecialBlockElements.test( oParentBlockNode.nodeName ) ? "P" : oParentBlockNode.tagName ) ;
182
oRange.setStartBefore( oStartNode ) ;
184
oRange.setEndBefore( oChildNode ) ;
186
oRange.setEnd( oChildNode, nOffset ) ;
187
oBlockNode1.appendChild( oRange.cloneContents() ) ;
188
oBlockNode1.innerHTML = oBlockNode1.innerHTML.replace(/[\x00-\x1F]/g, "") ; // Prevent any control characters returned within the innerHTML from causing problems
189
if ( FCKTools.NodeIsEmpty( oBlockNode1 ) )
190
oBlockNode1.innerHTML = GECKO_BOGUS ; // Ensure it has some content, required for Gecko
192
oBlockNode1.innerHTML = oBlockNode1.innerHTML.replace( FCKRegexLib.EmptyElement, "" ) ; // Strip out any empty tags that may have been generated by the split
193
if ( !FCKRegexLib.SpecialBlockElements.test( oParentBlockNode.nodeName ) )
194
FCKTools.SetElementAttributes( oBlockNode1, oParentBlockNode.attributes ) ; // Transfer across any class attributes, etc
197
// Create a new tag which holds the content of the affected node(s) located after (and including) the child node
198
if ( FCKRegexLib.BlockElements.test( oEndNode.nodeName ) && !FCKRegexLib.ListElements.test( oParentBlockNode.nodeName ) )
200
// Last element of the bunch is already a block element so we don't want to wrap it with a new block element.
201
oBlockNode2 = oEndNode.cloneNode( true ) ;
205
// Last element of the bunch is not a block element (or it is a LI element which is a special case).
206
// So ensure all of the inline nodes after and including the child/offset are wrapped with a suitable block element.
207
var oBlockNode2 = FCK.EditorDocument.createElement( FCKRegexLib.SpecialBlockElements.test( oParentBlockNode.nodeName ) ? "P" : oParentBlockNode.tagName );
208
oRange.setEndAfter( oEndNode );
210
oRange.setStartBefore( oChildNode ) ;
212
oRange.setStart( oChildNode, nOffset );
213
oBlockNode2.appendChild( oRange.cloneContents() ) ;
214
oBlockNode2.innerHTML = oBlockNode2.innerHTML.replace(/[\x00-\x1F]/g, "") ; // Prevent any control characters returned within the innerHTML from causing problems
215
if ( FCKTools.NodeIsEmpty( oBlockNode2 ) )
216
oBlockNode2.innerHTML = GECKO_BOGUS ; // Ensure it has some content, required for Gecko
218
oBlockNode2.innerHTML = oBlockNode2.innerHTML.replace( FCKRegexLib.EmptyElement, "" ) ; // Strip out any empty tags that may have been generated by the split
219
if ( !FCKRegexLib.SpecialBlockElements.test( oParentBlockNode.nodeName ) )
220
FCKTools.SetElementAttributes( oBlockNode2, oParentBlockNode.attributes ) ; // Transfer across any class attributes, etc
223
// Insert the resulting nodes into a document fragment
224
oFragment.appendChild( oBlockNode1 );
225
oFragment.appendChild( oBlockNode2 );
227
// Replace the affected nodes with the new nodes (fragment)
228
FCKTools.ReplaceNodes( oParentBlockNode, oStartNode, oEndNode, oFragment ) ;
230
// Return the second node so it can be used for setting cursor position, etc
234
// Function that replaces the specified range of nodes (inclusive) within the supplied parent
235
// with the nodes stored in the supplied document fragment.
236
FCKTools.ReplaceNodes = function( oParentBlockNode, oStartNode, oEndNode, oFragment )
238
var oRange = FCK.EditorDocument.createRange() ;
240
// Delete the affected node(s)
241
if ( !FCKRegexLib.SpecialBlockElements.test( oParentBlockNode.nodeName ) && (oParentBlockNode.firstChild == oStartNode) && (oParentBlockNode.lastChild == oEndNode) )
243
// Entire parent block node is to be replaced so insert the two new block elements before it
244
// and then remove the old node
245
oRange.selectNode ( oParentBlockNode );
249
// Only part of the parent block node is to be replaced so insert the two new block elements
250
// before the first inline node of the affected content and then remove the old nodes
251
oRange.setEndAfter( oEndNode ) ;
252
oRange.setStartBefore( oStartNode ) ;
255
// Insert the replacement nodes
256
oRange.deleteContents() ;
257
oRange.insertNode( oFragment ) ;
260
// END iCM Modifications