From 51b95a62465d2034fb64f785fb1d731c30755c17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=BClker?= Date: Tue, 6 May 2025 14:26:15 +0200 Subject: [PATCH] Serialize attribute nodes as the empty string (#36875) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The existing code asserts that attribute nodes are never serialized. This is wrong, because you can pass an attribute node to `XMLSerializer::serializeToString`. Instead, the spec mandates that these are serialized as empty strings (https://w3c.github.io/DOM-Parsing/#dfn-xml-serialization-algorithm). Testing: Includes a new web platform test Fixes: https://github.com/servo/servo/issues/36872 --------- Signed-off-by: Simon Wülker --- components/script/dom/servoparser/html.rs | 3 +-- tests/wpt/meta/MANIFEST.json | 2 +- .../wpt/tests/domparsing/XMLSerializer-serializeToString.html | 4 ++++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/components/script/dom/servoparser/html.rs b/components/script/dom/servoparser/html.rs index 07848c87678..7fd0429612a 100644 --- a/components/script/dom/servoparser/html.rs +++ b/components/script/dom/servoparser/html.rs @@ -302,11 +302,10 @@ pub(crate) fn serialize_html_fragment( serializer.write_processing_instruction(pi.target(), &data)?; }, - NodeTypeId::DocumentFragment(_) => {}, + NodeTypeId::DocumentFragment(_) | NodeTypeId::Attr => {}, NodeTypeId::Document(_) => panic!("Can't serialize Document node itself"), NodeTypeId::Element(_) => panic!("Element shouldn't appear here"), - NodeTypeId::Attr => panic!("Attr shouldn't appear here"), }, SerializationCommand::SerializeShadowRoot(shadow_root) => { // Shadow roots are serialized as template elements with a fixed set of diff --git a/tests/wpt/meta/MANIFEST.json b/tests/wpt/meta/MANIFEST.json index 1469a123dd5..df2beac06bd 100644 --- a/tests/wpt/meta/MANIFEST.json +++ b/tests/wpt/meta/MANIFEST.json @@ -627813,7 +627813,7 @@ ] ], "XMLSerializer-serializeToString.html": [ - "6c294e464a5dc787abd4d10281ab2fe0555a0a3c", + "352a62c7d5db0710da6819bbc094ebfae46f4099", [ null, {} diff --git a/tests/wpt/tests/domparsing/XMLSerializer-serializeToString.html b/tests/wpt/tests/domparsing/XMLSerializer-serializeToString.html index 6c294e464a5..352a62c7d5d 100644 --- a/tests/wpt/tests/domparsing/XMLSerializer-serializeToString.html +++ b/tests/wpt/tests/domparsing/XMLSerializer-serializeToString.html @@ -256,6 +256,10 @@ test(function () { root.setAttributeNS(XMLNS_URI, 'xmlns:foo', ''); assert_equals(serialize(root), ''); }, 'Check if a prefix bound to an empty namespace URI ("no namespace") serialize'); + +test(function() { + assert_equals(serialize(document.createAttribute("foobar")), "") +}, 'Attribute nodes are serialized as the empty string')