Class: Oga::XML::Node
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
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#after(other) ⇒ Object
Inserts the given node after the current node.
-
#before(other) ⇒ Object
Inserts the given node before the current node.
-
#children ⇒ Oga::XML::NodeSet
Returns the child nodes of the current node.
-
#children=(nodes) ⇒ Object
Sets the child nodes of the element.
- #html? ⇒ TrueClass|FalseClass
-
#initialize(options = {}) ⇒ Node
constructor
A new instance of Node.
-
#next ⇒ Oga::XML::Node
Returns the following node, or nil if there is none.
-
#next_element ⇒ Oga::XML::Element
Returns the next element node or nil if there is none.
-
#parent ⇒ Oga::XML::Node
Returns the parent node of the current node.
-
#previous ⇒ Oga::XML::Node
Returns the preceding node, or nil if there is none.
-
#previous_element ⇒ Oga::XML::Element
Returns the previous element node or nil if there is none.
-
#remove ⇒ Oga::XML::Node
Removes the current node from the owning node set.
-
#replace(other) ⇒ Object
Replaces the current node with another.
-
#root_node ⇒ Oga::XML::Document|Oga::XML::Node
Returns the root document/node of the current node.
- #xml? ⇒ TrueClass|FalseClass
Methods included from Traversal
Constructor Details
#initialize(options = {}) ⇒ Node
Returns a new instance of Node.
23 24 25 26 |
# File 'lib/oga/xml/node.rb', line 23 def initialize( = {}) self.node_set = [:node_set] self.children = [:children] if [:children] end |
Instance Attribute Details
#node_set ⇒ Oga::XML::NodeSet
12 13 14 |
# File 'lib/oga/xml/node.rb', line 12 def node_set @node_set end |
Instance Method Details
#after(other) ⇒ Object
Inserts the given node after the current node.
192 193 194 195 196 |
# File 'lib/oga/xml/node.rb', line 192 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.
181 182 183 184 185 |
# File 'lib/oga/xml/node.rb', line 181 def before(other) index = node_set.index(self) node_set.insert(index, other) end |
#children ⇒ Oga::XML::NodeSet
Returns the child nodes of the current node.
42 43 44 |
# File 'lib/oga/xml/node.rb', line 42 def children @children ||= NodeSet.new([], self) end |
#children=(nodes) ⇒ Object
Sets the child nodes of the element.
51 52 53 54 55 56 57 |
# File 'lib/oga/xml/node.rb', line 51 def children=(nodes) if nodes.is_a?(NodeSet) @children = nodes else @children = NodeSet.new(nodes, self) end end |
#html? ⇒ TrueClass|FalseClass
201 202 203 204 205 206 207 208 209 |
# File 'lib/oga/xml/node.rb', line 201 def html? if @html_p.nil? root = root_node @html_p = root.is_a?(Document) && root.html? end @html_p end |
#next ⇒ Oga::XML::Node
Returns the following node, or nil if there is none.
85 86 87 88 89 90 |
# File 'lib/oga/xml/node.rb', line 85 def next index = node_set.index(self) + 1 length = node_set.length index <= length ? node_set[index] : nil end |
#next_element ⇒ Oga::XML::Element
Returns the next element node or nil if there is none.
112 113 114 115 116 117 118 119 120 |
# File 'lib/oga/xml/node.rb', line 112 def next_element node = self while node = node.next return node if node.is_a?(Element) end return end |
#parent ⇒ Oga::XML::Node
Returns the parent node of the current node. If there is no parent node
nil
is returned instead.
65 66 67 |
# File 'lib/oga/xml/node.rb', line 65 def parent node_set ? node_set.owner : nil end |
#previous ⇒ Oga::XML::Node
Returns the preceding node, or nil if there is none.
74 75 76 77 78 |
# File 'lib/oga/xml/node.rb', line 74 def previous index = node_set.index(self) - 1 index >= 0 ? node_set[index] : nil end |
#previous_element ⇒ Oga::XML::Element
Returns the previous element node or nil if there is none.
97 98 99 100 101 102 103 104 105 |
# File 'lib/oga/xml/node.rb', line 97 def previous_element node = self while node = node.previous return node if node.is_a?(Element) end return end |
#remove ⇒ Oga::XML::Node
Removes the current node from the owning node set.
151 152 153 |
# File 'lib/oga/xml/node.rb', line 151 def remove return node_set.delete(self) if node_set end |
#replace(other) ⇒ Object
Replaces the current node with another.
167 168 169 170 171 172 173 174 |
# File 'lib/oga/xml/node.rb', line 167 def replace(other) if other.is_a?(String) other = Text.new(:text => other) end before(other) remove end |
#root_node ⇒ Oga::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.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/oga/xml/node.rb', line 128 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
214 215 216 |
# File 'lib/oga/xml/node.rb', line 214 def xml? !html? end |