Skip to content

Commit

Permalink
Use UTF-8 as default encoding for Node#serialize
Browse files Browse the repository at this point in the history
Node#serialize used to return UTF-8 if no encoding was given.
However this got broken in commit 53f9b66.

Since UTF-8 is the default in XML and HTML5 specs, it makes sense to
use UTF-8 in serialize as well and enforce this in the tests.

Fixes #1659
  • Loading branch information
larskanis committed Sep 20, 2017
1 parent 1fa9d18 commit 076a741
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/nokogiri/xml/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,7 @@ def serialize *args, &block
options[:encoding] = encoding

outstring = String.new
if encoding && outstring.respond_to?(:force_encoding)
outstring.force_encoding(Encoding.find(encoding))
end
outstring.force_encoding(Encoding.find(encoding || 'utf-8'))
io = StringIO.new(outstring)
write_to io, options, &block
io.string
Expand Down
6 changes: 6 additions & 0 deletions test/html/test_node_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def test_serialize_encoding_html
assert_equal @html.serialize, @doc.serialize
end

def test_default_encoding
doc = Nokogiri::HTML(nil)
assert_nil doc.encoding
assert_equal 'UTF-8', doc.serialize.encoding.name
end

def test_encode_special_chars
foo = @html.css('a').first.encode_special_chars('foo')
assert_equal 'UTF-8', foo.encoding.name
Expand Down
6 changes: 6 additions & 0 deletions test/xml/test_node_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ def test_serialize_encoding_xml
assert_equal @xml.serialize, @doc.serialize
end

def test_default_encoding
doc = Nokogiri::XML(VEHICLE_XML)
assert_nil doc.encoding
assert_equal 'UTF-8', doc.serialize.encoding.name
end

def test_encoding_GH_1113
utf8 = '<frag>shahid ὡ 𐄣 𢂁</frag>'
hex = '<frag>shahid &#x1f61; &#x10123; &#x22081;</frag>'
Expand Down

0 comments on commit 076a741

Please sign in to comment.