Class: Oga::XML::Node

Inherits:
Object
  • Object
show all
Includes:
ToXML, Traversal
Defined in:
lib/oga/xml/node.rb

Overview

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

Direct Known Subclasses

CharacterNode, Doctype, Element

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ToXML

#to_xml

Methods included from Traversal

#each_node

Constructor Details

#initialize(options = {}) ⇒ Node

Returns a new instance of Node.

Parameters:

  • (defaults to: {})

Options Hash (options):



26
27
28
29
# 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

Instance Attribute Details

#nextOga::XML::Node

Returns:



17
18
19
# File 'lib/oga/xml/node.rb', line 17

def next
  @next
end

#node_setOga::XML::NodeSet

Returns:



11
12
13
# File 'lib/oga/xml/node.rb', line 11

def node_set
  @node_set
end

#previousOga::XML::Node

Returns:



14
15
16
# File 'lib/oga/xml/node.rb', line 14

def previous
  @previous
end

Instance Method Details

#after(other) ⇒ Object

Inserts the given node after the current node.

Parameters:



154
155
156
157
158
# 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) ⇒ Object

Inserts the given node before the current node.

Parameters:



145
146
147
148
149
# File 'lib/oga/xml/node.rb', line 145

def before(other)
  index = node_set.index(self)

  node_set.insert(index, other)
end

#childrenOga::XML::NodeSet

Returns the child nodes of the current node.

Returns:



43
44
45
# File 'lib/oga/xml/node.rb', line 43

def children
  @children ||= NodeSet.new([], self)
end

#children=(nodes) ⇒ Object

Sets the child nodes of the element.

Parameters:



50
51
52
53
54
55
56
57
58
# 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 {|| ... } ⇒ Object

Yields all ancestor elements of the current node.

Examples:

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

Yield Parameters:



184
185
186
187
188
189
190
191
192
193
194
# 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?TrueClass|FalseClass

Returns:



161
162
163
164
165
166
167
168
169
# 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_elementOga::XML::Element

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

Returns:



84
85
86
87
88
89
90
91
92
# 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

#parentOga::XML::Node

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

Returns:



64
65
66
# File 'lib/oga/xml/node.rb', line 64

def parent
  node_set ? node_set.owner : nil
end

#previous_elementOga::XML::Element

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

Returns:



71
72
73
74
75
76
77
78
79
# 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

#removeOga::XML::Node

Removes the current node from the owning node set.

Returns:



119
120
121
# File 'lib/oga/xml/node.rb', line 119

def remove
  return node_set.delete(self) if node_set
end

#replace(other) ⇒ Object

Replaces the current node with another.

Examples:

Replacing with an element

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

Replacing with a String

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

Parameters:



133
134
135
136
137
138
139
140
# 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_nodeOga::XML::Document|Oga::XML::Node

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

Returns:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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?TrueClass|FalseClass

Returns:



172
173
174
# File 'lib/oga/xml/node.rb', line 172

def xml?
  !html?
end