Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow resetting the default namespace on prefixed elements #96

Merged
merged 2 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/dom-parsing/serializationAlgorithms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,10 @@ function serializeAttributes(
// value of attr's value attribute is the empty string, then throw an exception;
// namespace prefix declarations cannot be used to undeclare a namespace (use a
// default namespace declaration instead).
if (requireWellFormed && attr.value === '') {
// (we deviate from the spec here by only throwing for prefix declarations, the
// implementations of this in browsers and the spec text suggest that default
// namespace declarations should be allowed to reset the default namespace to null)
if (requireWellFormed && attr.prefix !== null && attr.value === '') {
throw new Error(
'Namespace prefix declarations cannot be used to undeclare a namespace ' +
'(use a default namespace declaration instead)'
Expand Down
19 changes: 19 additions & 0 deletions test/dom-parsing/XMLSerializer.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,23 @@ describe('serializeToWellFormedString', () => {
slimdom.serializeToWellFormedString(el);
}).not.toThrow();
});

it('allows resetting the default namespace', () => {
const root = document.appendChild(document.createElementNS('ns_root', 'root'));
const child = root.appendChild(document.createElementNS('ns_child', 'p:child'));
const grandChild = child.appendChild(document.createElementNS(null, 'grandchild'));
expect(slimdom.serializeToWellFormedString(document)).toBe(
'<root xmlns="ns_root"><p:child xmlns:p="ns_child"><grandchild xmlns=""/></p:child></root>'
);
});

it('allows resetting the default namespace using an explicit declaration', () => {
const root = document.appendChild(document.createElementNS('ns_root', 'root'));
const child = root.appendChild(document.createElementNS('ns_child', 'p:child'));
child.setAttributeNS(XMLNS_NAMESPACE, 'xmlns', '');
const grandChild = child.appendChild(document.createElementNS(null, 'grandchild'));
expect(slimdom.serializeToWellFormedString(document)).toBe(
'<root xmlns="ns_root"><p:child xmlns:p="ns_child" xmlns=""><grandchild/></p:child></root>'
);
});
});