ckeditor插入代码<pre></pre>标签里的代码会被转义
如果使用网上的解决方法:在config.js里添加config.protectedSource.push(/<pre[\s\S]*?pre>/g);
在可视化编辑里将看不见插入的代码,所以干脆让他转义,这里的解决办法是对代码编辑的时候进行反转义,用到如下代码:
function html_decode(str) { var s = ""; if (str.length == 0) return ""; s = str.replace(/>/g, ">"); s = s.replace(/</g, "<"); s = s.replace(/ /g, " "); s = s.replace(/'/g, "\'"); s = s.replace(/"/g, "\""); s = s.replace(/<br>/g, "\n"); s = s.replace(/&/g,"&"); return s; }
_ueditor_page_break_tag_
放到syntaxhighlight.js里,再在获取代码的地方使用该方法:
this.setValue(html_decode(e.code))
附:html转义
function html_encode(str) { var s = ""; if (str.length == 0) return ""; s = str.replace(/&/g, "&"); s = s.replace(/</g, "<"); s = s.replace(/>/g, ">"); s = s.replace(/ /g, " "); s = s.replace(/\'/g, "'"); s = s.replace(/\"/g, """); s = s.replace(/\n/g, " "); return s; }