Altering Elements

When you alter Hpricot elements in a list, your changes will be reflected in the document you started searching from.

 #!ruby
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." 

Using Builder-style methods (Hpricot 0.6):

doc.at("i").swap { em "grapefruit spoon" }
doc.to_html
# => "That's my <em>grapefruit spoon</em>, Tyler."

Want to remove something?

# 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>"

Adding HTML after an element (snippet from linkhints.user.rb):

document.search('//a[@href]') do |link|
    href = URI(link.attributes['href']) rescue nil
    next unless href && href.host

    link.after '<span style="font-size:8px">[' + href.host + ']</span>'
end