Update Action View and Action Dispatch to use HTML5 when available

This commit is contained in:
Mike Dalessio 2023-06-19 15:31:34 -04:00
parent 676fdb1414
commit 2362f57cd8
No known key found for this signature in database
GPG Key ID: 6291CAA755BBD80D
4 changed files with 20 additions and 3 deletions

@ -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:

@ -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

@ -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

@ -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)