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_ |
|
| BRACK_ |
|
| CATCH_ |
|
| CUST_ |
|
| FUNC_ |
|
| Public Methods | |
|---|---|
| % | Alias for #at |
| / | Alias for #search |
| add_ |
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_ |
Returns an HTML fragment built of the contents of each element in this list. |
| inner_ |
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_ |
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_ |
Remove an attribute from each of the matched elements. |
| remove_ |
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_ |
Convert this group of elements into a complete HTML fragment, returned as a string. |
| to_ |
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_ |
|
<code/>and<pre/>for code samples.