Created
May 15, 2011 14:09
-
-
Save denisdefreyne/973188 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
require 'nokogiri' | |
doc = Nokogiri::HTML('<p>abc</p>') | |
doc.inner_html = '<p>…</p>' | |
puts doc.inner_html | |
# output: <p>â¦</p> | |
######################################## | |
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
require 'nokogiri' | |
original_string = '<div><p>abc</p></div>' | |
replacement_string = '<p>…</p>' | |
puts "original_string: #{original_string.inspect} (#{original_string.encoding.inspect})" | |
puts "replacement_string: #{replacement_string.inspect} (#{replacement_string.encoding.inspect})" | |
puts | |
doc = Nokogiri::HTML.fragment(original_string, 'UTF-8') | |
puts "original: #{doc.inner_html} (#{doc.inner_html.encoding.inspect})" | |
doc.css('div').first.inner_html = replacement_string | |
puts "replaced (1): #{doc.inner_html} (#{doc.inner_html.encoding.inspect})" | |
doc.inner_html = replacement_string | |
puts "replaced (2): #{doc.inner_html} (#{doc.inner_html.encoding.inspect})" | |
# output: | |
# | |
# original_string: "<div><p>abc</p></div>" (#<Encoding:UTF-8>) | |
# replacement_string: "<p>…</p>" (#<Encoding:UTF-8>) | |
# | |
# original: <div><p>abc</p></div> (#<Encoding:UTF-8>) | |
# replaced (1): <div><p>â¦</p></div> (#<Encoding:UTF-8>) | |
# replaced (2): <p>…</p> (#<Encoding:UTF-8>) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment