Module: Oga::XML::Traversal

Included in:
Document, Node
Defined in:
lib/oga/xml/traversal.rb

Overview

Module that provides methods to traverse DOM trees.

Instance Method Summary collapse

Instance Method Details

#each_node {|The| ... } ⇒ Object

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.

Examples:

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

Yield Parameters:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/oga/xml/traversal.rb', line 29

def each_node
  return to_enum(:each_node) unless block_given?

  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