class Slacken::DocumentComponent

Public: An intermediate object that is used when a HTML source is translated into a MarkupElement

representing structure of a markup text.
A DocumentComponent has tree structure and has child nodes as `children`.

Constants

NORMALIZE_FILTERS

Attributes

attrs[R]
children[R]
marks[R]
type[R]

Public Class Methods

build_by_html(html_source) click to toggle source

Public: Parse a html source with nokogiri and create a component.

html_source - A String or IO.

Returns a DocumentComponent or nil.

# File lib/slacken/document_component.rb, line 35
def self.build_by_html(html_source)
  DomContainer.new(Nokogiri::HTML(html_source)).to_component
end
new(type, children = [], attrs = {}) click to toggle source
# File lib/slacken/document_component.rb, line 39
def initialize(type, children = [], attrs = {})
  @type = NodeType.create(type)
  @attrs = attrs
  @children = children
  @marks = {}
end

Public Instance Methods

==(other) click to toggle source
# File lib/slacken/document_component.rb, line 73
def ==(other)
  other.instance_of?(self.class) &&
    type.name == other.type.name &&
    attrs == other.attrs &&
    marks == other.marks &&
    children == other.children
end
derive(new_children, updates = {}) click to toggle source
# File lib/slacken/document_component.rb, line 46
def derive(new_children, updates = {})
  self.class.new(
    updates[:type] || type,
    new_children,
    updates[:attrs] || attrs
  )
end
normalize() click to toggle source
# File lib/slacken/document_component.rb, line 59
def normalize
  NORMALIZE_FILTERS.reduce(self) do |component, filter_klass|
    filter_klass.new.call(component)
  end
end
produce_element() click to toggle source
# File lib/slacken/document_component.rb, line 65
def produce_element
  if type.member_of?(:table)
    TableElement.new(children.map(&:produce_element))
  else
    RenderElement.new(type, children.map(&:produce_element), attrs)
  end
end
to_element() click to toggle source

Public: Normalize this object's structure and convert it to MarkupElement.

# File lib/slacken/document_component.rb, line 55
def to_element
  normalize.produce_element
end