I'm not sure whether this is a bug in w3c-xmlserializer or whether the current behaviour matches the spec and browsers implement the spec differently.
When an xmlns attribute is set manually on an element, even if it's set to the same value as the node's namespace URI, w3c-xmlserializer repeats the xmlns attribute.
Browsers:
const element = document.createElementNS('http://example.com', 'div')
element.setAttribute('xmlns', 'http://example.com')
const serializer = new XMLSerializer()
console.log(serializer.serializeToString(element))
// <div xmlns="http://example.com"/>
w3c-xmlserializer:
import { XMLSerializer } from 'w3c-xmlserializer'
const element = document.createElementNS('http://example.com', 'div')
element.setAttribute('xmlns', 'http://example.com')
const serializer = new XMLSerializer.interface()
console.log(serializer.serializeToString(element))
// <div xmlns="http://example.com" xmlns="http://example.com"/>
I note that setting requireWellFormed to true (i.e. calling produceXMLSerialization(element, true)) throws an "InvalidStateError: Failed to serialize XML: Invalid attribute localName value" error, because it explicitly forbids attributes named xmlns that aren't in the http://www.w3.org/2000/xmlns/ namespace, so this may be valid, but maybe it should match the behaviour of the browsers' native XMLSerializer?
I'm not sure whether this is a bug in
w3c-xmlserializeror whether the current behaviour matches the spec and browsers implement the spec differently.When an
xmlnsattribute is set manually on an element, even if it's set to the same value as the node's namespace URI,w3c-xmlserializerrepeats thexmlnsattribute.Browsers:
w3c-xmlserializer:
I note that setting
requireWellFormedtotrue(i.e. callingproduceXMLSerialization(element, true)) throws an "InvalidStateError: Failed to serialize XML: Invalid attribute localName value" error, because it explicitly forbids attributes namedxmlnsthat aren't in thehttp://www.w3.org/2000/xmlns/namespace, so this may be valid, but maybe it should match the behaviour of the browsers' native XMLSerializer?