CKEditor encode script tag code and show it as text - not commenting it

Viewed 36

I use CKEditor and I want to add a script tag snippet:

<script>alert();></script>

The problem is that ckeditor is commenting this code so it doesn't appear in the editor. What I want is let ckeditor consider this code as text not as code so it appear like normal text, also not execute it in the editor.

currently I encode this code snippet and save it as encoded, but when the editor reload the saved data, even when its characters was encoded, the editor transform it to HTML and commenting it as mentioned above.

Thank you.

1 Answers

finally got a solution but it's server side, also can be implemented using JavaScript. For me, when I want to reload the saved content, I regex all the scripts in the HTML string and then convert "<" and ">" to "&lt;" / "&gt;" then to "&amp;lt;" / "&amp;gt;"

This is important since only converting to "&lt;" / ">" will lead CKEditor to think it's a script code not a text.

After matching all scripts I replaced it with the converted version and all works just fine. This is perfect because there's no need to change the saving logic and only convert the wanted tag(s) and display in the editor.

In PHP I convert using: htmlspecialchars() function.

Related