Skip to content

Commit bc45da7

Browse files
authored
Merge pull request #2426 from jrochkind/render_inline_nokogiri_contextless
using context:'template' for Nokogiri::HTML5.fragment allows parsing of otherwise illegal fragments like <td>
2 parents 78e7e84 + 9f1f3da commit bc45da7

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
* Remove known issue from docs as ActiveScaffold is [now compatible](https://github.com/activescaffold/active_scaffold/pull/743) with ViewComponent.
1418

1519
*David Löwenfels*

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" => 72..74, "3.3" => 72, "3.2" => 75..76}) do
23+
assert_allocations({"3.5" => 67, "3.4" => 72..74, "3.3" => 75, "3.2" => 78..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" => 70..82, "3.3" => 86, "3.2" => 89..90}
37+
allocations = {"3.5" => 66, "3.4" => 70..82, "3.3" => 89, "3.2" => 92..93}
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)