Skip to content

using context:'template' for Nokogiri::HTML5.fragment allows parsing of otherwise illegal fragments like <td> #2426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <td>)

*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`.
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/sandbox/app/components/table_contents_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class TableContentsComponent < ViewComponent::Base
def call
"<td>td contents</td>".html_safe
end
end
8 changes: 6 additions & 2 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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>td contents</td>"
end

def test_render_inline_sets_rendered_content
render_inline(MyComponent.new)

Expand Down
Loading