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

fix(ns): imported node namespace is not overidden when specified #179

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 15 additions & 10 deletions src/builder/XMLBuilderImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,17 @@ export class XMLBuilderImpl implements XMLBuilder {

const importedNode = node.node

const updateImportedNodeNs = (clone: Element) => {
// update namespace of imported node only when not specified
if (!clone._namespace) {
const [prefix] = namespace_extractQName(
clone.prefix ? clone.prefix + ':' + clone.localName : clone.localName
);
const namespace = hostNode.lookupNamespaceURI(prefix)
new XMLBuilderImpl(clone)._updateNamespace(namespace)
}
};

if (Guard.isDocumentNode(importedNode)) {
// import document node
const elementNode = importedNode.documentElement
Expand All @@ -385,28 +396,22 @@ export class XMLBuilderImpl implements XMLBuilder {
}
const clone = hostDoc.importNode(elementNode, true) as Element
hostNode.appendChild(clone)
const [prefix] = namespace_extractQName(clone.prefix ? clone.prefix + ':' + clone.localName : clone.localName)
const namespace = hostNode.lookupNamespaceURI(prefix)
new XMLBuilderImpl(clone)._updateNamespace(namespace)
updateImportedNodeNs(clone)
} else if (Guard.isDocumentFragmentNode(importedNode)) {
// import child nodes
for (const childNode of importedNode.childNodes) {
const clone = hostDoc.importNode(childNode, true)
hostNode.appendChild(clone)
if (Guard.isElementNode(clone)) {
const [prefix] = namespace_extractQName(clone.prefix ? clone.prefix + ':' + clone.localName : clone.localName)
const namespace = hostNode.lookupNamespaceURI(prefix)
new XMLBuilderImpl(clone)._updateNamespace(namespace)
updateImportedNodeNs(clone)
}
}
} else {
// import node
const clone = hostDoc.importNode(importedNode, true)
hostNode.appendChild(clone)
if (Guard.isElementNode(clone)) {
const [prefix] = namespace_extractQName(clone.prefix ? clone.prefix + ':' + clone.localName : clone.localName)
const namespace = hostNode.lookupNamespaceURI(prefix)
new XMLBuilderImpl(clone)._updateNamespace(namespace)
updateImportedNodeNs(clone)
}
}

Expand Down Expand Up @@ -766,7 +771,7 @@ export class XMLBuilderImpl implements XMLBuilder {
for (const childNode of ele.childNodes) {
const newChildNode = childNode.cloneNode(true)
newEle.appendChild(newChildNode)
if (Guard.isElementNode(newChildNode)) {
if (Guard.isElementNode(newChildNode) && !newChildNode._namespace) {
const [newChildNodePrefix] = namespace_extractQName(newChildNode.prefix ? newChildNode.prefix + ':' + newChildNode.localName : newChildNode.localName)
const newChildNodeNS = newEle.lookupNamespaceURI(newChildNodePrefix)
new XMLBuilderImpl(newChildNode)._updateNamespace(newChildNodeNS)
Expand Down
57 changes: 57 additions & 0 deletions test/issues/issue-178.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import $$ from '../TestHelpers';

describe('Replicate issue', () => {
// https://github.com/oozcitak/xmlbuilder2/issues/90
describe(`#178 - Namespace is removed when importing fragments.`, () => {
const expectedOutput = $$.t`<?xml version="1.0"?><Root xmlns="ns1"><Parent xmlns="ns2"><Child xmlns="ns3"><GrandChild xmlns="ns4">txt</GrandChild></Child></Parent></Root>`;
describe(`with defined namespaces on each element`, () => {
test(`using .ele()`, () => {
const doc = $$.create();
const root = doc.ele('ns1', 'Root');
const parent = root.ele('ns2', 'Parent');
const child = parent.ele('ns3', 'Child');
child.ele('ns4', 'GrandChild').txt('txt');

expect(doc.end()).toBe(expectedOutput);
});
test(`using .import()`, () => {
const doc = $$.create();
const root = doc.ele('ns1', 'Root');
const parent = $$.fragment().ele('ns2', 'Parent');
const child = $$.fragment().ele('ns3', 'Child');
const grandChild = $$.fragment().ele('ns4', 'GrandChild').txt('txt');

child.import(grandChild);
parent.import(child);
root.import(parent);

expect(doc.end()).toBe(expectedOutput);
});
});
describe(`with undefined namespaces on parent element`, () => {
const expectedOutput = $$.t`<?xml version="1.0"?><Root xmlns="ns1"><Parent><Child xmlns="ns3"><GrandChild xmlns="ns4">txt</GrandChild></Child></Parent></Root>`;
test(`using .ele()`, () => {
const doc = $$.create();
const root = doc.ele('ns1', 'Root');
const parent = root.ele('Parent');
const child = parent.ele('ns3', 'Child');
child.ele('ns4', 'GrandChild').txt('txt');

expect(doc.end()).toBe(expectedOutput);
});
test(`using .import()`, () => {
const doc = $$.create();
const root = doc.ele('ns1', 'Root');
const parent = $$.fragment().ele('Parent');
const child = $$.fragment().ele('ns3', 'Child');
const grandChild = $$.fragment().ele('ns4', 'GrandChild').txt('txt');

child.import(grandChild);
parent.import(child);
root.import(parent);

expect(doc.end()).toBe(expectedOutput);
});
});
});
});