Backtestsjsouprun-20261006-181938#2573
XML serialization drops attributes whose names start with a digit or hyphen
The merged fix was judged better than our patch.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fix the root cause the same way. Each changes `Attribute.getValidKey` so that an XML key with an invalid first character (digit, hyphen, dot) gets a leading `_` instead of returning null. That fixes the dropped attributes in the issue (`1a` → `_1a`, `-a` → `_-a`). Because element tag names go through the same helper, both also repair invalid element names, and both simplify the W3CDom fallback paths that this change makes redundant. The difference is correctness at an edge the fix itself creates. Once invalid names are repaired by prefixing `_`, a repaired name can collide with an existing valid one. For example, `<root 1a='one' _1a='two'>` under Patch A serializes as `_1a="one" _1a="two"`. Duplicate attributes are not well-formed XML, so the output is still broken, just in a different way. Patch B addresses this directly in `Attributes.html`: - The set of source keys is built lazily, only when a repair actually happens, so ordinary output does not allocate. - Valid source names always keep their name, regardless of attribute order. - A repaired name gets extra `_` prefixes until it is unique. - The comparison is case-insensitive in HTML syntax and case-sensitive in XML. Patch B also follows the issue's request to simplify and unify the normalization flow. `getValidKey` becomes non-null; the null checks in `Attribute.html`, `Element.safeTagName` and W3CDom go away; and `Normalizer.xmlSafeTagName` becomes a deprecated delegate. Patch A keeps the nullable contract (null for an empty key), so the null-handling branches stay. On tests, both add tests that fail before the change and pass after: XML attribute repair and programmatic tag repair. Patch A also checks that HTML syntax output is unchanged and unit-tests `getValidKey`. Patch B tests the collision handling in both XML and HTML, plus element-name repair. Patch B has some churn: a `StringUtil.concat` helper, a `keysEqual` refactor, and empty keys now serializing as `_` rather than being skipped. None of this adds meaningful risk. Overall, B's output stays well-formed in cases where A's does not, and B delivers the requested unification, so B is the better fix.
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 38s
Wall-clock time in the container
Full test suite
No new failures
2,167 passed · 0 skipped · 2,167 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
77vs 154 merged
Changed lines, ours vs merged
The issue
View on GitHubWhen XML input contains an attribute whose name starts with a digit or hyphen, jsoup retains the attribute and its value in the DOM but silently drops it during serialization.
For example:
Document doc = Jsoup.parse("<root 1a='one' -a='two' />", Parser.xmlParser());
System.out.println(doc.html());
This currently outputs:
<root />
It will be better to repair the attribute names with a leading underscore, preserving the attribute name and values.
<root _1a="one" _-a="two" />
Also we can simplify and unify the HTML and XML normalization flow.
The two patches
CHANGES.mdindex 88764bdf..36eb1b1c 100644@@ -20,6 +20,7 @@ * 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 XML serialization silently dropping attributes whose names start with a digit or hyphen, such as `1a` or `-a`. These names (and similarly invalid element names) are now repaired with a leading underscore, e.g. `_1a="one"`, preserving the attribute and its value. [#2573](https://github.com/jhy/jsoup/issues/2573) * Fixed XML doctype serialization when a system identifier contains a double quote, which could otherwise produce invalid XML. [#2571](https://github.com/jhy/jsoup/issues/2571) * 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)src/main/java/org/jsoup/helper/W3CDom.javaindex 1ec3c2b6..2595b8e8 100644@@ -546,10 +546,7 @@ public class W3CDom { /** 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) {- normalized = Attribute.getValidKey("_" + name, syntax);- if (normalized == null) return "_";- }+ if (normalized == null) return "_"; // only null when empty if (normalized.indexOf(':') == -1) return normalized; Matcher parts = QNameParts.matcher(normalized);@@ -565,8 +562,6 @@ public class W3CDom { String colonSafe = name.replace(':', '_'); @Nullable String normalized = Attribute.getValidKey(colonSafe, Syntax.xml);- if (normalized != null) return normalized;- normalized = Attribute.getValidKey("_" + colonSafe, Syntax.xml); return normalized != null ? normalized : "_"; } src/main/java/org/jsoup/nodes/Attribute.javaindex 3ca0057e..88cac2ff 100644@@ -221,22 +221,26 @@ public class Attribute implements Map.Entry<String, String>, Cloneable { private static final Pattern htmlKeyReplace = Pattern.compile("[\\x00-\\x1f\\x7f-\\x9f \"'/=]+"); /** * Get a valid attribute key for the given syntax. If the key is not valid, it will be coerced into a valid key.+ * Invalid characters are replaced with "_"; and in XML, if the key does not start with a valid name start+ * character (e.g. it starts with a digit or hyphen), it is prefixed with "_". * @param key the original attribute key * @param syntax HTML or XML- * @return the original key if it's valid; a key with invalid characters replaced with "_" otherwise; or null if a valid key could not be created.+ * @return the original key if it's valid; a coerced valid key otherwise; or null if the key is empty. */ @Nullable public static String getValidKey(String key, Syntax syntax) {- if (syntax == Syntax.xml && !isValidXmlKey(key)) {- key = xmlKeyReplace.matcher(key).replaceAll("_");- return isValidXmlKey(key) ? key : null; // null if could not be coerced- }- else if (syntax == Syntax.html && !isValidHtmlKey(key)) {- key = htmlKeyReplace.matcher(key).replaceAll("_");- return isValidHtmlKey(key) ? key : null; // null if could not be coerced- }+ final boolean xml = syntax == Syntax.xml;+ if (xml ? isValidXmlKey(key) : isValidHtmlKey(key)) return key;+ if (key.isEmpty()) return null; // can't coerce an empty key+ key = (xml ? xmlKeyReplace : htmlKeyReplace).matcher(key).replaceAll("_");+ if (xml && !isXmlNameStart(key.charAt(0)))+ key = "_" + key; // e.g. "1a" or "-a" -> "_1a", "_-a" return key; } + private static boolean isXmlNameStart(char c) {+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == ':';+ }+ // perf critical in html() so using manual scan vs regex: // note that we aren't using anything in supplemental space, so OK to iter charAt private static boolean isValidXmlKey(String key) {@@ -244,7 +248,7 @@ public class Attribute implements Map.Entry<String, String>, Cloneable { final int length = key.length(); if (length == 0) return false; char c = key.charAt(0);- if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == ':'))+ if (!isXmlNameStart(c)) return false; for (int i = 1; i < length; i++) { c = key.charAt(i);src/test/java/org/jsoup/nodes/AttributeTest.javaindex 4cff3205..9b1b3e54 100644@@ -116,4 +116,30 @@ public class AttributeTest { Attribute attr = new Attribute("one", "two"); assertEquals("", attr.namespace()); }++ @Test void getValidKeyCoercesInvalidNames() {+ assertEquals("_1a", Attribute.getValidKey("1a", Document.OutputSettings.Syntax.xml));+ assertEquals("_-a", Attribute.getValidKey("-a", Document.OutputSettings.Syntax.xml));+ assertEquals("_1a_b", Attribute.getValidKey("1a b", Document.OutputSettings.Syntax.xml));+ assertEquals("_", Attribute.getValidKey("\"", Document.OutputSettings.Syntax.xml));+ assertEquals("a_b", Attribute.getValidKey("a\"b", Document.OutputSettings.Syntax.xml));+ assertEquals("ok:a-1", Attribute.getValidKey("ok:a-1", Document.OutputSettings.Syntax.xml));+ assertNull(Attribute.getValidKey("", Document.OutputSettings.Syntax.xml));++ assertEquals("1a", Attribute.getValidKey("1a", Document.OutputSettings.Syntax.html));+ assertEquals("-a", Attribute.getValidKey("-a", Document.OutputSettings.Syntax.html));+ assertEquals("a_b", Attribute.getValidKey("a b", Document.OutputSettings.Syntax.html));+ assertNull(Attribute.getValidKey("", Document.OutputSettings.Syntax.html));+ }++ @Test void xmlAttributeHtmlPrefixesInvalidStart() {+ Document doc = Jsoup.parse("<root 1a='one' />", Parser.xmlParser());+ Attribute attr = doc.expectFirst("root").attribute("1a");+ assertNotNull(attr);+ assertEquals("1a=\"one\"", attr.html()); // default output settings are html syntax++ StringBuilder sb = new StringBuilder();+ attr.html(org.jsoup.internal.QuietAppendable.wrap(sb), doc.outputSettings()); // xml syntax+ assertEquals("_1a=\"one\"", sb.toString());+ } }src/test/java/org/jsoup/parser/XmlTreeBuilderTest.javaindex e1a0e68b..fbada134 100644@@ -286,6 +286,25 @@ public class XmlTreeBuilderTest { assertEquals("<a bB1-_:.=\"foo\" _9_=\"bar\" xmlns:p1=\"qux\">One</a>", out); // first is same, second coerced } + @Test void xmlOutputPrefixesAttributeNamesWithInvalidStart() {+ Document doc = Jsoup.parse("<root 1a='one' -a='two' .b='three' 9='four' />", Parser.xmlParser());+ Element root = doc.expectFirst("root");+ assertEquals("one", root.attr("1a")); // retained as-is in DOM+ assertEquals("<root _1a=\"one\" _-a=\"two\" _.b=\"three\" _9=\"four\" />", doc.html());+ }++ @Test void xmlOutputPrefixesTagNamesWithInvalidStart() {+ Document doc = Jsoup.parse("<root></root>", Parser.xmlParser());+ doc.expectFirst("root").appendElement("1a").attr("2b", "x").appendElement("-c").text("Hi");+ assertEquals("<root><_1a _2b=\"x\"><_-c>Hi</_-c></_1a></root>", doc.html());+ }++ @Test void htmlSyntaxRetainsAttributeNamesWithDigitStart() {+ Document doc = Jsoup.parse("<root 1a='one' -a='two' />", Parser.xmlParser());+ doc.outputSettings().syntax(Syntax.html);+ assertEquals("<root 1a=\"one\" -a=\"two\" />", doc.html());+ }+ @Test void customTagsAreFlyweights() { String xml = "<foo>Foo</foo><foo>Foo</foo><FOO>FOO</FOO><FOO>FOO</FOO>"; Document doc = Jsoup.parse(xml, Parser.xmlParser());
CHANGES.mdindex 88764bdf..117558e1 100644@@ -8,6 +8,7 @@ * Improved `W3CDom` conversion performance for documents with many nested namespace declarations. The W3C converter now uses the same optimized namespace tracking as the XML parser. [#2559](https://github.com/jhy/jsoup/pull/2559) * Improved `W3CDom` XML conversion to retain processing instructions, comments outside the root element, and CDATA sections, which were previously dropped or converted to text. [#2572](https://github.com/jhy/jsoup/issues/2572) * DOM mutation methods, including child insertion and replacement, now reject operations that would create a cycle, such as making a node its own child or moving an ancestor beneath a descendant. [#2552](https://github.com/jhy/jsoup/issues/2552)+* XML serialization now repairs element and attribute names that start with an invalid character, rather than outputting `<null>` elements or dropping attributes. For example, an attribute named `1a` is written as `_1a`. Additional leading underscores keep repaired attribute names unique if they conflict with another attribute. [#2573](https://github.com/jhy/jsoup/issues/2573) ### Changes * Aligned the XML parser stack depth and lookups to the configured maximum, which now defaults to 512 for both HTML and XML. [#2570](https://github.com/jhy/jsoup/pull/2570)src/main/java/org/jsoup/helper/W3CDom.javaindex 1ec3c2b6..2e399c7a 100644@@ -545,11 +545,7 @@ public class W3CDom { /** 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) {- normalized = Attribute.getValidKey("_" + name, syntax);- if (normalized == null) return "_";- }+ String normalized = Attribute.getValidKey(name, syntax); if (normalized.indexOf(':') == -1) return normalized; Matcher parts = QNameParts.matcher(normalized);@@ -560,14 +556,11 @@ public class W3CDom { /** Normalizes one QName component while preserving valid XML name characters. */ private String w3cSafeNcName(String name) { if (isValidNcName(name)) return name;- String safeStart = "_" + name;+ String safeStart = StringUtil.concat('_', name); if (isValidNcName(safeStart)) return safeStart; String colonSafe = name.replace(':', '_');- @Nullable String normalized = Attribute.getValidKey(colonSafe, Syntax.xml);- if (normalized != null) return normalized;- normalized = Attribute.getValidKey("_" + colonSafe, Syntax.xml);- return normalized != null ? normalized : "_";+ return Attribute.getValidKey(colonSafe, Syntax.xml); } /** Tests a component against the XML NCName rules used by the output DOM. */src/main/java/org/jsoup/internal/Normalizer.javaindex 9fe85df3..8d51f69d 100644@@ -2,7 +2,6 @@ package org.jsoup.internal; import org.jsoup.nodes.Attribute; import org.jsoup.nodes.Document;-import org.jspecify.annotations.Nullable; import java.util.Locale; @@ -31,8 +30,13 @@ 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+ /**+ * Gets an XML-safe tag name.+ * @deprecated Internal helper; use {@link Attribute#getValidKey(String, Document.OutputSettings.Syntax)}.+ * Will be removed in jsoup 1.24.1.+ */+ @Deprecated+ public static String xmlSafeTagName(final String tagName) {+ return Attribute.getValidKey(tagName, Document.OutputSettings.Syntax.xml); } }src/main/java/org/jsoup/internal/StringUtil.javaindex 33193084..83ab2298 100644@@ -348,6 +348,18 @@ public final class StringUtil { private static final SoftPool<StringBuilder> BuilderPool = new SoftPool<>( () -> new StringBuilder(InitBuilderSize)); + /**+ Concatenates a character and string using a pooled builder.+ @param first the leading character+ @param second the following string+ @return the concatenated string+ */+ public static String concat(char first, String second) {+ StringBuilder builder = borrowBuilder();+ builder.append(first).append(second);+ return releaseBuilder(builder);+ }+ /** * Maintains cached StringBuilders in a flyweight pattern, to minimize new StringBuilder GCs. The StringBuilder is * prevented from growing too large.src/main/java/org/jsoup/nodes/Attribute.javaindex 3ca0057e..cbea8238 100644@@ -191,7 +191,6 @@ public class Attribute implements Map.Entry<String, String>, Cloneable { static void html(String key, @Nullable String val, QuietAppendable accum, Document.OutputSettings out) { key = getValidKey(key, out.syntax());- if (key == null) return; // can't write it :( htmlNoValidate(key, val, accum, out); } @@ -219,20 +218,23 @@ public class Attribute implements Map.Entry<String, String>, Cloneable { private static final Pattern xmlKeyReplace = Pattern.compile("[^-a-zA-Z0-9_:.]+"); private static final Pattern htmlKeyReplace = Pattern.compile("[\\x00-\\x1f\\x7f-\\x9f \"'/=]+");+ /**- * Get a valid attribute key for the given syntax. If the key is not valid, it will be coerced into a valid key.- * @param key the original attribute key- * @param syntax HTML or XML- * @return the original key if it's valid; a key with invalid characters replaced with "_" otherwise; or null if a valid key could not be created.+ Get a valid key for the output syntax. Invalid character runs are replaced with {@code _}, and XML keys with+ invalid starts are prefixed with {@code _}.++ @param key the input key+ @param syntax HTML or XML+ @return a valid key for the output syntax */- @Nullable public static String getValidKey(String key, Syntax syntax) {+ public static String getValidKey(String key, Syntax syntax) {+ if (key.isEmpty()) return "_"; if (syntax == Syntax.xml && !isValidXmlKey(key)) { key = xmlKeyReplace.matcher(key).replaceAll("_");- return isValidXmlKey(key) ? key : null; // null if could not be coerced- }- else if (syntax == Syntax.html && !isValidHtmlKey(key)) {+ if (!isValidXmlKeyStart(key.charAt(0)))+ key = StringUtil.concat('_', key);+ } else if (syntax == Syntax.html && !isValidHtmlKey(key)) { key = htmlKeyReplace.matcher(key).replaceAll("_");- return isValidHtmlKey(key) ? key : null; // null if could not be coerced } return key; }@@ -244,8 +246,7 @@ public class Attribute implements Map.Entry<String, String>, Cloneable { final int length = key.length(); if (length == 0) return false; char c = key.charAt(0);- if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == ':'))- return false;+ if (!isValidXmlKeyStart(c)) return false; for (int i = 1; i < length; i++) { c = key.charAt(i); if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_' || c == ':' || c == '.'))@@ -254,6 +255,11 @@ public class Attribute implements Map.Entry<String, String>, Cloneable { return true; } + /** Check that the character can start an XML name */+ private static boolean isValidXmlKeyStart(char c) {+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == ':';+ }+ private static boolean isValidHtmlKey(String key) { // =~ [\x00-\x1f\x7f-\x9f "'/=]+ final int length = key.length();src/main/java/org/jsoup/nodes/Attributes.javaindex ace37da9..acaf8ba7 100644@@ -4,6 +4,7 @@ import org.jsoup.helper.Validate; import org.jsoup.internal.QuietAppendable; import org.jsoup.internal.SharedConstants; import org.jsoup.internal.StringUtil;+import org.jsoup.nodes.Document.OutputSettings.Syntax; import org.jsoup.parser.ParseSettings; import org.jspecify.annotations.Nullable; @@ -14,6 +15,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.ConcurrentModificationException; import java.util.HashMap;+import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map;@@ -22,6 +24,7 @@ import java.util.Objects; import java.util.Set; import static org.jsoup.internal.Normalizer.lowerCase;+import static org.jsoup.nodes.Document.OutputSettings.Syntax.xml; import static org.jsoup.nodes.Range.AttributeRange.UntrackedAttr; /**@@ -562,15 +565,45 @@ public class Attributes implements Iterable<Attribute>, Cloneable { final void html(final QuietAppendable accum, final Document.OutputSettings out) { final int sz = size;+ final Syntax syntax = out.syntax();+ @Nullable Set<String> usedKeys = null; // avoid allocation for normal serialization for (int i = 0; i < sz; i++) { String key = keys[i]; assert key != null; if (isInternalKey(key)) continue;- final String validated = Attribute.getValidKey(key, out.syntax());- if (validated != null)- Attribute.htmlNoValidate(validated, (String) vals[i], accum.append(' '), out);+ String validated = Attribute.getValidKey(key, syntax);+ if (!validated.equals(key)) {+ if (usedKeys == null)+ usedKeys = collectSourceKeys(syntax); // valid source names win regardless of attribute order+ // preserve repaired attributes without outputting duplicate names+ while (!usedKeys.add(comparisonKey(validated, syntax)))+ validated = StringUtil.concat('_', validated);+ }+ Attribute.htmlNoValidate(validated, (String) vals[i], accum.append(' '), out);+ }+ }++ /** Collects source keys so repaired names can be made unique without changing valid names. */+ private Set<String> collectSourceKeys(Syntax syntax) {+ Set<String> sourceKeys = new HashSet<>(size);+ for (int i = 0; i < size; i++) {+ String key = keys[i];+ assert key != null;+ if (!isInternalKey(key))+ sourceKeys.add(comparisonKey(key, syntax)); }+ return sourceKeys;+ }++ /** Normalizes a key for the output syntax's case sensitivity. */+ private static String comparisonKey(String key, Syntax syntax) {+ return syntax == xml ? key : lowerCase(key);+ }++ /** Compares keys with the requested case sensitivity. */+ private static boolean keysEqual(String first, String second, boolean caseSensitive) {+ return caseSensitive ? first.equals(second) : first.equalsIgnoreCase(second); } @Override@@ -667,7 +700,7 @@ public class Attributes implements Iterable<Attribute>, Cloneable { String keyI = keys[i]; assert keyI != null; for (int j = i + 1; j < size; j++) {- if ((preserve && keyI.equals(keys[j])) || (!preserve && keyI.equalsIgnoreCase(keys[j]))) {+ if (keysEqual(keyI, keys[j], preserve)) { dupes++; remove(j); j--;src/main/java/org/jsoup/nodes/Element.javaindex efc34a67..b91e8e60 100644@@ -1,7 +1,6 @@ package org.jsoup.nodes; import org.jsoup.helper.Validate;-import org.jsoup.internal.Normalizer; import org.jsoup.internal.QuietAppendable; import org.jsoup.helper.Regex; import org.jsoup.internal.StringUtil;@@ -2023,9 +2022,9 @@ 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) {- return syntax == xml ? Normalizer.xmlSafeTagName(tagName()) : tagName();+ /** Gets the tag name normalized for the output syntax. */+ private String safeTagName(Document.OutputSettings.Syntax syntax) {+ return syntax == xml ? Attribute.getValidKey(tagName(), xml) : tagName(); } /**src/test/java/org/jsoup/internal/StringUtilTest.javaindex 4e686c46..81e876cf 100644@@ -19,6 +19,12 @@ public class StringUtilTest { assertEquals("one two three", StringUtil.join(Arrays.asList("one", "two", "three"), " ")); } + @Test void concatCharacterAndString() {+ // joins the values without exposing the pooled builder lifecycle+ assertEquals("_key", StringUtil.concat('_', "key"));+ assertEquals("_", StringUtil.concat('_', ""));+ }+ @Test public void padding() { assertEquals("", StringUtil.padding(0)); assertEquals(" ", StringUtil.padding(1));src/test/java/org/jsoup/nodes/AttributesTest.javaindex f1c54846..6e694470 100644@@ -45,6 +45,15 @@ public class AttributesTest { assertEquals(a.html(), a.toString()); } + @Test void makesRepairedHtmlAttributeNameUnique() {+ // valid names are preserved and repaired collisions receive an underscore prefix+ Attributes attributes = new Attributes();+ attributes.put("A=B", "one");+ attributes.put("a_b", "two");++ assertEquals(" _A_B=\"one\" a_b=\"two\"", attributes.html());+ }+ @Test public void testIteratorRemovable() { Attributes a = new Attributes();src/test/java/org/jsoup/nodes/ElementTest.javaindex 229fe210..6b908f91 100644@@ -3359,6 +3359,15 @@ public class ElementTest { assertEquals("<p_>One</p_>", xml); } + @Test void normalizesElementNamesWithInvalidXmlStarts() {+ // programmatic elements can hold names the XML parser would reject+ Document doc = new Document("");+ doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml);+ doc.appendChild(new Element("1root").text("One"));++ assertEquals("<_1root>One</_1root>", doc.html());+ }+ @Test public void invalidCharactersDiscardedInXml() { // https://github.com/jhy/jsoup/issues/1743src/test/java/org/jsoup/parser/XmlTreeBuilderTest.javaindex e1a0e68b..175524ec 100644@@ -286,6 +286,20 @@ public class XmlTreeBuilderTest { assertEquals("<a bB1-_:.=\"foo\" _9_=\"bar\" xmlns:p1=\"qux\">One</a>", out); // first is same, second coerced } + @Test void normalizesAttributeNamesWithInvalidXmlStarts() {+ // digits and hyphens are valid within XML names, but not at the start+ Document doc = Jsoup.parse("<root 1a='one' -a='two' />", Parser.xmlParser());++ assertEquals("<root _1a=\"one\" _-a=\"two\" />", doc.html());+ }++ @Test void makesRepairedXmlAttributeNameUnique() {+ // valid names are preserved and repaired collisions receive underscore prefixes until unique+ Document doc = Jsoup.parse("<root 1a='one' _1a='two' __1a='three' />", Parser.xmlParser());++ assertEquals("<root ___1a=\"one\" _1a=\"two\" __1a=\"three\" />", doc.html());+ }+ @Test void customTagsAreFlyweights() { String xml = "<foo>Foo</foo><foo>Foo</foo><FOO>FOO</FOO><FOO>FOO</FOO>"; Document doc = Jsoup.parse(xml, Parser.xmlParser());