Sha256: c1b356c43f34210b4702db6a59d4053ae634f8e223db521a62db0b35a264700e

Contents?: true

Size: 1.62 KB

Versions: 37

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module ActionText
  class AttachmentGallery
    include ActiveModel::Model

    class << self
      def fragment_by_canonicalizing_attachment_galleries(content)
        fragment_by_replacing_attachment_gallery_nodes(content) do |node|
          "<#{TAG_NAME}>#{node.inner_html}</#{TAG_NAME}>"
        end
      end

      def fragment_by_replacing_attachment_gallery_nodes(content)
        Fragment.wrap(content).update do |source|
          find_attachment_gallery_nodes(source).each do |node|
            node.replace(yield(node).to_s)
          end
        end
      end

      def find_attachment_gallery_nodes(content)
        Fragment.wrap(content).find_all(SELECTOR).select do |node|
          node.children.all? do |child|
            if child.text?
              /\A(\n|\ )*\z/.match?(child.text)
            else
              child.matches? ATTACHMENT_SELECTOR
            end
          end
        end
      end

      def from_node(node)
        new(node)
      end
    end

    attr_reader :node

    def initialize(node)
      @node = node
    end

    def attachments
      @attachments ||= node.css(ATTACHMENT_SELECTOR).map do |node|
        ActionText::Attachment.from_node(node).with_full_attributes
      end
    end

    def size
      attachments.size
    end

    def inspect
      "#<#{self.class.name} size=#{size.inspect}>"
    end

    TAG_NAME = "div"
    ATTACHMENT_SELECTOR = "#{ActionText::Attachment::SELECTOR}[presentation=gallery]"
    SELECTOR = "#{TAG_NAME}:has(#{ATTACHMENT_SELECTOR} + #{ATTACHMENT_SELECTOR})"

    private_constant :TAG_NAME, :ATTACHMENT_SELECTOR, :SELECTOR
  end
end

Version data entries

37 entries across 37 versions & 4 rubygems

Version Path
actiontext-6.1.7.10 lib/action_text/attachment_gallery.rb
actiontext-6.1.7.9 lib/action_text/attachment_gallery.rb
actiontext-6.1.7.8 lib/action_text/attachment_gallery.rb
actiontext-6.1.7.7 lib/action_text/attachment_gallery.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actiontext-6.1.6.1/lib/action_text/attachment_gallery.rb
actiontext-6.1.7.6 lib/action_text/attachment_gallery.rb
actiontext-6.1.7.5 lib/action_text/attachment_gallery.rb
actiontext-6.1.7.4 lib/action_text/attachment_gallery.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actiontext-6.1.6.1/lib/action_text/attachment_gallery.rb
actiontext-6.1.7.3 lib/action_text/attachment_gallery.rb
actiontext-6.1.7.2 lib/action_text/attachment_gallery.rb
actiontext-6.1.7.1 lib/action_text/attachment_gallery.rb
actiontext-6.1.7 lib/action_text/attachment_gallery.rb
actiontext-6.1.6.1 lib/action_text/attachment_gallery.rb
actiontext-6.1.6 lib/action_text/attachment_gallery.rb
actiontext-6.1.5.1 lib/action_text/attachment_gallery.rb
actiontext-6.1.5 lib/action_text/attachment_gallery.rb
actiontext-6.1.4.7 lib/action_text/attachment_gallery.rb
actiontext-6.1.4.6 lib/action_text/attachment_gallery.rb
actiontext-6.1.4.5 lib/action_text/attachment_gallery.rb