Open Bug 1763779 Opened 3 years ago Updated 9 months ago

JavaScript XMLSerializer is removing xmlns attributes

Categories

(Core :: DOM: Serializers, defect)

defect

Tracking

()

UNCONFIRMED

People

(Reporter: luca, Unassigned)

Details

Steps to reproduce:

The behavior of the JS engine in Firefox is different from that in Chrome. Apparently Firefox seems to remove xmlns attributes during XML serialization.

Steps to reproduce -> Just execute the following lines:

let doc = document.implementation.createDocument("", "", null);
let xmlEl= doc.createElement("Plan");
xmlEl.setAttribute("version", "1.0");
xmlEl.setAttribute("xmlns", "http://www.example.com/");
doc.appendChild(xmlEl);
let outputString = new XMLSerializer().serializeToString(doc.documentElement);
console.log(outputString)

Actual results:

The code produces the following output: <Plan version="1.0"/>
Apparently the xmlns attribute is not part of the output.

Expected results:

In Chrome the code produces the following output: <Plan version="1.0" xmlns="http://www.example.com/"/>
In my opinion this is also correct, because all added attributes are also included in the output.

The Bugbug bot thinks this bug should belong to the 'Core::DOM: Core & HTML' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.

Component: Untriaged → DOM: Core & HTML
Product: Firefox → Core
Component: DOM: Core & HTML → JavaScript Engine
Component: JavaScript Engine → DOM: Serializers

If you want to create an element in a custom namespace you must use createElementNS and not createElement so

let xmlEl= doc.createElementNS("http://www.example.com/", "Plan");

Then you don't need the setAttribute call.

Is Chrome's behavior expected here?

Flags: needinfo?(hsivonen)

I think Chrome's behavior isn't expected in the sense that, IIRC, this is an API that Gecko introduced and then WebKit implemented differently before the Blink fork, so Chrome got the WebKit behavior that allows DOM Level 1 manipulation of namespaces while Gecko requires DOM Level 2 namespace-awareness. The spec seems to support the WebKit/Blink behavior. It's unclear to me what the justification for the spec being the way it is is. It seems the spec comes from the era when Microsoft was aligning Trident/EdgeHTML to be more Web-compatible, so perhaps the spec is justified in terms of Web compat. Hard to know now.

Flags: needinfo?(hsivonen)

I think we want probably file a spec issue to clarify this Henri?

Severity: -- → S3
Flags: needinfo?(hsivonen)
Flags: needinfo?(hsivonen)
You need to log in before you can comment on or make changes to this bug.