Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializing unparented HTML node on JRuby #2895

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

---

## next / unreleased

### Fixed

* [JRuby] Fixed NPE when serializing an unparented HTML node. [[#2559](https://github.com/sparklemotion/nokogiri/issues/2559), [#2895](https://github.com/sparklemotion/nokogiri/issues/2895)] (Thanks, [@cbasguti](https://github.com/cbasguti)!)


## 1.15.2 / 2023-05-24

### Dependencies
Expand Down
13 changes: 5 additions & 8 deletions ext/java/nokogiri/internals/SaveContextVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -808,16 +808,13 @@ public int compare(Attr attr0, Attr attr1) {
}

private boolean
isHtmlScript(Text text)
isCDATA(Text text)
{
return htmlDoc && text.getParentNode().getNodeName().equals("script");
Node parentNode = text.getParentNode();
return htmlDoc && parentNode != null && (parentNode.getNodeName().equals("style")
|| parentNode.getNodeName().equals("script"));
}

private boolean
isHtmlStyle(Text text)
{
return htmlDoc && text.getParentNode().getNodeName().equals("style");
}

public boolean
enter(Text text)
Expand All @@ -831,7 +828,7 @@ public int compare(Attr attr0, Attr attr1) {
}
}

if (shouldEncode(text) && !isHtmlScript(text) && !isHtmlStyle(text)) {
if (shouldEncode(text) && !isCDATA(text)) {
textContent = encodeJavaString(textContent);
}

Expand Down
5 changes: 5 additions & 0 deletions test/xml/test_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,11 @@ def test_text_node_robustness_gh1426
end
end

# issue 2559
def test_serialize_unparented_node
assert_equal("asdf", Nokogiri::HTML4::Document.parse("<div></div>").create_text_node("asdf").to_s)
end

describe "#wrap" do
let(:xml) { "<root><thing><div>important thing</div></thing></root>" }
let(:doc) { Nokogiri::XML(xml) }
Expand Down