From bb8ca6b202d9c641f373e20d9f015dddd48a2aad Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Tue, 21 May 2013 10:22:48 -0300 Subject: [PATCH] Don't treat the 'data' attribute specially. Fixes #678 --- CHANGELOG.md | 1 + lib/haml/buffer.rb | 23 ++++++++++++++--------- test/engine_test.rb | 14 +++++++++----- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f66d77627e..babc8f154e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ for detailed info. (Matt Wildig) * Make escape_once respect hexadecimal references. (Matt Wildig) * General performance and memory usage improvements. (Akira Matsuda) +* Don't treat the 'data' attribute specially when merging attribute hashes. (Matt Wildig and Norman Clarke) ## 4.0.2 diff --git a/lib/haml/buffer.rb b/lib/haml/buffer.rb index b76e328735..99db875fe1 100644 --- a/lib/haml/buffer.rb +++ b/lib/haml/buffer.rb @@ -246,16 +246,21 @@ def self.merge_attrs(to, from) from['class'] ||= to['class'] end - from_data = from.delete('data') - # forces to_data & from_data into a hash - from_data = { nil => from_data } if from_data && !from_data.is_a?(Hash) - to['data'] = { nil => to['data'] } if to['data'] && !to['data'].is_a?(Hash) - - if from_data && !to['data'] - to['data'] = from_data - elsif from_data && to['data'] - to['data'].merge! from_data + from.keys.each do |key| + next unless from[key].kind_of?(Hash) || to[key].kind_of?(Hash) + + from_data = from.delete(key) + # forces to_data & from_data into a hash + from_data = { nil => from_data } if from_data && !from_data.is_a?(Hash) + to[key] = { nil => to[key] } if to[key] && !to[key].is_a?(Hash) + + if from_data && !to[key] + to[key] = from_data + elsif from_data && to[key] + to[key].merge! from_data + end end + to.merge!(from) end diff --git a/test/engine_test.rb b/test/engine_test.rb index 5f0b46a708..fbc942774c 100644 --- a/test/engine_test.rb +++ b/test/engine_test.rb @@ -214,11 +214,6 @@ def test_nil_should_render_empty_tag render(".no_attributes{:nil => nil}").chomp) end - def test_attribute_method_with_both_attrib_styles_and_non_static_hashes - assert_equal("
", - render("%div{{:foo => 'bar'}, :baz => 'qux'}(zig = val)", :locals => {:val => 'zag'}).chomp) - end - def test_strings_should_get_stripped_inside_tags assert_equal("
This should have no spaces in front of it
", render(".stripped This should have no spaces in front of it").chomp) @@ -1458,6 +1453,15 @@ def test_html5_arbitrary_hash_valued_attributes_with render("%div{:foo => {:baz => 'bang'}}")) end + def test_arbitrary_attribute_hash_merging + assert_equal(%Q{\n}, render(<<-HAML)) +- h1 = {:aria => {:foo => :bar}} +- h2 = {:baz => :qux} +%a{h1, :aria => h2} +HAML + end + + def test_html5_data_attributes_with_nested_hash assert_equal("
\n", render(<<-HAML)) - hash = {:a => {:b => 'c'}}