Skip to content

Truly independent web browser

LibWeb: Preserve a free attribute prefix in XMLSerializer

Shannon Booth299521428128
diff --git a/Libraries/LibWeb/HTML/XMLSerializer.cpp b/Libraries/LibWeb/HTML/XMLSerializer.cpp
index 72d4dbb0b4c..6136f8092b6 100644
--- a/Libraries/LibWeb/HTML/XMLSerializer.cpp
+++ b/Libraries/LibWeb/HTML/XMLSerializer.cpp
@@ -345,7 +345,7 @@ struct LocalNameSetEntry {
};
// https://w3c.github.io/DOM-Parsing/#dfn-xml-serialization-of-the-attributes
-static WebIDL::ExceptionOr<Utf16String> serialize_element_attributes(DOM::Element const& element, NamespacePrefixMap& namespace_prefix_map, u64& prefix_index, LocalPrefixesMap const& local_prefixes_map, bool ignore_namespace_definition_attribute, RequireWellFormed require_well_formed)
+static WebIDL::ExceptionOr<Utf16String> serialize_element_attributes(DOM::Element const& element, NamespacePrefixMap& namespace_prefix_map, u64& prefix_index, LocalPrefixesMap& local_prefixes_map, bool ignore_namespace_definition_attribute, RequireWellFormed require_well_formed)
{
// 1. Let result be the empty string.
Utf16StringBuilder result;
@@ -432,12 +432,28 @@ static WebIDL::ExceptionOr<Utf16String> serialize_element_attributes(DOM::Elemen
candidate_prefix = "xmlns"_utf16_fly_string;
}
- // 3. Otherwise, the attribute namespace in not the XMLNS namespace. Run these steps:
- else {
- // 1. Let candidate prefix be the result of generating a prefix providing map, attribute namespace, and prefix index as input.
- candidate_prefix = generate_a_prefix(namespace_prefix_map, attribute_namespace_for_map, prefix_index);
+ // 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())) {
+ candidate_prefix = attribute->prefix();
+ }
+
+ // 2. Otherwise, set candidate prefix to the result of generating a prefix given map, attribute namespace, and prefix index.
+ else {
+ candidate_prefix = generate_a_prefix(namespace_prefix_map, attribute_namespace_for_map, prefix_index);
+ }
+
+ // 3. Add candidate prefix to map given attribute namespace.
+ add_prefix_to_namespace_prefix_map(namespace_prefix_map, candidate_prefix, attribute_namespace_for_map);
+
+ // 4. Let map value be the empty string if attribute namespace is null, and attribute namespace otherwise.
+ auto map_value = attribute_namespace_for_map.value_or(Utf16FlyString {});
+
+ // 5. Set local prefixes map[candidate prefix] to map value.
+ local_prefixes_map.set(*candidate_prefix, move(map_value));
- // 2. Append the following to result, in the order listed:
+ // 6. Append the following to result, in the order listed:
// 1. " " (U+0020 SPACE);
// 2. The string "xmlns:";
result.append_ascii(" xmlns:"sv);
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 bb291e8dc19..cfefdc68d55 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/domparsing/XMLSerializer-serializeToString.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/domparsing/XMLSerializer-serializeToString.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 27 tests
-20 Pass
-7 Fail
+22 Pass
+5 Fail
Pass check XMLSerializer.serializeToString method could parsing xmldoc to string
Pass check XMLSerializer.serializeToString method could parsing document to string
Pass Check if the default namespace is correctly reset.
@@ -11,14 +11,14 @@ Pass Check if there is no redundant empty namespace declaration.
Pass Check if redundant xmlns="..." is dropped.
Pass Check if inconsistent xmlns="..." is dropped.
Fail Drop inconsistent xmlns="..." by matching on local name
-Fail Check if an attribute with namespace and no prefix is serialized with the nearest-declared prefix
-Fail Check if an attribute with namespace and no prefix is serialized with the nearest-declared prefix even if the prefix is assigned to another namespace.
-Fail Check if the prefix of an attribute is replaced with another existing prefix mapped to the same namespace URI.
-Pass Check if the prefix of an attribute is NOT preserved in a case where neither its prefix nor its namespace URI is not already used.
+Pass Check if an attribute with namespace and no prefix is serialized with the nearest-declared prefix
+Pass Check if an attribute with namespace and no prefix is serialized with the nearest-declared prefix even if the prefix is assigned to another namespace.
+Pass Check if the prefix of an attribute is replaced with another existing prefix mapped to the same namespace URI.
+Fail Check if the prefix of an attribute is NOT preserved in a case where neither its prefix nor its namespace URI is not already used.
Pass Check if the prefix of an attribute is replaced with a generated one in a case where the prefix is already mapped to a different namespace URI.
Fail check XMLSerializer.serializeToString escapes attribute values for roundtripping
Pass Check if attribute serialization takes into account of following xmlns:* attributes
-Pass Check if attribute serialization takes into account of the same prefix declared in an ancestor element
+Fail 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 start tag serialization takes into account of its xmlns:* attributes
@@ -26,7 +26,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 generated prefixes match to "ns${index}".
Pass Check if "ns1" is generated even if the element already has xmlns:ns1.
-Fail Check if no special handling for XLink namespace unlike HTML serializer.
+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
Fail Check if a prefix bound to an empty namespace URI ("no namespace") serialize
diff --git a/Tests/LibWeb/Text/expected/wpt-import/domxpath/fn-lang.txt b/Tests/LibWeb/Text/expected/wpt-import/domxpath/fn-lang.txt
index bbdbf44a35b..0c921ec59b2 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/domxpath/fn-lang.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/domxpath/fn-lang.txt
@@ -4,10 +4,10 @@ Found 7 tests
6 Pass
1 Fail
-Pass lang("en"): <root><match xmlns:ns1="http://www.w3.org/XML/1998/namespace" ns1:lang="en"/></root>
-Pass lang("en"): <root><match xmlns:ns1="http://www.w3.org/XML/1998/namespace" ns1:lang="EN"/></root>
-Pass lang("en"): <root><match xmlns:ns1="http://www.w3.org/XML/1998/namespace" ns1:lang="en-us"/></root>
+Pass lang("en"): <root><match xml:lang="en"/></root>
+Pass lang("en"): <root><match xml:lang="EN"/></root>
+Pass lang("en"): <root><match xml:lang="en-us"/></root>
Pass lang("en"): <root><unmatch/></root>
-Fail lang("ja"): <root xmlns:ns1="http://www.w3.org/XML/1998/namespace" ns1:lang="ja"><match/></root>
-Pass lang("ja"): <root xmlns:ns1="http://www.w3.org/XML/1998/namespace" ns1:lang="ja-jp"><unmatch xmlns:ns2="http://www.w3.org/XML/1998/namespace" ns2:lang="ja_JP"/></root>
-Pass lang("ko"): <root><unmatch xmlns:ns1="http://www.w3.org/XML/1998/namespace" ns1:lang="Ko"/></root>
\ No newline at end of file
+Fail lang("ja"): <root xml:lang="ja"><match/></root>
+Pass lang("ja"): <root xml:lang="ja-jp"><unmatch xml:lang="ja_JP"/></root>
+Pass lang("ko"): <root><unmatch xml:lang="Ko"/></root>
\ No newline at end of file