diff --git a/lib/nokogiri/xml/node_set.rb b/lib/nokogiri/xml/node_set.rb index f5f2015c2a..8d31ea5b9a 100644 --- a/lib/nokogiri/xml/node_set.rb +++ b/lib/nokogiri/xml/node_set.rb @@ -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 diff --git a/test/xml/test_node_set.rb b/test/xml/test_node_set.rb index fb81d6ffbe..18fb2b8b90 100644 --- a/test/xml/test_node_set.rb +++ b/test/xml/test_node_set.rb @@ -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