Skip to content

Instantly share code, notes, and snippets.

@yorickpeterse
Last active January 12, 2016 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorickpeterse/de4fdfb5d148476206a7 to your computer and use it in GitHub Desktop.
Save yorickpeterse/de4fdfb5d148476206a7 to your computer and use it in GitHub Desktop.
import oga::xml::Node
trait Traversal:
def children -> Array<Node>
def each_node(func: |Node|):
---
Traverses through the node and yields every child node to the supplied
block.
The block's body can also determine whether or not to traverse child
nodes. Preventing a node's children from being traversed can be done by
using `throw :skip_children`
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.
Example:
document.each_node do |node|
p node.class
end
Skipping the children of a certain node:
document.each_node do |node|
if node.is_a?(Oga::XML::Element) and node.name == 'book'
throw :skip_children
end
end
---
let mut visit = children.reverse
| visit.empty? |.while_false:
let current = visit.pop
func(current)
current.children.reverse_each |child|:
visit << child
module Oga
module XML
# Module that provides methods to traverse DOM trees.
module Traversal
# Traverses through the node and yields every child node to the supplied
# block.
#
# The block's body can also determine whether or not to traverse child
# nodes. Preventing a node's children from being traversed can be done by
# using `throw :skip_children`
#
# 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.
#
# @example
# document.each_node do |node|
# p node.class
# end
#
# @example Skipping the children of a certain node
# document.each_node do |node|
# if node.is_a?(Oga::XML::Element) and node.name == 'book'
# throw :skip_children
# end
# end
#
# @yieldparam [Oga::XML::Node] The current node.
def each_node
visit = children.to_a.reverse
until visit.empty?
current = visit.pop
catch :skip_children do
yield current
current.children.to_a.reverse_each do |child|
visit << child
end
end
end
end
end # Traversal
end # XML
end # Oga
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment