Module Hpricot::Traverse
In: lib/hpricot/traverse.rb
lib/hpricot/modules.rb
lib/hpricot/elements.rb

Methods

Public Class methods

Public Instance methods

%(expr)

Alias for at

/(expr, &blk)

Alias for search

Adds elements immediately after this element, contained in the html string.

Find the first matching node for the CSS or XPath expr string.

Adds elements immediately before this element, contained in the html string.

Is this object a stranded end tag?

Find children of a given tag_name.

  ele.children_of_type('p')
    #=> [...array of paragraphs...]

Is this object a comment?

Builds a unique CSS string for this node, from the root of the document containing it.

Is this object the enclosing HTML or XML document?

Is this object a doctype tag?

Is this object an HTML or XML element?

innerHTML()

Alias for inner_html

innerHTML=(inner)

Alias for inner_html=

innerText()

Alias for inner_text

Builds an HTML string from the contents of this node.

Inserts new contents into the current node, based on the HTML contained in string inner.

Builds a string from the text contained in this node. All HTML elements are removed.

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.

Is this object an XML processing instruction?

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.

Replace this element and its contents with the nodes contained in the html string.

Is this object an HTML text node?

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.

Builds a string from the text contained in this node. All HTML elements are removed.

to_s()

Alias for to_html

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?">}

traverse_text traverses texts in the tree

Is this object an XML declaration?

Builds a unique XPath string for this node, from the root of the document containing it.

[Validate]