2

how can I accept just certain tags and strip_tags all others when paste text to editor? just like php's strip_tags function?

and how can I remove all styles from that tags?

input is a string.

4

1 回答 1

0

我有这个功能

var strip_tags = function(str,tags,attrs){
    var reg2 = /\s*(\w+)=\"[^\"]+\"/gm;
    var reg = /<\s*(\w+).*?>/gm;
    str = str.replace(reg,function(match, i) {
        var r_ = match;
        var reg_ = /<\s*(\w+).*?>/gm;
        var m_ = reg_.exec(match);
        if(m_!=null){
            if(tags.indexOf(m_[1])>=0){
                r_ = match.replace(reg2,function(match_, i) {
                    var reg2_ = /\s*(\w+)=\"[^\"]+\"/gm;
                    var m = reg2_.exec(match_);
                    if(m!=null){
                        if(attrs.indexOf(m[1])>=0){
                            return match_;
                        }
                    }
                    return '';
                });
            }else{
                r_ = '';
            }
        }else{
            r_ = '';
        }
        return r_;
    });
    var reg3 = /<\/\s*(\w+).*?>/gm;
    str = str.replace(reg3,function(match, i) {
        var r_ = match;
        var reg_ = /<\/\s*(\w+).*?>/gm;
        var m_ = reg_.exec(match);
        if(m_!=null){
            if(tags.indexOf(m_[1])>=0){
                return match;
            }
        }
        return '';
    });
    return str;
};
tinyMCE.init(
    ...
    plugins: "paste...
    paste_preprocess : function(pl, o) {
        var allowed_tags = ['ul','li','b','p','table','tr','td'];
        var allowed_attributes = ['href','colspan','rowspan'];
        o.content = strip_tags(o.content,allowed_tags,allowed_attributes);
    },
    ...
);
于 2015-09-15T01:41:39.927 回答