Skip to content

Commit

Permalink
Merge pull request #2753 from larskanis/fix-2752
Browse files Browse the repository at this point in the history
Fix writing to an IO with no external_encoding
  • Loading branch information
flavorjones committed Jan 4, 2023
2 parents a6e369c + 795464c commit b57c926
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ext/nokogiri/nokogiri.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ noko_io_write(void *io, char *c_buffer, int c_buffer_len)
{
VALUE rb_args[2], rb_n_bytes_written;
VALUE rb_io = (VALUE)io;
rb_encoding *io_encoding = rb_to_encoding(rb_funcall(rb_io, id_external_encoding, 0));
VALUE rb_enc = rb_funcall(rb_io, id_external_encoding, 0);
rb_encoding *io_encoding = RB_NIL_P(rb_enc) ? rb_ascii8bit_encoding() : rb_to_encoding(rb_enc);

rb_args[0] = rb_io;
rb_args[1] = rb_enc_str_new(c_buffer, (long)c_buffer_len, io_encoding);
Expand Down
8 changes: 8 additions & 0 deletions test/xml/test_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,14 @@ def test_write_to_with_block
end
end

def test_write_to_file_without_encoding
Tempfile.create do |io|
xml.write_to(io)
io.rewind
assert_equal(xml.to_xml, io.read)
end
end

def test_serialize_with_block
called = false
conf = nil
Expand Down

0 comments on commit b57c926

Please sign in to comment.