Skip to content

Duplicate xmlns attributes should be combined? #14

Description

@hubgit

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions