Class: Oga::XML::Node

Inherits:
Object
  • Object
show all
Includes:
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, Element

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Traversal

#each_node

Constructor Details

#initialize(options = {}) ⇒ Node

Returns a new instance of Node

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):


25
26
27
28
29
# File 'lib/oga/xml/node.rb', line 25

def initialize(options = {})
  @node_set = options[:node_set]

  self.children = options[:children] if options[:children]
end

Instance Attribute Details

#node_setOga::XML::NodeSet

Returns:


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/oga/xml/node.rb', line 11

class Node
  include Traversal

  attr_accessor :node_set

  ##
  # @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.
  #
  def initialize(options = {})
    @node_set = options[:node_set]

    self.children = options[:children] if options[:children]
  end

  ##
  # Returns the child nodes of the current node.
  #
  # @return [Oga::XML::NodeSet]
  #
  def children
    return @children ||= NodeSet.new([], self)
  end

  ##
  # Sets the child nodes of the element.
  #
  # @param [Oga::XML::NodeSet|Array] nodes
  #
  def children=(nodes)
    if nodes.is_a?(NodeSet)
      @children = nodes
    else
      @children = NodeSet.new(nodes, self)
    end
  end

  ##
  # Returns the parent node of the current node. If there is no parent node
  # `nil` is returned instead.
  #
  # @return [Oga::XML::Node]
  #
  def parent
    return node_set ? node_set.owner : nil
  end

  ##
  # Returns the preceding node, or nil if there is none.
  #
  # @return [Oga::XML::Node]
  #
  def previous
    index = node_set.index(self) - 1

    return index >= 0 ? node_set[index] : nil
  end

  ##
  # Returns the following node, or nil if there is none.
  #
  # @return [Oga::XML::Node]
  #
  def next
    index  = node_set.index(self) + 1
    length = node_set.length

    return index <= length ? node_set[index] : nil
  end

  ##
  # Returns the previous element node or nil if there is none.
  #
  # @return [Oga::XML::Element]
  #
  def previous_element
    node = self

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

    return
  end

  ##
  # Returns the next element node or nil if there is none.
  #
  # @return [Oga::XML::Element]
  #
  def next_element
    node = self

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

    return
  end

  ##
  # 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]
  #
  def root_node
    node = self

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

    return node
  end

  ##
  # Removes the current node from the owning node set.
  #
  # @return [Oga::XML::Node]
  #
  def remove
    return node_set.delete(self) if node_set
  end

  ##
  # Inserts the given node before the current node.
  #
  # @param [Oga::XML::Node] other
  #
  def before(other)
    index = node_set.index(self)

    node_set.insert(index, other)
  end

  ##
  # Inserts the given node after the current node.
  #
  # @param [Oga::XML::Node] other
  #
  def after(other)
    index = node_set.index(self) + 1

    node_set.insert(index, other)
  end
end

Instance Method Details

#after(other) ⇒ Object

Inserts the given node after the current node.

Parameters:


161
162
163
164
165
# File 'lib/oga/xml/node.rb', line 161

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:


150
151
152
153
154
# File 'lib/oga/xml/node.rb', line 150

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:


36
37
38
# File 'lib/oga/xml/node.rb', line 36

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

#children=(nodes) ⇒ Object

Sets the child nodes of the element.

Parameters:


45
46
47
48
49
50
51
# File 'lib/oga/xml/node.rb', line 45

def children=(nodes)
  if nodes.is_a?(NodeSet)
    @children = nodes
  else
    @children = NodeSet.new(nodes, self)
  end
end

#nextOga::XML::Node

Returns the following node, or nil if there is none.

Returns:


79
80
81
82
83
84
# File 'lib/oga/xml/node.rb', line 79

def next
  index  = node_set.index(self) + 1
  length = node_set.length

  return index <= length ? node_set[index] : nil
end

#next_elementOga::XML::Element

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

Returns:


106
107
108
109
110
111
112
113
114
# File 'lib/oga/xml/node.rb', line 106

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:


59
60
61
# File 'lib/oga/xml/node.rb', line 59

def parent
  return node_set ? node_set.owner : nil
end

#previousOga::XML::Node

Returns the preceding node, or nil if there is none.

Returns:


68
69
70
71
72
# File 'lib/oga/xml/node.rb', line 68

def previous
  index = node_set.index(self) - 1

  return index >= 0 ? node_set[index] : nil
end

#previous_elementOga::XML::Element

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

Returns:


91
92
93
94
95
96
97
98
99
# File 'lib/oga/xml/node.rb', line 91

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:


141
142
143
# File 'lib/oga/xml/node.rb', line 141

def remove
  return node_set.delete(self) if node_set
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.


122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/oga/xml/node.rb', line 122

def root_node
  node = self

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

  return node
end