diff --git a/lib/oga/xml/node_set.rb b/lib/oga/xml/node_set.rb new file mode 100644 index 0000000..cfe092f --- /dev/null +++ b/lib/oga/xml/node_set.rb @@ -0,0 +1,71 @@ +module Oga + module XML + ## + # The NodeSet class contains a set of {Oga::XML::Node} instances that can + # be queried and modified. + # + class NodeSet + include Enumerable + + def initialize(nodes = []) + @nodes = nodes + + associate_nodes + end + + def each + @nodes.each { |node| yield node } + end + + def length + return @nodes.length + end + + alias_method :size, :length + + def push(node) + node.node_set = self + node.index = node.length + + @nodes << node + end + + alias_method :<<, :push + + def attr(name) + return @nodes.map { |node| node.attr(name) } + end + + def remove + @nodes.each do |node| + # Remove references to the node from the parent node, if any. + node.parent.children.delete!(node) if node.parent + end + end + + ## + # Removes a node from the current set only. + # + def delete!(node) + @nodes.delete(node) + end + + def text + text = '' + + @nodes.each do |node| + text << node.text + end + + return text + end + + def associate_nodes + @nodes.each_with_index do |node, index| + node.node_set = self + node.index = index + end + end + end # NodeSet + end # XML +end # Oga