Skip to content

Commit

Permalink
Don't treat the 'data' attribute specially.
Browse files Browse the repository at this point in the history
Fixes haml#678
  • Loading branch information
norman committed May 21, 2013
1 parent c434e8c commit bb8ca6b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 14 additions & 9 deletions lib/haml/buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 9 additions & 5 deletions test/engine_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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("<div baz='qux' foo='bar' zig='zag'></div>",
render("%div{{:foo => 'bar'}, :baz => 'qux'}(zig = val)", :locals => {:val => 'zag'}).chomp)
end

def test_strings_should_get_stripped_inside_tags
assert_equal("<div class='stripped'>This should have no spaces in front of it</div>",
render(".stripped This should have no spaces in front of it").chomp)
Expand Down Expand Up @@ -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{<a aria-baz='qux' aria-foo='bar'></a>\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("<div data-a-b='c'></div>\n", render(<<-HAML))
- hash = {:a => {:b => 'c'}}
Expand Down

0 comments on commit bb8ca6b

Please sign in to comment.