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

发布于 2024-09-18 01:15:54 字数 259 浏览 4 评论 0原文

我们的网站使用诸如 <# TAGNAME #> 之类的标签但 CKEditor 转换 <和>到 &lt 和 &gt 这会破坏这些标签以便在我们的软件中使用。

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

Our site is using tags like <# TAGNAME #> but CKEditor converts < and > to < and > which breaks these tags for use in our software.

I've discovered this option: config.protectedSource.push( /<#[\s\S]*##>/g ); which seems to stop the conversion if the data is saved from Source mode, but in WYSIWYG mode I can't find a way to stop the conversion. I've tried many options in their API but none of them seem to have helped, how can I fix this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朱染 2024-09-25 01:15:54

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

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

这对我们有用:

//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(/>/g,'>');
                            replacedString = replacedString.replace(/</g,'<');
                            replacedString = replacedString.replace(/&/g,'&'); 

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

                    return text;

            }

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

我想你可以通过改变上面的代码来做类似的事情 - 我将其保留原样,因为我认为我们的问题和所需的解决方案非常相似。

Were were looking at using CKEDitor to edit Smarty templates. The problem we were hitting was that it was replacing all the angle brackets and ampersands within the curly brackets, which messed everything up. This came up in a Google search so our solution should help anyone with similar issues.

CKEditor rebuilds the HTML every time you switch to Source mode and when you save, so you need to add to the HTML http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Data_Processor htmlFilter.

This worked for us:

//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(/>/g,'>');
                            replacedString = replacedString.replace(/</g,'<');
                            replacedString = replacedString.replace(/&/g,'&'); 

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

                    return text;

            }

The onText function processes all the bits that aren't in tags or comments.

I'd imagine you can do something similar by altering the code above - I've left it as is as I think our problems and required solutions are very similar.

上课铃就是安魂曲 2024-09-25 01:15:54
editor.on( 'mode', function(ev) {
      if ( ev.editor.mode == 'source' ) {
                        var str=ev.editor.getData();
         str=str.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, "\"");
         ev.editor.textarea.setValue(str);
      }
});

http://cksource.com/forums/viewtopic.php ?f=11&t=20647&start=10

editor.on( 'mode', function(ev) {
      if ( ev.editor.mode == 'source' ) {
                        var str=ev.editor.getData();
         str=str.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, "\"");
         ev.editor.textarea.setValue(str);
      }
});

http://cksource.com/forums/viewtopic.php?f=11&t=20647&start=10

郁金香雨 2024-09-25 01:15:54

如果您输入 <或>在任何所见即所得编辑器中,它们都将在源模式下转换为 HTML 实体。

If you type < or > in any WYSIWYG editor, they will be converted to their HTML entities in source mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文