[Haml] Make sure the :data attribute is merged properly when using attribute methods.

This commit is contained in:
Nathan Weizenbaum 2010-03-14 16:38:14 -07:00
parent e7be384f4d
commit d41c130304
2 changed files with 33 additions and 1 deletions

View File

@ -233,10 +233,11 @@ RUBY
# Merges two attribute hashes.
# This is the same as `to.merge!(from)`,
# except that it merges id and class attributes.
# except that it merges id, class, and data attributes.
#
# ids are concatenated with `"_"`,
# and classes are concatenated with `" "`.
# data hashes are simply merged.
#
# Destructively modifies both `to` and `from`.
#
@ -259,6 +260,16 @@ RUBY
from['class'] ||= to['class']
end
from_data = from['data'].is_a?(Hash)
to_data = to['data'].is_a?(Hash)
if from_data && to_data
to['data'] = to['data'].merge(from['data'])
elsif to_data
to = Haml::Util.map_keys(to.delete('data')) {|name| "data-#{name}"}.merge(to)
elsif from_data
from = Haml::Util.map_keys(from.delete('data')) {|name| "data-#{name}"}.merge(from)
end
to.merge!(from)
end

View File

@ -1214,6 +1214,27 @@ SASS
render("%div{'data-foo' => 'first', :data => {:foo => 'second'}}"))
end
def test_html5_data_attributes_with_attr_method
Haml::Helpers.module_eval do
def data_hash
{:data => {:foo => "bar", :baz => "bang"}}
end
def data_val
{:data => "dat"}
end
end
assert_equal("<div data-baz='bang' data-brat='wurst' data-foo='blip'></div>\n",
render("%div{data_hash, :data => {:foo => 'blip', :brat => 'wurst'}}"))
assert_equal("<div data-baz='bang' data-foo='blip'></div>\n",
render("%div{data_hash, 'data-foo' => 'blip'}"))
assert_equal("<div data-baz='bang' data-foo='bar' data='dat'></div>\n",
render("%div{data_hash, :data => 'dat'}"))
assert_equal("<div data-brat='wurst' data-foo='blip' data='dat'></div>\n",
render("%div{data_val, :data => {:foo => 'blip', :brat => 'wurst'}}"))
end
# New attributes
def test_basic_new_attributes