diff --git a/CHANGELOG.md b/CHANGELOG.md index 03eb01a4d..0c11b6aab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/coverage/coverage.txt b/coverage/coverage.txt index 648271d93..f41d208e4 100644 --- a/coverage/coverage.txt +++ b/coverage/coverage.txt @@ -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) diff --git a/lib/view_component/slotable.rb b/lib/view_component/slotable.rb index a8552b6a9..bff6b5cab 100644 --- a/lib/view_component/slotable.rb +++ b/lib/view_component/slotable.rb @@ -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 diff --git a/test/app/components/empty_slot_component.rb b/test/app/components/empty_slot_component.rb new file mode 100644 index 000000000..705005271 --- /dev/null +++ b/test/app/components/empty_slot_component.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class EmptySlotComponent < ViewComponent::Base + include ViewComponent::Slotable + + with_slot :title + + def call + title.content + end +end diff --git a/test/app/views/integration_examples/empty_slot.html.erb b/test/app/views/integration_examples/empty_slot.html.erb new file mode 100644 index 000000000..e0e6ef7ae --- /dev/null +++ b/test/app/views/integration_examples/empty_slot.html.erb @@ -0,0 +1,4 @@ +<%= render(EmptySlotComponent.new) do |component| %> + <% component.slot(:title) do %> + <% end %> +<% end %> diff --git a/test/config/routes.rb b/test/config/routes.rb index 39b89207d..3cbb22f43 100644 --- a/test/config/routes.rb +++ b/test/config/routes.rb @@ -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" diff --git a/test/view_component/integration_test.rb b/test/view_component/integration_test.rb index fdbacafdc..59ea70fd6 100644 --- a/test/view_component/integration_test.rb +++ b/test/view_component/integration_test.rb @@ -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