[795] | 1 | /* |
---|
| 2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net |
---|
| 3 | * Copyright (C) 2003-2007 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 | * FCKEditingArea Class: renders an editable area. |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * @constructor |
---|
| 26 | * @param {String} targetElement The element that will hold the editing area. Any child element present in the target will be deleted. |
---|
| 27 | */ |
---|
| 28 | var FCKEditingArea = function( targetElement ) |
---|
| 29 | { |
---|
| 30 | this.TargetElement = targetElement ; |
---|
| 31 | this.Mode = FCK_EDITMODE_WYSIWYG ; |
---|
| 32 | |
---|
| 33 | if ( FCK.IECleanup ) |
---|
| 34 | FCK.IECleanup.AddItem( this, FCKEditingArea_Cleanup ) ; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * @param {String} html The complete HTML for the page, including DOCTYPE and the <html> tag. |
---|
| 40 | */ |
---|
| 41 | FCKEditingArea.prototype.Start = function( html, secondCall ) |
---|
| 42 | { |
---|
| 43 | var eTargetElement = this.TargetElement ; |
---|
| 44 | var oTargetDocument = FCKTools.GetElementDocument( eTargetElement ) ; |
---|
| 45 | |
---|
| 46 | // Remove all child nodes from the target. |
---|
| 47 | while( eTargetElement.childNodes.length > 0 ) |
---|
| 48 | eTargetElement.removeChild( eTargetElement.childNodes[0] ) ; |
---|
| 49 | |
---|
| 50 | if ( this.Mode == FCK_EDITMODE_WYSIWYG ) |
---|
| 51 | { |
---|
| 52 | // Create the editing area IFRAME. |
---|
| 53 | var oIFrame = this.IFrame = oTargetDocument.createElement( 'iframe' ) ; |
---|
| 54 | oIFrame.src = 'javascript:void(0)' ; |
---|
| 55 | oIFrame.frameBorder = 0 ; |
---|
| 56 | oIFrame.width = oIFrame.height = '100%' ; |
---|
| 57 | |
---|
| 58 | // Append the new IFRAME to the target. |
---|
| 59 | eTargetElement.appendChild( oIFrame ) ; |
---|
| 60 | |
---|
| 61 | // IE has a bug with the <base> tag... it must have a </base> closer, |
---|
| 62 | // otherwise the all sucessive tags will be set as children nodes of the <base>. |
---|
| 63 | if ( FCKBrowserInfo.IsIE ) |
---|
| 64 | html = html.replace( /(<base[^>]*?)\s*\/?>(?!\s*<\/base>)/gi, '$1></base>' ) ; |
---|
| 65 | else if ( !secondCall ) |
---|
| 66 | { |
---|
| 67 | // If nothing in the body, place a BOGUS tag so the cursor will appear. |
---|
| 68 | if ( FCKBrowserInfo.IsGecko ) |
---|
| 69 | html = html.replace( /(<body[^>]*>)\s*(<\/body>)/i, '$1' + GECKO_BOGUS + '$2' ) ; |
---|
| 70 | |
---|
| 71 | // Gecko moves some tags out of the body to the head, so we must use |
---|
| 72 | // innerHTML to set the body contents (SF BUG 1526154). |
---|
| 73 | |
---|
| 74 | // Extract the BODY contents from the html. |
---|
| 75 | var oMatch = html.match( FCKRegexLib.BodyContents ) ; |
---|
| 76 | |
---|
| 77 | if ( oMatch ) |
---|
| 78 | { |
---|
| 79 | html = |
---|
| 80 | oMatch[1] + // This is the HTML until the <body...> tag, inclusive. |
---|
| 81 | ' ' + |
---|
| 82 | oMatch[3] ; // This is the HTML from the </body> tag, inclusive. |
---|
| 83 | |
---|
| 84 | this._BodyHTML = oMatch[2] ; // This is the BODY tag contents. |
---|
| 85 | } |
---|
| 86 | else |
---|
| 87 | this._BodyHTML = html ; // Invalid HTML input. |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | // Get the window and document objects used to interact with the newly created IFRAME. |
---|
| 91 | this.Window = oIFrame.contentWindow ; |
---|
| 92 | |
---|
| 93 | // IE: Avoid JavaScript errors thrown by the editing are source (like tags events). |
---|
| 94 | // TODO: This error handler is not being fired. |
---|
| 95 | // this.Window.onerror = function() { alert( 'Error!' ) ; return true ; } |
---|
| 96 | |
---|
| 97 | var oDoc = this.Document = this.Window.document ; |
---|
| 98 | |
---|
| 99 | oDoc.open() ; |
---|
| 100 | oDoc.write( html ) ; |
---|
| 101 | oDoc.close() ; |
---|
| 102 | |
---|
| 103 | // Firefox 1.0.x is buggy... ohh yes... so let's do it two times and it |
---|
| 104 | // will magicaly work. |
---|
| 105 | if ( FCKBrowserInfo.IsGecko10 && !secondCall ) |
---|
| 106 | { |
---|
| 107 | this.Start( html, true ) ; |
---|
| 108 | return ; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | this.Window._FCKEditingArea = this ; |
---|
| 112 | |
---|
| 113 | // FF 1.0.x is buggy... we must wait a lot to enable editing because |
---|
| 114 | // sometimes the content simply disappears, for example when pasting |
---|
| 115 | // "bla1!<img src='some_url'>!bla2" in the source and then switching |
---|
| 116 | // back to design. |
---|
| 117 | if ( FCKBrowserInfo.IsGecko10 ) |
---|
| 118 | this.Window.setTimeout( FCKEditingArea_CompleteStart, 500 ) ; |
---|
| 119 | else |
---|
| 120 | FCKEditingArea_CompleteStart.call( this.Window ) ; |
---|
| 121 | } |
---|
| 122 | else |
---|
| 123 | { |
---|
| 124 | var eTextarea = this.Textarea = oTargetDocument.createElement( 'textarea' ) ; |
---|
| 125 | eTextarea.className = 'SourceField' ; |
---|
| 126 | eTextarea.dir = 'ltr' ; |
---|
| 127 | eTextarea.style.width = eTextarea.style.height = '100%' ; |
---|
| 128 | eTextarea.style.border = 'none' ; |
---|
| 129 | eTargetElement.appendChild( eTextarea ) ; |
---|
| 130 | |
---|
| 131 | eTextarea.value = html ; |
---|
| 132 | |
---|
| 133 | // Fire the "OnLoad" event. |
---|
| 134 | FCKTools.RunFunction( this.OnLoad ) ; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | // "this" here is FCKEditingArea.Window |
---|
| 139 | function FCKEditingArea_CompleteStart() |
---|
| 140 | { |
---|
| 141 | // Of Firefox, the DOM takes a little to become available. So we must wait for it in a loop. |
---|
| 142 | if ( !this.document.body ) |
---|
| 143 | { |
---|
| 144 | this.setTimeout( FCKEditingArea_CompleteStart, 50 ) ; |
---|
| 145 | return ; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | var oEditorArea = this._FCKEditingArea ; |
---|
| 149 | oEditorArea.MakeEditable() ; |
---|
| 150 | |
---|
| 151 | // Fire the "OnLoad" event. |
---|
| 152 | FCKTools.RunFunction( oEditorArea.OnLoad ) ; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | FCKEditingArea.prototype.MakeEditable = function() |
---|
| 156 | { |
---|
| 157 | var oDoc = this.Document ; |
---|
| 158 | |
---|
| 159 | if ( FCKBrowserInfo.IsIE ) |
---|
| 160 | { |
---|
| 161 | oDoc.body.contentEditable = true ; |
---|
| 162 | |
---|
| 163 | /* The following commands don't throw errors, but have no effect. |
---|
| 164 | oDoc.execCommand( 'AutoDetect', false, false ) ; |
---|
| 165 | oDoc.execCommand( 'KeepSelection', false, true ) ; |
---|
| 166 | */ |
---|
| 167 | } |
---|
| 168 | else |
---|
| 169 | { |
---|
| 170 | try |
---|
| 171 | { |
---|
| 172 | // Disable Firefox 2 Spell Checker. |
---|
| 173 | oDoc.body.spellcheck = ( this.FFSpellChecker !== false ) ; |
---|
| 174 | |
---|
| 175 | if ( this._BodyHTML ) |
---|
| 176 | { |
---|
| 177 | oDoc.body.innerHTML = this._BodyHTML ; |
---|
| 178 | this._BodyHTML = null ; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | oDoc.designMode = 'on' ; |
---|
| 182 | |
---|
| 183 | // Tell Gecko to use or not the <SPAN> tag for the bold, italic and underline. |
---|
| 184 | try |
---|
| 185 | { |
---|
| 186 | oDoc.execCommand( 'styleWithCSS', false, FCKConfig.GeckoUseSPAN ) ; |
---|
| 187 | } |
---|
| 188 | catch (e) |
---|
| 189 | { |
---|
| 190 | // As evidenced here, useCSS is deprecated in favor of styleWithCSS: |
---|
| 191 | // http://www.mozilla.org/editor/midas-spec.html |
---|
| 192 | oDoc.execCommand( 'useCSS', false, !FCKConfig.GeckoUseSPAN ) ; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | // Analysing Firefox 1.5 source code, it seams that there is support for a |
---|
| 196 | // "insertBrOnReturn" command. Applying it gives no error, but it doesn't |
---|
| 197 | // gives the same behavior that you have with IE. It works only if you are |
---|
| 198 | // already inside a paragraph and it doesn't render correctly in the first enter. |
---|
| 199 | // oDoc.execCommand( 'insertBrOnReturn', false, false ) ; |
---|
| 200 | |
---|
| 201 | // Tell Gecko (Firefox 1.5+) to enable or not live resizing of objects (by Alfonso Martinez) |
---|
| 202 | oDoc.execCommand( 'enableObjectResizing', false, !FCKConfig.DisableObjectResizing ) ; |
---|
| 203 | |
---|
| 204 | // Disable the standard table editing features of Firefox. |
---|
| 205 | oDoc.execCommand( 'enableInlineTableEditing', false, !FCKConfig.DisableFFTableHandles ) ; |
---|
| 206 | } |
---|
| 207 | catch (e) {} |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | FCKEditingArea.prototype.Focus = function() |
---|
| 212 | { |
---|
| 213 | try |
---|
| 214 | { |
---|
| 215 | if ( this.Mode == FCK_EDITMODE_WYSIWYG ) |
---|
| 216 | { |
---|
| 217 | // The following check is important to avoid IE entering in a focus loop. Ref: |
---|
| 218 | // http://sourceforge.net/tracker/index.php?func=detail&aid=1567060&group_id=75348&atid=543653 |
---|
| 219 | if ( FCKBrowserInfo.IsIE && this.Document.hasFocus() ) |
---|
| 220 | return ; |
---|
| 221 | |
---|
| 222 | if ( FCKBrowserInfo.IsSafari ) |
---|
| 223 | this.IFrame.focus() ; |
---|
| 224 | else |
---|
| 225 | { |
---|
| 226 | this.Window.focus() ; |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | else |
---|
| 230 | { |
---|
| 231 | var oDoc = FCKTools.GetElementDocument( this.Textarea ) ; |
---|
| 232 | if ( (!oDoc.hasFocus || oDoc.hasFocus() ) && oDoc.activeElement == this.Textarea ) |
---|
| 233 | return ; |
---|
| 234 | |
---|
| 235 | this.Textarea.focus() ; |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | catch(e) {} |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | function FCKEditingArea_Cleanup() |
---|
| 242 | { |
---|
| 243 | this.TargetElement = null ; |
---|
| 244 | this.IFrame = null ; |
---|
| 245 | this.Document = null ; |
---|
| 246 | this.Textarea = null ; |
---|
| 247 | |
---|
| 248 | if ( this.Window ) |
---|
| 249 | { |
---|
| 250 | this.Window._FCKEditingArea = null ; |
---|
| 251 | this.Window = null ; |
---|
| 252 | } |
---|
| 253 | } |
---|