I was testing something and I came across a weird bug (I don't know if it's really a bug, although).
Take this simple document
<!DOCTYPE html>
<html><head><meta charset="utf-8">
<script>
window.onload = function() {
var p = document.createElement('p');
p.appendChild( document.createTextNode('x') );
p.appendChild( document.createTextNode('y') );
p.appendChild( document.createTextNode('z') );
document.body.appendChild(p);
console.log( p.childNodes.length );
p.normalize();
console.log( p.childNodes.length );
};
</script></head><body></body></html>
In all browsers but IE 9, the console output is first 3 then 1. In IE-9 it is 3 both times -- which means that normalize()
is not doing its thing. What's more surprising is that if I change the "Document mode" to IE 7 or 8 or even quirks mode, the console output is 3 and 1.
Is this some bug in IE or am I getting something wrong?
-- UPDATE --
Strangely, if the element is NOT added to the DOM, IE 9 also behaves in the right way. That is, in the above code, if I remove the line that adds the paragraph into the body:
document.body.appendChild(p);
then, the IE 9 console also displays first 3 then 1.
-- UPDATE 2 --
A simple script to check if your browser does normalize()
properly or not:
var p = document.createElement('p');
// you can not put empty strings -- put blank strings instead
p.appendChild( document.createTextNode(' ') );
p.appendChild( document.createTextNode(' ') );
p.appendChild( document.createTextNode(' ') );
var normalizeOk;
document.body.appendChild(p);
p.normalize();
normalizeOk = p.childNodes.length == 1;
document.body.removeChild(p);
console.log("normalize OK: ", normalizeOk);