Skip to content

Commit

Permalink
fix(jruby): XHTML formatting of non-container element closing tags
Browse files Browse the repository at this point in the history
The JRuby code for SaveContextVisitor is brittle and clearly
buggy (since this bug exists for fragment nodes but not fragments or
document nodes?), but this seems like an easy fix.

Fixes #2355
  • Loading branch information
flavorjones committed Nov 6, 2021
1 parent 3800769 commit f7d847f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA
* [CRuby] Fix memory leak in XPath custom handlers where string arguments were not being cleaned up. [[#2345](https://github.com/sparklemotion/nokogiri/issues/2345)]
* [CRuby] Fix memory leak in `Reader#base_uri` where the string returned by libxml2 was not freed. [[#2347](https://github.com/sparklemotion/nokogiri/issues/2347)]
* [JRuby] Deleting a `Namespace` from a `NodeSet` no longer modifies the `href` to be the default namespace URL.
* [JRuby] Fix XHTML formatting of closing tags for non-container elements. [[#2355](https://github.com/sparklemotion/nokogiri/issues/2355)]


### Improved
Expand Down
2 changes: 2 additions & 0 deletions ext/java/nokogiri/internals/SaveContextVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,8 @@ public int compare(Attr attr0, Attr attr1) {
if (!isEmpty(name) && noEmpty) {
buffer.append("</").append(name).append('>');
}
} else if (asXhtml && !isEmpty(name)) {
buffer.append("</").append(name).append('>');
}
if (needBreakInClosing(element)) {
if (!containsText(element)) { indentation.pop(); }
Expand Down
6 changes: 6 additions & 0 deletions test/html4/test_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ def test_GH_1042
# are not set. the fix for GH1042 ensures a proper working clone.
trs.inspect # assert_nothing_raised
end

def test_fragment_node_to_xhtml # see #2355
html = "<h1><a></a><br>First H1</h1>"
fragment = Nokogiri::HTML4::DocumentFragment.parse(html)
assert_equal("<h1><a></a><br />First H1</h1>", fragment.at_css("h1").to_xhtml)
end
end
end
end

0 comments on commit f7d847f

Please sign in to comment.