From 5ca0f21a6cc15ccfee5892ab7910d32d5cc4be42 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Fri, 11 Apr 2008 09:44:15 -0700 Subject: [PATCH] haml_tag should only close elements that are listed as autoclose. E.g. we want haml_tag :br #=> "
\n" but haml_tag :p #=> "

\n

\n" --- lib/haml/engine.rb | 1 + lib/haml/helpers.rb | 5 ++++- test/haml/helper_test.rb | 8 ++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/haml/engine.rb b/lib/haml/engine.rb index dff38647..a0fee2bb 100644 --- a/lib/haml/engine.rb +++ b/lib/haml/engine.rb @@ -265,6 +265,7 @@ END # This should remain loadable from #inspect. def options_for_buffer { + :autoclose => @options[:autoclose], :preserve => @options[:preserve], :attr_wrapper => @options[:attr_wrapper], :ugly => @options[:ugly], diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index 85fa6c55..1086c8c9 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -286,6 +286,8 @@ module Haml # # def haml_tag(name, attributes = {}, alt_atts = {}, &block) + name = name.to_s + text = nil if attributes.is_a? String text = attributes @@ -294,7 +296,8 @@ module Haml attributes = Haml::Precompiler.build_attributes( haml_buffer.html?, haml_buffer.options[:attr_wrapper], attributes) - if text.nil? && block.nil? + + if text.nil? && block.nil? && haml_buffer.options[:autoclose].include?(name) puts "<#{name}#{attributes} />" return nil end diff --git a/test/haml/helper_test.rb b/test/haml/helper_test.rb index 5b620ff7..1f433093 100644 --- a/test/haml/helper_test.rb +++ b/test/haml/helper_test.rb @@ -100,6 +100,14 @@ class HelperTest < Test::Unit::TestCase assert_equal("

baz

\n", render("%p{:id => 'foo&bar'} baz", :escape_html => true)) end + def test_haml_tag_autoclosed_tags_are_closed + assert_equal("
\n", render("- haml_tag :br, :class => 'foo'")) + end + + def test_haml_tag_non_autoclosed_tags_arent_closed + assert_equal("

\n

\n", render("- haml_tag :p")) + end + def test_is_haml assert(!ActionView::Base.new.is_haml?) assert_equal("true\n", render("= is_haml?"))