Skip to content

Commit 2239749

Browse files
authored
Merge pull request rails#49612 from Shopify/action-view-capture-nil-not-blank
Fix `capture` view helper for HAML and Slim
2 parents 1705fa2 + 79a242d commit 2239749

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

actionview/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Fix the `capture` view helper compatibility with HAML and Slim
2+
3+
When a blank string was captured in HAML or Slim (and possibly other template engines)
4+
it would instead return the entire buffer.
5+
6+
*Jean Boussier*
7+
18
* Updated `@rails/ujs` files to ignore certain data-* attributes when element is contenteditable.
29

310
This fix was already landed in >= 7.0.4.3, < 7.1.0.

actionview/lib/action_view/helpers/capture_helper.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ def capture(*args, &block)
4949
@output_buffer ||= ActionView::OutputBuffer.new
5050
buffer = @output_buffer.capture { value = yield(*args) }
5151

52-
case string = buffer.presence || value
52+
string = if @output_buffer.equal?(value)
53+
buffer
54+
else
55+
buffer.presence || value
56+
end
57+
58+
case string
5359
when OutputBuffer
5460
string.to_s
5561
when ActiveSupport::SafeBuffer

actionview/test/template/capture_helper_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ def test_with_output_buffer_does_not_assume_there_is_an_output_buffer
230230
assert_equal "", @av.with_output_buffer { }.to_s
231231
end
232232

233+
def test_ignore_the_block_return_if_its_the_buffer
234+
@av.output_buffer << "something"
235+
string = @av.capture do
236+
@av.output_buffer << "foo"
237+
@av.output_buffer << "bar"
238+
@av.output_buffer
239+
end
240+
assert_equal "foobar", string
241+
end
242+
233243
def alt_encoding(output_buffer)
234244
output_buffer.encoding == Encoding::US_ASCII ? Encoding::UTF_8 : Encoding::US_ASCII
235245
end

0 commit comments

Comments
 (0)