如果文本格式不正确,则在 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>
现在我在 ASP.net MVC 应用程序中使用 ckeditor WYSIWYG,它使用保存在数据库中的相同数据,但我没有找到将 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;
};
渲染时显示如下:
这些是 htmlentities。您需要将这些符号转换为真实字符。
在 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;
}
尝试在视图中使用它:
@Html.Raw(HttpUtility.HtmlDecode(Model.MyContent)).ToHtmlString();
只需验证 CKEditor 中的输入即可检查 XSS och 非法标签。
一种方法是使用外部 anti-XSS 库,在保存到数据库之前,您应该 运行 通过消毒剂。重要的是在 Server-side.
上进行
下面只是对 anti-XSS 库的一个建议(不知道有没有更好的,因为我很久以前就用过这个)
我有一个旧的 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>
现在我在 ASP.net MVC 应用程序中使用 ckeditor WYSIWYG,它使用保存在数据库中的相同数据,但我没有找到将 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;
};
渲染时显示如下:
这些是 htmlentities。您需要将这些符号转换为真实字符。
在 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;
}
尝试在视图中使用它:
@Html.Raw(HttpUtility.HtmlDecode(Model.MyContent)).ToHtmlString();
只需验证 CKEditor 中的输入即可检查 XSS och 非法标签。
一种方法是使用外部 anti-XSS 库,在保存到数据库之前,您应该 运行 通过消毒剂。重要的是在 Server-side.
上进行下面只是对 anti-XSS 库的一个建议(不知道有没有更好的,因为我很久以前就用过这个)