Mozilla's 4096 Character TextNode Limit

Ugh, I hate it when one browser works different than all the rest of them and displays some unexpected behavior. Believe it or not, no, I'm not talking about IE this time, it's Mozilla that's giving me a headache. Mozilla is the only major browser that imposes a 4096 character limit on TextNodes in its DOM structure.

I'm calling a web service and trying to traverse the resulting XML response. The easiest way to do this on Mozilla is to feed the XML into a DOMParser and get back an XML document. I'm getting a parse error, however, when I try to feed the value from my first TextNode into the DOMParser. I look at the input value and I see it's incomplete. Why is it incomplete? Because Mozilla chunked the full response into several sibling TextNodes. So I look more carefully and see that yes, in fact, next to my first TextNode there are several sibling TextNodes that when taken as a whole contain the entire XML response.

So what's a guy to do? Concatenate the sibling TextNodes, of course. It's an easy enough job — here's an easy way to it in Javascript:

var nodes = response.childNodes;
var xmlString = '';

for (var i = 0; i < nodes.length; i++)
        xmlString += nodes[i].nodeValue;

(Note: this works on IE as well, because the length of the childNodes array will always be 1 since it doesn't do the data chunking.)

Then just pass xmlString into your favorite XML parser.

var innerXml;

if (window.DOMParser)
{
        parser = new DOMParser();
        innerXml = parser.parseFromString(innerXmlString,"text/xml");
}
else // Internet Explorer
{
        innerXml = new ActiveXObject("Microsoft.XMLDOM");
        innerXml.async = "false";
        innerXml.loadXML(innerXmlString);
}

This guy has another way of doing it by checking for the presence of a DOM textContent attribute - click to see - try it out and let me know which way you like better.

Category:

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.