safe_join和content_tag方法替换html_safe

zih*_*aow 4 ruby-on-rails rubocop

我正在使用Rails的content_tag帮助器构建一个HTML代码块.我现在面临的挑战是从数组中加入HTML字符串,并生成HTML元素content_tag.

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>
Run Code Online (Sandbox Code Playgroud)

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

%{<ul>
   <li>One</li>
   <li>Two</li>
   #{options.join('')}
 </ul>
}.html_safe
Run Code Online (Sandbox Code Playgroud)

关于我应该改变什么的任何建议?

# 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>
Run Code Online (Sandbox Code Playgroud)

chu*_*off 6

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

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

编辑

首先,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
Run Code Online (Sandbox Code Playgroud)

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

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

content_tag(:li, "Two").html_safe? # => true
options.join('').html_safe?        # => false
Run Code Online (Sandbox Code Playgroud)

那时候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>"
Run Code Online (Sandbox Code Playgroud)

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

  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
   )
Run Code Online (Sandbox Code Playgroud)