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

Fix NullPointerException in case a custom document is passed to W3CDom #2114

Merged
merged 1 commit into from
Jul 1, 2024
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
12 changes: 7 additions & 5 deletions src/main/java/org/jsoup/helper/W3CDom.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,14 @@ public W3CBuilder(Document doc) {
namespacesStack.push(new HashMap<>());
dest = doc;
contextElement = (org.jsoup.nodes.Element) doc.getUserData(ContextProperty); // Track the context jsoup Element, so we can save the corresponding w3c element
final org.jsoup.nodes.Document inDoc = contextElement.ownerDocument();
if (namespaceAware && inDoc != null && inDoc.parser().getTreeBuilder() instanceof HtmlTreeBuilder) {
// as per the WHATWG HTML5 spec § 2.1.3, elements are in the HTML namespace by default
namespacesStack.peek().put("", Parser.NamespaceHtml);
if (contextElement != null) {
final org.jsoup.nodes.Document inDoc = contextElement.ownerDocument();
if ( namespaceAware && inDoc != null && inDoc.parser().getTreeBuilder() instanceof HtmlTreeBuilder ) {
// as per the WHATWG HTML5 spec § 2.1.3, elements are in the HTML namespace by default
namespacesStack.peek().put("", Parser.NamespaceHtml);
}
}
}
}

public void head(org.jsoup.nodes.Node source, int depth) {
namespacesStack.push(new HashMap<>(namespacesStack.peek())); // inherit from above on the stack
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/jsoup/helper/W3CDomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
Expand Down Expand Up @@ -187,6 +188,20 @@ public void handlesInvalidTagAsText() {
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><html xmlns=\"http://www.w3.org/1999/xhtml\"><head/><body>&lt;インセンティブで高収入!&gt;Text <p>More</p></body></html>", xml);
}

@Test
public void canConvertToCustomDocument() throws ParserConfigurationException {
org.jsoup.nodes.Document document = Jsoup.parse("<html><div></div></html>");

DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
Document customDocumentResult = localDocumentBuilderFactory.newDocumentBuilder().newDocument();

W3CDom w3cDom = new W3CDom();
w3cDom.convert(document, customDocumentResult);

String html = W3CDom.asString(customDocumentResult, W3CDom.OutputHtml());
assertEquals("<html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body><div></div></body></html>", html);
}

@Test
public void treatsUndeclaredNamespaceAsLocalName() {
String html = "<fb:like>One</fb:like>";
Expand Down
Loading