FCKeditor 源代码分析附中文注释(fcos源码)越早知道越好

随心笔谈9个月前发布 admin
79 0

var FCKeditor=function( instanceName, width, height, toolbarSet, value )

{

//编辑器的基本属性 注意:这些东西优先于FCKConfig.js中的配置

this.InstanceName=instanceName ; //编辑器的唯一名称(相当于ID)(必须有!)

this.Width=width || ‘100%’ ; //宽度 默认是100%

this.Height=height || ‘200’ ; //宽度 默认是200

this.ToolbarSet=toolbarSet || ‘Default’ ;//工具集名称,默认值是Default

this.Value=value || ” ; //初始化编辑器的HTML代码,默认值为空

//编辑器初始化的时候默认的根路径, 其作用是编写fck中,凡是用到的路径,均从FCKeditor.BasePath目录开始 默认为/Fckeditor/

this.BasePath=FCKeditor.BasePath ;

this.CheckBrowser=true ; //是否在显示编辑器前检查浏览器兼容性,默认为true

this.DisplayErrors=true ; //是否显示提示错误,默为true

this.Config=new Object() ;

// Events

this.OnError=null ; // function( source, errorNumber, errorDescription )自定义的错误处理函数

}

FCKeditor.BasePath=’/fckeditor/’ ; // fck默认的根目录

FCKeditor.MinHeight=200 ; //高和宽的限制

FCKeditor.MinWidth=750 ;

FCKeditor.prototype.Version=’2.6.5′ ; //版本号

FCKeditor.prototype.VersionBuild=’23959′ ;

FCKeditor.prototype.Create=function()

{

//调用createhtml()方法

document.write( this.CreateHtml() ) ;

}

FCKeditor.prototype.CreateHtml=function()

{

// 检查有无InstanceName 如果没有则不生成html代码

if ( !this.InstanceName || this.InstanceName.length==0 )

{

this._ThrowError( 701, ‘You must specify an instance name.’ ) ;

return ” ;

}

//函数的返回值

var sHtml=” ;

if ( !this.CheckBrowser || this._IsCompatibleBrowser() )

{

//将此时FCK初始值通过转义之后放入这个input

sHtml +=” ;

//生成一个隐藏的INPUT来放置this.config中的内容

sHtml +=this._GetConfigHtml() ;

//生成编辑器的iframe的代码

sHtml +=this._GetIFrameHtml() ;

}

else

{

var sWidth=this.Width.toString().indexOf(‘%’) > 0 ? this.Width : this.Width + ‘px’ ;

var sHeight=this.Height.toString().indexOf(‘%’) > 0 ? this.Height : this.Height + ‘px’ ;

sHtml +=’

‘” rows=”4″ cols=”40″ style=”width:’ + sWidth +

‘;height:’ + sHeight ;

if ( this.TabIndex )

sHtml +='” tabindex=”‘ + this.TabIndex ;

sHtml +='”>’ +

this._HTMLEncode( this.Value ) +

” ;

}

return sHtml ;

}

FCKeditor.prototype.ReplaceTextarea=function()

{

//如果已经有了 id=THIS.INSTANCENAME___Frame 的标签时,直接返回

if ( document.getElementById( this.InstanceName + ‘___Frame’ ) )

return ;

//当用户的浏览器符合预设的几种浏览器时

if ( !this.CheckBrowser || this._IsCompatibleBrowser() )

{

// We must check the elements firstly using the Id and then the name.

//获取id=this.InstanceName的html标签

var oTextarea=document.getElementById( this.InstanceName ) ;

//获取所有name=THIS.instancename的标签

var colElementsByName=document.getElementsByName( this.InstanceName ) ;

var i=0;

while ( oTextarea || i==0 )

{

//遍历,直到找到name=this.instancename的textarea标签,并赋给oTextarea

if ( oTextarea && oTextarea.tagName.toLowerCase()==’textarea’ )

break ;

oTextarea=colElementsByName[i++] ;

}

//如果不存在id或者name为this.instancename的标签时,弹出错误框

if ( !oTextarea )

{

alert( ‘Error: The TEXTAREA with id or name set to “‘ + this.InstanceName + ‘” was not found’ ) ;

return ;

}

oTextarea.style.display=’none’ ;

//如果页面上对这样的textarea标签定义了tab键的顺序,赋给this.TabIndex待用

if ( oTextarea.tabIndex )

this.TabIndex=oTextarea.tabIndex ;

this._InsertHtmlBefore( this._GetConfigHtml(), oTextarea ) ;

this._InsertHtmlBefore( this._GetIFrameHtml(), oTextarea ) ;

}

}

FCKeditor.prototype._InsertHtmlBefore=function( html, element )

{

if ( element.insertAdjacentHTML ) // IE 私有的 insertAdjacentHTML 方法

element.insertAdjacentHTML( ‘beforeBegin’, html ) ;

else // 非ie浏览器

{

var oRange=document.createRange() ;

oRange.setStartBefore( element ) ;

var oFragment=oRange.createContextualFragment( html );

element.parentNode.insertBefore( oFragment, element ) ;

}

}

FCKeditor.prototype._GetConfigHtml=function()

{

var sConfig=” ;

for ( var o in this.Config )

{

if ( sConfig.length > 0 ) sConfig +=’&’ ;

//encodeURIComponent函数转换成百分比编码

sConfig +=encodeURIComponent( o ) + ‘=’ + encodeURIComponent( this.Config[o] ) ;

}

return ” ;

}

FCKeditor.prototype._GetIFrameHtml=function()

{

var sFile=’fckeditor.html’ ;

//特殊情况 fckedito所在的窗口没有嵌入在浏览器中

try

{

if ( (/fcksource=true/i).test( window.top.location.search ) )

sFile=’fckeditor.original.html’ ;

}

catch (e) { }

//sLink就是这个事实上的页面了,从fck的根目录开始,例如 sLink=/fckeditor/editor/fckeditor.html?InstanceName=nileader&Toolbar=nileadersbar

var sLink=this.BasePath + ‘editor/’ + sFile + ‘?InstanceName=’ + encodeURIComponent( this.InstanceName ) ;

if (this.ToolbarSet)

sLink +=’&Toolbar=’ + this.ToolbarSet ;

//生成一个真正的编辑iframer的html代码 当然,放入了src=https://www.jb51.net/article/slink

var html=’

‘___Frame” src=”https://www.jb51.net/article/’ + sLink +

‘” src=”https://www.jb51.net/article/’ + sLink +

‘” width=”‘ + this.Width +

‘” height=”‘ + this.Height ;

//如果设定了使用”Tab”键的遍历顺序,则赋给iframe

if ( this.TabIndex )

html +='” tabindex=”‘ + this.TabIndex ;

html +='” frameborder=”0″ scrolling=”no”>’ ;

return html ;

}

FCKeditor.prototype._IsCompatibleBrowser=function()

{

return FCKeditor_IsCompatibleBrowser() ;

}

FCKeditor.prototype._ThrowError=function( errorNumber, errorDescription )

{

this.ErrorNumber=errorNumber ;

this.ErrorDescription=errorDescription ;

//是否显示提示错误,默为true

if ( this.DisplayErrors )

{ //将错误编号和错误概述打印出来

document.write( ‘

‘ ) ;

document.write( ‘[ FCKeditor Error ‘ + this.ErrorNumber + ‘: ‘ + this.ErrorDescription + ‘ ]’ ) ;

document.write( ” ) ;

}

//OnError是否自定义了错误处理函数,若定义了,由其处理

if ( typeof( this.OnError )==’function’ )

this.OnError( this, errorNumber, errorDescription ) ;

}

FCKeditor.prototype._HTMLEncode=function( text )

{

if ( typeof( text ) !=”string” )

text=text.toString() ;

//将字符串中的所有 & ” < > 用对应的转义字符代换

text=text.replace(

/&/g, “&”).replace(

/”/g, “””).replace(

/

/>/g, “>”) ;

return text ;

}

;(function()

{

//把页面上的textarea元素赋给editor变量

var textareaToEditor=function( textarea )

{

var editor=new FCKeditor( textarea.name ) ;

editor.Width=Math.max( textarea.offsetWidth, FCKeditor.MinWidth ) ;

editor.Height=Math.max( textarea.offsetHeight, FCKeditor.MinHeight ) ;

return editor ;

}

FCKeditor.ReplaceAllTextareas=function()

{

//获取所有的textarea元素

var textareas=document.getElementsByTagName( ‘textarea’ ) ;

for ( var i=0 ; i < textareas.length ; i++ )

{

var editor=null ;

var textarea=textareas[i] ;

var name=textarea.name ;

// The “name” attribute must exist.

if ( !name || name.length==0 )

continue ;

if ( typeof arguments[0]==’string’ )

{

// The textarea class name could be passed as the function

// parameter.

var classRegex=new RegExp( ‘(?:^| )’ + arguments[0] + ‘(?:$| )’ ) ;

if ( !classRegex.test( textarea.className ) )

continue ;

}

else if ( typeof arguments[0]==’function’ )

{

// An assertion function could be passed as the function parameter.

// It must explicitly return “false” to ignore a specific

editor=textareaToEditor( textarea ) ;

if ( arguments[0]( textarea, editor )===false )

continue ;

}

if ( !editor )

editor=textareaToEditor( textarea ) ;

editor.ReplaceTextarea() ;

}

}

})() ;

function FCKeditor_IsCompatibleBrowser()

{

var sAgent=navigator.userAgent.toLowerCase() ;

// 当前浏览器是Internet Explorer 5.5+

//利用条件编译判断IE 在IE中,false==!false==true,

//如果是非IE浏览器,则忽略,false==false

if ( false && sAgent.indexOf(“mac”)==-1 ) //不是apple mac os

{

var sBrowserVersion=navigator.appVersion.match(/MSIE (.\..)/)[1] ;

return ( sBrowserVersion >=5.5 ) ;

}

// Gecko (Opera 9 tries to behave like Gecko at this point).

//检测是否是OPERA 9 浏览器

if ( navigator.product==”Gecko” && navigator.productSub >=20030210 && !( typeof(opera)==’object’ && opera.postError ) )

return true ;

// Opera 9.50+

if ( window.opera && window.opera.version && parseFloat( window.opera.version() ) >=9.5 )

return true ;

// Adobe AIR

// Checked before Safari because AIR have the WebKit rich text editor

// features from Safari 3.0.4, but the version reported is 420.

if ( sAgent.indexOf( ‘ adobeair/’ ) !=-1 )

return ( sAgent.match( / adobeair/(\d+)/ )[1] >=1 ) ; // Build must be at least v1

// Safari 3+

if ( sAgent.indexOf( ‘ applewebkit/’ ) !=-1 )

return ( sAgent.match( / applewebkit/(\d+)/ )[1] >=522 ) ; // Build must be at least 522 (v3)

return false ;

}

© 版权声明

相关文章