Kevin Whinnery
jQuery, XML Parsing, and XML Namespaces

I recently ran into a fun problem as I was using jQuery to parse an XML document that contained namespaced nodes. I first tried something along these lines: $(xmlDoc).find("namespace:tag").text();.

FAIL. This shouldn’t work since : is a special character in jQuery selectors. So as per the instructions in the Selectors documentation, I escaped the colon in the tag name: $(xmlDoc).find("namespace\\:tag").text();.

FAIL. Using jQuery 1.3.x, this fails in all WebKit browsers. WTF? After some Googling, I figured out a round-about way to get at this tag using an attribute filter for nodeName:

Attribute filters, FTW! Hopefully if you find this post first, it will save you some time.

Beware of jacked toJSON implementations in Prototype and MooTools

I recently came across an annoying problem with the MooTools and Prototype JavaScript libraries where JSON serialization is concerned. Both add a toJSON function to Object which does not work very well at all. Both implementations do not serialize arrays nested in objects, so any Object containing an array will not be serialized properly (As taken from Google’s hosted version of Prototype:

toJSON: function(object) {
var type = typeof object;
switch (type) {
  case 'undefined':
  case 'function':
  case 'unknown': return;
  case 'boolean': return object.toString();
}

if (object === null) return 'null';
if (object.toJSON) return object.toJSON();
if (Object.isElement(object)) return;

var results = [];
for (var property in object) {
  var value = Object.toJSON(object[property]);
  if (!Object.isUndefined(value))
    results.push(property.toJSON() + ': ' + value);
}

return '{' + results.join(', ') + '}';
}

Just a word of caution. I’ll try and file some bugs on the matter as time allows.