cmsware让默认的新闻编辑器增加清除多余的html功能
今天看到http://blog.98xx.com/zleaf/?p=68 的博客,他这里为默认编辑器增加了一个自动清除多余html的功能,但并不适用了新闻编辑器,所以我改了一下,参考了http://www.lonmo.com/bbs/read.php?tid=10605的函数. 修改cmsware/skin/common/editor/editor_source.js 增加下面的函数:
function doCleanCode(code) { /* // removes all Class attributes on a tag eg. '<p>xxx</p>' returns '<p>xxx</p>' code = code.replace(/<([\w]+) class=([^ |>]*)([^>]*)/gi, "<$1$3") // removes all style attributes eg. '<tag style="asd asdfa aasdfasdf" something else>' returns '<tag something else>' code = code.replace(/<([\w]+) style="([^"]*)"([^>]*)/gi, "<$1$3") // gets rid of all xml stuff... <xml>,<\xml>,<?xml> or <\?xml> code = code.replace(/<\\?\??xml[^>]>/gi, "") // get rid of ugly colon tags <a:b> or </a:b> code = code.replace(/<\/?\w+:[^>]*>/gi, "") // removes all empty <p> tags code = code.replace(/<p([^>])*>( )*\s*<\/p>/gi,"") // removes all empty span tags code = code.replace(/<span([^>])*>( )*\s*<\/span>/gi,"") */ // removes all javascript code code = code.replace(/onabort=\"[^"]*\"/gi,"") code = code.replace(/onactivate=\"[^"]*\"/gi,"") code = code.replace(/onafterprint=\"[^"]*\"/gi,"") code = code.replace(/onafterupdate=\"[^"]*\"/gi,"") code = code.replace(/onbeforeactivate=\"[^"]*\"/gi,"") code = code.replace(/onbeforecopy=\"[^"]*\"/gi,"") code = code.replace(/onbeforecut=\"[^"]*\"/gi,"") code = code.replace(/onbeforedeactivate=\"[^"]*\"/gi,"") code = code.replace(/onbeforeeditfocus=\"[^"]*\"/gi,"") code = code.replace(/onbeforepaste=\"[^"]*\"/gi,"") code = code.replace(/onbeforeprint=\"[^"]*\"/gi,"") code = code.replace(/onbeforeunload=\"[^"]*\"/gi,"") code = code.replace(/onbeforeupdate=\"[^"]*\"/gi,"") code = code.replace(/onblur=\"[^"]*\"/gi,"") code = code.replace(/onbounce=\"[^"]*\"/gi,"") code = code.replace(/oncellchange=\"[^"]*\"/gi,"") code = code.replace(/onchange=\"[^"]*\"/gi,"") code = code.replace(/onclick=\"[^"]*\"/gi,"") code = code.replace(/oncontextmenu=\"[^"]*\"/gi,"") code = code.replace(/oncontrolselect=\"[^"]*\"/gi,"") code = code.replace(/oncopy=\"[^"]*\"/gi,"") code = code.replace(/oncut=\"[^"]*\"/gi,"") code = code.replace(/ondataavailable=\"[^"]*\"/gi,"") code = code.replace(/ondatasetchanged=\"[^"]*\"/gi,"") code = code.replace(/ondatasetcomplete=\"[^"]*\"/gi,"") code = code.replace(/ondblclick=\"[^"]*\"/gi,"") code = code.replace(/ondeactivate=\"[^"]*\"/gi,"") code = code.replace(/ondrag=\"[^"]*\"/gi,"") code = code.replace(/ondragend=\"[^"]*\"/gi,"") code = code.replace(/ondragenter=\"[^"]*\"/gi,"") code = code.replace(/ondragleave=\"[^"]*\"/gi,"") code = code.replace(/ondragover=\"[^"]*\"/gi,"") code = code.replace(/ondragstart=\"[^"]*\"/gi,"") code = code.replace(/ondrop=\"[^"]*\"/gi,"") code = code.replace(/onerror=\"[^"]*\"/gi,"") code = code.replace(/onerrorupdate=\"[^"]*\"/gi,"") code = code.replace(/onfilterchange=\"[^"]*\"/gi,"") code = code.replace(/onfinish=\"[^"]*\"/gi,"") code = code.replace(/onfocus=\"[^"]*\"/gi,"") code = code.replace(/onfocusin=\"[^"]*\"/gi,"") code = code.replace(/onfocusout=\"[^"]*\"/gi,"") code = code.replace(/onhelp=\"[^"]*\"/gi,"") code = code.replace(/onkeydown=\"[^"]*\"/gi,"") code = code.replace(/onkeypress=\"[^"]*\"/gi,"") code = code.replace(/onkeyup=\"[^"]*\"/gi,"") code = code.replace(/onlayoutcomplete=\"[^"]*\"/gi,"") code = code.replace(/onload=\"[^"]*\"/gi,"") code = code.replace(/onlosecapture=\"[^"]*\"/gi,"") code = code.replace(/onmousedown=\"[^"]*\"/gi,"") code = code.replace(/onmouseenter=\"[^"]*\"/gi,"") code = code.replace(/onmouseleave=\"[^"]*\"/gi,"") code = code.replace(/onmousemove=\"[^"]*\"/gi,"") code = code.replace(/onmouseout=\"[^"]*\"/gi,"") code = code.replace(/onmouseover=\"[^"]*\"/gi,"") code = code.replace(/onmouseup=\"[^"]*\"/gi,"") code = code.replace(/onmousewheel=\"[^"]*\"/gi,"") code = code.replace(/onmove=\"[^"]*\"/gi,"") code = code.replace(/onmoveend=\"[^"]*\"/gi,"") code = code.replace(/onmovestart=\"[^"]*\"/gi,"") code = code.replace(/onpaste=\"[^"]*\"/gi,"") code = code.replace(/onpropertychange=\"[^"]*\"/gi,"") code = code.replace(/onreadystatechange=\"[^"]*\"/gi,"") code = code.replace(/onreset=\"[^"]*\"/gi,"") code = code.replace(/onresize=\"[^"]*\"/gi,"") code = code.replace(/onresizeend=\"[^"]*\"/gi,"") code = code.replace(/onresizestart=\"[^"]*\"/gi,"") code = code.replace(/onrowenter=\"[^"]*\"/gi,"") code = code.replace(/onrowexit=\"[^"]*\"/gi,"") code = code.replace(/onrowsdelete=\"[^"]*\"/gi,"") code = code.replace(/onrowsinserted=\"[^"]*\"/gi,"") code = code.replace(/onscroll=\"[^"]*\"/gi,"") code = code.replace(/onselect=\"[^"]*\"/gi,"") code = code.replace(/onselectionchange=\"[^"]*\"/gi,"") code = code.replace(/onselectstart=\"[^"]*\"/gi,"") code = code.replace(/onstart=\"[^"]*\"/gi,"") code = code.replace(/onstop=\"[^"]*\"/gi,"") code = code.replace(/onsubmit=\"[^"]*\"/gi,"") //全面修改支持大小写全处理 // removes all Class attributes on a tag eg. '<p>xxx</p>' returns '<p>xxx</p>' code = code.replace(/<([\w]+) class=([^ |>]*)([^>]*)/gi, "<$1$3") // removes all style attributes eg. '<tag style="asd asdfa aasdfasdf" something else>' returns '<tag something else>' code = code.replace(/<([\w]+) style="([^"]*)"([^>]*)/gi, "<$1$3") // gets rid of all xml stuff... <xml>,<\xml>,<?xml> or <\?xml> code = code.replace(/<\\?\??xml[^>]>/gi, "") // get rid of ugly colon tags <a:b> or </a:b> code = code.replace(/<\/?\w+:[^>]*>/gi, "") // removes all empty <p> tags code = code.replace(/<[Pp]([^>])*>( )*\s*<\/[Pp]>/gi,"") // removes all <span> </span> tags code = code.replace(/<[Ss][Pp][Aa][Nn]([^>])*>( )*\s*<\/[Ss][Pp][Aa][Nn]>/gi,"") //删除 <span ......> </span> code = code.replace(/<[Ss][Pp][Aa][Nn]([^>])*>/gi,"") //删除 <span ......> code = code.replace(/<\/[Ss][Pp][Aa][Nn]([^>])*>/gi,"") //删除 </span> // removes <b> </b> code = code.replace(/<[Bb]>/gi,"") //删除 <b> 粗体字效果 code = code.replace(/<\/[Bb]>/gi,"") //删除 </b> // 处理<p> 段首空两格 先删除<p ...>中间的废代码,然后清除空格,再加空格 code = code.replace(/<[Pp]([^>])*>/gi,"<P>") //替换 <p .... >为<p> code = code.replace(/<[Pp]> ( ){0,}( ){0,}( ){0,}/gi,"<P>") //删除段首空格,解决空格多的问题 code = code.replace(/<[Pp]>( ){1,}/gi,"<P>") //同上 code = code.replace(/<[P]>/gi,"<P> ") //加空格 // 处理<br> 段首空两格,有时可能出现的情况 code = code.replace(/<[Bb][Rr]> ( ){0,}( ){0,}( ){0,}/gi,"<BR>") //删除段首空格,解决空格多的问题 code = code.replace(/<[Bb][Rr]>( ){1,}/gi,"<BR>") //同上 code = code.replace(/<BR>/gi,"<BR> ") //加空格 return code }修改:Word_Clean函数: 增加: html = doCleanCode(html);//这里为了做这个清空动作 修改:onPaste函数:
// 粘贴时自动检测是否来源于Word格式 function onPaste() { if (bMode == true){ var sHTML = GetClipboardHTML() ; if (BrowserInfo.IsIE55OrMore) { var re = /<\w[^>]*?/gi ; var re2 = /onclick/gi ; if ( re.test(sHTML)||re2.test(sHTML)){ if ( confirm( "你要粘贴的内容好象是从网页或者Word中拷出来的,是否要先清除多余的格式再粘贴?" ) ){ cleanAndPaste( sHTML ) ; return false ; } } } return true; }else{ idEdit.document.selection.createRange().pasteHTML(HTMLEncode( clipboardData.getData("Text"))) ; return false; } }这样,复制别外的新闻就不会出现多余html代码了,方便排版.
评论