<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mozilla Hacks - the Web developer blog &#187; John Resig</title>
	<atom:link href="http://hacks.mozilla.org/author/jresig/feed/" rel="self" type="application/rss+xml" />
	<link>http://hacks.mozilla.org</link>
	<description>hacks.mozilla.org</description>
	<lastBuildDate>Wed, 08 Feb 2012 22:52:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>DOM Traversal in Firefox 3.5</title>
		<link>http://hacks.mozilla.org/2009/06/dom-traversal/</link>
		<comments>http://hacks.mozilla.org/2009/06/dom-traversal/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 14:29:59 +0000</pubDate>
		<dc:creator>John Resig</dc:creator>
				<category><![CDATA[DOM]]></category>
		<category><![CDATA[Firefox 3.5]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[w3c]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=734</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>Firefox 3.5 includes new support for two W3C DOM traversal specifications. The first, the <a href="http://www.w3.org/TR/ElementTraversal/">Element Traversal API</a>, focuses on making element-by-element traversal easier, the second, the <a href="http://www.w3.org/TR/DOM-Level-2-Traversal-Range/traversal">NodeIterator</a> interface which makes finding all node types much easier.</p>
<p><strong>Element Traversal API</strong></p>
<p>The purpose of the <a href="http://www.w3.org/TR/ElementTraversal/">Element Traversal API</a> 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 <code>document.documentElement.firstChild</code> yielding different results depending on the whitespace structure of a document.</p>
<p>The Element Traversal API introduces a number of <a href="http://dev.w3.org/2006/webapi/ElementTraversal/publish/ElementTraversal.html#ecmascript-bindings">new DOM node properties</a> which can make this traversing much simpler.</p>
<p>Here&#8217;s a full break-down of the existing DOM node properties and their new counterparts:</p>
<table>
<tr>
<th>Purpose</th>
<th>All DOM Nodes</th>
<th>Just DOM Elements</th>
</tr>
<tr>
<td>First</td>
<td>.firstChild</td>
<td>.firstElementChild</td>
</tr>
<tr>
<td>Last</td>
<td>.lastChild</td>
<td>.lastElementChild</td>
</tr>
<tr>
<td>Previous</td>
<td>.previousSibling</td>
<td>.previousElementSibling</td>
</tr>
<tr>
<td>Next</td>
<td>.nextSibling</td>
<td>.nextElementSibling</td>
</tr>
<tr>
<td>Length</td>
<td>.childNodes.length</td>
<td>.childElementCount</td>
</tr>
</table>
<p>These properties provide a fairly simple addition to the DOM specification (and, honestly, they&#8217;re something that should&#8217;ve been in the specification to begin with).</p>
<p>There is one property that is conspicuously absent, though: <code>.childElements</code> (as a counterpart to <code>.childNodes</code>). 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.</p>
<p>But all is not lost. Right now Internet Explorer, Opera, and Safari all support a <code>.children</code> property which provides a super-set of the functionality that was supposed to have been made possible by <code>.childElements</code>. When support for the Element Traversal API was finally landed for Firefox 3.5, support for <code>.children</code> 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).</p>
<p>Some examples of the Element Traversal API (and <code>.children</code>) in action:</p>
<p>Show next element when a click occurs:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">someElement.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">nextSiblingElement</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Add classes to all of the child elements:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> someElement.<span style="color: #660066;">children</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    someElement.<span style="color: #660066;">children</span><span style="color: #009900;">&#91;</span> i <span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;active&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>NodeIterator API</strong></p>
<p>NodeIterator is a relatively old API that hasn&#8217;t seen wide adoption, and has just been implemented in Firefox 3.5. Specifically, the <a href="https://developer.mozilla.org/En/DOM/NodeIterator">NodeIterator API</a> is designed to allow for easy traversal of all nodes in a DOM document (this includes text nodes, comments, etc.).</p>
<p>The API itself is rather convoluted (containing a number of features that aren&#8217;t immediately important to most developers) but if you wish to use it for some simpler tasks it be quite easy.</p>
<p>The API works by creating a NodeIterator (using <code>document.createNodeIterator</code>) 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&#8217;ll want to filter it down to only show the ones that you desire. A simple example of this can be found below.</p>
<p>Construct a NodeIterator for iterating through all the comment nodes in a document.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> nodeIterator <span style="color: #339933;">=</span> document.<span style="color: #660066;">createNodeIterator</span><span style="color: #009900;">&#40;</span>
    document<span style="color: #339933;">,</span>
    NodeFilter.<span style="color: #660066;">SHOW_COMMENT</span><span style="color: #339933;">,</span>
    <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    <span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> node<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>node <span style="color: #339933;">=</span> nodeIterator.<span style="color: #660066;">nextNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    node.<span style="color: #660066;">parentNode</span>.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span> node <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Once constructed the NodeIterator is bi-directional (you can move in any direction, using <a href="https://developer.mozilla.org/En/DOM/NodeIterator.previousNode">previousNode</a> or <a href="https://developer.mozilla.org/En/DOM/NodeIterator.nextNode">nextNode</a>).</p>
<p>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 <code>getElementsByTagName</code>) this does come as a welcomed respite to the normal means of node traversal.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/06/dom-traversal/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>DOM selectors API in Firefox 3.5</title>
		<link>http://hacks.mozilla.org/2009/06/dom-selectors-api/</link>
		<comments>http://hacks.mozilla.org/2009/06/dom-selectors-api/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 22:21:53 +0000</pubDate>
		<dc:creator>John Resig</dc:creator>
				<category><![CDATA[35 Days]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Firefox 3.5]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=116</guid>
		<description><![CDATA[The Selectors API recommendation, published by the W3C, is a relatively new effort that gives JavaScript developers the ability to find DOM elements on a page using CSS selectors. This single API takes the complicated process of traversing and selecting elements from the DOM and unifies it under a simple unified interface. Out of all [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://dev.w3.org/2006/webapi/selectors-api/">Selectors API</a> recommendation, published by the W3C, is a relatively new effort that gives JavaScript developers the ability to find DOM elements on a page using CSS selectors. This single API takes the complicated process of traversing and selecting elements from the DOM and unifies it under a simple unified interface.</p>
<p>Out of all the recent work to come out of the standards process this is one of the better-supported efforts across all browsers: Usable today in Internet Explorer 8, Chrome, and Safari and arriving in Firefox 3.5 and Opera 10.</p>
<p><strong>Using <code>querySelectorAll</code></strong></p>
<p>The Selectors API provides two methods on all DOM documents, elements, and fragments: <code>querySelector</code> and <code>querySelectorAll</code>. The methods work virtually identically, they both accept a CSS selector and return the resulting DOM elements (the exception being that <code>querySelector</code> only returns the first element).</p>
<p>For example, given the following HTML snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;class&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>First paragraph.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Second paragraph.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>We would be able to use <code>querySelectorAll</code> to make the background of all the paragraphs, inside the div with the ID of &#8216;id&#8217;, red.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> p <span style="color: #339933;">=</span> document.<span style="color: #660066;">querySelectorAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#id p&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> p.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    p<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Or we could find the first child paragraph of a div that has a class of &#8216;class&#8217; and give it a class name of &#8216;first&#8217;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.class &gt; p:first-child&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;first&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Normally these types of traversals would be very tedious in long-form JavaScript/DOM code, taking up multiple lines and queries each.</p>
<p>While the actual use of the Selectors API methods is relatively simple (each taking a single argument) the challenging part comes in when choosing which CSS selectors to use. The Selectors API taps in to the native CSS selectors provided by the browser, for use in styling elements with CSS. For most browsers (Firefox, Safari, Chrome, and Opera) this means that you have access to the full gamut of <a href="http://www.w3.org/TR/css3-selectors/">CSS 3 selectors</a>. Internet Explorer 8 provides a more-limited subset that encompasses <a href="http://www.w3.org/TR/CSS2/selector.html">CSS 2 selectors</a> (which are still terribly useful).</p>
<p>The biggest hurdle, for most new users to the Selectors API, is determining which CSS selectors are appropriate for selecting the elements that you desire &#8211; especially since most developers who write cross-browser code only have significant experience with a limited subset of fully-working CSS 1 selectors.</p>
<p>While the <a href="http://www.w3.org/TR/CSS2/selector.html">CSS 2</a> and <a href="http://www.w3.org/TR/css3-selectors/">CSS 3 selector</a> specifications can serve as a good start for learning more about what&#8217;s available to you there also exist a number of useful guides for learning more:</p>
<ul>
<li><a href="http://www.456bereastreet.com/archive/200601/css_3_selectors_explained/">CSS 3 Selectors Explained</a></li>
<li><a href="http://www.xml.com/pub/a/2003/06/18/css3-selectors.html">XML.com: CSS 3 Selectors</a></li>
<li><a href="http://docs.jquery.com/Selectors">jQuery Selector Reference</a> (Note: Also includes custom, non-standard, selectors.)</li>
</ul>
<p><strong>Implementations in the Wild</strong></p>
<p>The most compelling use case of the Selectors API is not its direct use by web developers, but its use by 3rd-party libraries that already provide DOM CSS selector functionality. The trickiest problem towards adopting the use of the Selectors API, today, is that it isn&#8217;t available in all browsers that users develop for (this includes IE 6, IE 7 and Firefox 3). Thus, until those browsers are no longer used, we must use some intermediary utility to recreate the full DOM CSS selector experience.</p>
<p>Thankfully, a number of libraries already exist that provide an API compatible with the Selectors API (in fact, much of the inspiration for the Selectors API comes from the existence of these libraries in the first place). Additionally, many of these implementations already use the Selectors API behind the scenes. This means that you can use using DOM CSS selectors in all browsers that you support AND get the benefit of faster performance from the native Selectors API implementation, with no work to you.</p>
<p>Some existing implementations that gracefully use the new Selectors API are:</p>
<ul>
<li><a href="http://jquery.com/">jQuery</a></li>
<li><a href="http://prototypejs.org/">Prototype</a></li>
<li><a href="http://dojotoolkit.org/">Dojo</a></li>
<li><a href="http://mootools.net/">MooTools</a></li>
</ul>
<p>It&#8217;s important to emphasize the large leap in performance that you&#8217;ll gain from using this new API (in comparison to the traditional mix of DOM and JavaScript that you must employ). You can really see the difference when you look at the improvement that occurred when JavaScript libraries began to implement the new Selectors API.</p>
<p>When some tests were <a href="http://ejohn.org/blog/queryselectorall-in-firefox-31/">run previously</a> the results were as follows:</p>
<p><a href="http://ejohn.org/files/qsa-blog.full.png"><img src="http://ejohn.org/files/qsa-blog.png" width="500"></a></p>
<p>You can see the dramatic increase in performance that occurred once the libraries began using the native Selectors API implementations &#8211; it&#8217;s quite likely that this performance increase will happen in your applications, as well.</p>
<p><strong>Test Suite</strong></p>
<p>To coincide with the definition of the <a href="http://dev.w3.org/2006/webapi/selectors-api/">Selectors API specification</a> a Selectors API test suite was created by John Resig of Mozilla. This <a href="http://ejohn.org/apps/selectortest/">test suite</a> can be used as a way to determine the quality of the respective Selectors API implementations in the major browsers.</p>
<p>The current results for the browsers that support the API are:</p>
<ul>
<li>Firefox 3.5: 99.3%</li>
<li>Safari 4: 99.3%</li>
<li>Chrome 2: 99.3%</li>
<li>Opera 10b1: 97.5%</li>
<li>Internet Explorer 8: 47.4%</li>
</ul>
<p>Internet Explorer 8, as mentioned before, is missing most CSS 3 selectors &#8211; thus failing most of the associated tests.</p>
<p>As it stands, the Selectors API should serve as a simple, and fast, way of selecting DOM elements on a page. It&#8217;s already benefiting those who use JavaScript libraries that provide similar functionality so feel free to dig in today and give the API a try.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/06/dom-selectors-api/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

