Skip to Content Skip to Search

class ActionText::Fragment

Attributes

[R] source

Public class methods

Source code GitHub
# File actiontext/lib/action_text/fragment.rb, line 19
def from_html(html)
  new(ActionText::HtmlConversion.fragment_for_html(html.to_s.strip))
end
Source code GitHub
# File actiontext/lib/action_text/fragment.rb, line 28
def initialize(source)
  @source = source
end
Source code GitHub
# File actiontext/lib/action_text/fragment.rb, line 8
def wrap(fragment_or_html)
  case fragment_or_html
  when self
    fragment_or_html
  when Nokogiri::XML::DocumentFragment # base class for all fragments
    new(fragment_or_html)
  else
    from_html(fragment_or_html)
  end
end

Public instance methods

Source code GitHub
# File actiontext/lib/action_text/fragment.rb, line 32
def find_all(selector)
  source.css(selector)
end
Source code GitHub
# File actiontext/lib/action_text/fragment.rb, line 41
def replace(selector)
  update do |source|
    source.css(selector).each do |node|
      replacement_node = yield(node)
      node.replace(replacement_node.to_s) if node != replacement_node
    end
  end
end
Source code GitHub
# File actiontext/lib/action_text/fragment.rb, line 54
def to_html
  @html ||= HtmlConversion.node_to_html(source)
end
Source code GitHub
# File actiontext/lib/action_text/fragment.rb, line 50
def to_plain_text
  @plain_text ||= PlainTextConversion.node_to_plain_text(source)
end
Source code GitHub
# File actiontext/lib/action_text/fragment.rb, line 58
def to_s
  to_html
end
Source code GitHub
# File actiontext/lib/action_text/fragment.rb, line 36
def update
  yield source = self.source.dup
  self.class.new(source)
end

Definition files