class Gammo::Node

Class for representing Node. html.spec.whatwg.org/multipage/parsing.html#tokenization

Constants

Comment

Represents the comment token like “<!– foo –>”.

DEFAULT_SCOPE_MARKER

Default scope marker is inserted when entering applet, object, marquee, template, td, th, and caption elements, and are used to prevent formatting from “leaking” into applet, object, marquee, template, td, th, and caption elements“

Doctype

Represents the document type token.

Error

Represents the error token.

HierarchyRequestError

Raised if anything goes wrong on hierarchy while node operations. @!visibility private

ScopeMarker

Represents the marker defined in 12.2.4.3. html.spec.whatwg.org/multipage/parsing.html#tokenization

UncaughtTypeError

Raised if uncaught node is given for particular operations. @!visibility private

Attributes

attributes[R]

Reader for attributes associated with this node.

data[RW]

Properties required to represent node.

first_child[RW]

`first_child` and `last_child` are pointers for the first and the last nodes.

last_child[RW]

`first_child` and `last_child` are pointers for the first and the last nodes.

namespace[RW]

Properties required to represent node.

next_sibling[RW]

`previous_sibling` and `next_sibling` are pointers for the previous and next sibling nodes.

parent[RW]

`parent` is the pointer for the parent node.

previous_sibling[RW]

`previous_sibling` and `next_sibling` are pointers for the previous and next sibling nodes.

tag[RW]

Properties required to represent node.

Public Class Methods

new(tag: nil, data: nil, namespace: nil, attributes: Attributes.new([])) click to toggle source

Constructs a node which represents HTML element node. @param [String] tag @param [String] data @param [String, NilClass] namespace @param [Gammo::Attributes] attributes @return [Gammo::Node]

# File lib/gammo/node.rb, line 112
def initialize(tag: nil, data: nil, namespace: nil, attributes: Attributes.new([]))
  @tag        = tag
  @data       = data
  @namespace  = namespace
  @attributes = Attributes.new(attributes, owner_element: self)
end

Public Instance Methods

append_child(child) click to toggle source

Appends given `child` into self node. @param [Gammo::Node] child @raise [HierarchyRequestError] Raised if given node is already attached to the self node. @return [Gammo::Node] A node appended into the self node.

# File lib/gammo/node.rb, line 160
def append_child(child)
  raise HierarchyRequestError,
    'append_child called for an attached child node' if attached?(child)
  if last = last_child
    last.next_sibling = child
  else
    @first_child = child
  end
  @last_child = child
  child.parent = self
  child.previous_sibling = last
  child
end
attributes=(attrs) click to toggle source

Sets attributes in self. @param [Gammo::Attributes] attrs

# File lib/gammo/node.rb, line 121
def attributes=(attrs)
  cloned = attrs.dup
  cloned.each { |attr| attr.owner_element = self }
  @attributes = cloned
end
children() click to toggle source
# File lib/gammo/node.rb, line 219
def children
  ret = []
  child = first_child
  while child
    ret << child
    child = child.next_sibling
  end
  ret
end
clone() click to toggle source

Clones self into a new node. @return [Gammo::Node] @!visibility private

# File lib/gammo/node.rb, line 192
def clone
  self.class.new(tag: self.tag, data: self.data, attributes: self.attributes.dup)
end
document?() click to toggle source
# File lib/gammo/node.rb, line 235
def document?
  self.instance_of?(Document)
end
each_descendant() { |node| ... } click to toggle source
# File lib/gammo/node.rb, line 42
def each_descendant
  stack = [self]
  until stack.empty?
    node = stack.pop
    yield node unless node == self
    stack << node.next_sibling if node != self && node.next_sibling
    stack << node.first_child if node.first_child
  end
end
get_attribute_node(key, namespace: nil) click to toggle source
# File lib/gammo/node.rb, line 38
def get_attribute_node(key, namespace: nil)
  attributes.find { |attr| attr.key == key && attr.namespace == namespace }
end
insert_before(node, ref) click to toggle source

Inserts a node before a reference node as a child of a specified parent node. @param [Gammo::Node] node @param [Gammo::Node] ref @raise [HierarchyRequestError] Raised if given node is already attached to the self node. @return [Gammo::Node] A node inserted before the reference node.

# File lib/gammo/node.rb, line 132
def insert_before(node, ref)
  raise HierarchyRequestError,
    'insert_before called for an attached child node' if attached?(node)
  if ref
    previous_sibling, next_sibling = ref.previous_sibling, ref
  else
    previous_sibling = last_child
  end
  if previous_sibling
    previous_sibling.next_sibling = node
  else
    @first_child = node
  end
  if next_sibling
    next_sibling.previous_sibling = node
  else
    @last_child = node
  end
  node.parent = self
  node.previous_sibling = previous_sibling
  node.next_sibling = next_sibling
  node
end
owner_document() click to toggle source
# File lib/gammo/node.rb, line 229
def owner_document
  node = self
  node = node.parent until node.document?
  node
end
remove_child(child) click to toggle source

Removes given `child` from self node. @param [Gammo::Node] child @raise [UncaughtTypeError] Raised unless given node is not child of the self node. @return [Gammo::Node] A node removed from the self node.

# File lib/gammo/node.rb, line 178
def remove_child(child)
  raise UncaughtTypeError,
    'remove_child called for a non-child node' unless child?(child)
  @first_child = child.next_sibling if first_child == child
  child.next_sibling.previous_sibling = child.previous_sibling if child.next_sibling
  @last_child = child.previous_sibling if last_child == child
  child.previous_sibling.next_sibling = child.next_sibling if child.previous_sibling
  child.parent = child.previous_sibling = child.next_sibling = nil
  child
end
select(&block) click to toggle source

Select all nodes whose the evaluation of a given block is true.

# File lib/gammo/node.rb, line 207
def select(&block)
  nodes = []
  stack = [self]
  until stack.empty?
    node = stack.pop
    nodes << node if block.call(node)
    stack << node.next_sibling if node.next_sibling
    stack << node.first_child if node.first_child
  end
  nodes
end
text_content() click to toggle source
# File lib/gammo/node.rb, line 34
def text_content
  nil
end
to_h() click to toggle source

@!visibility private

# File lib/gammo/node.rb, line 197
def to_h
  {
    tag: tag,
    data: data,
    attributes: attributes,
    type: self.class
  }
end

Private Instance Methods

attached?(node) click to toggle source

@!visibility private

# File lib/gammo/node.rb, line 242
def attached?(node)
  node.parent || node.previous_sibling || node.next_sibling
end
child?(node) click to toggle source

@!visibility private

# File lib/gammo/node.rb, line 247
def child?(node)
  node.parent == self
end