From e7be384f4d8ecb104e91378cd38e976ee520caeb Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Sun, 14 Mar 2010 16:26:49 -0700 Subject: [PATCH] [Haml] Make sure HTML5 custom data attributes don't lead to duplicate attrs. --- lib/haml/precompiler.rb | 9 ++++++--- test/haml/engine_test.rb | 10 +++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/haml/precompiler.rb b/lib/haml/precompiler.rb index b2f166bc..05eef1f6 100644 --- a/lib/haml/precompiler.rb +++ b/lib/haml/precompiler.rb @@ -542,6 +542,12 @@ END quote_escape = attr_wrapper == '"' ? """ : "'" other_quote_char = attr_wrapper == '"' ? "'" : '"' + if attributes['data'].is_a?(Hash) + attributes = attributes.dup + attributes = + Haml::Util.map_keys(attributes.delete('data')) {|name| "data-#{name}"}.merge(attributes) + end + result = attributes.collect do |attr, value| next if value.nil? @@ -553,9 +559,6 @@ END next " #{attr}=#{attr_wrapper}#{attr}#{attr_wrapper}" elsif value == false next - elsif attr == 'data' && value.is_a?(Hash) - next build_attributes(is_html, attr_wrapper, - Haml::Util.map_keys(value) {|name| "data-#{name}"}) end value = Haml::Helpers.preserve(Haml::Helpers.escape_once(value.to_s)) diff --git a/test/haml/engine_test.rb b/test/haml/engine_test.rb index 42b8eefe..9ddb2a40 100755 --- a/test/haml/engine_test.rb +++ b/test/haml/engine_test.rb @@ -1203,7 +1203,15 @@ SASS render("%div{:data => {:one_plus_one => 1+1}}")) assert_equal("
\n", - render(%{%div{:data => {:foo => %{Here's a "quoteful" string.}}}})) + render(%{%div{:data => {:foo => %{Here's a "quoteful" string.}}}})) #' + end + + def test_html5_data_attributes_with_multiple_defs + # Should always use the more-explicit attribute + assert_equal("
\n", + render("%div{:data => {:foo => 'first'}, 'data-foo' => 'second'}")) + assert_equal("
\n", + render("%div{'data-foo' => 'first', :data => {:foo => 'second'}}")) end # New attributes