Class

Elements

Extends:

Once you’ve matched a list of elements, you will often need to handle them as a group. Or you may want to perform the same action on each of them. Hpricot::Elements is an extension of Ruby’s array class, with some methods added for altering elements contained in the array.

If you need to create an element array from regular elements:

Hpricot::Elements[ele1, ele2, ele3]

Assuming that ele1, ele2 and ele3 contain element objects (Hpricot::Elem, Hpricot::Doc, etc.)

Continuing Searches

Usually the Hpricot::Elements you’re working on comes from a search you’ve done. Well, you can continue searching the list by using the same at and search methods you can use on plain elements.

elements = doc.search("/div/p")
elements = elements.search("/a[@href='http://hoodwink.d/']")
elements = elements.at("img")

Altering Elements

When you’re altering elements in the list, your changes will be reflected in the document you started searching from.

doc = Hpricot("That's my <b>spoon</b>, Tyler.")
doc.at("b").swap("<i>fork</i>")
doc.to_html
  #=> "That's my <i>fork</i>, Tyler."

Getting More Detailed

If you can’t find a method here that does what you need, you may need to loop through the elements and find a method in Hpricot::Container::Trav which can do what you need.

For example, you may want to search for all the H3 header tags in a document and grab all the tags underneath the header, but not inside the header. A good method for this is next_sibling:

doc.search("h3").each do |h3|
  while ele = h3.next_sibling
    ary << ele   # stuff away all the elements under the h3
  end
end

Most of the useful element methods are in the mixins Hpricot::Traverse and Hpricot::Container::Trav.

Constants
ATTR_RE
BRACK_RE
CATCH_RE
CUST_RE
FUNC_RE
Public Methods
% Alias for #at
/ Alias for #search
add_class Adds the class to all matched elements.
after Just after each element in this list, add some HTML. Pass in an HTML str, which is turned into Hpricot elements.
append Add to the end of the contents inside each element in this list. Pass in an HTML str, which is turned into Hpricot elements.
at Searches this list for the first element (or child of these elements) matching the CSS or XPath expression expr. Root is assumed to be the element scanned.
attr Gets and sets attributes on all matched elements.
before Add some HTML just previous to each element in this list. Pass in an HTML str, which is turned into Hpricot elements.
empty Empty the elements in this list, by removing their insides.
expand Given two elements, attempt to gather an Elements array of everything between (and including) those two elements.
filter
filter
html Alias for #inner_html
html= Alias for #inner_html=
innerHTML Alias for #inner_html
innerHTML= Alias for #inner_html=
inner_html Returns an HTML fragment built of the contents of each element in this list.
inner_html= Replaces the contents of each element in this list. Supply an HTML string, which is loaded into Hpricot objects and inserted into every element in this list.
inner_text Returns an string containing the text contents of each element in this list. All HTML tags are removed.
not
prepend Add to the start of the contents inside each element in this list. Pass in an HTML str, which is turned into Hpricot elements.
remove Remove all elements in this list from the document which contains them.
remove_attr Remove an attribute from each of the matched elements.
remove_class Removes a class from all matched elements.
search Searches this list for any elements (or children of these elements) matching the CSS or XPath expression expr. Root is assumed to be the element scanned.
set Alias for #attr
text Alias for #inner_text
to_html Convert this group of elements into a complete HTML fragment, returned as a string.
to_s Alias for #to_html
wrap Wraps each element in the list inside the element created by HTML str. If more than one element is found in the string, Hpricot locates the deepest spot inside the first element.
Private Methods
copy_node
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.