Firefox 3.5 includes new support for two W3C DOM traversal specifications. The first, the Element Traversal API, focuses on making element-by-element traversal easier, the second, the NodeIterator interface which makes finding all node types much easier.
Element Traversal API
The purpose of the Element Traversal API is to make it easier for developers to traverse through DOM elements without having to worry about intermediary text nodes, comment nodes, etc. This has long been a bane of web developers, in particular, with cases like document.documentElement.firstChild
yielding different results depending on the whitespace structure of a document.
The Element Traversal API introduces a number of new DOM node properties which can make this traversing much simpler.
Here’s a full break-down of the existing DOM node properties and their new counterparts:
Purpose | All DOM Nodes | Just DOM Elements |
---|---|---|
First | .firstChild | .firstElementChild |
Last | .lastChild | .lastElementChild |
Previous | .previousSibling | .previousElementSibling |
Next | .nextSibling | .nextElementSibling |
Length | .childNodes.length | .childElementCount |
These properties provide a fairly simple addition to the DOM specification (and, honestly, they’re something that should’ve been in the specification to begin with).
There is one property that is conspicuously absent, though: .childElements
(as a counterpart to .childNodes
). This property (which contained a live NodeSet of the child elements of the DOM element) was in previous iterations of the specification but it seems to have gone on the cutting room floor at some point in the interim.
But all is not lost. Right now Internet Explorer, Opera, and Safari all support a .children
property which provides a super-set of the functionality that was supposed to have been made possible by .childElements
. When support for the Element Traversal API was finally landed for Firefox 3.5, support for .children
was included. This means that every major browser now supports this property (far in advance of all browsers supporting the rest of the true Element Traversal specification).
Some examples of the Element Traversal API (and .children
) in action:
Show next element when a click occurs:
someElement.addEventListener("click", function(){
this.nextSiblingElement.style.display = "block";
}, false);
Add classes to all of the child elements:
for ( var i = 0; i < someElement.children.length; i++ ) {
someElement.children[ i ].className = "active";
}
NodeIterator API
NodeIterator is a relatively old API that hasn't seen wide adoption, and has just been implemented in Firefox 3.5. Specifically, the NodeIterator API is designed to allow for easy traversal of all nodes in a DOM document (this includes text nodes, comments, etc.).
The API itself is rather convoluted (containing a number of features that aren't immediately important to most developers) but if you wish to use it for some simpler tasks it be quite easy.
The API works by creating a NodeIterator (using document.createNodeIterator
) and passing in a series of filters. The NodeIterator is capable of returning all nodes in a document (or within the context of a given node) thus you'll want to filter it down to only show the ones that you desire. A simple example of this can be found below.
Construct a NodeIterator for iterating through all the comment nodes in a document.
var nodeIterator = document.createNodeIterator(
document,
NodeFilter.SHOW_COMMENT,
null,
false
);
var node;
while ( (node = nodeIterator.nextNode()) ) {
node.parentNode.removeChild( node );
}
Once constructed the NodeIterator is bi-directional (you can move in any direction, using previousNode or nextNode).
Perhaps the best use of the API is in traversing over commonly-used (but difficult to traverse) nodes like comments and text nodes. Since there already exist a few APIs for traversing DOM elements (such as getElementsByTagName
) this does come as a welcomed respite to the normal means of node traversal.
12 comments