diff --git a/lib/haml/buffer.rb b/lib/haml/buffer.rb index 4b2fd632..a7ab5186 100644 --- a/lib/haml/buffer.rb +++ b/lib/haml/buffer.rb @@ -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 diff --git a/test/haml/engine_test.rb b/test/haml/engine_test.rb index 9ddb2a40..b51104a1 100755 --- a/test/haml/engine_test.rb +++ b/test/haml/engine_test.rb @@ -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("
\n", + render("%div{data_hash, :data => {:foo => 'blip', :brat => 'wurst'}}")) + assert_equal("
\n", + render("%div{data_hash, 'data-foo' => 'blip'}")) + assert_equal("
\n", + render("%div{data_hash, :data => 'dat'}")) + assert_equal("
\n", + render("%div{data_val, :data => {:foo => 'blip', :brat => 'wurst'}}")) + end + # New attributes def test_basic_new_attributes