module HTree::Container::Trav

Public Instance Methods

each_child() { |child_node| ... } click to toggle source

each_child iterates over each child.

# File htree/traverse.rb, line 29
def each_child(&block) # :yields: child_node
  children.each(&block)
  nil
end
each_child_with_index() { |child_node, index| ... } click to toggle source

each_child_with_index iterates over each child.

# File htree/traverse.rb, line 35
def each_child_with_index(&block) # :yields: child_node, index
  children.each_with_index(&block)
  nil
end
each_uri(base_uri=nil) { |URI| ... } click to toggle source

each_uri traverses hyperlinks such as HTML href attribute of A element.

It yields URI for each hyperlink.

The URI objects are created with a base URI which is given by HTML BASE element or the argument ((|base_uri|)).

# File htree/traverse.rb, line 174
def each_uri(base_uri=nil) # :yields: URI
  each_hyperlink_uri(base_uri) {|hyperlink, uri| yield uri }
end
filter() { |descendant| ... } click to toggle source

filter rebuilds the tree without some components.

node.filter {|descendant_node| predicate } -> node
loc.filter {|descendant_loc| predicate } -> node

filter yields each node except top node. If given block returns false, corresponding node is dropped. If given block returns true, corresponding node is retained and inner nodes are examined.

filter returns an node. It doesn't return location object even if self is location object.

# File htree/traverse.rb, line 258
def filter(&block)
  subst = {}
  each_child_with_index {|descendant, i|
    if yield descendant
      if descendant.elem?
        subst[i] = descendant.filter(&block)
      else
        subst[i] = descendant
      end
    else
      subst[i] = nil
    end
  }
  to_node.subst_subnode(subst)
end
find_element(*names) click to toggle source

find_element searches an element which universal name is specified by the arguments. It returns nil if not found.

# File htree/traverse.rb, line 43
def find_element(*names)
  traverse_element(*names) {|e| return e }
  nil
end
traverse_element(*names) { |element| ... } click to toggle source

traverse_element traverses elements in the tree. It yields elements in depth first order.

If names are empty, it yields all elements. If non-empty names are given, it should be list of universal names.

A nested element is yielded in depth first order as follows.

t = HTree('<a id=0><b><a id=1 /></b><c id=2 /></a>') 
t.traverse_element("a", "c") {|e| p e}
# =>
{elem <a id="0"> {elem <b> {emptyelem <a id="1">} </b>} {emptyelem <c id="2">} </a>}
{emptyelem <a id="1">}
{emptyelem <c id="2">}

Universal names are specified as follows.

t = HTree(<<'End')
<html>
<meta name="robots" content="index,nofollow">
<meta name="author" content="Who am I?">    
</html>
End
t.traverse_element("{http://www.w3.org/1999/xhtml}meta") {|e| p e}
# =>
{emptyelem <{http://www.w3.org/1999/xhtml}meta name="robots" content="index,nofollow">}
{emptyelem <{http://www.w3.org/1999/xhtml}meta name="author" content="Who am I?">}
# File htree/traverse.rb, line 76
def traverse_element(*names, &block) # :yields: element
  if names.empty?
    traverse_all_element(&block)
  else
    name_set = {}
    names.each {|n| name_set[n] = true }
    traverse_some_element(name_set, &block)
  end
  nil
end
traverse_text_internal(&block) click to toggle source
# File htree/traverse.rb, line 227
def traverse_text_internal(&block)
  each_child {|c| c.traverse_text_internal(&block) }
end