Sha256: 2b996f3c2ac5848296f29a9030ed14925cde3822ab2344fc195bf5a77dae8371

Contents?: true

Size: 1.96 KB

Versions: 38

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

require "rails-html-sanitizer"

module ActionText
  module ContentHelper
    mattr_accessor(:sanitizer) { Rails::Html::Sanitizer.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)

    def render_action_text_content(content)
      self.prefix_partial_path_with_controller_namespace = false
      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|
        unless attachment.in?(content.gallery_attachments)
          attachment.node.tap do |node|
            node.inner_html = render_action_text_attachment attachment, locals: { in_gallery: false }
          end
        end
      end.render_attachment_galleries do |attachment_gallery|
        render(layout: attachment_gallery, object: attachment_gallery) do
          attachment_gallery.attachments.map do |attachment|
            attachment.node.inner_html = render_action_text_attachment attachment, locals: { in_gallery: true }
            attachment.to_html
          end.join.html_safe
        end.chomp
      end
    end

    def render_action_text_attachment(attachment, locals: {}) # :nodoc:
      options = { locals: locals, object: attachment, partial: attachment }

      if attachment.respond_to?(:to_attachable_partial_path)
        options[:partial] = attachment.to_attachable_partial_path
      end

      if attachment.respond_to?(:model_name)
        options[:as] = attachment.model_name.element
      end

      render(**options).chomp
    end
  end
end

Version data entries

38 entries across 36 versions & 4 rubygems

Version Path
actiontext-7.0.8.7 app/helpers/action_text/content_helper.rb
actiontext-7.0.8.6 app/helpers/action_text/content_helper.rb
actiontext-7.0.8.5 app/helpers/action_text/content_helper.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actiontext-7.0.8.4/app/helpers/action_text/content_helper.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actiontext-7.0.5.1/app/helpers/action_text/content_helper.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actiontext-7.0.5.1/app/helpers/action_text/content_helper.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actiontext-7.0.5.1/app/helpers/action_text/content_helper.rb
actiontext-7.0.8.4 app/helpers/action_text/content_helper.rb
actiontext-7.0.8.1 app/helpers/action_text/content_helper.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actiontext-7.0.3.1/app/helpers/action_text/content_helper.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actiontext-7.0.2.3/app/helpers/action_text/content_helper.rb
actiontext-7.0.8 app/helpers/action_text/content_helper.rb
actiontext-7.0.7.2 app/helpers/action_text/content_helper.rb
actiontext-7.0.7.1 app/helpers/action_text/content_helper.rb
actiontext-7.0.7 app/helpers/action_text/content_helper.rb
actiontext-7.0.6 app/helpers/action_text/content_helper.rb
actiontext-7.0.5.1 app/helpers/action_text/content_helper.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actiontext-7.0.2.3/app/helpers/action_text/content_helper.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actiontext-7.0.3.1/app/helpers/action_text/content_helper.rb
actiontext-7.0.5 app/helpers/action_text/content_helper.rb