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

Flatten data attributes

This commit is contained in:
Koszta Peter Pal 2012-05-02 17:47:12 +02:00 committed by Norman Clarke
parent 30b2167dc3
commit 29f6c85767
2 changed files with 13 additions and 0 deletions

View file

@ -362,6 +362,7 @@ END
if attributes['data'].is_a?(Hash)
data_attributes = attributes.delete('data')
data_attributes = flatten_data_attributes(data_attributes)
data_attributes = build_data_keys(data_attributes, hyphenate_data_attrs)
attributes = data_attributes.merge(attributes)
end
@ -426,6 +427,11 @@ END
end
end
def self.flatten_data_attributes(data, key = '')
return {key => data} unless data.is_a?(Hash)
data.inject({}){ |hash, k| hash.merge! flatten_data_attributes(k[-1], key.empty? ? k[0] : "#{key}-#{k[0]}") }
end
def prerender_tag(name, self_close, attributes)
attributes_string = Compiler.build_attributes(
html?, @options[:attr_wrapper], @options[:escape_attrs], @options[:hyphenate_data_attrs], attributes)

View file

@ -1432,6 +1432,13 @@ HAML
render("%div{:data => {:foo_bar => 'blip', :baz => 'bang'}}"))
end
def test_html5_data_attributes_with_nested_hash
assert_equal("<div data-foo-bar='blip'></div>\n",
render("%div{:data => {:foo => {:bar => 'blip'}}}"))
assert_equal("<div data-baz='bang' data-foo-bar='blip'></div>\n",
render("%div{:data => {:foo => {:bar => 'blip'}, :baz => 'bang'}}"))
end
def test_html5_data_attributes_with_multiple_defs
# Should always use the more-explicit attribute
assert_equal("<div data-foo='second'></div>\n",