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

Use underscores in nested data attributes when configured

This commit is contained in:
Norman Clarke 2012-05-23 16:15:53 -03:00
parent fbcf283cc6
commit 2d2687a28b
2 changed files with 19 additions and 6 deletions

View file

@ -357,12 +357,15 @@ END
# This is a class method so it can be accessed from Buffer.
def self.build_attributes(is_html, attr_wrapper, escape_attrs, hyphenate_data_attrs, attributes = {})
quote_escape = attr_wrapper == '"' ? """ : "'"
# @TODO this is an absolutely ridiculous amount of arguments. At least
# some of this needs to be moved into an instance method.
quote_escape = attr_wrapper == '"' ? """ : "'"
other_quote_char = attr_wrapper == '"' ? "'" : '"'
join_char = hyphenate_data_attrs ? '-' : '_'
if attributes['data'].is_a?(Hash)
data_attributes = attributes.delete('data')
data_attributes = flatten_data_attributes(data_attributes)
data_attributes = flatten_data_attributes(data_attributes, '', join_char)
data_attributes = build_data_keys(data_attributes, hyphenate_data_attrs)
attributes = data_attributes.merge(attributes)
end
@ -416,7 +419,7 @@ END
end
def self.build_data_keys(data_hash, hyphenate)
Haml::Util.map_keys(data_hash) do |name|
Haml::Util.map_keys(data_hash) do |name|
if name == nil
"data"
elsif hyphenate
@ -427,13 +430,15 @@ END
end
end
def self.flatten_data_attributes(data, key = '', seen = [])
def self.flatten_data_attributes(data, key, join_char, seen = [])
return {key => nil} if seen.include? data.object_id
seen << data.object_id
return {key => data} unless data.is_a?(Hash)
data.sort {|x, y| x[0].to_s <=> y[0].to_s}.inject({}) do |hash, a|
hash.merge! flatten_data_attributes(a[1], key == '' ? a[0] : "#{key}-#{a[0]}", seen)
data.sort {|x, y| x[0].to_s <=> y[0].to_s}.inject({}) do |hash, array|
k, v = array
joined = key == '' ? k : [key, k].join(join_char)
hash.merge! flatten_data_attributes(v, joined, join_char, seen)
end
end

View file

@ -1440,6 +1440,14 @@ HAML
HAML
end
def test_html5_data_attributes_with_nested_hash_and_without_hyphenation
assert_equal("<div data-a_b='c'></div>\n", render(<<-HAML, :hyphenate_data_attrs => false))
- hash = {:a => {:b => 'c'}}
- hash[:d] = hash
%div{:data => hash}
HAML
end
def test_html5_data_attributes_with_multiple_defs
# Should always use the more-explicit attribute
assert_equal("<div data-foo='second'></div>\n",