ckeditor 敏感词标记显示处理方法

直接在原型添加方法:

(function () {
    /*
    *      取消所有高亮
    */
    CKEDITOR.editor.prototype.CancleSensitiveWordsHighlight = function () {
        var regstrEpswh = '<span class="ep_ckeditor_sensitivewords" style="background-color:[^<>:]+[;]*">([^<>]+)<\\/span>';
        var htmlEpswh = this.getData();
        htmlEpswh = htmlEpswh.replace(eval("/" + regstrEpswh + "/ig"), "$1");
        if (this.document != null)
            this.document.getBody().setHtml(htmlEpswh);
        return htmlEpswh;
    }
    /*
    *    epswhlwords 敏感词
    *    epswhligChar 敏感词中忽略的特殊字符
    *    epswhlcolor 高亮底色
    */
    CKEDITOR.editor.prototype.SensitiveWordsHighlight = function (epswhlwords, epswhligChar, epswhlcolor) {
        //空的字符串
        if (typeof epswhlwords == "string" && !epswhlwords)
            return;
        //空数组
        if (typeof epswhlwords == "object" && epswhlwords[0] == undefined)
            return;
        var htmlEpswh = this.getData();
        //高亮模板
        var highLighCOde = '<span class="ep_ckeditor_sensitivewords" style="background-color:{$color};">{$word}</span>';
        if (!epswhlcolor)
            epswhlcolor = "#ffff00";
        highLighCOde = highLighCOde.replace("{$color}", epswhlcolor);
        //如果内容中有高亮内容先进行清理
        if (htmlEpswh.indexOf('ep_ckeditor_sensitivewords') > -1) {
            htmlEpswh = this.CancleSensitiveWordsHighlight();
        }
        //重新高亮
        var epswhlkeyWords = [];
        if (typeof epswhlwords == "string")
            epswhlkeyWords = epswhlwords.split(',');
        else
            epswhlkeyWords = epswhlwords;
        //需要忽略的分隔符
        if (epswhligChar && epswhligChar.length > 0) {
            epswhligChar = epswhligChar.replace(/[<>&"]/g, function (c) { return { '<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;' }[c]; });
            epswhligChar = "[" + epswhligChar + "]*";
        } else {
            epswhligChar = '';
        }
        for (var i = 0; i < epswhlkeyWords.length; i++) {
            var allkey = epswhlkeyWords[i].split('');
            var regstr = allkey.join(epswhligChar);
            regstr = "(" + regstr + ")";
            var reg = eval("/" + regstr + "/ig");
            var hcode = highLighCOde.replace("{$word}", "$1");
            htmlEpswh = htmlEpswh.replace(reg, hcode);
        }
        //document 对象在源码模式无效,this.setData是重新加载,不是同步方法,不能使用
        if (this.document!=null)
            this.document.getBody().setHtml(htmlEpswh);
    }
    
})();

或者添加插件:

CKEDITOR.plugins.add('sensitivewordshighlight', {
    init: function (editor) {
        /*
        *      取消所有高亮
        */
        editor.CancleSensitiveWordsHighlight=function () {
             var regstrEpswh = '<span class="ep_ckeditor_sensitivewords" style="background-color:[^<>:]+[;]*">([^<>]+)<\\/span>';
             var htmlEpswh = this.getData();
             htmlEpswh = htmlEpswh.replace(eval("/" + regstrEpswh + "/ig"), "$1");
             if (this.document != null)
                 this.document.getBody().setHtml(htmlEpswh);
             return htmlEpswh;
        }
        /*
        *    words 敏感词
        *    igChar 敏感词中忽略的特殊字符
        *    color 高亮底色
        */
        editor.SensitiveWordsHighlight = function (epswhlwords, epswhligChar, epswhlcolor) {
            //空的字符串
            if (typeof epswhlwords == "string" && !epswhlwords)
                return;
            //空数组
            if (typeof epswhlwords == "object" && epswhlwords[0] == undefined)
                return;
            var htmlEpswh = this.getData();
            //高亮模板
            var highLighCOde = '<span class="ep_ckeditor_sensitivewords" style="background-color:{$color};">{$word}</span>';
            if (!epswhlcolor)
                epswhlcolor = "#ffff00";
            highLighCOde = highLighCOde.replace("{$color}", epswhlcolor);
            //如果内容中有高亮内容先进行清理
            if (htmlEpswh.indexOf('ep_ckeditor_sensitivewords') > -1) {
                htmlEpswh = this.CancleSensitiveWordsHighlight();
            }
            //重新高亮
            var epswhlkeyWords = [];
            if (typeof epswhlwords == "string")
                epswhlkeyWords = epswhlwords.split(',');
            else
                epswhlkeyWords = epswhlwords;
            //需要忽略的分隔符
            if (epswhligChar && epswhligChar.length > 0) {
                epswhligChar = epswhligChar.replace(/[<>&"]/g, function (c) { return { '<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;' }[c]; });
                epswhligChar = "[" + epswhligChar + "]*";
            } else {
                epswhligChar = '';
            }
            for (var i = 0; i < epswhlkeyWords.length; i++) {
                var allkey = epswhlkeyWords[i].split('');
                var regstr = allkey.join(epswhligChar);
                regstr = "(" + regstr + ")";
                var reg = eval("/" + regstr + "/ig");
                var hcode = highLighCOde.replace("{$word}", "$1");
                htmlEpswh = htmlEpswh.replace(reg, hcode);
            }
            //document 对象在源码模式无效,this.setData是重新加载,不是同步方法,不能使用
            if (this.document != null)
                this.document.getBody().setHtml(htmlEpswh);
        }
    }
});

启用插件:

config.extraPlugins = "sensitivewordshighlight";

调用:

//设置
CKEDITOR.instances.MYCKDEMO.SensitiveWordsHighlight(["一二","哈哈"], "`~!@#$^&*()=|{}':;',\\[\\]\\.<>/?~!@#¥……&*()—|{}【】‘;:”“'。,、?  ","#FFFF00");
//取消
CKEDITOR.instances.MYCKDEMO.CancleSensitiveWordsHighlight();

 注意:ckeditor 中setData()方法要刷新iframe非同步方法,同时使用多次出现内容不按逻辑显示~,~

转载于:https://www.cnblogs.com/huhangfei/p/6023594.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值