From 8fe71f298b24e979d0070010d3c531ecfacda846 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 30 Jul 2014 19:56:56 +0200 Subject: [PATCH] Half-assed way of retrieving all document nodes. This currently only works for documents, is not tested and most likely will leak memory due to being recursive. --- lib/oga/xml/document.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/oga/xml/document.rb b/lib/oga/xml/document.rb index aa2e481..580f464 100644 --- a/lib/oga/xml/document.rb +++ b/lib/oga/xml/document.rb @@ -49,6 +49,34 @@ module Oga end end + ## + # Returns a NodeSet containing *all* the nodes in the current document. + # Nodes are inserted in the order they appear in the document. + # + # @return [Oga::XML::NodeSet] + # + def all_nodes + return gather_child_nodes(self) + end + + ## + # Recursively retrieves all child nodes of `node` and returns them as a + # node set. + # + # @param [Oga::XML::Document|Oga::XML::Node] node + # @return [Oga::XML::NodeSet] + # + def gather_child_nodes(node) + nodes = NodeSet.new + + node.children.each do |child| + nodes << child + nodes += gather_child_nodes(child) + end + + return nodes + end + ## # Converts the document and its child nodes to XML. #