Sha256: 3548428dff097e5bf17efcfe3a3a026d5c376537a2076542c4a39235c65a816f

Contents?: true

Size: 1.07 KB

Versions: 114

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module ActionText
  class Fragment
    class << self
      def wrap(fragment_or_html)
        case fragment_or_html
        when self
          fragment_or_html
        when Nokogiri::HTML::DocumentFragment
          new(fragment_or_html)
        else
          from_html(fragment_or_html)
        end
      end

      def from_html(html)
        new(ActionText::HtmlConversion.fragment_for_html(html.to_s.strip))
      end
    end

    attr_reader :source

    def initialize(source)
      @source = source
    end

    def find_all(selector)
      source.css(selector)
    end

    def update
      yield source = self.source.clone
      self.class.new(source)
    end

    def replace(selector)
      update do |source|
        source.css(selector).each do |node|
          node.replace(yield(node).to_s)
        end
      end
    end

    def to_plain_text
      @plain_text ||= PlainTextConversion.node_to_plain_text(source)
    end

    def to_html
      @html ||= HtmlConversion.node_to_html(source)
    end

    def to_s
      to_html
    end
  end
end

Version data entries

114 entries across 110 versions & 10 rubygems

Version Path
actiontext-7.0.8.6 lib/action_text/fragment.rb
actiontext-6.1.7.10 lib/action_text/fragment.rb
actiontext-6.1.7.9 lib/action_text/fragment.rb
actiontext-7.0.8.5 lib/action_text/fragment.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actiontext-7.0.8.4/lib/action_text/fragment.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actiontext-7.0.5.1/lib/action_text/fragment.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actiontext-7.0.5.1/lib/action_text/fragment.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actiontext-7.0.5.1/lib/action_text/fragment.rb
actiontext-7.0.8.4 lib/action_text/fragment.rb
actiontext-6.1.7.8 lib/action_text/fragment.rb
actiontext-7.0.8.1 lib/action_text/fragment.rb
actiontext-6.1.7.7 lib/action_text/fragment.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actiontext-7.0.2.3/lib/action_text/fragment.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actiontext-7.0.3.1/lib/action_text/fragment.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actiontext-6.1.6.1/lib/action_text/fragment.rb
actiontext-7.0.8 lib/action_text/fragment.rb
actiontext-7.0.7.2 lib/action_text/fragment.rb
actiontext-6.1.7.6 lib/action_text/fragment.rb
actiontext-7.0.7.1 lib/action_text/fragment.rb
actiontext-6.1.7.5 lib/action_text/fragment.rb