Open Bug 1958616 Opened 13 days ago Updated 11 days ago

lookupNamespaceURI(null) does not return default namespace

Categories

(Core :: DOM: Core & HTML, defect)

Firefox 137
defect

Tracking

()

UNCONFIRMED

People

(Reporter: ceving, Unassigned)

References

Details

User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:137.0) Gecko/20100101 Firefox/137.0

Steps to reproduce:

document.body.namespaceURI => "http://www.w3.org/1999/xhtml"

document.body.lookupNamespaceURI(null) => null

document.createElementNS("http://www.w3.org/2000/svg", "svg").namespaceURI => "http://www.w3.org/2000/svg"

document.createElementNS("http://www.w3.org/2000/svg", "svg").lookupNamespaceURI(null) => null

Actual results:

.lookupNamespaceURI(null) returns null for elements, which have a namespace.

Expected results:

The function should return the default namespace. Chromium 134 does it that way.

But that's not what the specification says is it?

https://dom.spec.whatwg.org/#dom-node-lookupnamespaceuri and https://dom.spec.whatwg.org/#locate-a-namespace

If it has an attribute whose namespace is the XMLNS namespace, namespace prefix is "xmlns", and local name is prefix, or if prefix is null and it has an attribute whose namespace is the XMLNS namespace, namespace prefix is null, and local name is "xmlns", then return its value if it is not the empty string, and null otherwise.

There's no attributes on the element and prefix is null so that means we should return null, no?

Component: Untriaged → DOM: Core & HTML
Product: Firefox → Core

My understanding is, that "svg" introduces a new default namespace. Otherwise it would not be possible to use "rect" or "line" without prefix. It seems to be not necessary to specify it explicitly using "xmlns", but I think something defines it implicitly.

BTW: setting the attribute does not change anything.

document.body.setAttribute('xmlns', document.body.namespaceURI);
document.body.lookupNamespaceURI(null); // => null

MDN itself says there are implied default namespaces:

https://developer.mozilla.org/en-US/docs/Web/SVG/Guides/Namespaces_crash_course

With HTML, http://www.w3.org/1999/xhtml is the implied namespace. With SVG, it is http://www.w3.org/2000/svg. MathML is http://www.w3.org/1998/Math/MathML.

The spec says
"If its namespace is non-null and its namespace prefix is prefix, then return namespace"
And prefix is by default null.

https://dom.spec.whatwg.org/#locate-a-namespace

Severity: -- → S3

I am not sure what Robert and Olli try to say. But if .lookupNamespaceURI(null) is not the right way to get the default namespace of an element what is the right way instead?

Example:

<body>
<svg><text x=10 y=10>abc</text></svg>
</body>

Now I want to add a "title" element with the correct namespace to the "text" element. I would do:

text_element.prepend(document.createElementNS(text_element.lookupNamespaceURI(null), "title"));

My expectation is that text_element, because it does not have any namespace definition, passes the request to its parent, which is the svg element. And the svg element returns its implied default namespace.

I'm saying that this is a valid bug given what reads in the spec. (MDN isn't any kind of specification, https://dom.spec.whatwg.org is)

You need to log in before you can comment on or make changes to this bug.