2013-07-10 34 views
2

我试图用' lt'替换html标记'<'>''和'& gt;'在我的TinyMCE文本中做页面回发之前。新TinyMCE的v.4.x我已经尝试用这种方式TinyMCE在提交之前替换Html标记

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

$(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; 
      }); 
     } 
    }); 
}); 

随着TinyMCE的v.3.xi可以这样做这样:

$(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.Fo检测RM值”

回答

2

请尝试以下方法在初始化

encoding : 'xml', 
setup: function (editor) {editor.on('SaveContent', function (e) {e.content = e.content.replace(/&#39/g, '&apos');});} 

FYI:改编自你上面的代码和REF:

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

作品对我来说:),希望能帮助到你。

+0

我已经修改了这种方式,而现在它的确定 编码: 'XML', 设置:功能(编辑){editor.on(” SaveContent',function(e){e.content = e.content.replace(/&#39/g,''')。替换(/ &/g,'&');});} – Alex

+0

谢谢,这非常有帮助。:D – Michael

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, '&'); }); } 
});