Skip to content

Commit 5f82c72

Browse files
committed
using context:'template' for Nokogiri::HTML5.fragment allows parsing of otherwise illegal fragments like <td>
1 parent 4bbea1c commit 5f82c72

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
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+
* Allow render_inline with Nokogiri::HTML5 to parse more arbitrary content including bare table content otherwise illegal fragments like <td>)
14+
15+
*Jonathan Rochkind*
16+
1317
## 4.0.2
1418

1519
* Share the view context in tests to prevent out-of-order rendering issues for certain advanced use-cases, eg. testing instances of Rails' `FormBuilder`.

lib/view_component/test_helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def render_inline(component, **args, &block)
4040
@page = nil
4141
@rendered_content = vc_test_view_context.render(component, args, &block)
4242

43-
fragment = Nokogiri::HTML5.fragment(@rendered_content)
43+
fragment = Nokogiri::HTML5.fragment(@rendered_content, context: "template")
4444
@vc_test_view_context = nil
4545
fragment
4646
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class TableContentsComponent < ViewComponent::Base
4+
def call
5+
"<td>td contents</td>".html_safe
6+
end
7+
end

test/sandbox/test/rendering_test.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_render_inline_allocations
2020
MyComponent.__vc_ensure_compiled
2121

2222
with_instrumentation_enabled_option(false) do
23-
assert_allocations({"3.5" => 67, "3.4" => 74, "3.3" => 72, "3.2" => 75..76}) do
23+
assert_allocations({"3.5" => 67, "3.4" => 74, "3.3" => 75, "3.2" => 77..79}) do
2424
render_inline(MyComponent.new)
2525
end
2626
end
@@ -34,7 +34,7 @@ def test_render_collection_inline_allocations
3434
ViewComponent::CompileCache.cache.delete(ProductComponent)
3535
ProductComponent.__vc_ensure_compiled
3636

37-
allocations = {"3.5" => 66, "3.4" => 82, "3.3" => 86, "3.2" => 89..90}
37+
allocations = {"3.5" => 66..76, "3.4" => 82, "3.3" => 86..89, "3.2" => 90..92}
3838

3939
products = [Product.new(name: "Radio clock"), Product.new(name: "Mints")]
4040
notice = "On sale"
@@ -77,6 +77,10 @@ def test_render_inline_returns_nokogiri_fragment
7777
assert_includes render_inline(MyComponent.new).css("div").to_html, "hello,world!"
7878
end
7979

80+
def test_render_inline_handles_table_contents
81+
assert_includes render_inline(TableContentsComponent.new).css("td").to_html, "<td>td contents</td>"
82+
end
83+
8084
def test_render_inline_sets_rendered_content
8185
render_inline(MyComponent.new)
8286

0 commit comments

Comments
 (0)