Skip to content
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

Parse ActionView::TestCase#rendered as DocumentFragment #50584

Merged

Conversation

seanpdoyle
Copy link
Contributor

@seanpdoyle seanpdoyle commented Jan 4, 2024

Motivation / Background

To integrate with rails-dom-testing and its selector assertions, ActionView::TestCase defines a #document_root_element method that parses the HTML into a fully valid HTML document and returns the "root".

In the case of most Action View partials rendered with render partial: "...", the resulting document would be invalid, so its constituent parts (its <html>, <head>, and <body> elements) are synthesized during the parsing process. This results in a document whose contents are equivalent to the original HTML string, but whose structure is not.

To share a concrete example:

irb(main):002:0> rendered = "<h1>Hello world</h1><h2>Goodbye world</h2>"
=> "<h1>Hello world</h1><h2>Goodbye world</h2>"
irb(main):003:0> root = Rails::Dom::Testing.html_document.parse(rendered).root
=>
#(Element:0x57080 {
...
irb(main):004:0> rendered.to_s
=> "<h1>Hello world</h1><h2>Goodbye world</h2>"
irb(main):005:0> root.to_s
=> "<html><head></head><body><h1>Hello world</h1><h2>Goodbye world</h2></body></html>"
irb(main):006:0> rendered.to_s == root.to_s
=> false

Prior to this commit, the parsed HTML content returned from calling rendered.html relied on the same mechanisms as
#document_root_element, and parsed the HTML fragment into a full document, with a synthesized <html> element as its root. The rendered.html value should reflect the content that was rendered by the partial, and should not behave the same as
#document_root_element.

When the parsing class is changed from Nokogiri::XML::Document to Nokogiri::XML::DocumentFragment, the returned value reflects the same exact content as what was rendered.

To elaborate on the previous example:

irb(main):007:0> fragment = Rails::Dom::Testing.html_document_fragment.parse(rendered)
=>
#(DocumentFragment:0x62ee4 {
...
irb(main):008:0> fragment.to_s
=> "<h1>Hello world</h1><h2>Goodbye world</h2>"
irb(main):009:0> rendered.to_s == fragment.to_s
=> true

Detail

This commit changes the default rendered.html behavior to rely on Nokogiri::XML::DocumentFragment instead of Nokogiri::XML::Document.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@rails-bot rails-bot bot added the actionview label Jan 4, 2024
To integrate with [rails-dom-testing][] and its selector assertions,
`ActionView::TestCase` [defines a `#document_root_element`
method][document_root_element] that parses the HTML into a fully valid
HTML document and returns the "root".

In the case of most Action View partials rendered with `render partial:
"..."`, the resulting document would be invalid, so its constituent
parts (its `<html>`, `<head>`, and `<body>` elements) are synthesized in
during the parsing process. This results in a document whose _contents_
are equivalent to the original HTML string, but whose structure is not.

To share a concrete example:

  ```ruby
  irb(main):002:0> rendered = "<h1>Hello world</h1><h2>Goodbye world</h2>"
  => "<h1>Hello world</h1><h2>Goodbye world</h2>"
  irb(main):003:0> root = Rails::Dom::Testing.html_document.parse(rendered).root
  =>
  #(Element:0x57080 {
  ...
  irb(main):004:0> rendered.to_s
  => "<h1>Hello world</h1><h2>Goodbye world</h2>"
  irb(main):005:0> root.to_s
  => "<html><head></head><body><h1>Hello world</h1><h2>Goodbye world</h2></body></html>"
  irb(main):006:0> rendered.to_s == root.to_s
  => false
  ```

Prior to this commit, the parsed HTML content returned from calling
`rendered.html` relied on the same mechanisms as
`#document_root_element`, and parsed the HTML fragment into a full
document, with a synthesized `<html>` element as its root. The
`rendered.html` value should reflect the content that was **rendered**
by the partial, and should not behave the same as
`#document_root_element`.

When the parsing class is changed from [Nokogiri::XML::Document][] to
[Nokogiri::XML::DocumentFragment][], the returned value reflects the
same **exact** content as what was rendered.

To elaborate on the previous example:

  ```ruby
  irb(main):007:0> fragment = Rails::Dom::Testing.html_document_fragment.parse(rendered)
  =>
  #(DocumentFragment:0x62ee4 {
  ...
  irb(main):008:0> fragment.to_s
  => "<h1>Hello world</h1><h2>Goodbye world</h2>"
  irb(main):009:0> rendered.to_s == fragment.to_s
  => true
  ```

This commit changes the default `rendered.html` behavior to rely on
`Nokogiri::XML::DocumentFragment` instead of `Nokogiri::XML::Document`.

[Nokogiri::XML::Document]: https://nokogiri.org/rdoc/Nokogiri/XML/Document.html
[Nokogiri::XML::DocumentFragment]: https://nokogiri.org/rdoc/Nokogiri/XML/DocumentFragment.html
[document_root_element]: https://github.com/rails/rails-dom-testing/blob/v2.2.0/lib/rails/dom/testing/assertions/selector_assertions.rb#L75
@seanpdoyle seanpdoyle force-pushed the action-view-rendered-html-parser branch from c491812 to bec8037 Compare January 4, 2024 19:50
@rafaelfranca rafaelfranca merged commit 64c9360 into rails:main Jan 4, 2024
3 of 4 checks passed
@seanpdoyle seanpdoyle deleted the action-view-rendered-html-parser branch January 5, 2024 01:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants