Backtestsjsouprun-20261006-181938#2560
W3CDom throws NPE for element names with invalid XML starts
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Patch B is the better fix: both resolve the reported NPE, but A widens the change into attribute handling without testing it, while B keeps the fix confined to tag names. **Correctness.** - Both patches stop the NPE by normalizing `1abc` to `_1abc` when the name cannot start an XML name. - Patch A puts the fallback inside the shared `w3cSafeName` and deletes the `if (key == null) return;` guard in `setAttribute`. Attributes that were previously skipped as unrepresentable are now emitted with a `_` prefix, or as a bare `_` as the last resort. - That contradicts the existing changelog note that unrepresentable attributes are skipped. It also allows several attributes to collapse to the same `_` key, or to collide with declaration handling. - None of this attribute-path change is tested. - Patch B fixes the root helper `Normalizer.xmlSafeTagName`, which is the shared tag-name normalizer, and applies it before QName normalization for element names only. Attribute behavior is unchanged. - B also fixes the same root cause in `Element.safeTagName`, where XML-syntax serialization previously emitted `<null>`. That is a closely related symptom of the same null return, and it carries little risk. **Tests.** - Patch A tests `1abc` and `-abc` with a nested child and checks parentage. - Patch B tests a renamed root (`1abc`), a prefixed name with an invalid local part (`p:2x` → `p:_2x`), and text content and serialized output. - B also covers a programmatically created element (`9lives`) in an HTML document, matching both scenarios in the issue (created and renamed elements). - B adds an `ElementTest` showing XML serialization is fixed while HTML syntax is unchanged. - B's tests are broader and exercise the prefix case. **Scope and craft.** - Patch A's CHANGES entry is a plus, but its silent change to attribute semantics adds risk. - Patch B's changes are cohesive: fix the helper, make it non-null, and update its callers.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
7m 39s
Wall-clock time in the container
Full test suite
No new failures
2,154 passed · 0 skipped · 2,154 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
61vs 28 merged
Changed lines, ours vs merged
The issue
View on GitHubW3CDom conversion throws a NullPointerException when a programmatically created or renamed element has a name that cannot start an XML name, such as 1abc.
Document doc = Jsoup.parse("<root/>", "", Parser.xmlParser());
doc.firstElementChild().tagName("1abc");
new W3CDom().fromJsoup(doc);
Attribute.getValidKey() returns null, which is then dereferenced by W3CDom.
The name should instead be normalized to a valid XML name, such as _1abc.
Raw <1abc/> input is already treated as text by the parser, so this applies specifically to DOM construction and mutation.
The two patches
src/main/java/org/jsoup/helper/W3CDom.javaindex 3f7f2490..01ae7cee 100644@@ -2,6 +2,7 @@ package org.jsoup.helper; import org.jsoup.internal.NamespaceBindings; import org.jsoup.internal.StringUtil;+import org.jsoup.internal.Normalizer; import org.jsoup.nodes.Attribute; import org.jsoup.nodes.Attributes; import org.jsoup.parser.Parser;@@ -373,7 +374,9 @@ public class W3CDom { outputNamespaces.pushScope(); sourceNamespaces.applyDeclarations(sourceEl.attributes()); @Nullable String namespace = namespaceAware ? w3cNamespace(sourceEl) : null;- String tagName = w3cSafeName(sourceEl.tagName(), Syntax.xml);+ // normalize to a valid XML name first (e.g. 1abc to _1abc), so the QName normalization can't yield null+ String tagName = w3cSafeName(Normalizer.xmlSafeTagName(sourceEl.tagName()), Syntax.xml);+ if (tagName == null) tagName = "_"; // not expected, as the input is already a valid XML name try { // use an empty namespace if none is present but the tag name has a prefix String imputedNamespace = namespace == null && tagName.contains(":") ? "" : namespace;src/main/java/org/jsoup/internal/Normalizer.javaindex 9fe85df3..76f67844 100644@@ -31,8 +31,15 @@ public final class Normalizer { return isStringLiteral ? lowerCase(input) : normalize(input); } - /** Minimal helper to get an otherwise OK HTML name like "foo<bar" to "foo_bar". */- @Nullable public static String xmlSafeTagName(final String tagname) {- return Attribute.getValidKey(tagname, Document.OutputSettings.Syntax.xml); // Reuses the Attribute key normal, which is same for xml tag names+ /**+ Minimal helper to get an otherwise OK HTML name like "foo<bar" to "foo_bar", or a name that cannot start an XML+ name like "1abc" to "_1abc".+ */+ public static String xmlSafeTagName(final String tagname) {+ // Reuses the Attribute key normal, which is same for xml tag names+ @Nullable String safe = Attribute.getValidKey(tagname, Document.OutputSettings.Syntax.xml);+ if (safe == null) // invalid start char (or empty); prefix so it can start an XML name+ safe = Attribute.getValidKey("_" + tagname, Document.OutputSettings.Syntax.xml);+ return safe != null ? safe : "_"; } }src/main/java/org/jsoup/nodes/Element.javaindex efc34a67..d015eb1c 100644@@ -2023,8 +2023,8 @@ public class Element extends Node implements Iterable<Element> { // if empty, we have already closed in htmlHead } - /* If XML syntax, normalizes < to _ in tag name. */- @Nullable private String safeTagName(Document.OutputSettings.Syntax syntax) {+ /* If XML syntax, normalizes the tag name to a valid XML name (e.g. < to _, or 1abc to _1abc). */+ private String safeTagName(Document.OutputSettings.Syntax syntax) { return syntax == xml ? Normalizer.xmlSafeTagName(tagName()) : tagName(); } src/test/java/org/jsoup/helper/W3CDomTest.javaindex 14ca00f9..4fd6c582 100644@@ -242,6 +242,33 @@ public class W3CDomTest { assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><html xmlns=\"http://www.w3.org/1999/xhtml\"><head/><body><インセンティブで高収入!>Text <p>More</p></body></html>", xml); } + @Test void normalizesProgrammaticTagNameWithInvalidXmlStart() {+ // a renamed element may have a name that cannot start an XML name; it should be prefixed, not NPE+ org.jsoup.nodes.Document jdoc = Jsoup.parse("<root><child>Text</child></root>", "", Parser.xmlParser());+ jdoc.expectFirst("root").tagName("1abc");+ jdoc.expectFirst("child").tagName("p:2x");++ Document w3Doc = new W3CDom().fromJsoup(jdoc);+ org.w3c.dom.Element root = w3Doc.getDocumentElement();+ assertEquals("_1abc", root.getTagName());+ org.w3c.dom.Element child = (org.w3c.dom.Element) root.getFirstChild();+ assertEquals("p:_2x", child.getTagName());+ assertEquals("Text", child.getTextContent());+ String xml = W3CDom.asString(w3Doc, W3CDom.OutputXml());+ assertTrue(xml.contains("<_1abc>"), xml);+ assertTrue(xml.contains(">Text</p:_2x></_1abc>"), xml);+ }++ @Test void normalizesCreatedElementWithInvalidXmlStart() {+ org.jsoup.nodes.Document jdoc = Jsoup.parseBodyFragment("<div></div>");+ jdoc.expectFirst("div").appendChild(new Element("9lives").text("cat"));++ Document w3Doc = new W3CDom().fromJsoup(jdoc);+ NodeList els = w3Doc.getElementsByTagNameNS("*", "_9lives");+ assertEquals(1, els.getLength());+ assertEquals("cat", els.item(0).getTextContent());+ }+ @Test void handlesHtmlElsWithLt() { // In HTML, elements can be named "foo<bar" (<foo<bar>). Test that we can convert to W3C, that we can HTML parse our HTML serial, XML parse our XML serial, and W3C XML parse the XML serial and the W3C serial // And similarly attributes may have "<" in their namesrc/test/java/org/jsoup/nodes/ElementTest.javaindex 229fe210..6a476997 100644@@ -3359,6 +3359,18 @@ public class ElementTest { assertEquals("<p_>One</p_>", xml); } + @Test void xmlSyntaxNormalizesTagNameWithInvalidXmlStart() {+ // previously serialized as <null>; the name should be prefixed to a valid XML name+ Document doc = Jsoup.parse("<root><a>One</a></root><empty></empty>", "", Parser.xmlParser());+ doc.outputSettings().prettyPrint(false);+ doc.expectFirst("root").tagName("1abc");+ doc.expectFirst("empty").tagName("2e");+ assertEquals("<_1abc><a>One</a></_1abc><_2e></_2e>", doc.html());++ doc.outputSettings().syntax(Document.OutputSettings.Syntax.html);+ assertEquals("<1abc><a>One</a></1abc><2e></2e>", doc.html()); // html syntax unchanged+ }+ @Test public void invalidCharactersDiscardedInXml() { // https://github.com/jhy/jsoup/issues/1743
CHANGES.mdindex 74d5272c..dfe7b1f9 100644@@ -15,6 +15,7 @@ * Namespace declarations now apply regardless of attribute order, and an empty declaration shadows an inherited binding only within its scope. * With namespace awareness disabled, inherited and undeclared prefixes now receive the declarations needed for XML serialization. * Valid HTML names that are not XML QNames, such as `a:b:c`, are normalized. Attributes that still cannot be represented are skipped, and unrepresentable elements no longer change the surrounding tree.+* Fixed `W3CDom` conversion of programmatically created or renamed elements whose names can be represented in a jsoup HTML DOM but are not valid XML names, such as `1abc`. These names are now normalized (e.g. `_1abc`) instead of causing a `NullPointerException`. [#2560](https://github.com/jhy/jsoup/issues/2560) * Fixed the JDK `HttpClient` implementation to accept responses without a `Content-Type` header, matching the `HttpURLConnection` implementation. [#2549](https://github.com/jhy/jsoup/pull/2549) * Fixed HTTP response content-type matching to handle media types case-insensitively and recognize structured `+xml` suffixes, including vendor-specific media types. [#2550](https://github.com/jhy/jsoup/pull/2550) * Corrected multipart form encoding to percent-escape CR and LF in field names and filenames, matching the HTML form submission specification. Multipart file content-types containing CR or LF are now rejected with a `ValidationException`. [#2555](https://github.com/jhy/jsoup/pull/2555)src/main/java/org/jsoup/helper/W3CDom.javaindex 3f7f2490..bebc53de 100644@@ -473,7 +473,6 @@ public class W3CDom { /** Copies an attribute with its resolved namespace and W3C-safe name. */ private void setAttribute(Element wEl, Attribute attribute, Syntax syntax) throws DOMException { String key = w3cSafeName(attribute.getKey(), syntax);- if (key == null) return; @Nullable String declarationPrefix = NamespaceBindings.declarationPrefix(key); if (declarationPrefix != null) {@@ -507,10 +506,13 @@ public class W3CDom { ensureOutputBinding(wEl, prefix, namespace); } - /** Normalizes a name to a W3C-compatible QName, converting {@code a:b:c} to {@code a:b_c}. */- private @Nullable String w3cSafeName(String name, Syntax syntax) {+ /** Normalizes a name to a W3C-compatible QName, converting {@code 1:a:b} to {@code _1:a_b}. */+ private String w3cSafeName(String name, Syntax syntax) { @Nullable String normalized = Attribute.getValidKey(name, syntax);- if (normalized == null) return null;+ if (normalized == null) {+ normalized = Attribute.getValidKey("_" + name, syntax);+ if (normalized == null) return "_";+ } if (normalized.indexOf(':') == -1) return normalized; Matcher parts = QNameParts.matcher(normalized);src/test/java/org/jsoup/helper/W3CDomTest.javaindex 14ca00f9..ae65b25a 100644@@ -698,6 +698,23 @@ public class W3CDomTest { assertSame(main, p.getParentNode()); } + @Test void normalizesElementNamesWithInvalidXmlStarts() {+ // DOM mutation can create names the parser rejects; conversion must still produce valid XML names+ org.jsoup.nodes.Document jdoc = Jsoup.parse("<root><first><child/></first><second/></root>", "", Parser.xmlParser());+ jdoc.expectFirst("first").tagName("1abc");+ jdoc.expectFirst("second").tagName("-abc");++ org.w3c.dom.Document w3CDoc = new W3CDom().fromJsoup(jdoc);+ org.w3c.dom.Element root = w3CDoc.getDocumentElement();+ org.w3c.dom.Element first = (org.w3c.dom.Element) w3CDoc.getElementsByTagName("_1abc").item(0);+ org.w3c.dom.Element child = (org.w3c.dom.Element) w3CDoc.getElementsByTagName("child").item(0);+ org.w3c.dom.Element second = (org.w3c.dom.Element) w3CDoc.getElementsByTagName("_-abc").item(0);++ assertSame(root, first.getParentNode());+ assertSame(first, child.getParentNode());+ assertSame(root, second.getParentNode());+ }+ @Test void preservesValidQNameCharacters() { // Unicode letters are valid starts; other valid characters are retained by prepending a safe start String html = "<html xmlns:a='urn:a'><body><div a:é='unicode' a:_b='underscore' a:9b='digit'></div></body></html>";