将内容复制并粘贴到 TinyMCE 输入结果中,结果为臃肿的 HTML

发布于 2024-10-27 01:38:58 字数 261 浏览 2 评论 0原文

我将 TinyMCE 用于各种项目。我的 ATM 遇到的问题是,许多用户将 Word 或 OpenOffice 等来源的内容复制并粘贴到 TinyMCE 输入中。这通常会导致代码臃肿(例如, 等内容被 OpenOffice 接管)。 TinyMCE 的清理似乎并没有删除这些标签。有没有办法在将文本粘贴到 TinyMCE 输入区域之前去除所有格式?或者是否有另一种方法来防止这种臃肿的代码,例如通过使用 PHP 在服务器端过滤它?

I use TinyMCE for various projects. The problem I'm having ATM is that a lot of users copy&paste content form sources like Word or OpenOffice into the TinyMCE input. This results often in bloated code (e.g. things like <span lang="EN-GB"> being taken over from OpenOffice). TinyMCE's cleanup doesn't seem to remove those tags. Is there a way to strip all formating befor the text is pasted into the TinyMCE input area? Or is there another way to prevent such bloated code, for example by filtring it server side with PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国粹 2024-11-03 01:38:58

我知道这是一个 opd 问题,但为了其他可能正在寻找此问题答案的人(就像我一样!)的利益,TinyMCE 现在包含控制粘贴到文本框中的内容的功能。

在 init 调用中,添加“paste”插件,然后设置您需要的任何选项,例如

tinyMCE.init({
    ...
    plugins: "paste",
    paste_auto_cleanup_on_paste : true,
    paste_remove_styles: true,
    paste_remove_styles_if_webkit: true,
    paste_strip_class_attributes: "all",
    paste_remove_spans : true,
    ...
});

您可以在 tinyMCE 维基

I know this is an opd question but for the benefit of others who may be searching for an answer to this (like I was!), TinyMCE now includes the ability to control what is pasted into the textbox.

In the init call, add the "paste" plugin and then set whatever options you require, e.g.

tinyMCE.init({
    ...
    plugins: "paste",
    paste_auto_cleanup_on_paste : true,
    paste_remove_styles: true,
    paste_remove_styles_if_webkit: true,
    paste_strip_class_attributes: "all",
    paste_remove_spans : true,
    ...
});

You can see all options in the tinyMCE wiki

日记撕了你也走了 2024-11-03 01:38:58

我使用以下c函数on_preprocess删除所有标签:

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;
};

在tinymce init中我放置

paste_preprocess : function(pl, o) {

// remove Clipboard header on MAC
var pos_sel = o.content.search("EndSelection:");
var pos_fra = o.content.search("EndFragment:");
var mac_header_found = false;

if (o.content.search("Version:") == 0 && pos_sel < 135 && pos_sel > 120){
    o.content = o.content.substring(pos_sel+23);
    mac_header_found = true;
}
else if (o.content.search("Version:") == 0 && pos_fra < 80 && pos_fra > 75){
    o.content = o.content.substring(pos_fra+23);
    mac_header_found = true;
}

// Copy from Word oder OpenOffice (MAC) - remove header
if (o.wordContent || mac_header_found) {
    // first style tag + content to be removed
    var pos_start_style = o.content.search('<style');
    var pos_end_style = o.content.search('</style>');
    if (pos_start_style > 0 && pos_end_style > pos_start_style) {
        o.content = o.content.substring(0, pos_start_style).concat(o.content.substring(pos_end_style + 8));
    }
    // complete Worddokument gets pasted
    else {
        var pos_start_p = o.content.search('<p');
        if (pos_start_p) o.content = o.content.substring(pos_start_p);
    }
}

    o.content = ir.im.strip_tags( o.content, '' );

// NO-Break Zero-width space if empty
if (o.content == '') {
    o.content = '';
}   
},

I remove all tags using the followingc function on_preprocess:

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;
};

In the tinymce init i place

paste_preprocess : function(pl, o) {

// remove Clipboard header on MAC
var pos_sel = o.content.search("EndSelection:");
var pos_fra = o.content.search("EndFragment:");
var mac_header_found = false;

if (o.content.search("Version:") == 0 && pos_sel < 135 && pos_sel > 120){
    o.content = o.content.substring(pos_sel+23);
    mac_header_found = true;
}
else if (o.content.search("Version:") == 0 && pos_fra < 80 && pos_fra > 75){
    o.content = o.content.substring(pos_fra+23);
    mac_header_found = true;
}

// Copy from Word oder OpenOffice (MAC) - remove header
if (o.wordContent || mac_header_found) {
    // first style tag + content to be removed
    var pos_start_style = o.content.search('<style');
    var pos_end_style = o.content.search('</style>');
    if (pos_start_style > 0 && pos_end_style > pos_start_style) {
        o.content = o.content.substring(0, pos_start_style).concat(o.content.substring(pos_end_style + 8));
    }
    // complete Worddokument gets pasted
    else {
        var pos_start_p = o.content.search('<p');
        if (pos_start_p) o.content = o.content.substring(pos_start_p);
    }
}

    o.content = ir.im.strip_tags( o.content, '' );

// NO-Break Zero-width space if empty
if (o.content == '') {
    o.content = '';
}   
},
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文