2018-10-08 17:44:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-06 20:25:28 -05:00
|
|
|
require "rails-html-sanitizer"
|
|
|
|
|
2018-10-03 14:41:47 -04:00
|
|
|
module ActionText
|
|
|
|
module ContentHelper
|
2019-05-14 00:09:39 -04:00
|
|
|
mattr_accessor(:sanitizer) { Rails::Html::Sanitizer.safe_list_sanitizer.new }
|
2019-04-22 10:08:15 -04:00
|
|
|
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)
|
2018-10-06 10:27:54 -04:00
|
|
|
|
2018-10-03 14:41:47 -04:00
|
|
|
def render_action_text_content(content)
|
2020-09-11 12:43:27 -04:00
|
|
|
self.prefix_partial_path_with_controller_namespace = false
|
2019-04-22 10:08:15 -04:00
|
|
|
sanitize_action_text_content(render_action_text_attachments(content))
|
|
|
|
end
|
|
|
|
|
|
|
|
def sanitize_action_text_content(content)
|
|
|
|
sanitizer.sanitize(content.to_html, tags: allowed_tags, attributes: allowed_attributes, scrubber: scrubber).html_safe
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_action_text_attachments(content)
|
|
|
|
content.render_attachments do |attachment|
|
2018-10-03 14:41:47 -04:00
|
|
|
unless attachment.in?(content.gallery_attachments)
|
|
|
|
attachment.node.tap do |node|
|
|
|
|
node.inner_html = render(attachment, in_gallery: false).chomp
|
|
|
|
end
|
|
|
|
end
|
2019-04-22 10:08:15 -04:00
|
|
|
end.render_attachment_galleries do |attachment_gallery|
|
2018-10-03 14:41:47 -04:00
|
|
|
render(layout: attachment_gallery, object: attachment_gallery) do
|
|
|
|
attachment_gallery.attachments.map do |attachment|
|
|
|
|
attachment.node.inner_html = render(attachment, in_gallery: true).chomp
|
|
|
|
attachment.to_html
|
2019-08-23 14:11:50 -04:00
|
|
|
end.join.html_safe
|
2018-10-03 14:41:47 -04:00
|
|
|
end.chomp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|