This re-implements the algorithms used to determine what prefixes to assign and what declarations to output for them during serialization. The algorithms in the DOM parsing and serialization spec contain too many bugs that have so far not been addressed, and contain complicated branching that makes it hard to reason about them.
In the replacement I've tried to remain close to the original behavior in spirit. That is, existing prefixes (or an inherited default namespace) are preferred over authored prefixes. Authored prefixes are used over generated ones. A new prefix is only generated if the authored prefix of an attribute conflicts with a declaration on the same element. Declaration attributes in the DOM are only preserved if they actually represent a change to the namespaces in scope for their element and do not cause conflicts - the spec preserved default namespace declarations in a few other cases but those seemed unnecessary.
This re-implements the algorithms used to determine what prefixes to assign and what declarations to output for them during serialization. The algorithms in the DOM parsing and serialization spec contain too many bugs that have so far not been addressed, and contain complicated branching that makes it hard to reason about them.
In the replacement I've tried to remain close to the original behavior in spirit. That is, existing prefixes (or an inherited default namespace) are preferred over authored prefixes. Authored prefixes are used over generated ones. A new prefix is only generated if the authored prefix of an attribute conflicts with a declaration on the same element. Declaration attributes in the DOM are only preserved if they actually represent a change to the namespaces in scope for their element and do not cause conflicts - the spec preserved default namespace declarations in a few other cases but those seemed unnecessary.