Skip to content

Commit

Permalink
Merge pull request #2791 from sparklemotion/2784-encoding-empty-strin…
Browse files Browse the repository at this point in the history
…gs-v1.14.x

fix: empty node set serialization when document encoding is nil (backport)
  • Loading branch information
flavorjones committed Feb 8, 2023
2 parents f6cecec + 975ae49 commit 5309477
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA

---

## 1.14.next / unreleased

### Fixed

* Calling `NodeSet#to_html` on an empty node set no longer raises an encoding-related exception. This bug was introduced in v1.14.0 while fixing [#2649](https://github.com/sparklemotion/nokogiri/issues/2649). [[#2784](https://github.com/sparklemotion/nokogiri/issues/2784)]


## 1.14.1 / 2023-01-30

### Fixed
Expand Down
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 5309477

Please sign in to comment.