Skip to content

Instantly share code, notes, and snippets.

@ashmckenzie
Created August 30, 2009 02:06
Show Gist options
  • Save ashmckenzie/177821 to your computer and use it in GitHub Desktop.
Save ashmckenzie/177821 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'hpricot'
s = %q{<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><html><body><input type="text" name="test" value="\" /><div id="footer"></div></body></html>}
selector = '#footer'
inner_html = 'testing'
# Nokogiri
#
n = Nokogiri.parse(s)
n.css(selector).each do |elements|
elements.inner_html = inner_html
#elements.send(:inner_html, inner_html) # nokogiri doesn't support this :(
end
puts "\n- Nokogiri"
puts "#{n.to_s.gsub!(/\n/, '')}\n\n"
# Hpricot
#
h = Hpricot(s)
elements = h.search(selector)
elements.send(:inner_html, inner_html)
puts "- Hpricot"
puts "#{h.to_s}\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment