Skip to content

Commit

Permalink
Fix #capture when run with a block that returns a string.
Browse files Browse the repository at this point in the history
Closes hamlgh-387.
  • Loading branch information
nex3 committed May 10, 2011
1 parent abfd8d1 commit 10109d4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions doc-src/HAML_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
* Table of contents
{:toc}

## 3.1.2 (Unreleased)

* If the ActionView `#capture` helper is used in a Haml template but without any Haml being run in the block,
return the value of the block rather than the captured buffer.

## 3.1.1

* Update the vendored Sass to version 3.1.0.
Expand Down
16 changes: 14 additions & 2 deletions lib/haml/helpers/action_view_mods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ def capture_with_haml(*args, &block)
# We've got to do the same thing for compatibility.

if is_haml? && block_is_haml?(block)
capture_haml(*args, &block)
value = nil
buffer = capture_haml(*args) { value = yield(*args) }
return buffer unless buffer.empty?
return value if value.is_a?(String)
else
capture_without_haml(*args, &block)
end
Expand Down Expand Up @@ -85,7 +88,16 @@ def concat_with_haml(string, binding = nil)
module CaptureHelper
def capture_with_haml(*args, &block)
if Haml::Helpers.block_is_haml?(block)
str = capture_haml(*args, &block)
value = nil
buffer = capture_haml(*args) { value = yield(*args) }
str =
if !buffer.empty?
buffer
elsif value.is_a?(String)
value
else
''
end
return ActionView::NonConcattingString.new(str) if defined?(ActionView::NonConcattingString)
return str
else
Expand Down
4 changes: 4 additions & 0 deletions test/haml/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ def trc(collection, &block)
assert_equal("1\n\n2\n\n3\n\n", render("- trc([1, 2, 3]) do |i|\n = i.inspect"))
end

def test_capture_with_string_block
assert_equal("foo\n", render("= capture { 'foo' }", :action_view))
end

def test_find_and_preserve_with_block
assert_equal("<pre>Foo&#x000A;Bar</pre>\nFoo\nBar\n",
render("= find_and_preserve do\n %pre\n Foo\n Bar\n Foo\n Bar"))
Expand Down

0 comments on commit 10109d4

Please sign in to comment.