diff --git a/CHANGELOG.md b/CHANGELOG.md index f5a97a70..b5a402d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,15 @@ * 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.5 + +Not released yet. + +* Fix for bug whereby when HAML :ugly option is "true", + ActionView::Helpers::CaptureHelper::capture returns the whole view buffer + when passed a block that returns nothing (thanks [Mircea + Moise](https://github.com/mmircea16)). + ## 4.0.4 Released on November 5, 2013 ([diff](https://github.com/haml/haml/compare/4.0.3...4.0.4)). @@ -1349,3 +1358,4 @@ has been moved to [YARD](http://yard.soen.ca). YARD is an excellent documentation system, and allows us to write our documentation in [Maruku](http://maruku.rubyforge.org), which is also excellent. +>>>>>>> External Changes diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index d5ada463..d2b6753e 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -372,25 +372,16 @@ MESSAGE position = haml_buffer.buffer.length haml_buffer.capture_position = position - block.call(*args) + value = block.call(*args) captured = haml_buffer.buffer.slice!(position..-1) - return captured if haml_buffer.options[:ugly] - # Note that the "reject" is needed for rbx 1.2.4, which includes empty - # strings in the returned array when splitting by /^/. - captured = captured.split(/^/) - captured.delete('') - min_tabs = nil - captured.each do |line| - tabs = line.index(/[^ ]/) || line.length - min_tabs ||= tabs - min_tabs = min_tabs > tabs ? tabs : min_tabs + if captured == '' and value != haml_buffer.buffer + captured = (value.is_a?(String) ? value : nil) end - captured.map do |line| - line.slice(min_tabs, line.length) - end.join + return nil if captured.nil? + return (haml_buffer.options[:ugly] ? captured : prettify(captured)) end ensure haml_buffer.capture_position = nil @@ -669,6 +660,22 @@ MESSAGE _erbout = _erbout = _hamlout.buffer proc { |*args| proc.call(*args) } end + + def prettify(text) + text = text.split(/^/) + text.delete('') + + min_tabs = nil + text.each do |line| + tabs = line.index(/[^ ]/) || line.length + min_tabs ||= tabs + min_tabs = min_tabs > tabs ? tabs : min_tabs + end + + text.map do |line| + line.slice(min_tabs, line.length) + end.join + end end end diff --git a/lib/haml/helpers/action_view_mods.rb b/lib/haml/helpers/action_view_mods.rb index 0542859d..588b8e45 100644 --- a/lib/haml/helpers/action_view_mods.rb +++ b/lib/haml/helpers/action_view_mods.rb @@ -41,16 +41,9 @@ module ActionView if Haml::Helpers.block_is_haml?(block) #double assignment is to avoid warnings _hamlout = _hamlout = eval('_hamlout', block.binding) # Necessary since capture_haml checks _hamlout - value = nil - buffer = capture_haml(*args) { value = yield(*args) } - str = - if !buffer.empty? - buffer - elsif value.is_a?(String) - value - else - nil - end + + str = capture_haml(*args, &block) + # NonCattingString is present in Rails less than 3.1.0. When support # for 3.0 is dropped, this can be removed. return ActionView::NonConcattingString.new(str) if str && defined?(ActionView::NonConcattingString) diff --git a/test/template_test.rb b/test/template_test.rb index d6f16122..4d5674f1 100644 --- a/test/template_test.rb +++ b/test/template_test.rb @@ -111,6 +111,29 @@ class TemplateTest < MiniTest::Unit::TestCase end end + def test_render_method_returning_null_with_ugly + @base.instance_eval do + def empty + nil + end + def render_something(&block) + capture(self, &block) + end + end + + content_to_render = "%h1 This is part of the broken view.\n= render_something do |thing|\n = thing.empty do\n = 'test'" + result = render(content_to_render, :ugly => true) + expected_result = "

This is part of the broken view.

\n" + assert_equal(expected_result, result) + end + + def test_simple_rendering_with_ugly + content_to_render = "%p test\n= capture { 'foo' }" + result = render(content_to_render, :ugly => true) + expected_result = "

test

\nfoo\n" + assert_equal(expected_result, result) + end + def test_templates_should_render_correctly_with_render_proc assert_renders_correctly("standard") do |name| engine = Haml::Engine.new(File.read(File.dirname(__FILE__) + "/templates/#{name}.haml"), :format => :xhtml)