2013-07-10 37 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, '&'); }); } 
});