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

Update Action View and Action Dispatch to use HTML5 when available #48523

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,3 +1,10 @@
* `ActionDispatch::Assertions#html_document` uses Nokogiri's HTML5 parser if it is available.

The HTML5 parser better represents what the DOM would be in a browser. Previously this test
helper always used Nokogiri's HTML4 parser.

*Mike Dalessio*

* The `with_routing` helper can now be called at the class level. When called at the class level, the routes will
be setup before each test, and reset after every test. For example:

Expand Down
4 changes: 3 additions & 1 deletion actionpack/lib/action_dispatch/testing/assertions.rb
Expand Up @@ -15,8 +15,10 @@ module Assertions
def html_document
@html_document ||= if @response.media_type&.end_with?("xml")
Nokogiri::XML::Document.parse(@response.body)
elsif defined?(Nokogiri::HTML5)
Nokogiri::HTML5::Document.parse(@response.body)
else
Nokogiri::HTML::Document.parse(@response.body)
Nokogiri::HTML4::Document.parse(@response.body)
end
end
end
Expand Down
10 changes: 9 additions & 1 deletion actionview/lib/action_view/test_case.rb
Expand Up @@ -177,9 +177,17 @@ def _test_case
end

private
def html_document_class
defined?(Nokogiri::HTML5) ? Nokogiri::HTML5::Document : Nokogiri::HTML4::Document
end

def html_document_fragment_class
defined?(Nokogiri::HTML5) ? Nokogiri::HTML5::DocumentFragment : Nokogiri::HTML4::DocumentFragment
end

# Need to experiment if this priority is the best one: rendered => output_buffer
def document_root_element
Nokogiri::HTML::Document.parse(@rendered.blank? ? @output_buffer.to_str : @rendered).root
html_document_class.parse(@rendered.blank? ? @output_buffer.to_str : @rendered).root
end

module Locals
Expand Down
2 changes: 1 addition & 1 deletion actionview/test/template/form_tag_helper_test.rb
Expand Up @@ -986,7 +986,7 @@ def protect_against_forgery?

private
def root_elem(rendered_content)
Nokogiri::HTML::DocumentFragment.parse(rendered_content).children.first # extract from nodeset
html_document_fragment_class.parse(rendered_content).children.first # extract from nodeset
end

def with_default_enforce_utf8(value)
Expand Down