Skip to content

Commit eacb45a

Browse files
authored
Merge pull request #2584 from ViewComponent/copilot/fix-render-in-html-safe-output
Fix `render_in` returning non-html_safe string when `render?` is false
2 parents ad17edd + 8733dad commit eacb45a

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 6
1010

1111
## main
1212

13+
* Return `html_safe` empty string from `render_in` when `render?` is false.
14+
15+
*Copilot*
16+
1317
## 4.5.0
1418

1519
* Fix initialization ordering issue causing missing asset errors in Sprockets.

lib/view_component/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def render_in(view_context, &block)
160160

161161
value
162162
else
163-
""
163+
"".html_safe
164164
end
165165
ensure
166166
view_context.instance_variable_set(:@virtual_path, @old_virtual_path)

test/sandbox/test/rendering_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,12 @@ def test_does_not_render_passed_in_content_if_render_is_false
877877
assert total < 1
878878
end
879879

880+
def test_render_in_returns_html_safe_string_when_render_is_false
881+
render_inline(ConditionalRenderComponent.new(should_render: false))
882+
883+
assert_predicate @rendered_content, :html_safe?
884+
end
885+
880886
def test_collection_parameter_does_not_require_compile
881887
dynamic_component =
882888
Class.new(ViewComponent::Base) do

0 commit comments

Comments
 (0)