Friday, June 19th, 2009p>John Resig has posted on the DOM traversal methods now in Firefox 3.5 and then a spin out post on the merits of the NodeIterator API. He isn’t impressed:
This API is, at best, bloated, and at worst incredibly misguided and impractical for day-to-day use.
Observe the method signature of createNodeIterator:
- var nodeIterator = document.createNodeIterator(
- root, // root node for the traversal
- whatToShow, // a set of constants to filter against
- filter, // an object with a function for advanced filtering
- entityReferenceExpansion // if entity reference children so be expanded
This is excessive for what should be, at most, a simple way to traverse DOM nodes.
One part of the critique involves the common pattern of bitwise operators that are common in C, C++, and a bit of Java (and elsewhere too). When space is at a premium, these are a good choice. There are also some nice side effects when you use them (building up the flags, munging them later, etc).
However, as John points out, these are more for CSci students than for the average Web developer.
But then the crazy comes in: In order to select multiple, different, types of nodes you must OR together the properties to creating a resulting number that'll be passed in. For example if you wanted to find all elements, comments, and text nodes you would do:
- NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT | NodeFilter.SHOW_TEXT
He goes on to propose some more Webby APIs such as:
- document.getNodes( Element, Comment, Text );
Posted by Dion Almaer at 2:09 pm
14 Comments3.3 rating from 28 votes