Skip to content

Commit

Permalink
fix: support Encoding class for write_to
Browse files Browse the repository at this point in the history
  • Loading branch information
ellaklara authored and flavorjones committed Feb 28, 2023
1 parent 343c792 commit 7696d6a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/nokogiri/xml/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,8 @@ def write_to(io, *options)
config = SaveOptions.new(save_options.to_i)
yield config if block_given?

encoding = encoding.is_a?(Encoding) ? encoding.name : encoding

native_write_to(io, encoding, indentation, config.options)
end

Expand Down
17 changes: 17 additions & 0 deletions test/xml/test_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module XML
class TestNode < Nokogiri::TestCase
describe Nokogiri::XML::Node do
let(:xml) { Nokogiri::XML(File.read(XML_FILE), XML_FILE) }
let(:xml_UTF_8) { Nokogiri::XML(File.read(XML_ATOM_FILE), XML_ATOM_FILE) }

def test_first_element_child
node = xml.root.first_element_child
Expand Down Expand Up @@ -679,6 +680,22 @@ def test_write_to_with_block
end
end

def test_write_to_file_with_encoding_string
Tempfile.create do |io|
xml_UTF_8.write_to(io, encoding: "UTF-8")
io.rewind
assert_equal(xml_UTF_8.to_xml, io.read)
end
end

def test_write_to_file_with_encoding_class
Tempfile.create do |io|
xml_UTF_8.write_to(io, encoding: Encoding::UTF_8)
io.rewind
assert_equal(xml_UTF_8.to_xml, io.read)
end
end

def test_write_to_file_without_encoding
Tempfile.create do |io|
xml.write_to(io)
Expand Down

0 comments on commit 7696d6a

Please sign in to comment.