From 2475f7356e7b8e03e68bef65dae284e47779dfe7 Mon Sep 17 00:00:00 2001 From: Mircea Moise Date: Wed, 6 Nov 2013 20:51:07 +0000 Subject: [PATCH 1/2] Fix for capture_haml and blocks that return nothing Resolves #694 Conflicts: lib/haml/helpers.rb lib/haml/helpers/action_view_mods.rb --- lib/haml/helpers.rb | 35 +++++++++++++++++----------- lib/haml/helpers/action_view_mods.rb | 13 +++-------- test/template_test.rb | 23 ++++++++++++++++++ 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index e0b948b8..8befd899 100755 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -365,24 +365,17 @@ 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(/^/).reject {|x| x == ""} - 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 @@ -637,6 +630,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 77e481b5..d06fb11f 100644 --- a/test/template_test.rb +++ b/test/template_test.rb @@ -116,6 +116,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) From b17b42271d11b1d3f2007b6bed9ffd0126e9ea87 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Wed, 4 Dec 2013 15:09:07 -0300 Subject: [PATCH 2/2] Bump version --- CHANGELOG.md | 8 ++++++++ lib/haml/version.rb | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91a45e7a..0b178e69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ # Haml Changelog ======= +## 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 diff --git a/lib/haml/version.rb b/lib/haml/version.rb index ef7fa521..904eeb63 100644 --- a/lib/haml/version.rb +++ b/lib/haml/version.rb @@ -1,3 +1,3 @@ module Haml - VERSION = "4.0.4" + VERSION = "4.0.5" end