Sha256: df9be38083531f8c90623888b5a007bc019801d6deb27fa6e41144e50028e26f

Contents?: true

Size: 1.66 KB

Versions: 53

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module ActionText
  class AttachmentGallery
    include ActiveModel::Model

    TAG_NAME = "div"
    private_constant :TAG_NAME

    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

      def attachment_selector
        "#{ActionText::Attachment.tag_name}[presentation=gallery]"
      end

      def selector
        "#{TAG_NAME}:has(#{attachment_selector} + #{attachment_selector})"
      end
    end

    attr_reader :node

    def initialize(node)
      @node = node
    end

    def attachments
      @attachments ||= node.css(ActionText::AttachmentGallery.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
  end
end

Version data entries

53 entries across 50 versions & 5 rubygems

Version Path
actiontext-7.1.5 lib/action_text/attachment_gallery.rb
actiontext-7.1.4.2 lib/action_text/attachment_gallery.rb
actiontext-7.0.8.6 lib/action_text/attachment_gallery.rb
actiontext-7.1.4.1 lib/action_text/attachment_gallery.rb
actiontext-7.0.8.5 lib/action_text/attachment_gallery.rb
actiontext-7.1.4 lib/action_text/attachment_gallery.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actiontext-7.1.3.4/lib/action_text/attachment_gallery.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actiontext-7.0.8.4/lib/action_text/attachment_gallery.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actiontext-7.0.5.1/lib/action_text/attachment_gallery.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actiontext-7.0.5.1/lib/action_text/attachment_gallery.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actiontext-7.0.5.1/lib/action_text/attachment_gallery.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/actiontext-7.1.3.4/lib/action_text/attachment_gallery.rb
actiontext-7.1.3.4 lib/action_text/attachment_gallery.rb
actiontext-7.0.8.4 lib/action_text/attachment_gallery.rb
actiontext-7.1.3.2 lib/action_text/attachment_gallery.rb
actiontext-7.1.3.1 lib/action_text/attachment_gallery.rb
actiontext-7.0.8.1 lib/action_text/attachment_gallery.rb
actiontext-7.1.3 lib/action_text/attachment_gallery.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actiontext-7.0.2.3/lib/action_text/attachment_gallery.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actiontext-7.0.3.1/lib/action_text/attachment_gallery.rb