[389] | 1 | /* |
---|
| 2 | * FCKeditor - The text editor for internet |
---|
| 3 | * Copyright (C) 2003-2006 Frederico Caldeira Knabben |
---|
| 4 | * |
---|
| 5 | * Licensed under the terms of the GNU Lesser General Public License: |
---|
| 6 | * http://www.opensource.org/licenses/lgpl-license.php |
---|
| 7 | * |
---|
| 8 | * For further information visit: |
---|
| 9 | * http://www.fckeditor.net/ |
---|
| 10 | * |
---|
| 11 | * "Support Open Source software. What about a donation today?" |
---|
| 12 | * |
---|
| 13 | * File Name: fck_1.js |
---|
| 14 | * This is the first part of the "FCK" object creation. This is the main |
---|
| 15 | * object that represents an editor instance. |
---|
| 16 | * |
---|
| 17 | * File Authors: |
---|
| 18 | * Frederico Caldeira Knabben (fredck@fckeditor.net) |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | var FCK_StartupValue ; |
---|
| 22 | |
---|
| 23 | FCK.Events = new FCKEvents( FCK ) ; |
---|
| 24 | FCK.Toolbar = null ; |
---|
| 25 | FCK.HasFocus = false ; |
---|
| 26 | |
---|
| 27 | FCK.StartEditor = function() |
---|
| 28 | { |
---|
| 29 | FCK.TempBaseTag = FCKConfig.BaseHref.length > 0 ? '<base href="' + FCKConfig.BaseHref + '" _fcktemp="true"></base>' : '' ; |
---|
| 30 | |
---|
| 31 | FCK.EditingArea = new FCKEditingArea( document.getElementById( 'xEditingArea' ) ) ; |
---|
| 32 | |
---|
| 33 | // Set the editor's startup contents |
---|
| 34 | this.SetHTML( FCKTools.GetLinkedFieldValue() ) ; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | FCK.Focus = function() |
---|
| 38 | { |
---|
| 39 | FCK.EditingArea.Focus() ; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | FCK.SetStatus = function( newStatus ) |
---|
| 43 | { |
---|
| 44 | this.Status = newStatus ; |
---|
| 45 | |
---|
| 46 | if ( newStatus == FCK_STATUS_ACTIVE ) |
---|
| 47 | { |
---|
| 48 | FCKFocusManager.AddWindow( window, true ) ; |
---|
| 49 | |
---|
| 50 | if ( FCKBrowserInfo.IsIE ) |
---|
| 51 | FCKFocusManager.AddWindow( window.frameElement, true ) ; |
---|
| 52 | |
---|
| 53 | // Force the focus in the editor. |
---|
| 54 | if ( FCKConfig.StartupFocus ) |
---|
| 55 | FCK.Focus() ; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | this.Events.FireEvent( 'OnStatusChange', newStatus ) ; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | // GetHTML is Deprecated : returns the same value as GetXHTML. |
---|
| 62 | FCK.GetHTML = FCK.GetXHTML = function( format ) |
---|
| 63 | { |
---|
| 64 | // We assume that if the user is in source editing, the editor value must |
---|
| 65 | // represent the exact contents of the source, as the user wanted it to be. |
---|
| 66 | if ( FCK.EditMode == FCK_EDITMODE_SOURCE ) |
---|
| 67 | return FCK.EditingArea.Textarea.value ; |
---|
| 68 | |
---|
| 69 | var sXHTML ; |
---|
| 70 | var oDoc = FCK.EditorDocument ; |
---|
| 71 | |
---|
| 72 | if ( FCKConfig.FullPage ) |
---|
| 73 | sXHTML = FCKXHtml.GetXHTML( oDoc.getElementsByTagName( 'html' )[0], true, format ) ; |
---|
| 74 | else |
---|
| 75 | { |
---|
| 76 | if ( FCKConfig.IgnoreEmptyParagraphValue && oDoc.body.innerHTML == '<P> </P>' ) |
---|
| 77 | sXHTML = '' ; |
---|
| 78 | else |
---|
| 79 | sXHTML = FCKXHtml.GetXHTML( oDoc.body, false, format ) ; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | // Restore protected attributes. |
---|
| 83 | sXHTML = FCK.ProtectEventsRestore( sXHTML ) ; |
---|
| 84 | |
---|
| 85 | if ( FCKBrowserInfo.IsIE ) |
---|
| 86 | sXHTML = sXHTML.replace( FCKRegexLib.ToReplace, '$1' ) ; |
---|
| 87 | |
---|
| 88 | if ( FCK.DocTypeDeclaration && FCK.DocTypeDeclaration.length > 0 ) |
---|
| 89 | sXHTML = FCK.DocTypeDeclaration + '\n' + sXHTML ; |
---|
| 90 | |
---|
| 91 | if ( FCK.XmlDeclaration && FCK.XmlDeclaration.length > 0 ) |
---|
| 92 | sXHTML = FCK.XmlDeclaration + '\n' + sXHTML ; |
---|
| 93 | |
---|
| 94 | return FCKConfig.ProtectedSource.Revert( sXHTML ) ; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | FCK.UpdateLinkedField = function() |
---|
| 98 | { |
---|
| 99 | FCK.LinkedField.value = FCK.GetXHTML( FCKConfig.FormatOutput ) ; |
---|
| 100 | FCK.Events.FireEvent( 'OnAfterLinkedFieldUpdate' ) ; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | FCK.RegisteredDoubleClickHandlers = new Object() ; |
---|
| 104 | |
---|
| 105 | FCK.OnDoubleClick = function( element ) |
---|
| 106 | { |
---|
| 107 | var oHandler = FCK.RegisteredDoubleClickHandlers[ element.tagName ] ; |
---|
| 108 | if ( oHandler ) |
---|
| 109 | oHandler( element ) ; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | // Register objects that can handle double click operations. |
---|
| 113 | FCK.RegisterDoubleClickHandler = function( handlerFunction, tag ) |
---|
| 114 | { |
---|
| 115 | FCK.RegisteredDoubleClickHandlers[ tag.toUpperCase() ] = handlerFunction ; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | FCK.OnAfterSetHTML = function() |
---|
| 119 | { |
---|
| 120 | FCKDocumentProcessor.Process( FCK.EditorDocument ) ; |
---|
| 121 | FCKUndo.SaveUndoStep() ; |
---|
| 122 | FCK.Events.FireEvent( 'OnSelectionChange' ) ; |
---|
| 123 | FCK.Events.FireEvent( 'OnAfterSetHTML' ) ; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | // Saves URLs on links and images on special attributes, so they don't change when |
---|
| 127 | // moving around. |
---|
| 128 | FCK.ProtectUrls = function( html ) |
---|
| 129 | { |
---|
| 130 | // <A> href |
---|
| 131 | html = html.replace( FCKRegexLib.ProtectUrlsA , '$1$4$2$3$5$2 _fcksavedurl=$2$3$5$2' ) ; |
---|
| 132 | |
---|
| 133 | // <IMG> src |
---|
| 134 | html = html.replace( FCKRegexLib.ProtectUrlsImg , '$1$4$2$3$5$2 _fcksavedurl=$2$3$5$2' ) ; |
---|
| 135 | |
---|
| 136 | return html ; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | // Saves event attributes (like onclick) so they don't get executed while |
---|
| 140 | // editing. |
---|
| 141 | FCK.ProtectEvents = function( html ) |
---|
| 142 | { |
---|
| 143 | return html.replace( FCKRegexLib.TagsWithEvent, _FCK_ProtectEvents_ReplaceTags ) ; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | // Replace all events attributes (like onclick). |
---|
| 147 | function _FCK_ProtectEvents_ReplaceTags( tagMatch ) |
---|
| 148 | { |
---|
| 149 | return tagMatch.replace( FCKRegexLib.EventAttributes, _FCK_ProtectEvents_ReplaceEvents ) ; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | // Replace an event attribute with its respective __fckprotectedatt attribute. |
---|
| 153 | // The original event markup will be encoded and saved as the value of the new |
---|
| 154 | // attribute. |
---|
| 155 | function _FCK_ProtectEvents_ReplaceEvents( eventMatch, attName ) |
---|
| 156 | { |
---|
| 157 | return ' ' + attName + '_fckprotectedatt="' + eventMatch.ReplaceAll( [/&/g,/'/g,/"/g,/=/g,/</g,/>/g,/\r/g,/\n/g], [''',''','"','=','<','>',' ',' '] ) + '"' ; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | FCK.ProtectEventsRestore = function( html ) |
---|
| 161 | { |
---|
| 162 | return html.replace( FCKRegexLib.ProtectedEvents, _FCK_ProtectEvents_RestoreEvents ) ; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | function _FCK_ProtectEvents_RestoreEvents( match, encodedOriginal ) |
---|
| 166 | { |
---|
| 167 | return encodedOriginal.ReplaceAll( [/'/g,/"/g,/=/g,/</g,/>/g,/ /g,/ /g,/'/g], ["'",'"','=','<','>','\r','\n','&'] ) ; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | FCK.IsDirty = function() |
---|
| 171 | { |
---|
| 172 | return ( FCK_StartupValue != FCK.EditorDocument.body.innerHTML ) ; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | FCK.ResetIsDirty = function() |
---|
| 176 | { |
---|
| 177 | if ( FCK.EditorDocument.body ) |
---|
| 178 | FCK_StartupValue = FCK.EditorDocument.body.innerHTML ; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | FCK.SetHTML = function( html ) |
---|
| 182 | { |
---|
| 183 | this.EditingArea.Mode = FCK.EditMode ; |
---|
| 184 | |
---|
| 185 | if ( FCK.EditMode == FCK_EDITMODE_WYSIWYG ) |
---|
| 186 | { |
---|
| 187 | html = FCKConfig.ProtectedSource.Protect( html ) ; |
---|
| 188 | html = FCK.ProtectEvents( html ) ; |
---|
| 189 | html = FCK.ProtectUrls( html ) ; |
---|
| 190 | |
---|
| 191 | // Firefox can't handle correctly the editing of the STRONG and EM tags. |
---|
| 192 | // We must replace them with B and I. |
---|
| 193 | if ( FCKBrowserInfo.IsGecko ) |
---|
| 194 | { |
---|
| 195 | html = html.replace( FCKRegexLib.StrongOpener, '<b$1' ) ; |
---|
| 196 | html = html.replace( FCKRegexLib.StrongCloser, '<\/b>' ) ; |
---|
| 197 | html = html.replace( FCKRegexLib.EmOpener, '<i$1' ) ; |
---|
| 198 | html = html.replace( FCKRegexLib.EmCloser, '<\/i>' ) ; |
---|
| 199 | } |
---|
| 200 | else if ( FCKBrowserInfo.IsIE ) |
---|
| 201 | { |
---|
| 202 | // IE doesn't support <abbr> and it breaks it. Let's protect it. |
---|
| 203 | html = html.replace( FCKRegexLib.AbbrOpener, '<FCK:abbr$1' ) ; |
---|
| 204 | html = html.replace( FCKRegexLib.AbbrCloser, '<\/FCK:abbr>' ) ; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | var sHtml = '' ; |
---|
| 208 | |
---|
| 209 | if ( FCKConfig.FullPage ) |
---|
| 210 | { |
---|
| 211 | // The HTML must be fixed if the <head> is not available. |
---|
| 212 | if ( !FCKRegexLib.HeadOpener.test( html ) ) |
---|
| 213 | { |
---|
| 214 | // Check if the <html> is available. |
---|
| 215 | if ( !FCKRegexLib.HtmlOpener.test( html ) ) |
---|
| 216 | html = '<html dir="' + FCKConfig.ContentLangDirection + '">' + html + '</html>' ; |
---|
| 217 | |
---|
| 218 | // Add the <head>. |
---|
| 219 | html = html.replace( FCKRegexLib.HtmlOpener, '$&<head></head>' ) ; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | // Save the DOCTYPE. |
---|
| 223 | FCK.DocTypeDeclaration = html.match( FCKRegexLib.DocTypeTag ) ; |
---|
| 224 | |
---|
| 225 | if ( FCKBrowserInfo.IsIE ) |
---|
| 226 | sHtml = FCK._GetBehaviorsStyle() ; |
---|
| 227 | else if ( FCKConfig.ShowBorders ) |
---|
| 228 | sHtml = '<link href="' + FCKConfig.FullBasePath + 'css/fck_showtableborders_gecko.css" rel="stylesheet" type="text/css" _fcktemp="true" />' ; |
---|
| 229 | |
---|
| 230 | sHtml += '<link href="' + FCKConfig.FullBasePath + 'css/fck_internal.css' + '" rel="stylesheet" type="text/css" _fcktemp="true" />' ; |
---|
| 231 | |
---|
| 232 | sHtml = html.replace( FCKRegexLib.HeadCloser, sHtml + '$&' ) ; |
---|
| 233 | |
---|
| 234 | // Insert the base tag (FCKConfig.BaseHref), if not exists in the source. |
---|
| 235 | if ( FCK.TempBaseTag.length > 0 && !FCKRegexLib.HasBaseTag.test( html ) ) |
---|
| 236 | sHtml = sHtml.replace( FCKRegexLib.HeadOpener, '$&' + FCK.TempBaseTag ) ; |
---|
| 237 | } |
---|
| 238 | else |
---|
| 239 | { |
---|
| 240 | sHtml = |
---|
| 241 | FCKConfig.DocType + |
---|
| 242 | '<html dir="' + FCKConfig.ContentLangDirection + '"' ; |
---|
| 243 | |
---|
| 244 | // On IE, if you are use a DOCTYPE differenft of HTML 4 (like |
---|
| 245 | // XHTML), you must force the vertical scroll to show, otherwise |
---|
| 246 | // the horizontal one may appear when the page needs vertical scrolling. |
---|
| 247 | if ( FCKBrowserInfo.IsIE && !FCKRegexLib.Html4DocType.test( FCKConfig.DocType ) ) |
---|
| 248 | sHtml += ' style="overflow-y: scroll"' ; |
---|
| 249 | |
---|
| 250 | sHtml += |
---|
| 251 | '><head><title></title>' + |
---|
| 252 | this._GetEditorAreaStyleTags() + |
---|
| 253 | '<link href="' + FCKConfig.FullBasePath + 'css/fck_internal.css' + '" rel="stylesheet" type="text/css" _fcktemp="true" />' ; |
---|
| 254 | |
---|
| 255 | if ( FCKBrowserInfo.IsIE ) |
---|
| 256 | sHtml += FCK._GetBehaviorsStyle() ; |
---|
| 257 | else if ( FCKConfig.ShowBorders ) |
---|
| 258 | sHtml += '<link href="' + FCKConfig.FullBasePath + 'css/fck_showtableborders_gecko.css" rel="stylesheet" type="text/css" _fcktemp="true" />' ; |
---|
| 259 | |
---|
| 260 | sHtml += FCK.TempBaseTag ; |
---|
| 261 | sHtml += '</head><body>' ; |
---|
| 262 | |
---|
| 263 | if ( FCKBrowserInfo.IsGecko && ( html.length == 0 || FCKRegexLib.EmptyParagraph.test( html ) ) ) |
---|
| 264 | sHtml += GECKO_BOGUS ; |
---|
| 265 | else |
---|
| 266 | sHtml += html ; |
---|
| 267 | |
---|
| 268 | sHtml += '</body></html>' ; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | this.EditingArea.OnLoad = FCK_EditingArea_OnLoad ; |
---|
| 272 | this.EditingArea.Start( sHtml ) ; |
---|
| 273 | } |
---|
| 274 | else |
---|
| 275 | { |
---|
| 276 | this.EditingArea.OnLoad = null ; |
---|
| 277 | this.EditingArea.Start( html ) ; |
---|
| 278 | |
---|
| 279 | // Enables the context menu in the textarea. |
---|
| 280 | this.EditingArea.Textarea._FCKShowContextMenu = true ; |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | if ( FCKBrowserInfo.IsGecko ) |
---|
| 284 | window.onresize() ; |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | function FCK_EditingArea_OnLoad() |
---|
| 288 | { |
---|
| 289 | // Get the editor's window and document (DOM) |
---|
| 290 | FCK.EditorWindow = FCK.EditingArea.Window ; |
---|
| 291 | FCK.EditorDocument = FCK.EditingArea.Document ; |
---|
| 292 | |
---|
| 293 | FCK.InitializeBehaviors() ; |
---|
| 294 | |
---|
| 295 | FCK.OnAfterSetHTML() ; |
---|
| 296 | |
---|
| 297 | // Check if it is not a startup call, otherwise complete the startup. |
---|
| 298 | if ( FCK.Status != FCK_STATUS_NOTLOADED ) |
---|
| 299 | return ; |
---|
| 300 | |
---|
| 301 | // Save the startup value for the "IsDirty()" check. |
---|
| 302 | FCK.ResetIsDirty() ; |
---|
| 303 | |
---|
| 304 | // Attach the editor to the form onsubmit event |
---|
| 305 | FCKTools.AttachToLinkedFieldFormSubmit( FCK.UpdateLinkedField ) ; |
---|
| 306 | |
---|
| 307 | FCK.SetStatus( FCK_STATUS_ACTIVE ) ; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | FCK._GetEditorAreaStyleTags = function() |
---|
| 311 | { |
---|
| 312 | var sTags = '' ; |
---|
| 313 | var aCSSs = FCKConfig.EditorAreaCSS ; |
---|
| 314 | |
---|
| 315 | for ( var i = 0 ; i < aCSSs.length ; i++ ) |
---|
| 316 | sTags += '<link href="' + aCSSs[i] + '" rel="stylesheet" type="text/css" />' ; |
---|
| 317 | |
---|
| 318 | return sTags ; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | // # Focus Manager: Manages the focus in the editor. |
---|
| 322 | var FCKFocusManager = FCK.FocusManager = new Object() ; |
---|
| 323 | FCKFocusManager.IsLocked = false ; |
---|
| 324 | FCK.HasFocus = false ; |
---|
| 325 | |
---|
| 326 | FCKFocusManager.AddWindow = function( win, sendToEditingArea ) |
---|
| 327 | { |
---|
| 328 | var oTarget ; |
---|
| 329 | |
---|
| 330 | if ( FCKBrowserInfo.IsIE ) |
---|
| 331 | oTarget = win.nodeType == 1 ? win : win.frameElement ? win.frameElement : win.document ; |
---|
| 332 | else |
---|
| 333 | oTarget = win.document ; |
---|
| 334 | |
---|
| 335 | FCKTools.AddEventListener( oTarget, 'blur', FCKFocusManager_Win_OnBlur ) ; |
---|
| 336 | FCKTools.AddEventListener( oTarget, 'focus', sendToEditingArea ? FCKFocusManager_Win_OnFocus_Area : FCKFocusManager_Win_OnFocus ) ; |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | FCKFocusManager.RemoveWindow = function( win ) |
---|
| 340 | { |
---|
| 341 | if ( FCKBrowserInfo.IsIE ) |
---|
| 342 | oTarget = win.nodeType == 1 ? win : win.frameElement ? win.frameElement : win.document ; |
---|
| 343 | else |
---|
| 344 | oTarget = win.document ; |
---|
| 345 | |
---|
| 346 | FCKTools.RemoveEventListener( oTarget, 'blur', FCKFocusManager_Win_OnBlur ) ; |
---|
| 347 | FCKTools.RemoveEventListener( oTarget, 'focus', FCKFocusManager_Win_OnFocus_Area ) ; |
---|
| 348 | FCKTools.RemoveEventListener( oTarget, 'focus', FCKFocusManager_Win_OnFocus ) ; |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | FCKFocusManager.Lock = function() |
---|
| 352 | { |
---|
| 353 | this.IsLocked = true ; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | FCKFocusManager.Unlock = function() |
---|
| 357 | { |
---|
| 358 | if ( this._HasPendingBlur ) |
---|
| 359 | FCKFocusManager._Timer = window.setTimeout( FCKFocusManager_FireOnBlur, 100 ) ; |
---|
| 360 | |
---|
| 361 | this.IsLocked = false ; |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | FCKFocusManager._ResetTimer = function() |
---|
| 365 | { |
---|
| 366 | this._HasPendingBlur = false ; |
---|
| 367 | |
---|
| 368 | if ( this._Timer ) |
---|
| 369 | { |
---|
| 370 | window.clearTimeout( this._Timer ) ; |
---|
| 371 | delete this._Timer ; |
---|
| 372 | } |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | function FCKFocusManager_Win_OnBlur() |
---|
| 376 | { |
---|
| 377 | if ( typeof(FCK) != 'undefined' && FCK.HasFocus ) |
---|
| 378 | { |
---|
| 379 | FCKFocusManager._ResetTimer() ; |
---|
| 380 | FCKFocusManager._Timer = window.setTimeout( FCKFocusManager_FireOnBlur, 100 ) ; |
---|
| 381 | } |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | function FCKFocusManager_FireOnBlur() |
---|
| 385 | { |
---|
| 386 | if ( FCKFocusManager.IsLocked ) |
---|
| 387 | FCKFocusManager._HasPendingBlur = true ; |
---|
| 388 | else |
---|
| 389 | { |
---|
| 390 | FCK.HasFocus = false ; |
---|
| 391 | FCK.Events.FireEvent( "OnBlur" ) ; |
---|
| 392 | } |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | function FCKFocusManager_Win_OnFocus_Area() |
---|
| 396 | { |
---|
| 397 | FCKFocusManager_Win_OnFocus() ; |
---|
| 398 | FCK.Focus() ; |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | function FCKFocusManager_Win_OnFocus() |
---|
| 402 | { |
---|
| 403 | FCKFocusManager._ResetTimer() ; |
---|
| 404 | |
---|
| 405 | if ( !FCK.HasFocus && !FCKFocusManager.IsLocked ) |
---|
| 406 | { |
---|
| 407 | FCK.HasFocus = true ; |
---|
| 408 | FCK.Events.FireEvent( "OnFocus" ) ; |
---|
| 409 | } |
---|
| 410 | } |
---|