Skip to content

Fix bug where rendering Slot with empty block resulted in error. #418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# master

# 2.17.1

* Fix bug where rendering Slot with empty block resulted in error.

*Joel Hawksley*

# 2.17.0

* Slots return stripped HTML, removing leading and trailing whitespace.
Expand Down
2 changes: 1 addition & 1 deletion coverage/coverage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Incomplete test coverage
/lib/view_component/preview.rb: 94.0% (missed: 47,57,107)
/lib/view_component/render_to_string_monkey_patch.rb: 83.33% (missed: 9)
/lib/view_component/test_helpers.rb: 91.67% (missed: 17,25)
/test/view_component/integration_test.rb: 96.73% (missed: 14,15,16,18,20,370,371,372,374)
/test/view_component/integration_test.rb: 96.76% (missed: 14,15,16,18,20,376,377,378,380)
2 changes: 1 addition & 1 deletion lib/view_component/slotable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def slot(slot_name, **args, &block)
slot_instance = args.present? ? slot_class.new(**args) : slot_class.new

# Capture block and assign to slot_instance#content
slot_instance.content = view_context.capture(&block).strip.html_safe if block_given?
slot_instance.content = view_context.capture(&block).to_s.strip.html_safe if block_given?

if slot[:collection]
# Initialize instance variable as an empty array
Expand Down
11 changes: 11 additions & 0 deletions test/app/components/empty_slot_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class EmptySlotComponent < ViewComponent::Base
include ViewComponent::Slotable

with_slot :title

def call
title.content
end
end
4 changes: 4 additions & 0 deletions test/app/views/integration_examples/empty_slot.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= render(EmptySlotComponent.new) do |component| %>
<% component.slot(:title) do %>
<% end %>
<% end %>
1 change: 1 addition & 0 deletions test/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
root to: "integration_examples#index"
get :content_areas, to: "integration_examples#content_areas"
get :slots, to: "integration_examples#slots"
get :empty_slot, to: "integration_examples#empty_slot"
get :partial, to: "integration_examples#partial"
get :content, to: "integration_examples#content"
get :variants, to: "integration_examples#variants"
Expand Down
6 changes: 6 additions & 0 deletions test/view_component/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ class IntegrationTest < ActionDispatch::IntegrationTest
assert_equal(title_node, expected_title_html)
end

test "renders empty slot without error" do
get "/empty_slot"

assert_response :success
end

if Rails.version.to_f >= 6.1
test "rendering component using the render_component helper raises an error" do
error = assert_raises ActionView::Template::Error do
Expand Down