From b63742e1f0b5dbc49518bbfedae73ca1da836c67 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Wed, 3 Jun 2009 13:18:04 -0700 Subject: [PATCH 1/3] [Haml] Fix a weird outer-whitespace-nuking bug. --- lib/haml/buffer.rb | 15 +++++++++++++++ lib/haml/helpers.rb | 3 +++ lib/haml/precompiler.rb | 2 +- test/haml/helper_test.rb | 15 +++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/haml/buffer.rb b/lib/haml/buffer.rb index 1832fc38..63e8f507 100644 --- a/lib/haml/buffer.rb +++ b/lib/haml/buffer.rb @@ -18,6 +18,10 @@ module Haml # It's nil at the top level (see #toplevel?). attr_accessor :upper + # nil if there's no capture_haml block running, + # and the position at which it's beginning the capture if there is one. + attr_accessor :capture_position + # See #active? attr_writer :active @@ -178,6 +182,17 @@ module Haml @real_tabs += 1 unless self_closing || nuke_inner_whitespace end + # Remove the whitespace from the right side of the buffer string. + # Doesn't do anything if we're at the beginning of a capture_haml block. + def rstrip! + if capture_position.nil? + buffer.rstrip! + return + end + + buffer << buffer.slice!(capture_position..-1).rstrip + end + def self.merge_attrs(to, from) if to['id'] && from['id'] to['id'] << '_' << from.delete('id') diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index 840f3ffc..cf7960de 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -275,6 +275,7 @@ module Haml with_haml_buffer(buffer) do position = haml_buffer.buffer.length + haml_buffer.capture_position = position block.call(*args) captured = haml_buffer.buffer.slice!(position..-1).split(/^/) @@ -290,6 +291,8 @@ module Haml line[min_tabs..-1] end.join end + ensure + haml_buffer.capture_position = nil end def puts(*args) # :nodoc: diff --git a/lib/haml/precompiler.rb b/lib/haml/precompiler.rb index c51c27b9..ff9a70b7 100644 --- a/lib/haml/precompiler.rb +++ b/lib/haml/precompiler.rb @@ -803,7 +803,7 @@ END unless @merged_text.empty? @merged_text.rstrip! else - push_silent("_erbout.rstrip!", false) + push_silent("_hamlout.rstrip!", false) @dont_tab_up_next_text = true end end diff --git a/test/haml/helper_test.rb b/test/haml/helper_test.rb index 093d6ecd..946cdaad 100644 --- a/test/haml/helper_test.rb +++ b/test/haml/helper_test.rb @@ -224,5 +224,20 @@ HAML def test_random_class_includes_tag_helper assert_equal "

some tag content

", ActsLikeTag.new.to_s end + + def test_capture_with_nuke_outer + assert_equal "
\n*
hi there!
\n", render(< hi there! +HAML + + assert_equal "
\n*
hi there!
\n", render(< hi there! +HAML + end end From db162f01674469db59e9f2ac36dd334a42abde79 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Tue, 2 Jun 2009 20:41:40 -0700 Subject: [PATCH 2/3] [Haml] Basic compatibility with edge rails. Closes gh-7 --- lib/haml/template/plugin.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/haml/template/plugin.rb b/lib/haml/template/plugin.rb index 9a7914d4..51f4ffad 100644 --- a/lib/haml/template/plugin.rb +++ b/lib/haml/template/plugin.rb @@ -12,7 +12,8 @@ module Haml # template is a template object in Rails >=2.1.0, # a source string previously if template.respond_to? :source - options[:filename] = template.filename + # Template has a generic identifier in Rails >=3.0.0 + options[:filename] = template.respond_to?(:identifier) ? template.identifier : template.filename source = template.source else source = template From f0b4a967d29ebe36e109941c07c82db477b20291 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Tue, 2 Jun 2009 21:21:57 -0700 Subject: [PATCH 3/3] [Haml] Edge-rails compatibility for tests. --- test/haml/helper_test.rb | 8 +++++++- test/haml/template_test.rb | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/test/haml/helper_test.rb b/test/haml/helper_test.rb index 287100d9..6c963c22 100644 --- a/test/haml/helper_test.rb +++ b/test/haml/helper_test.rb @@ -14,6 +14,12 @@ class HelperTest < Test::Unit::TestCase def setup @base = ActionView::Base.new @base.controller = ActionController::Base.new + + if defined?(ActionController::Response) + # This is needed for >=3.0.0 + @base.controller.response = ActionController::Response.new + end + @base.instance_variable_set('@post', Post.new("Foo bar\nbaz")) end @@ -61,7 +67,7 @@ class HelperTest < Test::Unit::TestCase begin ActionView::Base.new.render(:inline => "<%= flatten('Foo\\nBar') %>") - rescue NoMethodError + rescue NoMethodError, ActionView::TemplateError proper_behavior = true end assert(proper_behavior) diff --git a/test/haml/template_test.rb b/test/haml/template_test.rb index cfdab605..67525f71 100644 --- a/test/haml/template_test.rb +++ b/test/haml/template_test.rb @@ -35,6 +35,10 @@ class DummyController def self.controller_path '' end + + def controller_path + '' + end end class TemplateTest < Test::Unit::TestCase