Skip to content

Commit

Permalink
fix: empty node set serialization when document encoding is nil
Browse files Browse the repository at this point in the history
Fixes #2784

(backport)
  • Loading branch information
flavorjones committed Feb 8, 2023
1 parent f6cecec commit f13cdb4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/nokogiri/xml/node_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,9 @@ def to_html(*args)
args.insert(0, options)
end
if empty?
encoding = (args.first.is_a?(Hash) ? args.first[:encoding] : nil) || document.encoding
"".encode(encoding)
encoding = (args.first.is_a?(Hash) ? args.first[:encoding] : nil)
encoding ||= document.encoding
encoding.nil? ? "" : "".encode(encoding)
else
map { |x| x.to_html(*args) }.join
end
Expand Down
6 changes: 6 additions & 0 deletions test/xml/test_node_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,12 @@ def awesome!; end
assert_equal(doc2, node_set[1].document)
end
end

describe "empty sets" do
it "#to_html returns an empty string" do
assert_equal("", NodeSet.new(xml, []).to_html)
end
end
end
end
end
Expand Down

0 comments on commit f13cdb4

Please sign in to comment.