All Markup News

DOM Traversal

July 30th, 2010

The latest Platform Preview Build includes two great interoperable features for working with the DOM – DOM Traversal and Element Traversal. These features provide web developers with simple, flexible, and fast ways of traversing through a document using the same markup across browsers. These features come in the form of flat enumeration, simplifying the DOM tree to an iterative list, and filtering which enables you to tailor the set of nodes you traverse. These features work with the same markup across browsers – you can try out any of the code here in the IE9 platform preview and other browsers.

Without these features, finding an element of interest on a page requires you to do one or more depth-first traversals of the document using firstChild and nextSibling. This is usually accomplished with complex code that runs slowly. With the DOM and Element Traversal features, there are new and better ways of solving the problem. This blog post is a primer and provides a few best practices to get you on your way.

I’ll start with Element Traversal, since it’s the simplest of the interfaces and follows familiar patterns for enumerating elements in the DOM. Element Traversal is essentially a version of DOM Core optimized for Elements . Instead of calling firstChild and nextSibling, you call firstElementChild and nextElementSibling. For example:

if (elm.firstElementChild){ elm = elm.firstElementChild;

 while (elm.nextElementSibling) { // Do work... }}

This is faster and more convenient, saving you the trouble of having to check for text and comment nodes when you’re really only interested in elements.

DOM Traversal is designed for much broader use cases. First, you create a NodeIterator or a TreeWalker. Then you can use one of the iteration methods to traverse the tree:

var iter = document.createNodeIterator(elm, NodeFilter.SHOW_ELEMENT, null, false); // This would work fine with createTreeWalker, as well

var node = iter.nextNode();while (node = iter.nextNode()){ node.style.display = "none";}

The codepath above iterates through a flat list of all nodes in the tree. This can be incredibly useful since in many cases you don’t care whether something is a child or sibling of something else, just whether it occurs before or after your current position in the document.

A big benefit of DOM Traversal is that it introduces the idea of filtering, so that you only traverse the nodes you care about. While nodeIterator only performs flat iterations, TreeWalker has some additional methods, like firstChild(), that let you see as much or as little of the tree structure as you want.

The SHOW_* family of constants provides a way to include broad classes of nodes, such as text or elements (like SHOW_ELEMENT in the earlier example). In many cases, this will be enough. But when you need the most precise control, you can write your own filter via the NodeFilter interface. The NodeFilter interface uses a callback function to filter each node, as in the following example:

 

var iter = document.createNodeIterator(elm, NodeFilter.SHOW_ALL, keywordFilter, false);

function keywordFilter(node){ var altStr = node.getAttribute('alt').toLowerCase();

 if (altStr.indexOf("flight") != -1 || altStr.indexOf("space") != -1) return NodeFilter.FILTER_ACCEPT; else return NodeFilter.FILTER_REJECT; }

For a live example, check out my demo for DOM Traversal — I used NodeFilter extensively there. Complex filtering operations on the list of media elements were as simple as using a NodeFilter callback like the one above.

In this post, I showed that you have options in how to traverse a document. Here are suggested best practices for when you should use the various interfaces:

  • If the structure of the document is important – and you’re only interested in elements – consider Element Traversal. It’s fast and won’t leave a big footprint in your code.
  • If you don’t care about document structure, use NodeIterator instead of TreeWalker. That way, it’s obvious in your code that you’re only going to be using a flat list. NodeIterator also tends to be faster, which becomes important when traversing large sets of nodes.
  • If the SHOW_* constants do what you need for filtering, use them. Using constants makes your code simpler, as well as having slightly better performance. However, if you need fine-grained filtering, NodeFilter callbacks become indispensable.

I’ve already found these features to be a great help in my own coding, so I’m really excited to see what you do with them. Download the latest Platform Preview, try out the APIs, and let us know what you think.

Thanks!
Jonathan Seitel
Program Manager

Update on Firefox Home

July 29th, 2010

The response has been tremendous since Firefox Home, our free iPhone app, became available in Apple’s App Store. We made some updates to Firefox Home, which are now available, so if you haven’t tried it yet, get it now.
We read your comments in the App Store and on our forum and truly appreciate [...]

XHTML Modularization 1.1 – Second Edition is a W3C Recommendation

July 29th, 2010

The XHTML2 Working Group has published a W3C Recommendation of XHTML Modularization 1.1 – Second Edition. XHTML Modularization is a tool for people who design markup languages. XHTML Modularization helps people design and manage markup language schemas and DTDs; it…

First Draft of Emotion Markup Language (EmotionML) 1.0 Published

July 29th, 2010

The Multimodal Interaction Working Group has published the First Public Working Draft of Emotion Markup Language (EmotionML) 1.0. As the web is becoming ubiquitous, interactive, and multimodal, technology needs to deal increasingly with human factors, including emotions. The present draft…

Case Study: www.aidshilfe.de relaunch

July 28th, 2010

The Deutsche Aids-Hilfe (DAH) is the leading German non-governmental organization that deals with the concerns of people living with HIV/AIDS and helps raise awareness of effective HIV prevention techniques.
As the governing body for more than 120 local AIDS service organizations, it supports this work at many different levels.

The DAH website, aidshilfe.de, is one of the organization’s main communication channels. It offers information on HIV and other sexual transmitted infections and covers the field of counseling for private matters. The website also provides contact information for local self-help-centers, a broad selection of free information material, workshops, community features, etc.

Work on the aidshilfe.de project was supported by many partners. Christoph Schüßler designed the website, which was implemented in Drupal by Berlin-based Werk21. The new aidshilfe.de is a step forward toward a future-proof system that features an attractive new design, interactive features, user-friendly community functionality and great usability.

read more

Page 1 of 12212345...Last »