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

*Jonathan Rochkind*

## 4.0.1

* Setup Trusted Publishing to RubyGems to improve software supply chain safety.
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_controller.view_context.render(component, args, &block)

Nokogiri::HTML5.fragment(@rendered_content)
Nokogiri::HTML5.fragment(@rendered_content, context: "template")
end

# `JSON.parse`-d component output.
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
4 changes: 4 additions & 0 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
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