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
@ -425,6 +426,11 @@ END
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(

View file

@ -1431,6 +1431,13 @@ HAML
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_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