Document#all_nodes -> Document#each_node
This method has been renamed and now yields nodes and their indexes instead of buffering them in a node set.
This commit is contained in:
parent
4bbf0c98ae
commit
34e2d28bbd
|
@ -50,31 +50,33 @@ module Oga
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Returns a NodeSet containing *all* the nodes in the current document.
|
# Traverses through the document and yields every node to the supplied
|
||||||
# Nodes are inserted in the order they appear in the document.
|
# block.
|
||||||
|
#
|
||||||
|
# @example
|
||||||
|
# document.each_node do |node, index|
|
||||||
|
# p node.class
|
||||||
|
# end
|
||||||
#
|
#
|
||||||
# This method uses a breadth first search for tree traversal. See
|
# This method uses a breadth first search for tree traversal. See
|
||||||
# http://en.wikipedia.org/wiki/Breadth-first_search for more information.
|
# http://en.wikipedia.org/wiki/Breadth-first_search for more information.
|
||||||
#
|
#
|
||||||
# THINK: Turn into an actual search instead of returning everything?
|
# @yieldparam [Oga::XML::Node] The current node.
|
||||||
|
# @yieldparam [Fixnum] The current node's index.
|
||||||
#
|
#
|
||||||
# @return [Oga::XML::NodeSet]
|
def each_node
|
||||||
#
|
|
||||||
def all_nodes
|
|
||||||
nodes = NodeSet.new
|
|
||||||
visit = children.to_a.dup # copy it since we're using #pop below.
|
visit = children.to_a.dup # copy it since we're using #pop below.
|
||||||
|
index = 0
|
||||||
|
|
||||||
until visit.empty?
|
until visit.empty?
|
||||||
current = visit.pop
|
current = visit.pop
|
||||||
|
|
||||||
nodes << current
|
yield current, index
|
||||||
|
|
||||||
current.children.each do |child|
|
index += 1
|
||||||
visit << child
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return nodes
|
current.children.each { |child| visit << child }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
Loading…
Reference in New Issue