diff --git a/actiontext/app/helpers/action_text/content_helper.rb b/actiontext/app/helpers/action_text/content_helper.rb index e63bd781df..0980bf0f00 100644 --- a/actiontext/app/helpers/action_text/content_helper.rb +++ b/actiontext/app/helpers/action_text/content_helper.rb @@ -4,7 +4,7 @@ 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) diff --git a/actiontext/lib/action_text.rb b/actiontext/lib/action_text.rb index d6038a939f..6d6b1126ad 100644 --- a/actiontext/lib/action_text.rb +++ b/actiontext/lib/action_text.rb @@ -42,4 +42,18 @@ module Attachments 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 diff --git a/actiontext/lib/action_text/fragment.rb b/actiontext/lib/action_text/fragment.rb index 129344f90f..21a6897b8f 100644 --- a/actiontext/lib/action_text/fragment.rb +++ b/actiontext/lib/action_text/fragment.rb @@ -7,7 +7,7 @@ def wrap(fragment_or_html) 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 @@ def find_all(selector) end def update - yield source = self.source.clone + yield source = self.source.dup self.class.new(source) end diff --git a/actiontext/lib/action_text/html_conversion.rb b/actiontext/lib/action_text/html_conversion.rb index 1e1062ea3f..bef7730640 100644 --- a/actiontext/lib/action_text/html_conversion.rb +++ b/actiontext/lib/action_text/html_conversion.rb @@ -18,7 +18,7 @@ def create_element(tag_name, attributes = {}) 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 diff --git a/actiontext/test/integration/controller_render_test.rb b/actiontext/test/integration/controller_render_test.rb index c9d8817d8f..afedaae17c 100644 --- a/actiontext/test/integration/controller_render_test.rb +++ b/actiontext/test/integration/controller_render_test.rb @@ -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 diff --git a/actiontext/test/integration/job_render_test.rb b/actiontext/test/integration/job_render_test.rb index 86955b3794..d0f4211226 100644 --- a/actiontext/test/integration/job_render_test.rb +++ b/actiontext/test/integration/job_render_test.rb @@ -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