Skip to content

Truly independent web browser

LibWeb: Don't reuse a prefix that is already bound in XMLSerializer

Shannon Booth2d90317addb1
diff --git a/Libraries/LibWeb/HTML/XMLSerializer.cpp b/Libraries/LibWeb/HTML/XMLSerializer.cpp
index a383f330519..7d311dfd423 100644
--- a/Libraries/LibWeb/HTML/XMLSerializer.cpp
+++ b/Libraries/LibWeb/HTML/XMLSerializer.cpp
@@ -94,15 +94,29 @@ static Optional<Utf16FlyString> retrieve_a_preferred_prefix_string(Optional<Utf1
VERIFY_NOT_REACHED();
}
+static bool prefix_is_bound_in_prefix_map(Optional<Utf16FlyString> const& prefix, NamespacePrefixMap const& namespace_prefix_map)
+{
+ for (auto const& entry : namespace_prefix_map) {
+ if (entry.value.contains_slow(prefix))
+ return true;
+ }
+ return false;
+}
+
// https://w3c.github.io/DOM-Parsing/#dfn-generating-a-prefix
static Utf16FlyString generate_a_prefix(NamespacePrefixMap& namespace_prefix_map, Optional<Utf16FlyString> const& new_namespace, u64& prefix_index)
{
- // 1. Let generated prefix be the concatenation of the string "ns" and the current numerical value of prefix index.
- auto generated_prefix_string = Utf16String::formatted("ns{}", prefix_index);
- auto generated_prefix = Utf16FlyString::from_utf16(generated_prefix_string.utf16_view());
-
- // 2. Let the value of prefix index be incremented by one.
- ++prefix_index;
+ Utf16FlyString generated_prefix;
+ // AD-HOC: Generate prefixes until one is not already bound in map, so that its declaration doesn't conflict with
+ // one in scope. See https://github.com/w3c/DOM-Parsing/issues/44.
+ do {
+ // 1. Let generated prefix be the concatenation of the string "ns" and the current numerical value of prefix index.
+ auto generated_prefix_string = Utf16String::formatted("ns{}", prefix_index);
+ generated_prefix = Utf16FlyString::from_utf16(generated_prefix_string.utf16_view());
+
+ // 2. Let the value of prefix index be incremented by one.
+ ++prefix_index;
+ } while (prefix_is_bound_in_prefix_map(generated_prefix, namespace_prefix_map));
// 3. Add to map the generated prefix given the new namespace namespace.
add_prefix_to_namespace_prefix_map(namespace_prefix_map, generated_prefix, new_namespace);
@@ -458,7 +472,9 @@ static WebIDL::ExceptionOr<Utf16String> serialize_element_attributes(DOM::Elemen
// 3. Otherwise (attribute namespace is not the XMLNS namespace), if candidate prefix is null:
else if (!candidate_prefix.has_value()) {
// 1. If prefix is not null and local prefixes map does not contain prefix, set candidate prefix to prefix.
- if (attribute->prefix().has_value() && !local_prefixes_map.contains(*attribute->prefix())) {
+ // AD-HOC: Nor if map binds prefix to a namespace, so that its declaration doesn't conflict with the
+ // element's own prefix or shadow a binding that later attributes are serialized with.
+ if (attribute->prefix().has_value() && !local_prefixes_map.contains(*attribute->prefix()) && !prefix_is_bound_in_prefix_map(attribute->prefix(), namespace_prefix_map)) {
candidate_prefix = attribute->prefix();
}
diff --git a/Tests/LibWeb/Text/expected/DOM/XMLSerializer-attribute-prefix-conflicts.txt b/Tests/LibWeb/Text/expected/DOM/XMLSerializer-attribute-prefix-conflicts.txt
new file mode 100644
index 00000000000..4efb669b620
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/DOM/XMLSerializer-attribute-prefix-conflicts.txt
@@ -0,0 +1,3 @@
+<p:root xmlns:p="urn:a" xmlns:ns1="urn:b" ns1:attr="v"/>
+<root xmlns:ns1="urn:a" ns1:a="1" xmlns:ns2="urn:b" ns2:b="2"/>
+<parent xmlns:p="urn:a" p:x="1"><child xmlns:ns1="urn:b" ns1:y="2" p:c="3"/></parent>
diff --git a/Tests/LibWeb/Text/expected/wpt-import/domparsing/XMLSerializer-serializeToString.txt b/Tests/LibWeb/Text/expected/wpt-import/domparsing/XMLSerializer-serializeToString.txt
index ac6faa6d87c..a3d59b23d06 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/domparsing/XMLSerializer-serializeToString.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/domparsing/XMLSerializer-serializeToString.txt
@@ -22,7 +22,7 @@ Pass Attribute value escapes: quot
Pass Attribute value escapes: apos
Pass [TENTATIVE] check XMLSerializer.serializeToString escapes attribute values for roundtripping
Pass Check if attribute serialization takes into account of following xmlns:* attributes
-Fail Check if attribute serialization takes into account of the same prefix declared in an ancestor element
+Pass Check if attribute serialization takes into account of the same prefix declared in an ancestor element
Pass Check if start tag serialization drops element prefix if the namespace is same as inherited default namespace.
Pass Check if start tag serialization finds an appropriate prefix.
Pass Check if end tag serialization matches start tag when an appropriate prefix is found.
@@ -31,7 +31,7 @@ Pass Check if start tag serialization applied the original prefix even if it is
Pass Check if start tag serialization does NOT apply the default namespace if its namespace is declared in an ancestor.
Pass Check if end tag serialization matches start tag for elements in the XML namespace.
Pass Check if generated prefixes match to "ns${index}".
-Pass Check if "ns1" is generated even if the element already has xmlns:ns1.
+Fail Check if "ns1" is generated even if the element already has xmlns:ns1.
Pass Check if no special handling for XLink namespace unlike HTML serializer.
Pass Check if document fragment serializes.
Pass Check children were included for void elements
diff --git a/Tests/LibWeb/Text/input/DOM/XMLSerializer-attribute-prefix-conflicts.html b/Tests/LibWeb/Text/input/DOM/XMLSerializer-attribute-prefix-conflicts.html
new file mode 100644
index 00000000000..ebacefb7a55
--- /dev/null
+++ b/Tests/LibWeb/Text/input/DOM/XMLSerializer-attribute-prefix-conflicts.html
@@ -0,0 +1,29 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const serialize = node => new XMLSerializer().serializeToString(node);
+ const xmlDocument = () => document.implementation.createDocument(null, null);
+
+ // An attribute sharing the element's own prefix in another namespace.
+ let element = xmlDocument().createElementNS("urn:a", "p:root");
+ element.setAttributeNS("urn:b", "p:attr", "v");
+ println(serialize(element));
+
+ // A generated prefix after an attribute kept its own "ns1" prefix.
+ element = xmlDocument().createElementNS(null, "root");
+ element.setAttributeNS("urn:a", "ns1:a", "1");
+ element.setAttributeNS("urn:b", "b", "2");
+ println(serialize(element));
+
+ // A child attribute whose prefix is bound on the parent, then one in the parent's namespace.
+ const doc = xmlDocument();
+ const parent = doc.createElementNS(null, "parent");
+ parent.setAttributeNS("urn:a", "p:x", "1");
+ const child = doc.createElementNS(null, "child");
+ child.setAttributeNS("urn:b", "p:y", "2");
+ child.setAttributeNS("urn:a", "c", "3");
+ parent.appendChild(child);
+ println(serialize(parent));
+ });
+</script>