| Class | Hpricot::Elements |
| In: |
lib/hpricot/elements.rb
|
| Parent: | Array |
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.)
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")
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."
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.
| ATTR_RE | = | %r!\[ *(?:(@)([\w\(\)-]+)|([\w\(\)-]+\(\))) *([~\!\|\*$\^=]*) *'?"?([^\]'"]*)'?"? *\]!i |
| BRACK_RE | = | %r!(\[) *([^\]]*) *\]+!i |
| FUNC_RE | = | %r!(:)?([a-zA-Z0-9\*_-]*)\( *[\"']?([^ \)]*?)['\"]? *\)! |
| CUST_RE | = | %r!(:)([a-zA-Z0-9\*_-]*)()! |
| CATCH_RE | = | %r!([:\.#]*)([a-zA-Z0-9\*_-]+)! |
Given two elements, attempt to gather an Elements array of everything between (and including) those two elements.
Adds the class to all matched elements.
(doc/"p").add_class("bacon")
Now all paragraphs will have class="bacon".
Add to the end of the contents inside each element in this list. Pass in an HTML str, which is turned into Hpricot elements.
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.
See Hpricot::Container::Trav.at for more.
Gets and sets attributes on all matched elements.
Pass in a key on its own and this method will return the string value assigned to that attribute for the first elements. Or nil if the attribute isn‘t found.
doc.search("a").attr("href")
#=> "http://hacketyhack.net/"
Or, pass in a key and value. This will set an attribute for all matched elements.
doc.search("p").attr("class", "basic")
You may also use a Hash to set a series of attributes:
(doc/"a").attr(:class => "basic", :href => "http://hackety.org/")
Lastly, a block can be used to rewrite an attribute based on the element it belongs to. The block will pass in an element. Return from the block the new value of the attribute.
records.attr("href") { |e| e['href'] + "#top" }
This example adds a top anchor to each link.
Add some HTML just previous to each element in this list. Pass in an HTML str, which is turned into Hpricot elements.
Empty the elements in this list, by removing their insides.
doc = Hpricot("<p> We have <i>so much</i> to say.</p>")
doc.search("i").empty
doc.to_html
=> "<p> We have <i></i> to say.</p>"
Returns an HTML fragment built of the contents of each element in this list.
If a HTML string is supplied, this method acts like 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.
Returns an string containing the text contents of each element in this list. All HTML tags are removed.
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 all elements in this list from the document which contains them.
doc = Hpricot("<html>Remove this: <b>here</b></html>")
doc.search("b").remove
doc.to_html
=> "<html>Remove this: </html>"
Removes a class from all matched elements.
(doc/"span").remove_class("lightgrey")
Or, to remove all classes:
(doc/"span").remove_class
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.
See Hpricot::Container::Trav.search for more.
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.
doc.search("a[@href]").
wrap(%{<div class="link"><div class="link_inner"></div></div>})
This code wraps every link on the page inside a +div.link+ and a +div.link_inner+ nest.