1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

[Haml] Make sure HTML5 custom data attributes don't lead to duplicate attrs.

This commit is contained in:
Nathan Weizenbaum 2010-03-14 16:26:49 -07:00
parent 62f05fcdad
commit e7be384f4d
2 changed files with 15 additions and 4 deletions

View file

@ -542,6 +542,12 @@ END
quote_escape = attr_wrapper == '"' ? """ : "'" quote_escape = attr_wrapper == '"' ? """ : "'"
other_quote_char = attr_wrapper == '"' ? "'" : '"' other_quote_char = attr_wrapper == '"' ? "'" : '"'
if attributes['data'].is_a?(Hash)
attributes = attributes.dup
attributes =
Haml::Util.map_keys(attributes.delete('data')) {|name| "data-#{name}"}.merge(attributes)
end
result = attributes.collect do |attr, value| result = attributes.collect do |attr, value|
next if value.nil? next if value.nil?
@ -553,9 +559,6 @@ END
next " #{attr}=#{attr_wrapper}#{attr}#{attr_wrapper}" next " #{attr}=#{attr_wrapper}#{attr}#{attr_wrapper}"
elsif value == false elsif value == false
next next
elsif attr == 'data' && value.is_a?(Hash)
next build_attributes(is_html, attr_wrapper,
Haml::Util.map_keys(value) {|name| "data-#{name}"})
end end
value = Haml::Helpers.preserve(Haml::Helpers.escape_once(value.to_s)) value = Haml::Helpers.preserve(Haml::Helpers.escape_once(value.to_s))

View file

@ -1203,7 +1203,15 @@ SASS
render("%div{:data => {:one_plus_one => 1+1}}")) render("%div{:data => {:one_plus_one => 1+1}}"))
assert_equal("<div data-foo='Here&apos;s a \"quoteful\" string.'></div>\n", assert_equal("<div data-foo='Here&apos;s a \"quoteful\" string.'></div>\n",
render(%{%div{:data => {:foo => %{Here's a "quoteful" string.}}}})) render(%{%div{:data => {:foo => %{Here's a "quoteful" string.}}}})) #'
end
def test_html5_data_attributes_with_multiple_defs
# Should always use the more-explicit attribute
assert_equal("<div data-foo='second'></div>\n",
render("%div{:data => {:foo => 'first'}, 'data-foo' => 'second'}"))
assert_equal("<div data-foo='first'></div>\n",
render("%div{'data-foo' => 'first', :data => {:foo => 'second'}}"))
end end
# New attributes # New attributes