【问题标题】:how do add css-classname to the whitelist?如何将 css-classname 添加到白名单?
【发布时间】:2020-07-14 01:53:51
【问题描述】:

我有一个带有 html 标签的字符串:

html = '</span class="repository-content"> ... </span>'

我需要为 css 类设置一个唯一的特定名称。我使用 gem sanitize.
此代码运行良好,并允许 css 类的任何名称:

Sanitize.fragment(
  html,
  elements: ['span'],
  attributes: { 'span' => ['class'] }
)

但只需要允许类repository-content。任何其他类名不得通过。
如何才能做到这一点?任何建议

【问题讨论】:

    标签: html ruby-on-rails ruby sanitize


    【解决方案1】:

    如果不是输入 html,而是输入 ... 部分并像这样构建 span:

    content_tag(:span, ..., class: "repository-content")
    

    那么你可以编写 ruby​​ 来处理如果类不是 "repository-content" 时要做什么的逻辑。

    您可能需要清理...,但您可能不必清理content_tag 的输出。

    【讨论】:

      猜你喜欢
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 2013-06-11
      • 2018-09-22
      相关资源
      最近更新 更多