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

Fixed namespaced attributes exception in IE11 (and below probably). #14

Merged
merged 1 commit into from
Oct 27, 2016
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ You can get the XML result tree:

The result object can be accessed also via *oMX.dom* property. The properties available:

- **dom** - result XML DOM object
- **dom** - result XML DOM object - **Note that in older IE browsers this is an ActiveX Object and not a standard XML Document!**
- **nsp** - namespaces object (prefix:URI)
- **count** - number of sources merged
- **error** - error information
Expand Down
9 changes: 7 additions & 2 deletions mergexml.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@
var a = NameSpaces(doc.documentElement);
for (var c in a) {
if (!that.nsp[c]) {
that.dom.documentElement.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' + c, a[c]);
if (typeof that.dom.documentElement.setAttributeNS !== 'undefined') {
that.dom.documentElement.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' + c, a[c]);
} else {
// no choice but to use the incorrect setAttribute instead
that.dom.documentElement.setAttribute('xmlns:' + c, a[c]);
}
that.nsp[c] = a[c];
}
}
Expand Down Expand Up @@ -234,7 +239,7 @@
if (flg) {
try {
for (var j = 0; j < node.attributes.length; j++) { /* add/replace attributes */
if (node.attributes[j].namespaceURI) {
if (node.attributes[j].namespaceURI && typeof node.setAttributeNS !== 'undefined') {
obj.setAttributeNS(node.attributes[j].namespaceURI, node.attributes[j].nodeName, node.attributes[j].nodeValue);
} else {
obj.setAttribute(node.attributes[j].nodeName, node.attributes[j].nodeValue);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mergexml",
"version": "1.1.1",
"version": "1.1.2",
"description": "Merge multiple XML sources",
"main": "mergexml.js",
"repository": {
Expand Down
17 changes: 9 additions & 8 deletions test/spec/merge.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('Merging XML sources', function() {
b = '<r><b/></r>';
merger.AddSource(a);
merger.AddSource(b);
expect(merger.Get(1)).to.equal('<r><a/><b/></r>');
expect(merger.Get(1).trim()).to.equal('<r><a/><b/></r>');
});

it('fails to merge second source if sources do not have a common root name', function() {
Expand All @@ -58,7 +58,7 @@ describe('Merging XML sources', function() {
merger.AddSource(a);
merger.AddSource(b);

expect(merger.Get(1)).to.equal('<a/>');
expect(merger.Get(1).trim()).to.equal('<a/>');
});

});
Expand All @@ -82,13 +82,13 @@ describe('Merging XML sources', function() {
'</a>';
merger.AddSource(a);
merger.AddSource(b);
expect(merger.Get(1)).to.equal('<a><c>s1</c><c>s4</c><b>s2</b></a>');
expect(merger.Get(1).trim()).to.equal('<a><c>s1</c><c>s4</c><b>s2</b></a>');

merger = new MergeXML();
merger.AddSource(a);
merger.AddSource(b);

expect(merger.Get(1)).to.equal('<a><c>s1</c><c>s4</c><b>s2</b></a>');
expect(merger.Get(1).trim()).to.equal('<a><c>s1</c><c>s4</c><b>s2</b></a>');
});

});
Expand All @@ -112,7 +112,7 @@ describe('Merging XML sources', function() {
merger.AddSource(a);
merger.AddSource(b);

expect(merger.Get(1)).to.equal('<a><c>s1</c><c>s2</c><c>s4</c></a>');
expect(merger.Get(1).trim()).to.equal('<a><c>s1</c><c>s2</c><c>s4</c></a>');
});
});

Expand All @@ -135,9 +135,10 @@ describe('Merging XML sources', function() {

expect(merger.error.code).to.equal('');
expect(merger.error.text).to.equal('');
expect(merger.Get(1)).to.equal('<a xmlns:enk="' + ns + '"><c enk:custom="something">s2</c></a>');
expect(merger.Get(0).querySelector('c').attributes[0].localName).to.equal('custom');
expect(merger.Get(0).querySelector('c').attributes[0].namespaceURI).to.equal(ns);
expect(merger.Get(1).trim()).to.equal('<a xmlns:enk="' + ns + '"><c enk:custom="something">s2</c></a>');
// in IE11 and below, merger.Get(0) returns an ActiveXObject we use the internal "Query" function
expect(merger.Query('//c').attributes[0].localName).to.equal('custom'); // fails in IE because
expect(merger.Query('//c').attributes[0].namespaceURI).to.equal(ns);
})

})
Expand Down