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

Conversation

jrochkind
Copy link

What are you trying to accomplish?

Fixes #2422, and allows render_inline in test to make Nokogiri HTML5 fragments that would otherwise be illegal based on lack of parent context, such as a straight <td> or <tr>

What approach did you choose and why?

Created ticket at nokogiri, sparklemotion/nokogiri#3536, and while the response was that nokogiri was working as expected, as HTML5 rules don't allow context-free parsing generally, and can't parse a (eg) <td> or <tr> without a parent <table>.

However, responder @stevecheckoway, graciously helped us think through it and realize that the (hard to find documented!) context: argument, when passed "template", would trigger HTML5 parsing rules that would allow closer to the arbitrary fragments we are used to with HTML4.

Steve suggested that in retrospect perhaps context: 'template' should have been the default for fragments, instead of "body" which is in fact the default. It's unclear why the context arg hasn't made it into generated nokogiri docs at all!

It's clear to me that this is the right move, and I can't think of any problems or even backwards incompatibilites it would cause.

Anything you want to highlight for special attention from reviewers?

Confirmed new test failed without change.

@jrochkind
Copy link
Author

I don't understand the test failures, I don't think they have to do with me?

@martinechtner
Copy link

Looks like the memory allocations went up slightly with this change. Could update those specs to increase the range to fix the issues.

@jrochkind jrochkind force-pushed the render_inline_nokogiri_contextless branch from 6bc84f0 to 97c8209 Compare August 18, 2025 15:33
@jrochkind
Copy link
Author

jrochkind commented Aug 18, 2025

I realized this may not be a good fix, because context can't hold <body> tags, which some ViewComponents may have?

Not sure if there's any way to use Nokogiri::HTML5 fragment API that isn't awful? Not really sure what to do.

Ah, but including body is broken already anyway in current implementation, since also illegal in default body context, so this is still an improvement I think.

@jrochkind jrochkind force-pushed the render_inline_nokogiri_contextless branch 2 times, most recently from b6b2760 to 55c4ce2 Compare August 18, 2025 16:25
@jrochkind jrochkind force-pushed the render_inline_nokogiri_contextless branch from 55c4ce2 to 5f82c72 Compare August 18, 2025 16:27
@jrochkind
Copy link
Author

OK, fixed memory allocation count -- one of the counts for 3.5 went down, which is weird, but ok!

Failing "primer compatibility" for reasons that I suspect are unrelated to this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

version 4.0.0 render_inline in specs issues
2 participants