CKEditor:如何阻止尖括号转换为 HTML 实体

标签 ckeditor

我们的网站使用 <# TAGNAME #> 等标签,但 CKEditor 将 < 和 > 转换为 < 和 >,这会破坏这些标签在我们软件中的使用。

我发现了这个选项: config.protectedSource.push(/<#[\s\S]*##>/g );如果从源模式保存数据,这似乎会停止转换,但在所见即所得模式下,我找不到停止转换的方法。我在他们的 API 中尝试了很多选项,但似乎都没有帮助,我该如何解决这个问题?

最佳答案

我们正在考虑使用 CKEDitor 来编辑 Smarty 模板。我们遇到的问题是它替换了大括号内的所有尖括号和&符号,这把一切搞乱了。 Google 搜索中出现了此问题,因此我们的解决方案应该可以帮助遇到类似问题的任何人。

每次切换到源模式以及保存时,CKEditor 都会重建 HTML,因此您需要添加到 HTML http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Data_Processor html过滤器。

这对我们有用:

//replace Form_content with whatever your editor's id is.
  htmlParser = CKEDITOR.instances.Form_content.dataProcessor.htmlFilter;

            //We don't want HTML encoding on smarty tags
            //so we need to change things in curly brackets
            htmlParser.onText = function(text) {
                    //find all bits in curly brackets                       
                    var matches = text.match(/\{([^}]+)\}/g);

                    //go through each match and replace the encoded characters
                    if (matches!=null) {
                        for (match in matches) {    

                            var replacedString=matches[match];
                            replacedString = matches[match].replace(/&gt;/g,'>');
                            replacedString = replacedString.replace(/&lt;/g,'<');
                            replacedString = replacedString.replace(/&amp;/g,'&'); 

                            text = text.replace(matches[match],replacedString);
                            }
                    }

                    return text;

            }

onText 函数处理所有不在标签或注释中的位。

我想您可以通过更改上面的代码来完成类似的操作 - 我将其保留为原样,因为我认为我们的问题和所需的解决方案非常相似。

关于CKEditor:如何阻止尖括号转换为 HTML 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3668575/

相关文章:

javascript - 获取 ' ng2-ckeditor'中的光标位置

ckeditor - ckeditor 上的临时禁用按钮

ckeditor - 如何在启用 CKEditor 4.x contenteditable 的情况下编辑源 HTML?

css - Ckeditor 默认字体大小、颜色

CKEDITOR 4 : Table cell selection not coming properly on Chrome and IE

html - 更改ckeditor的背景?

javascript - CKEditor 4 处于只读模式 - 删除按钮会删除格式

CKEditor:设置光标/插入符位置

ckeditor - 如何在 CKEditor 中修改上下文菜单?

html - ckeditor 和 codesnippets 插件