TinyMCE删除有效标签



我在小型网站上使用tinyMCE,人们用它来写简单的文章。通常他们用word写,然后把文字复制到tinyMCE,然后提交这个。

这就是为什么我只允许几个标签:

valid_elements: "a[href|target],strong/b,em/i,div[align],br,p[style|align],ul,li,ol,table,tr,td,iframe[*],img[*]",

但尽管允许img[*]在插入图像后通过'Insert/edit image':

<img alt=""/>

出现在代码中。iframe也是如此(完全删除)我已经尝试了valid_elements与img和iframe属性的完整列表以及extended_valid_elements的每种组合。

当我删除valid_elements子句时,一切都很好,但是不允许的单词格式(h1, h2等)会弄乱样式。

我使用的是paste_preprocess设置和tinymce粘贴插件,我过滤掉不需要的标签。下面是一个例子:

在你的时间init:

paste_preprocess : function(pl, o) {
    //if(console) console.log('Object', o);
    //if(console) console.log('Content:', o.content);
        // usage param1 = the string to strip out tags from, param2 = tags to keep in the string
    o.content = ir.im.strip_tags( o.content,'<p><div><br><br/>' );
},

去除标签的帮助函数:

strip_tags = function (str, allowed_tags) {
    var key = '', allowed = false;
    var matches = [];    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = ''; 
    var replacer = function (search, replace, str) {
        return str.split(search).join(replace);
    };
     // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z0-9]+)/gi);
    }
     str += '';
    // Match tags
    matches = str.match(/(</?[S][^>]*>)/gi);
     // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;        }
        // Save HTML tag
        html = matches[key].toString();
         // Is tag not in allowed list? Remove from str!
        allowed = false;
        // Go through all allowed tags
        for (k in allowed_array) {            // Init
            allowed_tag = allowed_array[k];
            i = -1;
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}           
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
            // Determine
            if (i == 0) {                allowed = true;
                break;
            }
        }
         if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
     return str;
};

最新更新