【问题标题】:safe_join and content_tag approach to replace html_safe替换 html_safe 的 safe_join 和 content_tag 方法
【发布时间】:2017-09-05 19:28:04
【问题描述】:

我正在使用 Rails 的 content_tag 助手构建一段 HTML 代码。我现在面临的挑战是将数组中的 HTML 字符串与content_tag 生成的 HTML 元素连接起来。

RuboCop Rails/OutputSafety 参考。

例如:

options = ["<li>Three</li>", "<li>Four</li>", "<li>Five</li>"]

# This is code to generate blocks of HTML
out = []
out << content_tag(:ul,  
   content_tag(:li, "One") + 
   content_tag(:li, "Two") + 
   options.join(''),
:class => ["class_1", "class_2"])
safe_join(out)

# Expect result should be like
<ul class="class_1 class_2">
   <li>One</li>
   <li>Two</li>
   <li>Three</li>
   <li>Four</li>
   <li>Five</li>
</ul>

# Actual result
<ul class="class_1 class_2">
   <li>One</li>
   <li>Two</li>
   "<li>Three</li><li>Four</li><li>Five</li>"
</ul>

但是,如果我使用下面的 html_safe 方法,它会起作用。

%{<ul>
   <li>One</li>
   <li>Two</li>
   #{options.join('')}
 </ul>
}.html_safe

对我应该改变什么有什么建议吗?

# New apporach
options = ["Three", "Four", "Five"]
out = []
out << content_tag(:ul,  
   content_tag(:li, "One") + 
   content_tag(:li, "Two") + 
   options.collect do |option|
      content_tag(:li, "#{option[0]}")
   end.join(""),
:class => ["class_1", "class_2"])
safe_join(out)

# New approach result
<ul class="class_1 class_2">
   <li>One</li>
   <li>Two</li>
   "<li>Three</li><li>Four</li><li>Five</li>"
</ul>

【问题讨论】:

  • 对于那些正在寻找替代和更安全的方法来用 safe_join 和 content_tag 助手替换 html_safe 的人。查看完整和描述性解决方案的答案。

标签: ruby-on-rails rubocop


【解决方案1】:

问题是您将输出与来自options 数组的不安全字符串连接起来。这是唯一应该使用 html_safe 方法以确保整个输出安全的地方:

out << content_tag(:ul,  
   content_tag(:li, "One") + 
   content_tag(:li, "Two") + 
   options.join('').html_safe,
:class => ["class_1", "class_2"])

编辑

首先safe_join 方法不像html_safe 方法那样工作,它不仅使连接的字符串成为html_safe。如果加入的字符串不是 html_safe,它也会使 html 转义,以避免有害内容。

https://apidock.com/rails/ActionView/Helpers/OutputSafetyHelper/safe_join

在您的情况下,safe_join 方法根本没有对 out 数组中的字符串做任何事情,因为它们已经是 html_safe。

result = content_tag(:ul,  
           content_tag(:li, "One") + 
           content_tag(:li, "Two") + 
           options.join(''),
           :class => ["class_1", "class_2"])

result.html_safe? # => true

问题的原因是您将安全字符串与不安全字符串连接:

content_tag(:li, "Two") + options.join('')

content_tag(:li, "Two").html_safe? # => true
options.join('').html_safe?        # => false

当时options.join('') 被html 转义,因为它不安全。看例子:

# html tags in the second string are escaped, since it is not safe
"<li>One</li>".html_safe + "<li>Two</li>" # => "<li>One</li>&lt;li&gt;Two&lt;/li&gt;"

# nothing has been escaped, since everything is safe
"<li>One</li>".html_safe + "<li>Two</li>".html_safe # => "<li>One</li><li>Two</li>"

因此,为了获得预期的结果,必须满足两个条件:

  1. safe_join 方法必须采用 html_safe 字符串数组。如果它们不是 html_safe,则所有 html 标记都将被转义。
  2. 不要将安全字符串与不安全字符串连接,否则不安全字符串将被转义。

如您所见,您没有满足第二个条件。

关于新方法的建议

.join("") 方法使结果字符串不安全,即使数组包含安全字符串。使用safe_join:

   content_tag(:li, "One") + 
   content_tag(:li, "Two") + 
   safe_join(
     options.collect do |option|
       content_tag(:li, option)
     end
   )

【讨论】:

  • 是的,我采用了 html_safe 方法,效果很好。我试图找到一种不使用 html_safe 来实现的替代方法,这就是我使用 safe_join 方法的原因。(参考:rubocop.readthedocs.io/en/latest/cops_rails/#railsoutputsafety
  • 我添加了更多解释
  • 你的解释真的很有帮助。我想知道 content_tag 是否总是生成 HTML 安全字符串。但是,如果我将 content_tag 包装在 [collect .. end] 范围内。它不会生成 HTML 安全字符串。有任何想法吗?看看我的新方法。 @chumakoff
  • 添加的建议
  • 你是这方面的杀手。正是我想要的。 @chumakoff
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-29
  • 2014-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-05
相关资源
最近更新 更多