class Oga::XML::Node

A generic XML node. Instances of this class can belong to a {Oga::XML::NodeSet} and can be used to query surrounding and parent nodes.

Attributes

next[RW]

@return [Oga::XML::Node]

node_set[R]

@return [Oga::XML::NodeSet]

previous[RW]

@return [Oga::XML::Node]

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options

@option options [Oga::XML::NodeSet] :node_set The node set that this

node belongs to.

@option options [Oga::XML::NodeSet|Array] :children The child nodes of

the current node.
# File lib/oga/xml/node.rb, line 26
def initialize(options = {})
  self.node_set = options[:node_set]
  self.children = options[:children] if options[:children]
end

Public Instance Methods

after(other) click to toggle source

Inserts the given node after the current node.

@param [Oga::XML::Node] other

# File lib/oga/xml/node.rb, line 154
def after(other)
  index = node_set.index(self) + 1

  node_set.insert(index, other)
end
before(other) click to toggle source

Inserts the given node before the current node.

@param [Oga::XML::Node] other

# File lib/oga/xml/node.rb, line 145
def before(other)
  index = node_set.index(self)

  node_set.insert(index, other)
end
children() click to toggle source

Returns the child nodes of the current node.

@return [Oga::XML::NodeSet]

# File lib/oga/xml/node.rb, line 43
def children
  @children ||= NodeSet.new([], self)
end
children=(nodes) click to toggle source

Sets the child nodes of the element.

@param [Oga::XML::NodeSet|Array] nodes

# File lib/oga/xml/node.rb, line 50
def children=(nodes)
  if nodes.is_a?(NodeSet)
    nodes.owner = self
    nodes.take_ownership_on_nodes
    @children = nodes
  else
    @children = NodeSet.new(nodes, self)
  end
end
each_ancestor() { |node| ... } click to toggle source

Yields all ancestor elements of the current node.

@example

some_element.each_ancestor do |node|
  # ...
end

@yieldparam [Oga::XML::Node]

# File lib/oga/xml/node.rb, line 184
def each_ancestor
  return to_enum(:each_ancestor) unless block_given?

  node = parent

  while node.is_a?(XML::Element)
    yield node

    node = node.parent
  end
end
html?() click to toggle source

@return [TrueClass|FalseClass]

# File lib/oga/xml/node.rb, line 161
def html?
  if @html_p.nil?
    root = root_node

    @html_p = root.is_a?(Document) && root.html?
  end

  @html_p
end
next_element() click to toggle source

Returns the next element node or nil if there is none.

@return [Oga::XML::Element]

# File lib/oga/xml/node.rb, line 84
def next_element
  node = self

  while node = node.next
    return node if node.is_a?(Element)
  end

  return
end
node_set=(set) click to toggle source

@param [Oga::XML::NodeSet] set

# File lib/oga/xml/node.rb, line 32
def node_set=(set)
  @node_set  = set
  @root_node = nil
  @html_p    = nil
  @previous  = nil
  @next      = nil
end
parent() click to toggle source

Returns the parent node of the current node. If there is no parent node ‘nil` is returned instead.

@return [Oga::XML::Node]

# File lib/oga/xml/node.rb, line 64
def parent
  node_set ? node_set.owner : nil
end
previous_element() click to toggle source

Returns the previous element node or nil if there is none.

@return [Oga::XML::Element]

# File lib/oga/xml/node.rb, line 71
def previous_element
  node = self

  while node = node.previous
    return node if node.is_a?(Element)
  end

  return
end
remove() click to toggle source

Removes the current node from the owning node set.

@return [Oga::XML::Node]

# File lib/oga/xml/node.rb, line 119
def remove
  return node_set.delete(self) if node_set
end
replace(other) click to toggle source

Replaces the current node with another.

@example Replacing with an element

element = Oga::XML::Element.new(:name => 'div')
some_node.replace(element)

@example Replacing with a String

some_node.replace('this will replace the current node with a text node')

@param [String|Oga::XML::Node] other

# File lib/oga/xml/node.rb, line 133
def replace(other)
  if other.is_a?(String)
    other = Text.new(:text => other)
  end

  before(other)
  remove
end
root_node() click to toggle source

Returns the root document/node of the current node. The node is retrieved by traversing upwards in the DOM tree from the current node.

@return [Oga::XML::Document|Oga::XML::Node]

# File lib/oga/xml/node.rb, line 98
def root_node
  unless @root_node
    node = self

    loop do
      if !node.is_a?(Document) and node.node_set
        node = node.node_set.owner
      else
        break
      end
    end

    @root_node = node
  end

  @root_node
end
xml?() click to toggle source

@return [TrueClass|FalseClass]

# File lib/oga/xml/node.rb, line 172
def xml?
  !html?
end