/source/Plug-in/fck/editor/_source/classes/fckeditingarea.js

http://prosporous.googlecode.com/ · JavaScript · 324 lines · 190 code · 54 blank · 80 comment · 35 complexity · 662474092a841b94da3070b5b86b5851 MD5 · raw file

  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. * @constructor
  25. * @param {String} targetElement The element that will hold the editing area. Any child element present in the target will be deleted.
  26. */
  27. var FCKEditingArea = function( targetElement )
  28. {
  29. this.TargetElement = targetElement ;
  30. this.Mode = FCK_EDITMODE_WYSIWYG ;
  31. if ( FCK.IECleanup )
  32. FCK.IECleanup.AddItem( this, FCKEditingArea_Cleanup ) ;
  33. }
  34. /**
  35. * @param {String} html The complete HTML for the page, including DOCTYPE and the <html> tag.
  36. */
  37. FCKEditingArea.prototype.Start = function( html, secondCall )
  38. {
  39. var eTargetElement = this.TargetElement ;
  40. var oTargetDocument = FCKTools.GetElementDocument( eTargetElement ) ;
  41. // Remove all child nodes from the target.
  42. var oChild ;
  43. while( ( oChild = eTargetElement.firstChild ) ) // Only one "=".
  44. {
  45. // Set innerHTML = '' to avoid memory leak.
  46. if ( oChild.contentWindow )
  47. oChild.contentWindow.document.body.innerHTML = '' ;
  48. eTargetElement.removeChild( oChild ) ;
  49. }
  50. if ( this.Mode == FCK_EDITMODE_WYSIWYG )
  51. {
  52. // Create the editing area IFRAME.
  53. var oIFrame = this.IFrame = oTargetDocument.createElement( 'iframe' ) ;
  54. // Firefox will render the tables inside the body in Quirks mode if the
  55. // source of the iframe is set to javascript. see #515
  56. if ( !FCKBrowserInfo.IsGecko )
  57. oIFrame.src = 'javascript:void(0)' ;
  58. oIFrame.frameBorder = 0 ;
  59. oIFrame.width = oIFrame.height = '100%' ;
  60. // Append the new IFRAME to the target.
  61. eTargetElement.appendChild( oIFrame ) ;
  62. // IE has a bug with the <base> tag... it must have a </base> closer,
  63. // otherwise the all successive tags will be set as children nodes of the <base>.
  64. if ( FCKBrowserInfo.IsIE )
  65. html = html.replace( /(<base[^>]*?)\s*\/?>(?!\s*<\/base>)/gi, '$1></base>' ) ;
  66. else if ( !secondCall )
  67. {
  68. // Gecko moves some tags out of the body to the head, so we must use
  69. // innerHTML to set the body contents (SF BUG 1526154).
  70. // Extract the BODY contents from the html.
  71. var oMatchBefore = html.match( FCKRegexLib.BeforeBody ) ;
  72. var oMatchAfter = html.match( FCKRegexLib.AfterBody ) ;
  73. if ( oMatchBefore && oMatchAfter )
  74. {
  75. var sBody = html.substr( oMatchBefore[1].length,
  76. html.length - oMatchBefore[1].length - oMatchAfter[1].length ) ; // This is the BODY tag contents.
  77. html =
  78. oMatchBefore[1] + // This is the HTML until the <body...> tag, inclusive.
  79. '&nbsp;' +
  80. oMatchAfter[1] ; // This is the HTML from the </body> tag, inclusive.
  81. // If nothing in the body, place a BOGUS tag so the cursor will appear.
  82. if ( FCKBrowserInfo.IsGecko && ( sBody.length == 0 || FCKRegexLib.EmptyParagraph.test( sBody ) ) )
  83. sBody = '<br type="_moz">' ;
  84. this._BodyHTML = sBody ;
  85. }
  86. else
  87. this._BodyHTML = html ; // Invalid HTML input.
  88. }
  89. // Get the window and document objects used to interact with the newly created IFRAME.
  90. this.Window = oIFrame.contentWindow ;
  91. // IE: Avoid JavaScript errors thrown by the editing are source (like tags events).
  92. // TODO: This error handler is not being fired.
  93. // this.Window.onerror = function() { alert( 'Error!' ) ; return true ; }
  94. var oDoc = this.Document = this.Window.document ;
  95. oDoc.open() ;
  96. oDoc.write( html ) ;
  97. oDoc.close() ;
  98. // Firefox 1.0.x is buggy... ohh yes... so let's do it two times and it
  99. // will magically work.
  100. if ( FCKBrowserInfo.IsGecko10 && !secondCall )
  101. {
  102. this.Start( html, true ) ;
  103. return ;
  104. }
  105. this.Window._FCKEditingArea = this ;
  106. // FF 1.0.x is buggy... we must wait a lot to enable editing because
  107. // sometimes the content simply disappears, for example when pasting
  108. // "bla1!<img src='some_url'>!bla2" in the source and then switching
  109. // back to design.
  110. if ( FCKBrowserInfo.IsGecko10 )
  111. this.Window.setTimeout( FCKEditingArea_CompleteStart, 500 ) ;
  112. else
  113. FCKEditingArea_CompleteStart.call( this.Window ) ;
  114. }
  115. else
  116. {
  117. var eTextarea = this.Textarea = oTargetDocument.createElement( 'textarea' ) ;
  118. eTextarea.className = 'SourceField' ;
  119. eTextarea.dir = 'ltr' ;
  120. FCKDomTools.SetElementStyles( eTextarea,
  121. {
  122. width : '100%',
  123. height : '100%',
  124. border : 'none',
  125. resize : 'none',
  126. outline : 'none'
  127. } ) ;
  128. eTargetElement.appendChild( eTextarea ) ;
  129. eTextarea.value = html ;
  130. // Fire the "OnLoad" event.
  131. FCKTools.RunFunction( this.OnLoad ) ;
  132. }
  133. }
  134. // "this" here is FCKEditingArea.Window
  135. function FCKEditingArea_CompleteStart()
  136. {
  137. // On Firefox, the DOM takes a little to become available. So we must wait for it in a loop.
  138. if ( !this.document.body )
  139. {
  140. this.setTimeout( FCKEditingArea_CompleteStart, 50 ) ;
  141. return ;
  142. }
  143. var oEditorArea = this._FCKEditingArea ;
  144. oEditorArea.MakeEditable() ;
  145. // Fire the "OnLoad" event.
  146. FCKTools.RunFunction( oEditorArea.OnLoad ) ;
  147. }
  148. FCKEditingArea.prototype.MakeEditable = function()
  149. {
  150. var oDoc = this.Document ;
  151. if ( FCKBrowserInfo.IsIE )
  152. {
  153. // Kludge for #141 and #523
  154. oDoc.body.disabled = true ;
  155. oDoc.body.contentEditable = true ;
  156. oDoc.body.removeAttribute( "disabled" ) ;
  157. /* The following commands don't throw errors, but have no effect.
  158. oDoc.execCommand( 'AutoDetect', false, false ) ;
  159. oDoc.execCommand( 'KeepSelection', false, true ) ;
  160. */
  161. }
  162. else
  163. {
  164. try
  165. {
  166. // Disable Firefox 2 Spell Checker.
  167. oDoc.body.spellcheck = ( this.FFSpellChecker !== false ) ;
  168. if ( this._BodyHTML )
  169. {
  170. oDoc.body.innerHTML = this._BodyHTML ;
  171. this._BodyHTML = null ;
  172. }
  173. oDoc.designMode = 'on' ;
  174. // Tell Gecko (Firefox 1.5+) to enable or not live resizing of objects (by Alfonso Martinez)
  175. oDoc.execCommand( 'enableObjectResizing', false, !FCKConfig.DisableObjectResizing ) ;
  176. // Disable the standard table editing features of Firefox.
  177. oDoc.execCommand( 'enableInlineTableEditing', false, !FCKConfig.DisableFFTableHandles ) ;
  178. }
  179. catch (e)
  180. {
  181. // In Firefox if the iframe is initially hidden it can't be set to designMode and it raises an exception
  182. // So we set up a DOM Mutation event Listener on the HTML, as it will raise several events when the document is visible again
  183. FCKTools.AddEventListener( this.Window.frameElement, 'DOMAttrModified', FCKEditingArea_Document_AttributeNodeModified ) ;
  184. }
  185. }
  186. }
  187. // This function processes the notifications of the DOM Mutation event on the document
  188. // We use it to know that the document will be ready to be editable again (or we hope so)
  189. function FCKEditingArea_Document_AttributeNodeModified( evt )
  190. {
  191. var editingArea = evt.currentTarget.contentWindow._FCKEditingArea ;
  192. // We want to run our function after the events no longer fire, so we can know that it's a stable situation
  193. if ( editingArea._timer )
  194. window.clearTimeout( editingArea._timer ) ;
  195. editingArea._timer = FCKTools.SetTimeout( FCKEditingArea_MakeEditableByMutation, 1000, editingArea ) ;
  196. }
  197. // This function ideally should be called after the document is visible, it does clean up of the
  198. // mutation tracking and tries again to make the area editable.
  199. function FCKEditingArea_MakeEditableByMutation()
  200. {
  201. // Clean up
  202. delete this._timer ;
  203. // Now we don't want to keep on getting this event
  204. FCKTools.RemoveEventListener( this.Window.frameElement, 'DOMAttrModified', FCKEditingArea_Document_AttributeNodeModified ) ;
  205. // Let's try now to set the editing area editable
  206. // If it fails it will set up the Mutation Listener again automatically
  207. this.MakeEditable() ;
  208. }
  209. FCKEditingArea.prototype.Focus = function()
  210. {
  211. try
  212. {
  213. if ( this.Mode == FCK_EDITMODE_WYSIWYG )
  214. {
  215. // The following check is important to avoid IE entering in a focus loop. Ref:
  216. // http://sourceforge.net/tracker/index.php?func=detail&aid=1567060&group_id=75348&atid=543653
  217. if ( FCKBrowserInfo.IsIE && this.Document.hasFocus() )
  218. this._EnsureFocusIE() ;
  219. this.Window.focus() ;
  220. // In IE it can happen that the document is in theory focused but the active element is outside it
  221. if ( FCKBrowserInfo.IsIE )
  222. this._EnsureFocusIE() ;
  223. }
  224. else
  225. {
  226. var oDoc = FCKTools.GetElementDocument( this.Textarea ) ;
  227. if ( (!oDoc.hasFocus || oDoc.hasFocus() ) && oDoc.activeElement == this.Textarea )
  228. return ;
  229. this.Textarea.focus() ;
  230. }
  231. }
  232. catch(e) {}
  233. }
  234. FCKEditingArea.prototype._EnsureFocusIE = function()
  235. {
  236. // In IE it can happen that the document is in theory focused but the active element is outside it
  237. this.Document.body.setActive() ;
  238. // Kludge for #141... yet more code to workaround IE bugs
  239. var range = this.Document.selection.createRange() ;
  240. var parentNode = range.parentElement() ;
  241. var parentTag = parentNode.nodeName.toLowerCase() ;
  242. // Only apply the fix when in a block, and the block is empty.
  243. if ( parentNode.childNodes.length > 0 ||
  244. !( FCKListsLib.BlockElements[parentTag] ||
  245. FCKListsLib.NonEmptyBlockElements[parentTag] ) )
  246. {
  247. return ;
  248. }
  249. range.moveEnd( "character", 1 ) ;
  250. range.select() ;
  251. if ( range.boundingWidth > 0 )
  252. {
  253. range.moveEnd( "character", -1 ) ;
  254. range.select() ;
  255. }
  256. }
  257. function FCKEditingArea_Cleanup()
  258. {
  259. if ( this.Document )
  260. this.Document.body.innerHTML = "" ;
  261. this.TargetElement = null ;
  262. this.IFrame = null ;
  263. this.Document = null ;
  264. this.Textarea = null ;
  265. if ( this.Window )
  266. {
  267. this.Window._FCKEditingArea = null ;
  268. this.Window = null ;
  269. }
  270. }