Skip to content

Commit b7b460b

Browse files
domeniccodex
andcommitted
Update w3c-xmlserializer to v6
Fix serialization of namespaces named after Object prototype properties, distinguish the null namespace from the string "null", and prevent namespace prefix declarations from leaking between siblings. These fixes preserve namespace bindings when using XMLSerializer or reading outerHTML in XML documents. The update also rejects invalid XML characters in attribute values when well-formedness is required and avoids stack overflows during XML character validation of large strings. Co-authored-by: codex <[email protected]>
1 parent 71d562f commit b7b460b

3 files changed

Lines changed: 71 additions & 6 deletions

File tree

‎package-lock.json‎

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"saxes": "^6.0.0",
3939
"tough-cookie": "^6.0.2",
4040
"undici": "^8.10.2",
41-
"w3c-xmlserializer": "^5.0.0",
41+
"w3c-xmlserializer": "^6.0.0",
4242
"webidl-conversions": "^8.0.1",
4343
"whatwg-mimetype": "^5.0.0",
4444
"whatwg-url": "^17.1.1",
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<title>XML serialization preserves namespaces and prefix scope</title>
4+
<link rel="help" href="https://w3c.github.io/DOM-Parsing/#dfn-namespace-prefix-map">
5+
<script src="/resources/testharness.js"></script>
6+
<script src="/resources/testharnessreport.js"></script>
7+
<script>
8+
"use strict";
9+
10+
const serializers = [
11+
["XMLSerializer", root => new XMLSerializer().serializeToString(root)],
12+
["outerHTML", root => root.outerHTML]
13+
];
14+
15+
for (const [name, serialize] of serializers) {
16+
for (const namespace of ["constructor", "toString", "__proto__"]) {
17+
test(() => {
18+
const doc = document.implementation.createDocument(namespace, "root");
19+
const root = doc.documentElement;
20+
root.appendChild(doc.createElementNS(namespace, "child"));
21+
22+
assert_equals(serialize(root), `<root xmlns="${namespace}"><child/></root>`);
23+
}, `${name} preserves the namespace ${namespace}`);
24+
}
25+
26+
test(() => {
27+
const doc = new DOMParser().parseFromString(
28+
'<root xmlns="urn:parent" xmlns:p="null"/>',
29+
"application/xml"
30+
);
31+
const root = doc.documentElement;
32+
root.appendChild(doc.createElementNS(null, "child"));
33+
34+
assert_equals(
35+
serialize(root),
36+
'<root xmlns="urn:parent" xmlns:p="null"><child xmlns=""/></root>'
37+
);
38+
}, `${name} distinguishes an element's null namespace from the namespace "null"`);
39+
40+
test(() => {
41+
const doc = new DOMParser().parseFromString('<root xmlns:p="null"/>', "application/xml");
42+
const root = doc.documentElement;
43+
root.setAttribute("value", "one");
44+
root.setAttributeNS("null", "p:value", "two");
45+
46+
assert_equals(serialize(root), '<root xmlns:p="null" value="one" p:value="two"/>');
47+
}, `${name} distinguishes attributes in the null namespace from the namespace "null"`);
48+
49+
test(() => {
50+
const doc = new DOMParser().parseFromString(
51+
'<root xmlns:p="urn:example"><first xmlns:q="urn:example"/></root>',
52+
"application/xml"
53+
);
54+
const root = doc.documentElement;
55+
const second = doc.createElementNS("urn:example", "second");
56+
second.setAttributeNS("urn:example", "value", "text");
57+
root.appendChild(second);
58+
59+
assert_equals(
60+
serialize(root),
61+
'<root xmlns:p="urn:example"><first xmlns:q="urn:example"/><p:second p:value="text"/></root>'
62+
);
63+
}, `${name} does not reuse a sibling's namespace prefix for elements or attributes`);
64+
}
65+
</script>

0 commit comments

Comments
 (0)