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
24
var FCKTools = new Object() ;
26
FCKTools.CreateBogusBR = function( targetDocument )
28
var eBR = targetDocument.createElement( 'br' ) ;
29
// eBR.setAttribute( '_moz_editor_bogus_node', 'TRUE' ) ;
30
eBR.setAttribute( 'type', '_moz' ) ;
35
* Fixes relative URL entries defined inside CSS styles by appending a prefix
37
* @param (String) cssStyles The CSS styles definition possibly containing url()
39
* @param (String) urlFixPrefix The prefix to append to relative URLs.
41
FCKTools.FixCssUrls = function( urlFixPrefix, cssStyles )
43
if ( !urlFixPrefix || urlFixPrefix.length == 0 )
46
return cssStyles.replace( /url\s*\(([\s'"]*)(.*?)([\s"']*)\)/g, function( match, opener, path, closer )
48
if ( /^\/|^\w?:/.test( path ) )
51
return 'url(' + opener + urlFixPrefix + path + closer + ')' ;
55
FCKTools._GetUrlFixedCss = function( cssStyles, urlFixPrefix )
57
var match = cssStyles.match( /^([^|]+)\|([\s\S]*)/ ) ;
60
return FCKTools.FixCssUrls( match[1], match[2] ) ;
66
* Appends a <link css> or <style> element to the document.
67
* @param (Object) documentElement The DOM document object to which append the
69
* @param (Variant) cssFileOrDef A String pointing to the CSS file URL or an
70
* Array with many CSS file URLs or the CSS definitions for the <style>
72
* @return {Array} An array containing all elements created in the target
73
* document. It may include <link> or <style> elements, depending on the
74
* value passed with cssFileOrDef.
76
FCKTools.AppendStyleSheet = function( domDocument, cssFileOrArrayOrDef )
78
if ( !cssFileOrArrayOrDef )
81
if ( typeof( cssFileOrArrayOrDef ) == 'string' )
83
// Test if the passed argument is an URL.
84
if ( /[\\\/\.][^{}]*$/.test( cssFileOrArrayOrDef ) )
86
// The string may have several URLs separated by comma.
87
return this.AppendStyleSheet( domDocument, cssFileOrArrayOrDef.split(',') ) ;
90
return [ this.AppendStyleString( domDocument, FCKTools._GetUrlFixedCss( cssFileOrArrayOrDef ) ) ] ;
95
for ( var i = 0 ; i < cssFileOrArrayOrDef.length ; i++ )
96
styles.push( this._AppendStyleSheet( domDocument, cssFileOrArrayOrDef[i] ) ) ;
101
FCKTools.GetStyleHtml = (function()
103
var getStyle = function( styleDef, markTemp )
105
if ( styleDef.length == 0 )
108
var temp = markTemp ? ' _fcktemp="true"' : '' ;
109
return '<' + 'style type="text/css"' + temp + '>' + styleDef + '<' + '/style>' ;
112
var getLink = function( cssFileUrl, markTemp )
114
if ( cssFileUrl.length == 0 )
117
var temp = markTemp ? ' _fcktemp="true"' : '' ;
118
return '<' + 'link href="' + cssFileUrl + '" type="text/css" rel="stylesheet" ' + temp + '/>' ;
121
return function( cssFileOrArrayOrDef, markTemp )
123
if ( !cssFileOrArrayOrDef )
126
if ( typeof( cssFileOrArrayOrDef ) == 'string' )
128
// Test if the passed argument is an URL.
129
if ( /[\\\/\.][^{}]*$/.test( cssFileOrArrayOrDef ) )
131
// The string may have several URLs separated by comma.
132
return this.GetStyleHtml( cssFileOrArrayOrDef.split(','), markTemp ) ;
135
return getStyle( this._GetUrlFixedCss( cssFileOrArrayOrDef ), markTemp ) ;
141
for ( var i = 0 ; i < cssFileOrArrayOrDef.length ; i++ )
142
html += getLink( cssFileOrArrayOrDef[i], markTemp ) ;
149
FCKTools.GetElementDocument = function ( element )
151
return element.ownerDocument || element.document ;
154
// Get the window object where the element is placed in.
155
FCKTools.GetElementWindow = function( element )
157
return this.GetDocumentWindow( this.GetElementDocument( element ) ) ;
160
FCKTools.GetDocumentWindow = function( document )
162
// With Safari, there is not way to retrieve the window from the document, so we must fix it.
163
if ( FCKBrowserInfo.IsSafari && !document.parentWindow )
164
this.FixDocumentParentWindow( window.top ) ;
166
return document.parentWindow || document.defaultView ;
170
This is a Safari specific function that fix the reference to the parent
171
window from the document object.
173
FCKTools.FixDocumentParentWindow = function( targetWindow )
175
if ( targetWindow.document )
176
targetWindow.document.parentWindow = targetWindow ;
178
for ( var i = 0 ; i < targetWindow.frames.length ; i++ )
179
FCKTools.FixDocumentParentWindow( targetWindow.frames[i] ) ;
182
FCKTools.HTMLEncode = function( text )
187
text = text.replace( /&/g, '&' ) ;
188
text = text.replace( /</g, '<' ) ;
189
text = text.replace( />/g, '>' ) ;
194
FCKTools.HTMLDecode = function( text )
199
text = text.replace( />/g, '>' ) ;
200
text = text.replace( /</g, '<' ) ;
201
text = text.replace( /&/g, '&' ) ;
206
FCKTools._ProcessLineBreaksForPMode = function( oEditor, text, liState, node, strArray )
209
var blockStartTag = "<p>" ;
210
var blockEndTag = "</p>" ;
211
var lineBreakTag = "<br />" ;
214
blockStartTag = "<li>" ;
215
blockEndTag = "</li>" ;
219
// Are we currently inside a <p> tag now?
220
// If yes, close it at the next double line break.
221
while ( node && node != oEditor.FCK.EditorDocument.body )
223
if ( node.tagName.toLowerCase() == 'p' )
228
node = node.parentNode ;
231
for ( var i = 0 ; i < text.length ; i++ )
233
var c = text.charAt( i ) ;
243
// Now we have encountered a line break.
244
// Check if the next character is also a line break.
245
var n = text.charAt( i + 1 ) ;
249
n = text.charAt( i + 1 ) ;
253
i++ ; // ignore next character - we have already processed it.
255
strArray.push( blockEndTag ) ;
256
strArray.push( blockStartTag ) ;
260
strArray.push( lineBreakTag ) ;
264
FCKTools._ProcessLineBreaksForDivMode = function( oEditor, text, liState, node, strArray )
267
var blockStartTag = "<div>" ;
268
var blockEndTag = "</div>" ;
271
blockStartTag = "<li>" ;
272
blockEndTag = "</li>" ;
276
// Are we currently inside a <div> tag now?
277
// If yes, close it at the next double line break.
278
while ( node && node != oEditor.FCK.EditorDocument.body )
280
if ( node.tagName.toLowerCase() == 'div' )
285
node = node.parentNode ;
288
for ( var i = 0 ; i < text.length ; i++ )
290
var c = text.charAt( i ) ;
302
if ( strArray[ strArray.length - 1 ] == blockStartTag )
304
// A div tag must have some contents inside for it to be visible.
305
strArray.push( " " ) ;
307
strArray.push( blockEndTag ) ;
309
strArray.push( blockStartTag ) ;
313
strArray.push( blockEndTag ) ;
316
FCKTools._ProcessLineBreaksForBrMode = function( oEditor, text, liState, node, strArray )
319
var blockStartTag = "<br />" ;
320
var blockEndTag = "" ;
323
blockStartTag = "<li>" ;
324
blockEndTag = "</li>" ;
328
for ( var i = 0 ; i < text.length ; i++ )
330
var c = text.charAt( i ) ;
340
if ( closeState && blockEndTag.length )
341
strArray.push ( blockEndTag ) ;
342
strArray.push( blockStartTag ) ;
347
FCKTools.ProcessLineBreaks = function( oEditor, oConfig, text )
349
var enterMode = oConfig.EnterMode.toLowerCase() ;
352
// Is the caret or selection inside an <li> tag now?
354
var range = new oEditor.FCKDomRange( oEditor.FCK.EditorWindow ) ;
355
range.MoveToSelection() ;
356
var node = range._Range.startContainer ;
357
while ( node && node.nodeType != 1 )
358
node = node.parentNode ;
359
if ( node && node.tagName.toLowerCase() == 'li' )
362
if ( enterMode == 'p' )
363
this._ProcessLineBreaksForPMode( oEditor, text, liState, node, strArray ) ;
364
else if ( enterMode == 'div' )
365
this._ProcessLineBreaksForDivMode( oEditor, text, liState, node, strArray ) ;
366
else if ( enterMode == 'br' )
367
this._ProcessLineBreaksForBrMode( oEditor, text, liState, node, strArray ) ;
368
return strArray.join( "" ) ;
372
* Adds an option to a SELECT element.
374
FCKTools.AddSelectOption = function( selectElement, optionText, optionValue )
376
var oOption = FCKTools.GetElementDocument( selectElement ).createElement( "OPTION" ) ;
378
oOption.text = optionText ;
379
oOption.value = optionValue ;
381
selectElement.options.add(oOption) ;
386
FCKTools.RunFunction = function( func, thisObject, paramsArray, timerWindow )
389
this.SetTimeout( func, 0, thisObject, paramsArray, timerWindow ) ;
392
FCKTools.SetTimeout = function( func, milliseconds, thisObject, paramsArray, timerWindow )
394
return ( timerWindow || window ).setTimeout(
398
func.apply( thisObject, [].concat( paramsArray ) ) ;
400
func.apply( thisObject ) ;
405
FCKTools.SetInterval = function( func, milliseconds, thisObject, paramsArray, timerWindow )
407
return ( timerWindow || window ).setInterval(
410
func.apply( thisObject, paramsArray || [] ) ;
415
FCKTools.ConvertStyleSizeToHtml = function( size )
417
return size.EndsWith( '%' ) ? size : parseInt( size, 10 ) ;
420
FCKTools.ConvertHtmlSizeToStyle = function( size )
422
return size.EndsWith( '%' ) ? size : ( size + 'px' ) ;
425
// START iCM MODIFICATIONS
426
// Amended to accept a list of one or more ascensor tag names
427
// Amended to check the element itself before working back up through the parent hierarchy
428
FCKTools.GetElementAscensor = function( element, ascensorTagNames )
430
// var e = element.parentNode ;
432
var lstTags = "," + ascensorTagNames.toUpperCase() + "," ;
436
if ( lstTags.indexOf( "," + e.nodeName.toUpperCase() + "," ) != -1 )
443
// END iCM MODIFICATIONS
445
FCKTools.CreateEventListener = function( func, params )
449
var aAllParams = [] ;
451
for ( var i = 0 ; i < arguments.length ; i++ )
452
aAllParams.push( arguments[i] ) ;
454
func.apply( this, aAllParams.concat( params ) ) ;
460
FCKTools.IsStrictMode = function( document )
462
// There is no compatMode in Safari, but it seams that it always behave as
463
// CSS1Compat, so let's assume it as the default for that browser.
464
return ( 'CSS1Compat' == ( document.compatMode || ( FCKBrowserInfo.IsSafari ? 'CSS1Compat' : null ) ) ) ;
467
// Transforms a "arguments" object to an array.
468
FCKTools.ArgumentsToArray = function( args, startIndex, maxLength )
470
startIndex = startIndex || 0 ;
471
maxLength = maxLength || args.length ;
473
var argsArray = new Array() ;
475
for ( var i = startIndex ; i < startIndex + maxLength && i < args.length ; i++ )
476
argsArray.push( args[i] ) ;
481
FCKTools.CloneObject = function( sourceObject )
483
var fCloneCreator = function() {} ;
484
fCloneCreator.prototype = sourceObject ;
485
return new fCloneCreator ;
488
// Appends a bogus <br> at the end of the element, if not yet available.
489
FCKTools.AppendBogusBr = function( element )
494
var eLastChild = this.GetLastItem( element.getElementsByTagName('br') ) ;
496
if ( !eLastChild || ( eLastChild.getAttribute( 'type', 2 ) != '_moz' && eLastChild.getAttribute( '_moz_dirty' ) == null ) )
498
var doc = this.GetElementDocument( element ) ;
500
if ( FCKBrowserInfo.IsOpera )
501
element.appendChild( doc.createTextNode('') ) ;
503
element.appendChild( this.CreateBogusBR( doc ) ) ;
507
FCKTools.GetLastItem = function( list )
509
if ( list.length > 0 )
510
return list[ list.length - 1 ] ;
515
FCKTools.GetDocumentPosition = function( w, node )
520
var prevNode = null ;
521
var curWindow = FCKTools.GetElementWindow( curNode ) ;
522
while ( curNode && !( curWindow == w && ( curNode == w.document.body || curNode == w.document.documentElement ) ) )
524
x += curNode.offsetLeft - curNode.scrollLeft ;
525
y += curNode.offsetTop - curNode.scrollTop ;
527
if ( ! FCKBrowserInfo.IsOpera )
529
var scrollNode = prevNode ;
530
while ( scrollNode && scrollNode != curNode )
532
x -= scrollNode.scrollLeft ;
533
y -= scrollNode.scrollTop ;
534
scrollNode = scrollNode.parentNode ;
539
if ( curNode.offsetParent )
540
curNode = curNode.offsetParent ;
543
if ( curWindow != w )
545
curNode = curWindow.frameElement ;
548
curWindow = curNode.contentWindow.parent ;
555
// document.body is a special case when it comes to offsetTop and offsetLeft values.
556
// 1. It matters if document.body itself is a positioned element;
557
// 2. It matters is when we're in IE and the element has no positioned ancestor.
558
// Otherwise the values should be ignored.
559
if ( FCKDomTools.GetCurrentElementStyle( w.document.body, 'position') != 'static'
560
|| ( FCKBrowserInfo.IsIE && FCKDomTools.GetPositionedAncestor( node ) == null ) )
562
x += w.document.body.offsetLeft ;
563
y += w.document.body.offsetTop ;
566
return { "x" : x, "y" : y } ;
569
FCKTools.GetWindowPosition = function( w, node )
571
var pos = this.GetDocumentPosition( w, node ) ;
572
var scroll = FCKTools.GetScrollPosition( w ) ;
578
FCKTools.ProtectFormStyles = function( formNode )
580
if ( !formNode || formNode.nodeType != 1 || formNode.tagName.toLowerCase() != 'form' )
582
var hijackRecord = [] ;
583
var hijackNames = [ 'style', 'className' ] ;
584
for ( var i = 0 ; i < hijackNames.length ; i++ )
586
var name = hijackNames[i] ;
587
if ( formNode.elements.namedItem( name ) )
589
var hijackNode = formNode.elements.namedItem( name ) ;
590
hijackRecord.push( [ hijackNode, hijackNode.nextSibling ] ) ;
591
formNode.removeChild( hijackNode ) ;
594
return hijackRecord ;
597
FCKTools.RestoreFormStyles = function( formNode, hijackRecord )
599
if ( !formNode || formNode.nodeType != 1 || formNode.tagName.toLowerCase() != 'form' )
601
if ( hijackRecord.length > 0 )
603
for ( var i = hijackRecord.length - 1 ; i >= 0 ; i-- )
605
var node = hijackRecord[i][0] ;
606
var sibling = hijackRecord[i][1] ;
608
formNode.insertBefore( node, sibling ) ;
610
formNode.appendChild( node ) ;
615
// Perform a one-step DFS walk.
616
FCKTools.GetNextNode = function( node, limitNode )
618
if ( node.firstChild )
619
return node.firstChild ;
620
else if ( node.nextSibling )
621
return node.nextSibling ;
624
var ancestor = node.parentNode ;
627
if ( ancestor == limitNode )
629
if ( ancestor.nextSibling )
630
return ancestor.nextSibling ;
632
ancestor = ancestor.parentNode ;
638
FCKTools.GetNextTextNode = function( textnode, limitNode, checkStop )
640
node = this.GetNextNode( textnode, limitNode ) ;
641
if ( checkStop && node && checkStop( node ) )
643
while ( node && node.nodeType != 3 )
645
node = this.GetNextNode( node, limitNode ) ;
646
if ( checkStop && node && checkStop( node ) )
653
* Merge all objects passed by argument into a single object.
655
FCKTools.Merge = function()
657
var args = arguments ;
660
for ( var i = 1 ; i < args.length ; i++ )
671
* Check if the passed argument is a real Array. It may not working when
672
* calling it cross windows.
674
FCKTools.IsArray = function( it )
676
return ( it instanceof Array ) ;
680
* Appends a "length" property to an object, containing the number of
681
* properties available on it, excluded the append property itself.
683
FCKTools.AppendLengthProperty = function( targetObject, propertyName )
687
for ( var n in targetObject )
690
return targetObject[ propertyName || 'length' ] = counter ;
694
* Gets the browser parsed version of a css text (style attribute value). On
695
* some cases, the browser makes changes to the css text, returning a different
696
* value. For example, hexadecimal colors get transformed to rgb().
698
FCKTools.NormalizeCssText = function( unparsedCssText )
700
// Injects the style in a temporary span object, so the browser parses it,
701
// retrieving its final format.
702
var tempSpan = document.createElement( 'span' ) ;
703
tempSpan.style.cssText = unparsedCssText ;
704
return tempSpan.style.cssText ;
708
* Binding the "this" reference to an object for a function.
710
FCKTools.Bind = function( subject, func )
712
return function(){ return func.apply( subject, arguments ) ; } ;
716
* Retrieve the correct "empty iframe" URL for the current browser, which
717
* causes the minimum fuzz (e.g. security warnings in HTTPS, DNS error in
718
* IE5.5, etc.) for that browser, making the iframe ready to DOM use whithout
719
* having to loading an external file.
721
FCKTools.GetVoidUrl = function()
723
if ( FCK_IS_CUSTOM_DOMAIN )
724
return "javascript: void( function(){" +
726
"document.write('<html><head><title></title></head><body></body></html>');" +
727
"document.domain = '" + FCK_RUNTIME_DOMAIN + "';" +
728
"document.close();" +
731
if ( FCKBrowserInfo.IsIE )
733
if ( FCKBrowserInfo.IsIE7 || !FCKBrowserInfo.IsIE6 )
734
return "" ; // IE7+ / IE5.5
736
return "javascript: '';" ; // IE6+
739
return "javascript: void(0);" ; // All other browsers.
742
FCKTools.ResetStyles = function( element )
744
element.style.cssText = 'margin:0;' +
747
'background-color:transparent;' +
748
'background-image:none;' ;