diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index e6bb654e2..ef9a53ee4 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -10,6 +10,10 @@ nav_order: 6 ## main +* Allow render_inline with Nokogiri::HTML5 to parse more arbitrary content including bare table content otherwise illegal fragments like ) + + *Jonathan Rochkind* + ## 4.0.2 * Share the view context in tests to prevent out-of-order rendering issues for certain advanced use-cases, eg. testing instances of Rails' `FormBuilder`. diff --git a/lib/view_component/test_helpers.rb b/lib/view_component/test_helpers.rb index e3de34f1b..63d4f12fc 100644 --- a/lib/view_component/test_helpers.rb +++ b/lib/view_component/test_helpers.rb @@ -40,7 +40,7 @@ def render_inline(component, **args, &block) @page = nil @rendered_content = vc_test_view_context.render(component, args, &block) - fragment = Nokogiri::HTML5.fragment(@rendered_content) + fragment = Nokogiri::HTML5.fragment(@rendered_content, context: "template") @vc_test_view_context = nil fragment end diff --git a/test/sandbox/app/components/table_contents_component.rb b/test/sandbox/app/components/table_contents_component.rb new file mode 100644 index 000000000..9c20afaa5 --- /dev/null +++ b/test/sandbox/app/components/table_contents_component.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class TableContentsComponent < ViewComponent::Base + def call + "td contents".html_safe + end +end diff --git a/test/sandbox/test/rendering_test.rb b/test/sandbox/test/rendering_test.rb index b37bbffe4..90ed2dfaa 100644 --- a/test/sandbox/test/rendering_test.rb +++ b/test/sandbox/test/rendering_test.rb @@ -20,7 +20,7 @@ def test_render_inline_allocations MyComponent.__vc_ensure_compiled with_instrumentation_enabled_option(false) do - assert_allocations({"3.5" => 67, "3.4" => 74, "3.3" => 72, "3.2" => 75..76}) do + assert_allocations({"3.5" => 67, "3.4" => 74, "3.3" => 75, "3.2" => 77..79}) do render_inline(MyComponent.new) end end @@ -34,7 +34,7 @@ def test_render_collection_inline_allocations ViewComponent::CompileCache.cache.delete(ProductComponent) ProductComponent.__vc_ensure_compiled - allocations = {"3.5" => 66, "3.4" => 82, "3.3" => 86, "3.2" => 89..90} + allocations = {"3.5" => 66..76, "3.4" => 82, "3.3" => 86..89, "3.2" => 90..92} products = [Product.new(name: "Radio clock"), Product.new(name: "Mints")] notice = "On sale" @@ -77,6 +77,10 @@ def test_render_inline_returns_nokogiri_fragment assert_includes render_inline(MyComponent.new).css("div").to_html, "hello,world!" end + def test_render_inline_handles_table_contents + assert_includes render_inline(TableContentsComponent.new).css("td").to_html, "td contents" + end + def test_render_inline_sets_rendered_content render_inline(MyComponent.new)