I'm currently working on a tool which uses JS to parse an XML document and output it as JSON. Straightforward enough, you'd think. The issue I'm fighting against crops up when the XML tags have an arbitrary namespace. True enough, some of the files I'll be processing have this namespace defined at the top of the document but the tool has to be robust enough to cope when they don't.
To cut a long story short, IE6, IE7 and IE8 have an interesting attitude to innerHTML when the tags you are trying to insert have a namespace. IE9 seems to do the job as you'd expect. I've created some jsFiddle pages which try and identify the issues. They both use QUnit and the test code is included below.
createElement nodeName vs get(0).nodeName
I started off using jQuery to help identify this as the tool uses jQuery for the XML heavy-lifting. The two tests in this demo create elements in two different ways. First, we create the element using document.createElement and grab the nodeName then we use jQuery's constructor and use get(0) to grab the bare DOM element's nodeName. Also, in this first set of tests, we're creating non-standard elements.
test("Compare elements without namespace", function() {
var element1, element2;
element1 = document.createElement('spud').nodeName;
element2 = $('<spud/>').get(0).nodeName;
equals(element1, element2, "We expect these to match");
});
The code above runs fine everywhere – IE, FireFox, Opera, Chrome, etc. etc. Good.
test("Compare elements with namespace", function() {
var element1, element2;
element1 = document.createElement('a:spud').nodeName;
element2 = $('<a:spud/>').get(0).nodeName;
equals(element1, element2, "We expect these to match");
});
This runs fine in non-IE browsers, they all report the nodeName as 'a:spud'
. IE now reports the nodeName as 'spud'
. Ah. I dug through the jQuery source, tracking down the bare roots of the constructor and eventually figured out that just looking at the element itself isn't going to provide any clues. The bit that does the actual string-to-elements work (somewhere around line 5619 in jQuery 1.5.2) creates a container div then injects the (slightly modified) code as innerHTML. The issue must be in IE's interpretation of innerHTML, I thought to myself. And then to you by writing it here.
.innerHTML aside
or ‘jQuery is clever’
Before we continue with this long and, ultimately, unnecessary investigation into namespaces, I have to take a small diversion to cover some smart stuff jQuery does. One thing in particular, in fact. Around that line I mentioned earlier (5619-ish), an extra bit of text is inserted into the innerHTML to cope with IE's oddity. If you are trying to create a non-standard element using innerHTML, IE will not complain but also just do pretty much nothing at all:
var div = document.createElement('div');
div.innerHTML = '<spud></spud>';
alert(div.innerHTML);
The above code will alert '<spud></spud>'
in most browsers but ''
in IE. What jQuery does is firstly wrap your element in an extra <div></div>
(producing '<DIV></DIV>'
) then prepends the word 'div' to that. The innerHTML reported by IE is now 'div<DIV><SPUD></SPUD></DIV>'
! There it is! Next, the extra gubbins is removed by calling .lastChild and you're left with innerHTML = '<SPUD></SPUD>'
. That's pretty darned clever.
.innerHTML vs document.appendChild
Back on track. Armed with this little trick, we can reliably test innerHTML in IE using non-standard elements.
module("Known elements (span)");
test("Compare elements without namespace", function() {
var div1, div2;
div1 = document.createElement('div');
div1.innerHTML = '<span></span>';
div2 = document.createElement('div');
div2.appendChild(document.createElement('span'));
equals(div1.innerHTML.toLowerCase(), div2.innerHTML.toLowerCase(),
"We expect these to match");
});
test("Compare elements with namespace", function() {
var div1, div2;
div1 = document.createElement('div');
div1.innerHTML = '<u:span></u:span>';
div2 = document.createElement('div');
div2.appendChild(document.createElement('u:span'));
equals(div1.innerHTML.toLowerCase(), div2.innerHTML.toLowerCase(),
"We expect these to match");
});
The first test in this pair runs fine everywhere exactly as we'd hope and expect. The second fails miserably in IE. Let us quickly run the same test with unknown elements just to make sure we're identifying the right problem:
module("Unknown elements (spud)");
test("Compare elements without namespace", function() {
var div1, div2;
div1 = document.createElement('div');
div1.innerHTML = 'div<div>' + '<spud></spud>' + '</div>';
div1 = div1.lastChild;
div2 = document.createElement('div');
div2.appendChild(document.createElement('spud'));
equals(div1.innerHTML.toLowerCase(), div2.innerHTML.toLowerCase(),
"We expect these to match");
});
test("Compare elements with namespace", function() {
var div1, div2;
div1 = document.createElement('div');
div1.innerHTML = 'div<div>' + '<u:spud></u:spud>' + '</div>';
div1 = div1.lastChild;
div2 = document.createElement('div');
div2.appendChild(document.createElement('u:spud'));
equals(div1.innerHTML.toLowerCase(), div2.innerHTML.toLowerCase(),
"We expect these to match");
});
As before, the first test in this pair works fine, the second fails. Cool. Or not. Either way, you can now see that it doesn't really matter whether the elements are standard or custom and that little diversion we took earlier really was unnecessary. Still, you know more now about some of the cleverness in jQuery than you did before.
It turns out the reason IE reports the nodeNames as the non-namespaced ones is because it has been busy behind the scenes and added an extra XML namespace prefix into our current context. The innerHTML of the div we filled up using innerHTML has been modified to:
<?xml:namespace prefix = u />
<u:span></u:span>
Where'd that namespace declaration come from?! Goshdarnit, IE. From its point of view, within that little context, u:span
is equivalent to span
The most stripped-down example
Seriously, it does not get more fundamental than this.
element = document.createElement('div');
testHTML = '<div></div>';
element.innerHTML = testHTML;
element.innerHTML.toLowerCase() == testHTML.toLowerCase()
The last line there is true for all browsers.
element = document.createElement('div');
testHTML = '<a:div></a:div>';
element.innerHTML = testHTML;
element.innerHTML.toLowerCase() == testHTML.toLowerCase()
The last line there is true for all browsers except IE 6, 7 and 8!
In conclusion?
Ultimately, there are no winners here. Identifying the problem is quite different from fixing it. I've added a note to the relevant jQuery bug in the tracker but it's not so much a bug in jQuery as a humorous IE quirk. There's some talk of refactoring the .find() method to handle more complicated tagnames so this might get picked up then. The fix will probably be something along the lines of checking the outcome of the innerHTML doesn't have an unexpected namespace declaration when the selector has a colon in it:
div.replace( /<\?[^>]*>/g, '' )
I'd submit the patch myself but I'm having difficulty getting unmodified jQuery to build on any of my machines without failing most of the QUnit tests. I've probably typed something wrong.