Skip to content

Truly independent web browser

LibWeb: Escape tabs and line breaks in serialized XML attribute values

Shannon Booth1bd9695b5695
diff --git a/Libraries/LibWeb/HTML/XMLSerializer.cpp b/Libraries/LibWeb/HTML/XMLSerializer.cpp
index f4db4873320..13a236ff902 100644
--- a/Libraries/LibWeb/HTML/XMLSerializer.cpp
+++ b/Libraries/LibWeb/HTML/XMLSerializer.cpp
@@ -318,21 +318,45 @@ static WebIDL::ExceptionOr<Utf16String> serialize_an_attribute_value(Optional<Ut
return serialize_an_attribute_value(attribute_value->view(), require_well_formed);
}
+// https://w3c.github.io/DOM-Parsing/#dfn-serializing-an-attribute-value
static WebIDL::ExceptionOr<Utf16String> serialize_an_attribute_value(Utf16View attribute_value, [[maybe_unused]] RequireWellFormed require_well_formed)
{
- // FIXME: If the require well-formed flag is set, reject characters that are not matched by the XML Char production.
+ // FIXME: 1. If require well-formed is true, and attribute value contains characters that are not matched by the XML Char production, then throw an exception.
+ // The serialization of attribute value would not be well-formed.
+
+ // 3. Otherwise, return attribute value, first replacing any occurrences of the following:
Utf16StringBuilder final_attribute_value;
for (auto code_point : attribute_value) {
- if (code_point == '&')
+ // - "&" with "&amp;"
+ if (code_point == '&') {
final_attribute_value.append_ascii("&amp;"sv);
- else if (code_point == '"')
+ }
+ // - """ with "&quot;"
+ else if (code_point == '"') {
final_attribute_value.append_ascii("&quot;"sv);
- else if (code_point == '<')
+ }
+ // - "<" with "&lt;"
+ else if (code_point == '<') {
final_attribute_value.append_ascii("&lt;"sv);
- else if (code_point == '>')
+ }
+ // AD-HOC: The spec's list leaves out ">", though its note says it is replaced, and all engines replace it.
+ else if (code_point == '>') {
final_attribute_value.append_ascii("&gt;"sv);
- else
+ }
+ // - U+0009 CHARACTER TABULATION with "&#9;"
+ else if (code_point == '\t') {
+ final_attribute_value.append_ascii("&#9;"sv);
+ }
+ // - U+000A LINE FEED (LF) with "&#xA;"
+ else if (code_point == '\n') {
+ final_attribute_value.append_ascii("&#xA;"sv);
+ }
+ // - U+000D CARRIAGE RETURN (CR) with "&#xD;"
+ else if (code_point == '\r') {
+ final_attribute_value.append_ascii("&#xD;"sv);
+ } else {
final_attribute_value.append_code_point(code_point);
+ }
}
return final_attribute_value.to_string();
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 cfefdc68d55..ac6faa6d87c 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/domparsing/XMLSerializer-serializeToString.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/domparsing/XMLSerializer-serializeToString.txt
@@ -1,9 +1,9 @@
Harness status: OK
-Found 27 tests
+Found 33 tests
-22 Pass
-5 Fail
+29 Pass
+4 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.
@@ -16,14 +16,20 @@ Pass Check if an attribute with namespace and no prefix is serialized with the n
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 Attribute value escapes: lt
+Pass Attribute value escapes: gt
+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 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.
Pass Check if start tag serialization takes into account of its xmlns:* attributes
Pass Check if start tag serialization applied the original prefix even if it is declared in an ancestor element.
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.
Pass Check if no special handling for XLink namespace unlike HTML serializer.
diff --git a/Tests/LibWeb/Text/input/wpt-import/domparsing/XMLSerializer-serializeToString.html b/Tests/LibWeb/Text/input/wpt-import/domparsing/XMLSerializer-serializeToString.html
index eb32ccfb459..3db7caf8011 100644
--- a/Tests/LibWeb/Text/input/wpt-import/domparsing/XMLSerializer-serializeToString.html
+++ b/Tests/LibWeb/Text/input/wpt-import/domparsing/XMLSerializer-serializeToString.html
@@ -152,6 +152,28 @@ test(function() {
'<r xmlns:xx="uri" xmlns:ns1="uri2" ns1:name="value"/>');
}, '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.');
+// https://w3c.github.io/DOM-Parsing/#dfn-serializing-an-attribute-value
+test(function() {
+ var root = parse('<root attr="&lt;"/>');
+ assert_equals(serialize(root), '<root attr="&lt;"/>');
+}, 'Attribute value escapes: lt');
+
+test(function() {
+ var root = parse('<root attr=">"/>');
+ assert_equals(serialize(root), '<root attr="&gt;"/>');
+}, 'Attribute value escapes: gt');
+
+test(function() {
+ var root = parse('<root attr=\'"\'/>');
+ assert_equals(serialize(root), '<root attr="&quot;"/>');
+}, 'Attribute value escapes: quot');
+
+test(function() {
+ var root = parse('<root attr="\'"/>');
+ assert_equals(serialize(root), '<root attr="\'"/>');
+}, 'Attribute value escapes: apos');
+
+// This does not match the specification; see <https://github.com/w3c/DOM-Parsing/issues/59>.
test(function() {
var root = parse('<root />');
root.setAttribute('attr', '\t');
@@ -163,7 +185,7 @@ test(function() {
root.setAttribute('attr', '\r');
assert_in_array(serialize(root), [
'<root attr="&#xD;"/>', '<root attr="&#13;"/>']);
-}, 'check XMLSerializer.serializeToString escapes attribute values for roundtripping');
+}, '[TENTATIVE] check XMLSerializer.serializeToString escapes attribute values for roundtripping');
test(function() {
const root = (new Document()).createElement('root');
@@ -190,6 +212,15 @@ test(function() {
assert_equals(serialize(root), '<root xmlns:p1="u1"><child xmlns:p2="u1"><p2:child2/></child></root>');
}, 'Check if start tag serialization finds an appropriate prefix.');
+test(function() {
+ const root = parse('<root xmlns:p1="u1"><child xmlns:p2="u1"/></root>');
+ const child2 = root.ownerDocument.createElementNS('u1', 'child2');
+ const grandchild = root.ownerDocument.createElementNS('u1', 'grandchild');
+ child2.appendChild(grandchild);
+ root.firstChild.appendChild(child2);
+ assert_equals(serialize(root), '<root xmlns:p1="u1"><child xmlns:p2="u1"><p2:child2><p2:grandchild/></p2:child2></child></root>');
+}, 'Check if end tag serialization matches start tag when an appropriate prefix is found.');
+
test(function() {
const root = (new Document()).createElementNS('uri1', 'p:root');
root.setAttributeNS(XMLNS_URI, 'xmlns:p', 'uri2');
@@ -210,6 +241,16 @@ test(function() {
'<root xmlns:x="uri1"><x:table xmlns="uri1"/></root>');
}, 'Check if start tag serialization does NOT apply the default namespace if its namespace is declared in an ancestor.');
+test(function() {
+ const xmlNS = 'http://www.w3.org/XML/1998/namespace';
+ const root = parse('<root/>');
+ const child = root.ownerDocument.createElementNS(xmlNS, 'foo');
+ const grandchild = root.ownerDocument.createElementNS(xmlNS, 'bar');
+ child.appendChild(grandchild);
+ root.appendChild(child);
+ assert_equals(serialize(root), '<root><xml:foo><xml:bar/></xml:foo></root>');
+}, 'Check if end tag serialization matches start tag for elements in the XML namespace.');
+
test(function() {
const root = parse('<root><child1/><child2/></root>');
root.firstChild.setAttributeNS('uri1', 'attr1', 'value1');