PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/fckeditor/fckeditor.js

http://fckeditor-for-wordpress.googlecode.com/
JavaScript | 214 lines | 157 code | 26 blank | 31 comment | 28 complexity | 5161dfa69f99f4ecea8955fc662e3784 MD5 | raw file
Possible License(s): LGPL-2.1
  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. * This is the integration file for JavaScript.
  22. *
  23. * It defines the FCKeditor class that can be used to create editor
  24. * instances in a HTML page in the client side. For server side
  25. * operations, use the specific integration system.
  26. */
  27. // FCKeditor Class
  28. var FCKeditor = function( instanceName, width, height, toolbarSet, value )
  29. {
  30. // Properties
  31. this.InstanceName = instanceName ;
  32. this.Width = width || '100%' ;
  33. this.Height = height || '200' ;
  34. this.ToolbarSet = toolbarSet || 'Default' ;
  35. this.Value = value || '' ;
  36. this.BasePath = '/fckeditor/' ;
  37. this.CheckBrowser = true ;
  38. this.DisplayErrors = true ;
  39. this.EnableSafari = false ; // This is a temporary property, while Safari support is under development.
  40. this.EnableOpera = false ; // This is a temporary property, while Opera support is under development.
  41. this.Config = new Object() ;
  42. // Events
  43. this.OnError = null ; // function( source, errorNumber, errorDescription )
  44. }
  45. FCKeditor.prototype.Version = '2.4.2' ;
  46. FCKeditor.prototype.VersionBuild = '14978' ;
  47. FCKeditor.prototype.Create = function()
  48. {
  49. document.write( this.CreateHtml() ) ;
  50. }
  51. FCKeditor.prototype.CreateHtml = function()
  52. {
  53. // Check for errors
  54. if ( !this.InstanceName || this.InstanceName.length == 0 )
  55. {
  56. this._ThrowError( 701, 'You must specify an instance name.' ) ;
  57. return '' ;
  58. }
  59. var sHtml = '<div>' ;
  60. if ( !this.CheckBrowser || this._IsCompatibleBrowser() )
  61. {
  62. sHtml += '<input type="hidden" id="' + this.InstanceName + '" name="' + this.InstanceName + '" value="' + this._HTMLEncode( this.Value ) + '" style="display:none" />' ;
  63. sHtml += this._GetConfigHtml() ;
  64. sHtml += this._GetIFrameHtml() ;
  65. }
  66. else
  67. {
  68. var sWidth = this.Width.toString().indexOf('%') > 0 ? this.Width : this.Width + 'px' ;
  69. var sHeight = this.Height.toString().indexOf('%') > 0 ? this.Height : this.Height + 'px' ;
  70. sHtml += '<textarea name="' + this.InstanceName + '" rows="4" cols="40" style="width:' + sWidth + ';height:' + sHeight + '">' + this._HTMLEncode( this.Value ) + '<\/textarea>' ;
  71. }
  72. sHtml += '</div>' ;
  73. return sHtml ;
  74. }
  75. FCKeditor.prototype.ReplaceTextarea = function()
  76. {
  77. if ( !this.CheckBrowser || this._IsCompatibleBrowser() )
  78. {
  79. // We must check the elements firstly using the Id and then the name.
  80. var oTextarea = document.getElementById( this.InstanceName ) ;
  81. var colElementsByName = document.getElementsByName( this.InstanceName ) ;
  82. var i = 0;
  83. while ( oTextarea || i == 0 )
  84. {
  85. if ( oTextarea && oTextarea.tagName.toLowerCase() == 'textarea' )
  86. break ;
  87. oTextarea = colElementsByName[i++] ;
  88. }
  89. if ( !oTextarea )
  90. {
  91. alert( 'Error: The TEXTAREA with id or name set to "' + this.InstanceName + '" was not found' ) ;
  92. return ;
  93. }
  94. oTextarea.style.display = 'none' ;
  95. this._InsertHtmlBefore( this._GetConfigHtml(), oTextarea ) ;
  96. this._InsertHtmlBefore( this._GetIFrameHtml(), oTextarea ) ;
  97. }
  98. }
  99. FCKeditor.prototype._InsertHtmlBefore = function( html, element )
  100. {
  101. if ( element.insertAdjacentHTML ) // IE
  102. element.insertAdjacentHTML( 'beforeBegin', html ) ;
  103. else // Gecko
  104. {
  105. var oRange = document.createRange() ;
  106. oRange.setStartBefore( element ) ;
  107. var oFragment = oRange.createContextualFragment( html );
  108. element.parentNode.insertBefore( oFragment, element ) ;
  109. }
  110. }
  111. FCKeditor.prototype._GetConfigHtml = function()
  112. {
  113. var sConfig = '' ;
  114. for ( var o in this.Config )
  115. {
  116. if ( sConfig.length > 0 ) sConfig += '&amp;' ;
  117. sConfig += encodeURIComponent( o ) + '=' + encodeURIComponent( this.Config[o] ) ;
  118. }
  119. return '<input type="hidden" id="' + this.InstanceName + '___Config" value="' + sConfig + '" style="display:none" />' ;
  120. }
  121. FCKeditor.prototype._GetIFrameHtml = function()
  122. {
  123. var sFile = 'fckeditor.html' ;
  124. try
  125. {
  126. if ( (/fcksource=true/i).test( window.top.location.search ) )
  127. sFile = 'fckeditor.original.html' ;
  128. }
  129. catch (e) { /* Ignore it. Much probably we are inside a FRAME where the "top" is in another domain (security error). */ }
  130. var sLink = this.BasePath + 'editor/' + sFile + '?InstanceName=' + encodeURIComponent( this.InstanceName ) ;
  131. if (this.ToolbarSet) sLink += '&amp;Toolbar=' + this.ToolbarSet ;
  132. return '<iframe id="' + this.InstanceName + '___Frame" src="' + sLink + '" width="' + this.Width + '" height="' + this.Height + '" frameborder="0" scrolling="no"></iframe>' ;
  133. }
  134. FCKeditor.prototype._IsCompatibleBrowser = function()
  135. {
  136. return FCKeditor_IsCompatibleBrowser( this.EnableSafari, this.EnableOpera ) ;
  137. }
  138. FCKeditor.prototype._ThrowError = function( errorNumber, errorDescription )
  139. {
  140. this.ErrorNumber = errorNumber ;
  141. this.ErrorDescription = errorDescription ;
  142. if ( this.DisplayErrors )
  143. {
  144. document.write( '<div style="COLOR: #ff0000">' ) ;
  145. document.write( '[ FCKeditor Error ' + this.ErrorNumber + ': ' + this.ErrorDescription + ' ]' ) ;
  146. document.write( '</div>' ) ;
  147. }
  148. if ( typeof( this.OnError ) == 'function' )
  149. this.OnError( this, errorNumber, errorDescription ) ;
  150. }
  151. FCKeditor.prototype._HTMLEncode = function( text )
  152. {
  153. if ( typeof( text ) != "string" )
  154. text = text.toString() ;
  155. text = text.replace(
  156. /&/g, "&amp;").replace(
  157. /"/g, "&quot;").replace(
  158. /</g, "&lt;").replace(
  159. />/g, "&gt;") ;
  160. return text ;
  161. }
  162. function FCKeditor_IsCompatibleBrowser( enableSafari, enableOpera )
  163. {
  164. var sAgent = navigator.userAgent.toLowerCase() ;
  165. // Internet Explorer
  166. if ( sAgent.indexOf("msie") != -1 && sAgent.indexOf("mac") == -1 && sAgent.indexOf("opera") == -1 )
  167. {
  168. var sBrowserVersion = navigator.appVersion.match(/MSIE (.\..)/)[1] ;
  169. return ( sBrowserVersion >= 5.5 ) ;
  170. }
  171. // Gecko (Opera 9 tries to behave like Gecko at this point).
  172. if ( navigator.product == "Gecko" && navigator.productSub >= 20030210 && !( typeof(opera) == 'object' && opera.postError ) )
  173. return true ;
  174. // Opera
  175. if ( enableOpera && navigator.appName == 'Opera' && parseInt( navigator.appVersion, 10 ) >= 9 )
  176. return true ;
  177. // Safari
  178. if ( enableSafari && sAgent.indexOf( 'safari' ) != -1 )
  179. return ( sAgent.match( /safari\/(\d+)/ )[1] >= 312 ) ; // Build must be at least 312 (1.3)
  180. return false ;
  181. }