Update Action Text to use HTML5 when available

The change from `#clone` to `#dup` is necessary to work around an
issue in Nokogiri where `#clone` is not defined properly for HTML5
fragment and the fragment does not have a parent Document. `#dup`
behaves the way we expect, so this should be fine.
This commit is contained in:
Mike Dalessio 2023-06-19 14:32:07 -04:00
parent 676fdb1414
commit 55bca6b8e8
No known key found for this signature in database
GPG Key ID: 6291CAA755BBD80D
6 changed files with 20 additions and 6 deletions

View File

@ -4,7 +4,7 @@ require "rails-html-sanitizer"
module ActionText
module ContentHelper
mattr_accessor(:sanitizer) { Rails::Html::Sanitizer.safe_list_sanitizer.new }
mattr_accessor(:sanitizer) { Rails::Html::Sanitizer.best_supported_vendor.safe_list_sanitizer.new }
mattr_accessor(:allowed_tags) { sanitizer.class.allowed_tags + [ ActionText::Attachment.tag_name, "figure", "figcaption" ] }
mattr_accessor(:allowed_attributes) { sanitizer.class.allowed_attributes + ActionText::Attachment::ATTRIBUTES }
mattr_accessor(:scrubber)

View File

@ -42,4 +42,18 @@ module ActionText
autoload :Minification
autoload :TrixConversion
end
class << self
def html_document_class
return @html_document_class if defined?(@html_document_class)
@html_document_class =
defined?(Nokogiri::HTML5) ? Nokogiri::HTML5::Document : Nokogiri::HTML4::Document
end
def html_document_fragment_class
return @html_document_fragment_class if defined?(@html_document_fragment_class)
@html_document_fragment_class =
defined?(Nokogiri::HTML5) ? Nokogiri::HTML5::DocumentFragment : Nokogiri::HTML4::DocumentFragment
end
end
end

View File

@ -7,7 +7,7 @@ module ActionText
case fragment_or_html
when self
fragment_or_html
when Nokogiri::HTML::DocumentFragment
when Nokogiri::XML::DocumentFragment # base class for all fragments
new(fragment_or_html)
else
from_html(fragment_or_html)
@ -30,7 +30,7 @@ module ActionText
end
def update
yield source = self.source.clone
yield source = self.source.dup
self.class.new(source)
end

View File

@ -18,7 +18,7 @@ module ActionText
private
def document
Nokogiri::HTML::Document.new.tap { |doc| doc.encoding = "UTF-8" }
ActionText.html_document_class.new.tap { |doc| doc.encoding = "UTF-8" }
end
end
end

View File

@ -20,7 +20,7 @@ class ActionText::ControllerRenderTest < ActionDispatch::IntegrationTest
host! "loocalhoost"
get message_path(message, format: :json)
content = Nokogiri::HTML::DocumentFragment.parse(response.parsed_body["content"])
content = ActionText.html_document_fragment_class.parse(response.parsed_body["content"])
assert_select content, "img:match('src', ?)", %r"//loocalhoost/.+/racecar"
end

View File

@ -18,7 +18,7 @@ class ActionText::JobRenderTest < ActiveJob::TestCase
perform_enqueued_jobs
end
rendered = Nokogiri::HTML::DocumentFragment.parse(File.read(file))
rendered = ActionText.html_document_fragment_class.parse(File.read(file))
assert_select rendered, "img:match('src', ?)", %r"//foo.example.com:9001/.+/racecar"
end
end