From 4771e550d34d0f85bb9512f49d02be248324bfd0 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Sun, 25 Oct 2009 14:36:09 -0700 Subject: [PATCH] [Haml] Fix a bug with elements with dynamic attributes and no content. Closes gh-47 --- doc-src/HAML_CHANGELOG.md | 5 +++++ lib/haml/precompiler.rb | 5 +++-- test/haml/engine_test.rb | 11 +++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/doc-src/HAML_CHANGELOG.md b/doc-src/HAML_CHANGELOG.md index 189a2dc0..3ded8720 100644 --- a/doc-src/HAML_CHANGELOG.md +++ b/doc-src/HAML_CHANGELOG.md @@ -3,6 +3,11 @@ * Table of contents {:toc} +## 2.2.10 (Unreleased) + +* Fixed a bug where elements with dynamic attributes and no content + would have too much whitespace between the opening and closing tag. + ## [2.2.9](http://github.com/nex3/haml/commit/2.2.9) * Fixed a bug where Haml's text was concatenated to the wrong buffer diff --git a/lib/haml/precompiler.rb b/lib/haml/precompiler.rb index 30806dde..9e701ee4 100644 --- a/lib/haml/precompiler.rb +++ b/lib/haml/precompiler.rb @@ -727,6 +727,7 @@ END raise SyntaxError.new("Self-closing tags can't have content.", last_line - 1) if self_closing && !value.empty? self_closing ||= !!( !block_opened? && value.empty? && @options[:autoclose].include?(tag_name) ) + value = nil if value.empty? && (block_opened? || self_closing) dont_indent_next_line = (nuke_outer_whitespace && !block_opened?) || @@ -751,7 +752,7 @@ END return if tag_closed else flush_merged_text - content = value.empty? || parse ? 'nil' : value.dump + content = parse ? 'nil' : value.inspect if attributes_hashes.empty? attributes_hashes = '' elsif attributes_hashes.size == 1 @@ -769,7 +770,7 @@ END return if self_closing - if value.empty? + if value.nil? push_and_tabulate([:element, [tag_name, nuke_outer_whitespace, nuke_inner_whitespace]]) @output_tabs += 1 unless nuke_inner_whitespace return diff --git a/test/haml/engine_test.rb b/test/haml/engine_test.rb index d14688a9..acc0ad82 100644 --- a/test/haml/engine_test.rb +++ b/test/haml/engine_test.rb @@ -123,6 +123,17 @@ class EngineTest < Test::Unit::TestCase assert_equal("

foo

", render("%p{:class => 1+2} foo").chomp) end + def test_dynamic_attributes_with_no_content + assert_equal(< + +

+HTML +%p + %a{:href => "http://" + "haml-lang.com"} +HAML + end + def test_nil_should_render_empty_tag assert_equal("
", render(".no_attributes{:nil => nil}").chomp)