繁体   English   中英

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

[英]format html in ckeditor if text is in not correct format

我有一个旧的ASP.NET应用程序,它使用freetextbox WYSIWYG编辑器。 但它将一个奇怪的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;

};

渲染时显示如下: 在此输入图像描述

尝试在视图中使用它:

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

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM