| Module | Hpricot::Traverse |
| In: |
lib/hpricot/traverse.rb
lib/hpricot/modules.rb lib/hpricot/elements.rb |
Returns the node neighboring this node to the south: just below it. This method includes text nodes and comments and such.
Puts together an array of neighboring nodes based on their proximity to this node. So, for example, to get the next node, you could use nodes_at(1). Or, to get the previous node, use <tt>nodes_at(1).
This method also accepts ranges and sets of numbers.
ele.nodes_at(-3..-1, 1..3) # gets three nodes before and three after ele.nodes_at(1, 5, 7) # gets three nodes at offsets below the current node ele.nodes_at(0, 5..6) # the current node and two others
Returns to node neighboring this node to the north: just above it. This method includes text nodes and comments and such.
Searches this node for all elements matching the CSS or XPath expr. Returns an Elements array containing the matching nodes. If blk is given, it is used to iterate through the matching set.
Builds an HTML string from this node and its contents. If you need to write to a stream, try calling output(io) as a method on this object.
Attempts to preserve the original HTML of the document, only outputing new tags for elements which have changed.
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 = Hpricot('<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 = Hpricot(<<'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?">}