如何在CKEditor中定义允许的标签?

发布于 2024-09-02 15:39:50 字数 208 浏览 4 评论 0原文

  • 有时用户将文本从不同来源复制并粘贴到 CKEditor,但我想限制他们可以复制到 CKEditor 的标签。

    有时用户将文本从不同来源复制并粘贴
  • 我只需要在 CKEditor 中使用某些标签:列表标签、中断标签等...

  • < p>我可以定义它们吗?禁用 CKEditor 中的其他标签?

  • Sometimes users copy and paste text from different sources to CKEditor, but I want to restrict what tags they can copy to CKEditor.

  • I only need to use certain tags in CKEditor: The list tag, break tag, etc...

  • Can I define them & disable the other tags in CKEditor?

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

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

发布评论

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

评论(6

梦与时光遇 2024-09-09 15:39:50

您可以使用一些设置。您可以通过编辑 ckeditor 根目录中的 config.js 文件来定义这些设置。例如,如果您想像我一样激进,您可以输入:

config.forcePasteAsPlainText = true;

如果您只想像您所说的那样限制某些标签,我找到了以下设置:

config.removeFormatTags = 'b,big,code,del,dfn,em,font,i,ins,kbd';

仅当用户执行“删除格式”命令时才会完成最后一个。
更多信息:http://docs.cksource.com/ckeditor_api/symbols /CKEDITOR.config.html#.format_tags

一直以来,我认为您已经找到了答案,但其他人可以得到帮助。

There are some settings you can use. You define these settings editing the config.js file in the ckeditor's root directory. For example, if you want to be radical like me, you could put:

config.forcePasteAsPlainText = true;

If you want to restrict only certain tags exactly like you said, I found the setting bellow:

config.removeFormatTags = 'b,big,code,del,dfn,em,font,i,ins,kbd';

The last will be done only when user execute the "remove format" command.
More information: http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.format_tags

Considering all the time, I think that you already have found your answer, but others can be helped.

萝莉病 2024-09-09 15:39:50

我这样做只是为了确保没有人可以在编辑器中放置 标签。它可能可以扩展到其他标签:

            CKEDITOR.on('instanceReady', function(ev)
            {
                var editor = ev.editor;
                var dataProcessor = editor.dataProcessor;
                var htmlFilter = dataProcessor && dataProcessor.htmlFilter;
                htmlFilter.addRules(
                {
                    elements : 
                    {
                        input: function(element)
                        {
                            return false;
                        },
                    }
                });
            });

I just did this to make sure nobody could put an <input> tag in the editor. It could probably be extended to other tags:

            CKEDITOR.on('instanceReady', function(ev)
            {
                var editor = ev.editor;
                var dataProcessor = editor.dataProcessor;
                var htmlFilter = dataProcessor && dataProcessor.htmlFilter;
                htmlFilter.addRules(
                {
                    elements : 
                    {
                        input: function(element)
                        {
                            return false;
                        },
                    }
                });
            });
记忆消瘦 2024-09-09 15:39:50

您可以使用白名单插件在您的配置中定义元素和属性列表,被允许并拒绝其他任何事情。

它基本上与 Paul Tomblin 提出的解决方案相同,但处理更多元素应该更容易,而不是复制大量代码,并且它使用白名单而不是黑名单,因此任何不允许的内容都会被删除。

You can use the whitelist plugin to define in your configuration a list of elements and attributes that are allowed and deny anything else.

It's basically the same solution presented by Paul Tomblin but it should be easier to handle more elements instead of copy lots of code and instead of blacklist it uses a whitelist so anything that isn't allowed is removed.

演多会厌 2024-09-09 15:39:50

为了添加我的输入,从 4.1 开始就有了高级内容过滤器功能,它允许对允许的内容制定非常具体的规则(哪些标签以及它们的样式/属性/类)。我发现它非常强大并且配置非常好。

欲了解更多信息,请访问 http://docs.ckeditor.com/#!/guide/dev_advanced_content_filter 但这里有一些来自该页面的示例

config.allowedContent = true; // to allow all

// A rule accepting <p> and <h1> elements with optional "left" and "right" classes.
// Note: Both elements may contain these classes, not only <h1>.
config.allowedContent = "p h1(left,right)";

// Rules allowing:
// * <p> and <h1> elements with an optional "text-align" style,
// * <a> with a required "href" attribute,
// * <strong> and <em> elements,
// * <p> with an optional "tip" class (so <p> element may contain
//  a "text-align" style and a "tip" class at the same time).
config.allowedContent = "p h1{text-align}; a[!href]; strong em; p(tip)";

To add my input, there is since 4.1 the Advanced Content Filter feature, which allows for very specific rules for allowed content (what tags and which styles/attributes/classes for them). I find it very powerful and very nice to configure.

Read more at http://docs.ckeditor.com/#!/guide/dev_advanced_content_filter but here's a few examples from the page

config.allowedContent = true; // to allow all

// A rule accepting <p> and <h1> elements with optional "left" and "right" classes.
// Note: Both elements may contain these classes, not only <h1>.
config.allowedContent = "p h1(left,right)";

// Rules allowing:
// * <p> and <h1> elements with an optional "text-align" style,
// * <a> with a required "href" attribute,
// * <strong> and <em> elements,
// * <p> with an optional "tip" class (so <p> element may contain
//  a "text-align" style and a "tip" class at the same time).
config.allowedContent = "p h1{text-align}; a[!href]; strong em; p(tip)";
挽容 2024-09-09 15:39:50

我使用 phpjs.org 中的 strip_tags 方法,将有限选择的 html 标签直接应用于粘贴操作。

CKEDITOR.on('instanceReady', function(ev) {
   ev.editor.on('paste', function(evt) {
      evt.data['html'] = strip_tags(evt.data['html'], 
         '<i><em><b><strong><blockquote><p><br><div><ul><li><ol>' // allowed list
      );
   });
});

function strip_tags (input, allowed) {
   // http://phpjs.org/functions/strip_tags (http://kevin.vanzonneveld.net)
   allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
   var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
      commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
   return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
      return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
   });
}

I applied a limited selection of html tags directly to the paste operation, using the strip_tags method from phpjs.org.

CKEDITOR.on('instanceReady', function(ev) {
   ev.editor.on('paste', function(evt) {
      evt.data['html'] = strip_tags(evt.data['html'], 
         '<i><em><b><strong><blockquote><p><br><div><ul><li><ol>' // allowed list
      );
   });
});

function strip_tags (input, allowed) {
   // http://phpjs.org/functions/strip_tags (http://kevin.vanzonneveld.net)
   allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
   var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
      commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
   return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
      return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
   });
}
寂寞清仓 2024-09-09 15:39:50
CKEDITOR.config.fullPage = false

指示是否将要编辑的内容作为完整的 HTML 页面输入。完整页面包括 元素。最终输出也将反映此设置,仅当禁用此设置时才包括 内容。

CKEDITOR.config.fullPage = false

Indicates whether the contents to be edited are being input as a full HTML page. A full page includes the <html>, <head>, and <body> elements. The final output will also reflect this setting, including the <body> contents only if this setting is disabled.

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