format html in ckeditor if text is in not correct format

1.5k Views Asked by At

I have an old ASP.NET application that uses freetextbox WYSIWYG editor. But it saves a weird html (not particular format of html)into database.

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

Now I'm using ckeditor WYSIWYG in as ASP.net MVC application which uses same data that is saved in databse but i'm not getting a perfect way to render that html into editor. My config.js of ckeditor is :

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;

};

While rendering it shows like: enter image description here

2

There are 2 best solutions below

0
On BEST ANSWER

Try using this in the View:

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

Just verify the input in the CKEditor checks for XSS och illegal tags.

One way to do this is using a external anti-XSS library and before save to the database you should run it trough the sanitizer. The important thing is to do it on the Server-side.

Below is just a suggestion on a anti-XSS library (don't know if there is something better since i used this a long time ago)

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

0
On

These are htmlentities. You need to convert these symbols to real characters.

One popular way to do this in JS is:

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

Or the following cleaner way:

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