Skip to content

Commit 6bc84f0

Browse files
committed
using context:'template' for Nokogiri::HTML5.fragment allows parsing of otherwise illegal fragments like <td>
1 parent 1ed16e3 commit 6bc84f0

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-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+
* Allow render_inline with Nokogiri::HTML5 to parse more arbitrary content including bare table content
14+
15+
*Jonathan Rochkind*
16+
1317
## 4.0.1
1418

1519
* Setup Trusted Publishing to RubyGems to improve software supply chain safety.

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_controller.view_context.render(component, args, &block)
4242

43-
Nokogiri::HTML5.fragment(@rendered_content)
43+
Nokogiri::HTML5.fragment(@rendered_content, context: "template")
4444
end
4545

4646
# `JSON.parse`-d component output.
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)