Skip to content

Commit 07bccc3

Browse files
committed
Fix a bug where capture could return nil when nothing was captured
It now returns an empty string which matches the behavior of `capture` in Phlex.
1 parent ea5b98d commit 07bccc3

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/phlex/rails/sgml/state.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def capture
1717
ensure
1818
@capturing = original_capturing
1919
@fragments = original_fragments
20-
end
20+
end || ""
2121
else
2222
super
2323
end

test/capture.test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
class Component < Phlex::HTML
4+
def view_template
5+
yield if block_given?
6+
end
7+
end
8+
9+
UNSET = Object.new.freeze
10+
11+
test "capturing nothing returns an empty string" do
12+
output = UNSET
13+
view_context = ActionController::Base.new.view_context
14+
15+
Component.render_in(view_context) do |c|
16+
output = c.capture do
17+
# Intentionally empty to capture nothing
18+
end
19+
end
20+
21+
assert_equal "", output
22+
end

0 commit comments

Comments
 (0)