TinyMCE在提交之前替换Html标签

时间:2013-07-10 13:16:09

标签: asp.net tinymce

我正在尝试替换html标签'<' '>'与'& lt;'和'& gt;'在我的文字来自TinyMCE之前做了页面回发。

使用TinyMCE v.3.x我可以这样做:

function saveCallback(element_id, html, body) {
    html = html.replace(/</gi, "&lt;");
    html = html.replace(/>/gi, "&gt;");
    return html;
}

function loadCallback(type, value) {
    if (type == "insert_to_editor") {
        value = value.replace(/&lt;/gi, "<");
        value = value.replace(/&gt;/gi, ">");
    }
    return value;
}

tinyMCE.init({
 ...
 save_callback: "saveCallback",
 cleanup_callback: "loadCallback" 
});

使用新的TinyMCE v.4.x我试试这种方式:

$(function () {
    tinymce.init({
        language: "it",
        width: 500,
        height: 400,
        formats: false,
        menubar: false,    
        mode: "exact",
        elements: "Testo",
        setup: function (editor) {
            editor.on('SaveContent', function (e) {                            
                html = editor.getContent();
                html = html.replace(/</gi, "&lt;");
                html = html.replace(/>/gi, "&gt;");
                editor.getElement().value = html;
            });
        }
    });
});

以这种方式:

$(function () {
    tinymce.init({
        language: "it",
        width: 500,
        height: 400,
        formats: false,
        menubar: false,    
        mode: "exact",
        elements: "Testo",
        setup: function (editor) {
            editor.on('submit', function (e) {                            
                html = editor.getContent();
                html = html.replace(/</gi, "&lt;");
                html = html.replace(/>/gi, "&gt;");
                editor.getElement().value = html;
            });
        }
    });
});

但是回发值总是包含html标记,页面返回消息“检测到潜在危险的Request.Form值”

3 个答案:

答案 0 :(得分:2)

init

中尝试以下选项
encoding : 'xml',
setup: function (editor) {editor.on('SaveContent', function (e) {e.content = e.content.replace(/&#39/g, '&apos');});}

仅供参考:改编自上面的代码并参考:

http://blog.tentaclesoftware.com/archive/2012/05/21/asp-net-4-0-tinymce-and-ldquoa-potentially-dangerous-request.aspx

对我有用:),希望它有所帮助。

答案 1 :(得分:0)

我已经用这种方式解决了:

tinymce.init({
    selector: "#MySelector",
    width: ...,
    height: ...,
    encoding: 'xml',                        
    setup: function (editor) { editor.on('SaveContent', function (e) { e.content = e.content.replace(/&#39/g, '&apos').replace(/&amp;/g, '&'); }); }
});

答案 2 :(得分:0)

对于tinyMCE 4,请使用PostProcess事件尝试以下干净的解决方案:

    tinymce.init({
    language: "it",
    width: 500,
    height: 400,
    formats: false,
    menubar: false,    
    mode: "exact",
    elements: "Testo",                
    init_instance_callback: function (editor) {
            editor.on('PostProcess', function (e) {
                e.content = e.content.replace(/</g, '&lt');
                e.content = e.content.replace(/>/g, '&gt');
            });
        },
});
相关问题