Skip to content

Commit

Permalink
Update Action Text to use HTML5 when available
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
flavorjones committed Jun 19, 2023
1 parent 676fdb1 commit 55bca6b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion actiontext/app/helpers/action_text/content_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions actiontext/lib/action_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions actiontext/lib/action_text/fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion actiontext/lib/action_text/html_conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion actiontext/test/integration/controller_render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion actiontext/test/integration/job_render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 55bca6b

Please sign in to comment.