如果文本格式不正确,请在ckeditor中格式化html

时间:2016-01-27 04:54:55

标签: javascript ckeditor4.x freetextbox

我有一个使用freetextbox WYSIWYG编辑器的旧ASP.NET应用程序。但它将一个奇怪的html(不是特定的html格式)保存到数据库中。

<TABLE class=mceVisualAid border=0 width=560 align=center height=395>
<TBODY>
<TR align=center>
<TD class=mceVisualAid><SPAN>
<H1 style=COLOR: rgb(0,0,0)    align=center><SPAN><SPAN><SPAN><STRONG><FONT size=3><STRONG><FONT size=3><STRONG><FONT size=2><STRONG><FONT size=3> Message</FONT></STRONG></FONT></STRONG></FONT></STRONG></FONT></STRONG></SPAN></SPAN></SPAN></H1>
<H1 style=COLOR: rgb(0,0,0) align=center><SPAN><SPAN><SPAN><STRONG><FONT size=3><STRONG><FONT size=3><STRONG><FONT size=2><STRONG><FONT size=3>16 August 2013</FONT>

现在我使用ckeditor WYSIWYG作为ASP.net MVC应用程序,它使用数据库中保存的相同数据,但我没有得到将html渲染到编辑器的完美方法。我的ckeditor的config.js是:

CKEDITOR.editorConfig = function( config ) {
// Define changes to default configuration here. For example:
// config.language = 'fr';
// config.uiColor = '#AADC6E';
config.entities = false;
config.basicEntities = false;
config.entities_greek = false;
config.entities_latin = false;

};

渲染时显示如下: enter image description here

2 个答案:

答案 0 :(得分:1)

尝试在视图中使用它:

@Html.Raw(HttpUtility.HtmlDecode(Model.MyContent)).ToHtmlString();

只需验证CKEditor中的输入检查XSS och非法标记。

执行此操作的一种方法是使用外部防XSS库,在保存到数据库之前,您应该通过清理程序运行它。重要的是在服务器端执行此操作。

以下是关于反XSS库的建议(不知道自从我很久以前使用它以来是否有更好的东西)

https://msdn.microsoft.com/en-us/security/aa973814.aspx

答案 1 :(得分:0)

这些都是愚蠢的。您需要将这些符号转换为真实字符。

在JS中执行此操作的一种流行方式是:

var htmlEntities = $('#MyId').ckeditor();   //Or whatever the way you read data 
var pureHtml = $('<textarea />').html(htmlEntities).text();  //Convert

或以下更清洁的方式:

function decodeHTMLEntities (str) {
    if(str && typeof str === 'string') {
      // strip script/html tags
      str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '');
      str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '');
      element.innerHTML = str;
      str = element.textContent;
      element.textContent = '';
    }

    return str;
  }