本帖最后由 hoiuan 于 2010-4-2 00:01 编辑
用以下函数替代edit.js里的原有函数即可!
由于不懂正则,琢磨了4小时左右才在fckediter的基础上完成
清除了所有冗余代码,保全了90%以上的格式!
希望高人能做个自动检测粘贴进编辑器里的内容是否为word格式的功能出来,这样自动调用此函数进行清理就完美了!- function CleanCode() {
- var oRTE = getFrameNode(sRTE);
- oRTE.focus();
- if (! window.Event){
- var body = oRTE.document.body;
- var html = oRTE.document.body.innerHTML;
- html = html.replace(/<o:p>\s*<\/o:p>/g, '') ;
- html = html.replace(/<o:p>[\s\S]*?<\/o:p>/g, ' ') ;
- html = html.replace( /\s*mso-[^:]+:[^;"]+;?/gi, '' ) ;
- html = html.replace( /\s*MARGIN: 0(?:cm|in) 0(?:cm|in) 0pt\s*;/gi, '' ) ;
- html = html.replace( /\s*MARGIN: 0(?:cm|in) 0(?:cm|in) 0pt\s*"/gi, """ ) ;
- html = html.replace( /\s*TEXT-INDENT: 0(?:cm|in)\s*;/gi, '' ) ;
- html = html.replace( /\s*TEXT-INDENT: 0(?:cm|in)\s*"/gi, """ ) ;
- html = html.replace( /\s*TEXT-ALIGN: [^\s;]+;?"/gi, """ ) ;
- html = html.replace( /\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/gi, """ ) ;
- html = html.replace( /\s*FONT-VARIANT: [^\s;]+;?"/gi, """ ) ;
- html = html.replace( /\s*tab-stops:[^;"]*;?/gi, '' ) ;
- html = html.replace( /\s*tab-stops:[^"]*/gi, '' ) ;
- html = html.replace( /\s*face="[^"]*"/gi, '' ) ;
- html = html.replace( /\s*face=[^ >]*/gi, '' ) ;
- html = html.replace( /\s*FONT-FAMILY:[^;"]*;?/gi, '' ) ;
- html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
- html = html.replace( /<(\w[^>]*)LINE-HEIGHT:\s+\d+%;([^>]*)/gi, "<$1$2" ) ;
- html = html.replace( /<(\w[^>]*)VERTICAL-ALIGN:(\s+[A-Za-z]+)([^>]*)/gi, "<$1$3" ) ;
- html = html.replace( /<STYLE[^>]*>[\s\S]*?<\/STYLE[^>]*>/gi, '' ) ;
- html = html.replace( /<(?:META|LINK)[^>]*>\s*/gi, '' ) ;
- html = html.replace( /\s*style="\s*"/gi, '' ) ;
- html = html.replace( /<SPAN\s*[^>]*>\s* \s*<\/SPAN>/gi, ' ' ) ;
- html = html.replace( /<SPAN\s*[^>]*><\/SPAN>/gi, '' ) ;
- html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
- html = html.replace( /<SPAN\s*>([\s\S]*?)<\/SPAN>/gi, '$1' ) ;
- html = html.replace( /<FONT\s*>([\s\S]*?)<\/FONT>/gi, '$1' ) ;
- html = html.replace(/<\\?\?xml[^>]*>/gi, '' ) ;
- html = html.replace( /<w:[^>]*>[\s\S]*?<\/w:[^>]*>/gi, '' ) ;
- html = html.replace(/<\/?\w+:[^>]*>/gi, '' ) ;
- html = html.replace(/<\!--[\s\S]*?-->/g, '' ) ;
- html = html.replace( /<(U|I|STRIKE)> <\/\1>/g, ' ' ) ;
- html = html.replace( /<H\d>\s*<\/H\d>/gi, '' ) ;
- html = html.replace( /<(\w+)[^>]*\sstyle="[^"]*DISPLAY\s?:\s?none[\s\S]*?<\/\1>/ig, '' ) ;
- html = html.replace( /<(\w[^>]*) language=([^ |>]*)([^>]*)/gi, "<$1$3") ;
- html = html.replace( /<(\w[^>]*) onmouseover="([^"]*)"([^>]*)/gi, "<$1$3") ;
- html = html.replace( /<(\w[^>]*) onmouseout="([^"]*)"([^>]*)/gi, "<$1$3") ;
- html = html.replace( /<H1([^>]*)>/gi, '<div$1><b><font size="6">' ) ;
- html = html.replace( /<H2([^>]*)>/gi, '<div$1><b><font size="5">' ) ;
- html = html.replace( /<H3([^>]*)>/gi, '<div$1><b><font size="4">' ) ;
- html = html.replace( /<H4([^>]*)>/gi, '<div$1><b><font size="3">' ) ;
- html = html.replace( /<H5([^>]*)>/gi, '<div$1><b><font size="2">' ) ;
- html = html.replace( /<H6([^>]*)>/gi, '<div$1><b><font size="1">' ) ;
- html = html.replace( /<\/H\d>/gi, '<\/font><\/b><\/div>' ) ;
- var re = new RegExp( '(<P)([^>]*>[\\s\\S]*?)(<\/P>)', 'gi' ) ;
- html = html.replace( re, '<div$2<\/div>' ) ;
- html = html.replace( /<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '' ) ;
- html = html.replace( /<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '' ) ;
- html = html.replace( /<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '' ) ;
- html=html.replace(/<(table)>/gi,"<$1 border=1>");
- oRTE.document.body.innerHTML = html;
-
- }else{
- var oMozText = oRTE.document.body.ownerDocument.createRange();
- oMozText.selectNodeContents(oRTE.document.body);
- oRTE.document.body.innerHTML = oMozText.toString();
- }
- }
复制代码 |