未验证 提交 8c121ce9 编写于 作者: A Aaron Patterson 提交者: GitHub

Merge pull request #37872 from joelhawksley/content-tag-hash-class-conditional

Add support for conditional values to TagBuilder
* Add support for conditional values to TagBuilder.
*Joel Hawksley*
* `ActionView::Helpers::FormOptionsHelper#select` should mark option for `nil` as selected.
```ruby
......
......@@ -92,7 +92,9 @@ def boolean_tag_option(key)
end
def tag_option(key, value, escape)
if value.is_a?(Array)
case value
when Array, Hash
value = build_tag_values(value)
value = escape ? safe_join(value, " ") : value.join(" ")
else
value = escape ? ERB::Util.unwrapped_html_escape(value) : value.to_s
......@@ -102,6 +104,25 @@ def tag_option(key, value, escape)
end
private
def build_tag_values(*args)
tag_values = []
args.each do |tag_value|
case tag_value
when String
tag_values << tag_value if tag_value.present?
when Hash
tag_value.each do |key, val|
tag_values << key if val
end
when Array
tag_values << build_tag_values(*tag_value).presence
end
end
tag_values.compact.flatten
end
def prefix_tag_option(prefix, key, value, escape)
key = "#{prefix}-#{key.to_s.dasherize}"
unless value.is_a?(String) || value.is_a?(Symbol) || value.is_a?(BigDecimal)
......@@ -240,6 +261,9 @@ def method_missing(called, *args, **options, &block)
#
# tag("div", data: { name: 'Stephen', city_state: %w(Chicago IL) })
# # => <div data-name="Stephen" data-city-state="[&quot;Chicago&quot;,&quot;IL&quot;]" />
#
# tag("div", class: { highlight: current_user.admin? })
# # => <div class="highlight" />
def tag(name = nil, options = nil, open = false, escape = true)
if name.nil?
tag_builder
......@@ -267,6 +291,8 @@ def tag(name = nil, options = nil, open = false, escape = true)
# # => <div class="strong"><p>Hello world!</p></div>
# content_tag(:div, "Hello world!", class: ["strong", "highlight"])
# # => <div class="strong highlight">Hello world!</div>
# content_tag(:div, "Hello world!", class: ["strong", { highlight: current_user.admin? }])
# # => <div class="strong highlight">Hello world!</div>
# content_tag("select", options, multiple: true)
# # => <select multiple="multiple">...options...</select>
#
......
......@@ -246,6 +246,68 @@ def test_tag_builder_with_unescaped_empty_array_class
assert_equal '<p class="">limelight</p>', str
end
def test_content_tag_with_conditional_hash_classes
str = content_tag("p", "limelight", class: { "song": true, "play": false })
assert_equal "<p class=\"song\">limelight</p>", str
str = content_tag("p", "limelight", class: [{ "song": true }, { "play": false }])
assert_equal "<p class=\"song\">limelight</p>", str
str = content_tag("p", "limelight", class: { song: true, play: false })
assert_equal "<p class=\"song\">limelight</p>", str
str = content_tag("p", "limelight", class: [{ song: true }, nil, false])
assert_equal "<p class=\"song\">limelight</p>", str
str = content_tag("p", "limelight", class: ["song", { foo: false }])
assert_equal "<p class=\"song\">limelight</p>", str
str = content_tag("p", "limelight", class: { "song": true, "play": true })
assert_equal "<p class=\"song play\">limelight</p>", str
str = content_tag("p", "limelight", class: { "song": false, "play": false })
assert_equal '<p class="">limelight</p>', str
end
def test_tag_builder_with_conditional_hash_classes
str = tag.p "limelight", class: { "song": true, "play": false }
assert_equal "<p class=\"song\">limelight</p>", str
str = tag.p "limelight", class: [{ "song": true }, { "play": false }]
assert_equal "<p class=\"song\">limelight</p>", str
str = tag.p "limelight", class: { song: true, play: false }
assert_equal "<p class=\"song\">limelight</p>", str
str = tag.p "limelight", class: [{ song: true }, nil, false]
assert_equal "<p class=\"song\">limelight</p>", str
str = tag.p "limelight", class: ["song", { foo: false }]
assert_equal "<p class=\"song\">limelight</p>", str
str = tag.p "limelight", class: { "song": true, "play": true }
assert_equal "<p class=\"song play\">limelight</p>", str
str = tag.p "limelight", class: { "song": false, "play": false }
assert_equal '<p class="">limelight</p>', str
end
def test_content_tag_with_unescaped_conditional_hash_classes
str = content_tag("p", "limelight", { class: { "song": true, "play>": true } }, false)
assert_equal "<p class=\"song play>\">limelight</p>", str
str = content_tag("p", "limelight", { class: ["song", { "play>": true }] }, false)
assert_equal "<p class=\"song play>\">limelight</p>", str
end
def test_tag_builder_with_unescaped_conditional_hash_classes
str = tag.p "limelight", class: { "song": true, "play>": true }, escape_attributes: false
assert_equal "<p class=\"song play>\">limelight</p>", str
str = tag.p "limelight", class: ["song", { "play>": true }], escape_attributes: false
assert_equal "<p class=\"song play>\">limelight</p>", str
end
def test_content_tag_with_data_attributes
assert_dom_equal '<p data-number="1" data-string="hello" data-string-with-quotes="double&quot;quote&quot;party&quot;">limelight</p>',
content_tag("p", "limelight", data: { number: 1, string: "hello", string_with_quotes: 'double"quote"party"' })
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册