Google Ads

Recent Posts

Archives

Topics


« Image map coordinates calculated incorrectly by IE7 | Main | Filtered advertising »

Whitespace in XML - Firefox vs. IE

By Shawn Bradley | September 15, 2007

Today, for a change, I am actually more approving of this particular difference between IE and Firefox. In this case, Firefox is a little TOO rigid in standards adherence, in my opinion. I’m talking about how the two handle whitespace in XML documents.

Adhering to the absolute letter of the standards law, Firefox interprets whitespace as empty text nodes. The end result here is that you cannot format your XML document for readability without introducing a bunch of bogus nodes when the data is processed in Firefox. IE doe things differently - it treats whitespace as most programmers are accustomed to treating whitespace, namely, it ignores it!

Therefore, to get the two browsers to produce the same results, I had to introduce a new function into my AjaxCaller wrapper that would eliminate blank text nodes after receiving the XML from the server. The following code worked very well:

_cleanWhiteSpace: function(node) {
  var notWhitespace = /S/;
  for (var i=0; i < node.childNodes.length; i++)
  {
    var childNode = node.childNodes[i];
    if ((childNode.nodeType == 3)&&
        (!notWhitespace.test(childNode.nodeValue)))
    {
      // that is, if it's a whitespace text node
      node.removeChild(node.childNodes[i]);
      i--;
    }
    if(childNode.nodeType == 1)
    {
      // elements can have text child nodes of their own
      this._cleanWhiteSpace(childNode);
    }
  }
},

Topics: Web Development, Software Development |

Comments