From 5c23333f4600e04426d5748ebf9871a160189d79 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 1 Aug 2014 23:34:32 +0200 Subject: [PATCH] Traverse document nodes in document order. The method Document#each_node now yields the nodes in the correct order. --- lib/oga/xml/document.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/oga/xml/document.rb b/lib/oga/xml/document.rb index 18a4122..3abbe11 100644 --- a/lib/oga/xml/document.rb +++ b/lib/oga/xml/document.rb @@ -58,14 +58,15 @@ module Oga # p node.class # end # - # This method uses a breadth first search for tree traversal. See + # This method uses a combination of breadth-first and depth-first + # traversal to traverse the entire XML tree in document order. See # http://en.wikipedia.org/wiki/Breadth-first_search for more information. # # @yieldparam [Oga::XML::Node] The current node. # @yieldparam [Fixnum] The current node's index. # def each_node - visit = children.to_a.dup # copy it since we're using #pop below. + visit = children.to_a.dup # copy it since we're modifying the array index = 0 until visit.empty? @@ -75,7 +76,7 @@ module Oga index += 1 - current.children.each { |child| visit << child } + visit = current.children.to_a + visit end end