从word粘贴时冗余HTML代码过滤增强版

后台日志编辑默认的编辑器貌似不支持从word粘贴功能,无奈换上了最新的kindeditor3.5.1,过滤功能很强大。赞!不过还是剩下一些没用的<span></span>。吃流量,不环保。自己动手增强了一把,做法如下,感兴趣的也可以照此自己改改这一块(改之前务必备份原来的文件!!!):

1、kindeditor.js(3.5.1版)的3236行,“//add start”和“//add end”之间是我加的代码。

KE.plugin['wordpaste'] = {
click : function(id) {
KE.util.selection(id);
this.dialog = new KE.dialog({
id : id,
cmd : 'wordpaste',
file : 'wordpaste.html',
width : 450,
height : 300,
loadingMode : true,
title : KE.lang['wordpaste'],
yesButton : KE.lang['yes'],
noButton : KE.lang['no']
});
this.dialog.show();
},
exec : function(id) {
var dialogDoc = KE.util.getIframeDoc(this.dialog.iframe);
var wordIframe = KE.$('wordIframe', dialogDoc);
var str = KE.util.getIframeDoc(wordIframe).body.innerHTML;
str = str.replace(/<meta(\n|.)*?>/ig, "");
str = str.replace(/<!(\n|.)*?>/ig, "");
str = str.replace(/<style[^>]*>(\n|.)*?<\/style>/ig, "");
str = str.replace(/<script[^>]*>(\n|.)*?<\/script>/ig, "");
str = str.replace(/<w:[^>]+>(\n|.)*?<\/w:[^>]+>/ig, "");
str = str.replace(/<xml>(\n|.)*?<\/xml>/ig, "");
str = str.replace(/\r\n|\n|\r/ig, "");
//add start
str = str.replace(/font-family:[^\;\"\']+;/ig,"");
str = str.replace(/style=\"\s*\"/ig,"");
str = str.replace(/<span\s+>/ig,"<span>");
//add end
str = KE.util.execGetHtmlHooks(id, str);
str = KE.format.getHtml(str, KE.g[id].htmlTags, KE.g[id].urlType);
//add start
str = str.replace(/font-size:small;/ig,"");
str = str.replace(/<p\s+style=\"[^\"]*\"\s*>/ig,"<p class='prg'>");
str = str.replace(/style=\"font-size:12pt;\"/ig,"class='s5'");
str = str.replace(/style=\"font-size:14pt;\"/ig,"class='ss4'");
str = str.replace(/style=\"font-size:15pt;\"/ig,"class='s4'");
str = str.replace(/style=\"font-size:16pt;\"/ig,"class='ss3'");
str = str.replace(/style=\"font-size:18pt;\"/ig,"class='s3'");
str = str.replace(/style=\"\s*\"/ig,"");
str = str.replace(/<span\s+>/ig,"<span>");
var pat = new RegExp("<\s*span|</span>","ig");//remove needless <span></span> tags
var rst,tmp='',idx=0,cnt=0;
while ((rst = pat.exec(str)) != null){
if(rst.index>0){
tmp += str.substring(idx,rst.index);
}
if (rst[0]=='</span>'){
if(cnt>0){
--cnt;
idx=rst.index;
}else{
idx=rst.lastIndex;
}
}else{
if(str.charAt(rst.lastIndex)=='>'){
idx=rst.lastIndex+1;
}else{
idx=rst.index;
++cnt;
}
}
}
str=tmp;
//add end
KE.util.insertHtml(id, str);
this.dialog.hide();
KE.util.focus(id);
}
};


2、在kindeditor\skins\default.css的最后一行加入:
 .prg{font-size:1em;text-indent:2em;}.s5{font-size:1em;}.ss4{font-size:1.2em;}.s4{font-size:1.25em;}.ss3{font-size:1.4em;}.s3{font-size:1.5em;}

3、content\templates\你的模板目录\main.css里同样加入:
 .prg{font-size:1em;text-indent:2em;}.s5{font-size:1em;}.ss4{font-size:1.2em;}.s4{font-size:1.25em;}.ss3{font-size:1.4em;}.s3{font-size:1.5em;}

4、使用google closure压缩kindeditor.js

java -jar compiler.jar --js admin\kindeditor\kindeditor.js --js_output_file admin\kindeditor\kindeditor-min.js
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值