Skip to content

Commit

Permalink
Allow content_tag options to take an array [#1741 state:resolved] [ri…
Browse files Browse the repository at this point in the history
…zwanreza, Nick Quaranto]

Example:
  content_tag('p', "limelight", :class => ["song", "play"])
  # => <p class="song play">limelight</p>

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
rizwanreza authored and lifo committed Aug 8, 2009
1 parent 761283f commit 5786395
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
16 changes: 7 additions & 9 deletions actionpack/lib/action_view/helpers/tag_helper.rb
Expand Up @@ -134,16 +134,14 @@ def content_tag_string(name, content, options, escape = true)
def tag_options(options, escape = true)
unless options.blank?
attrs = []
if escape
options.each_pair do |key, value|
if BOOLEAN_ATTRIBUTES.include?(key)
attrs << %(#{key}="#{key}") if value
else
attrs << %(#{key}="#{escape_once(value)}") if !value.nil?
end
options.each_pair do |key, value|
if BOOLEAN_ATTRIBUTES.include?(key)
attrs << %(#{key}="#{key}") if value
elsif !value.nil?
final_value = value.is_a?(Array) ? value.join(" ") : value
final_value = escape_once(final_value) if escape
attrs << %(#{key}="#{final_value}")
end
else
attrs = options.map { |key, value| %(#{key}="#{value}") }
end
" #{attrs.sort * ' '}" unless attrs.empty?
end
Expand Down
13 changes: 13 additions & 0 deletions actionpack/test/template/tag_helper_test.rb
Expand Up @@ -71,6 +71,19 @@ def test_content_tag_nested_in_content_tag_in_erb
assert_equal '<p><b>Hello</b></p>', output_buffer
end

def test_content_tag_with_escaped_array_class
str = content_tag('p', "limelight", :class => ["song", "play>"])
assert_equal "<p class=\"song play&gt;\">limelight</p>", str

str = content_tag('p', "limelight", :class => ["song", "play"])
assert_equal "<p class=\"song play\">limelight</p>", str
end

def test_content_tag_with_unescaped_array_class
str = content_tag('p', "limelight", {:class => ["song", "play>"]}, false)
assert_equal "<p class=\"song play>\">limelight</p>", str
end

def test_cdata_section
assert_equal "<![CDATA[<hello world>]]>", cdata_section("<hello world>")
end
Expand Down

0 comments on commit 5786395

Please sign in to comment.