Annotation of 2006/webapi/selectors-api/Overview.html, revision 1.49

1.1       avankest    1: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
                      2: 
1.11      avankest    3: <html lang=en-US>
1.20      lhunt       4:  <head><meta content="text/html;charset=UTF-8" http-equiv=Content-Type>
                      5: 
1.1       avankest    6:   <title>Selectors API</title>
                      7: 
                      8:   <style type="text/css">
1.36      lhunt       9:        pre.idl { border:solid thin; background:#eee; color:#000; padding:0.5em }
1.20      lhunt      10: 
1.36      lhunt      11:        pre.idl :link, pre.idl :visited { color:inherit; background:transparent }
                     12:        div.example { border-left:double; padding-left:1em }
                     13:        dfn { font-style:normal; font-weight:bolder }
                     14:        em.ct { font-style:normal; font-weight:normal; font-variant:small-caps }
                     15:        p.note {  margin-left:2em; color:green; font-style:italic; font-weight:bold }
                     16:        p.note:before { content:"Note: " }
                     17:        .issue { padding:.5em; border:solid red }
                     18:        .issue:before { content:"Issue: " }
                     19:        code { color:#FF4500; }
                     20:        code :link, code :visited { color:inherit }
                     21:        </style>
1.46      lhunt      22:   <link href="http://www.w3.org/StyleSheets/TR/base.css" rel=stylesheet>
1.41      lhunt      23:   <link href="http://www.w3.org/StyleSheets/TR/W3C-ED" rel=stylesheet
1.29      lhunt      24:   type="text/css">
1.1       avankest   25: 
                     26:  <body>
1.11      avankest   27:   <div class=head>
1.31      lhunt      28:    <p><a href="http://www.w3.org/"><img alt=W3C height=48
                     29:     src="http://www.w3.org/Icons/w3c_home" width=72></a></p>
1.1       avankest   30: 
1.46      lhunt      31:    <h1 id=title>Selectors API</h1>
1.12      avankest   32:    <!-- "DOM Selectors" was not acceptable. "DOM Level 4 Selectors" and
1.36      lhunt      33:                conforming to the DOM specification template (if there is such a thing) is
                     34:                just silly so we got stuck with this weird name. -->
1.12      avankest   35:    
1.48      lhunt      36:    <h2 class="no-num no-toc" id=W3C-doctype>W3C Working Draft 9 May 2008</h2>
1.1       avankest   37: 
                     38:    <dl>
1.5       avankest   39:     <dt>This Version:
1.1       avankest   40: 
1.11      avankest   41:     <dd><a
1.48      lhunt      42:      href="http://www.w3.org/TR/2008/ED-selectors-api-20080509/">http://www.w3.org/TR/2008/ED-selectors-api-20080509/</a>
1.1       avankest   43: 
1.5       avankest   44:     <dt>Latest Version:
1.1       avankest   45: 
                     46:     <dd><a
                     47:      href="http://www.w3.org/TR/selectors-api/">http://www.w3.org/TR/selectors-api/</a>
                     48: 
1.12      avankest   49:     <dt>Previous Versions:
                     50: 
                     51:     <dd><a
1.44      lhunt      52:      href="http://www.w3.org/TR/2007/WD-selectors-api-20071221/">http://www.w3.org/TR/2007/WD-selectors-api-20071221/</a>
                     53: 
                     54:     <dd><a
1.31      lhunt      55:      href="http://www.w3.org/TR/2007/WD-selectors-api-20071019/">http://www.w3.org/TR/2007/WD-selectors-api-20071019/</a>
1.29      lhunt      56: 
                     57:     <dd><a
1.12      avankest   58:      href="http://www.w3.org/TR/2006/WD-selectors-api-20060926/">http://www.w3.org/TR/2006/WD-selectors-api-20060926/</a>
1.1       avankest   59: 
                     60:     <dd><a
                     61:      href="http://www.w3.org/TR/2006/WD-selectors-api-20060525/">http://www.w3.org/TR/2006/WD-selectors-api-20060525/</a>
                     62: 
1.20      lhunt      63:     <dt>Editors:
                     64: 
1.46      lhunt      65:     <dd><a href="http://annevankesteren.nl/">Anne van Kesteren</a> (<a
                     66:      href="http://www.opera.com/">Opera Software ASA</a>) &lt;<a
                     67:      href="mailto:annevk@opera.com">annevk@opera.com</a>&gt;
                     68: 
1.29      lhunt      69:     <dd><a href="http://lachy.id.au/">Lachlan Hunt</a> (<a
                     70:      href="http://www.opera.com/">Opera Software ASA</a>) &lt;<a
1.20      lhunt      71:      href="mailto:lachlan.hunt@lachy.id.au">lachlan.hunt@lachy.id.au</a>&gt;
1.1       avankest   72:    </dl>
                     73: 
1.11      avankest   74:    <p class=copyright><a
1.1       avankest   75:     href="http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">Copyright</a>
1.44      lhunt      76:     © 2006-2007 <a href="http://www.w3.org/"><acronym title="World Wide Web
                     77:     Consortium">W3C</acronym></a><sup>®</sup> (<a
1.31      lhunt      78:     href="http://www.csail.mit.edu/"><acronym title="Massachusetts Institute
                     79:     of Technology">MIT</acronym></a>, <a
                     80:     href="http://www.ercim.org/"><acronym title="European Research Consortium
                     81:     for Informatics and Mathematics">ERCIM</acronym></a>, <a
                     82:     href="http://www.keio.ac.jp/">Keio</a>), All Rights Reserved. W3C <a
                     83:     href="http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">liability</a>,
                     84:     <a
                     85:     href="http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark</a>
                     86:     and <a
                     87:     href="http://www.w3.org/Consortium/Legal/copyright-documents">document
                     88:     use</a> rules apply.</p>
1.33      lhunt      89:   </div>
1.1       avankest   90: 
                     91:   <hr>
                     92: 
1.11      avankest   93:   <h2 class="no-num no-toc" id=abstract>Abstract</h2>
1.1       avankest   94: 
1.23      lhunt      95:   <p>Selectors, which are widely used in CSS, are patterns that match against
                     96:    elements in a tree structure [<cite><a
1.24      lhunt      97:    href="#ref-selectors">Selectors</a></cite>] [<cite><a
                     98:    href="#ref-css21">CSS21</a></cite>]. The Selectors API specification
                     99:    defines methods for retrieving <code>Element</code> nodes from the <abbr
1.23      lhunt     100:    title="Document Object Model">DOM</abbr> by matching against a group of
                    101:    selectors. It is often desirable to perform DOM operations on a specific
                    102:    set of elements in a document. These methods simplify the process of
1.36      lhunt     103:    acquiring specific elements, especially compared with the more verbose
1.23      lhunt     104:    techniques defined and used in the past.
1.1       avankest  105: 
1.11      avankest  106:   <h2 class="no-num no-toc" id=sotd>Status of this Document</h2>
1.1       avankest  107: 
                    108:   <p><em>This section describes the status of this document at the time of
                    109:    its publication. Other documents may supersede this document. A list of
                    110:    current W3C publications and the latest revision of this technical report
                    111:    can be found in the <a href="http://www.w3.org/TR/">W3C technical reports
                    112:    index</a> at http://www.w3.org/TR/.</em>
                    113: 
1.29      lhunt     114:   <p>This is a Last Call Working Draft of "Selectors API". The W3C Membership
                    115:    and other interested parties are invited to review the document and send
                    116:    comments to <a href="mailto:public-webapi@w3.org">public-webapi@w3.org</a>
                    117:    (<a href="http://lists.w3.org/Archives/Public/public-webapi/">public
                    118:    archive</a>) with <kbd>[selectors-api]</kbd> in the subject, through 06
                    119:    January 2008.
                    120: 
                    121:   <p><span class=notetoeditor>Web content and browser developers are
                    122:    encouraged to review this draft. This draft is considered relatively
                    123:    stable and is expected to progress to Candidate Recommendation after the
1.44      lhunt     124:    review period.</span> The editor’s copy of this specification is <a
1.30      lhunt     125:    href="http://dev.w3.org/2006/webapi/selectors-api/">available in W3C
                    126:    CVS</a>. A detailed list of changes is also available <a
1.1       avankest  127:    href="http://dev.w3.org/cvsweb/2006/webapi/selectors-api/">from the CVS
1.29      lhunt     128:    server</a>.
1.1       avankest  129: 
1.29      lhunt     130:   <p>This document was developed by the <a
                    131:    href="http://www.w3.org/2006/webapi">Web API Working Group</a>. The
                    132:    Working Group expects to advance this Working Draft to Recommendation
                    133:    Status.
                    134: 
                    135:   <p>Publication as a Working Draft does not imply endorsement by the W3C
                    136:    Membership. This is a draft document and may be updated, replaced or
                    137:    obsoleted by other documents at any time. It is inappropriate to cite this
                    138:    document as other than work in progress.
1.1       avankest  139: 
                    140:   <p>This document was produced by a group operating under the <a
                    141:    href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5 February
                    142:    2004 W3C Patent Policy</a>. W3C maintains a <a
                    143:    href="http://www.w3.org/2004/01/pp-impl/38482/status"
1.11      avankest  144:    rel=disclosure>public list of any patent disclosures</a> made in
1.1       avankest  145:    connection with the deliverables of the group; that page also includes
                    146:    instructions for disclosing a patent. An individual who has actual
                    147:    knowledge of a patent which the individual believes contains <a
                    148:    href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential">Essential
1.31      lhunt     149:    Claim(s)</a> must disclose the information in accordance with <a
1.1       avankest  150:    href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure">section
                    151:    6 of the W3C Patent Policy</a>.
                    152: 
1.11      avankest  153:   <h2 class="no-num no-toc" id=toc>Table of Contents</h2>
1.1       avankest  154:   <!--begin-toc-->
                    155: 
1.11      avankest  156:   <ul class=toc>
                    157:    <li><a href="#introduction"><span class=secno>1. </span>Introduction</a>
                    158:     <ul class=toc>
1.36      lhunt     159:      <li><a href="#examples"><span class=secno>1.1 </span>Examples</a>
1.49    ! lhunt     160:     </ul>
1.1       avankest  161: 
1.49    ! lhunt     162:    <li><a href="#conformance"><span class=secno>2. </span>Conformance
        !           163:     Requirements</a>
        !           164:     <ul class=toc>
        !           165:      <li><a href="#terminology"><span class=secno>2.1 </span>Terminology and
        !           166:       Conventions</a>
        !           167:     </ul>
1.1       avankest  168: 
1.49    ! lhunt     169:    <li><a href="#interoperability"><span class=secno>3.
        !           170:     </span>Interoperability Considerations</a>
        !           171:     <ul class=toc>
        !           172:      <li><a href="#extensibility"><span class=secno>3.1
        !           173:       </span>Extensibility</a>
        !           174:     </ul>
        !           175: 
        !           176:    <li><a href="#security"><span class=secno>4. </span>Security
        !           177:     Considerations</a>
1.21      lhunt     178: 
1.49    ! lhunt     179:    <li><a href="#privacy"><span class=secno>5. </span>Privacy Considerations
        !           180:     </a>
1.1       avankest  181: 
1.49    ! lhunt     182:    <li><a href="#nodeselector"><span class=secno>6. </span>The <code
1.27      lhunt     183:     title="">querySelector()</code> and <code
                    184:     title="">querySelectorAll()</code> Methods</a>
1.16      avankest  185:     <ul class=toc>
1.49    ! lhunt     186:      <li><a href="#nsresolver-interface"><span class=secno>6.1 </span>The
1.16      avankest  187:       <code title="">NSResolver</code> Interface</a>
                    188:     </ul>
1.1       avankest  189: 
1.11      avankest  190:    <li class=no-num><a href="#references">References</a>
1.1       avankest  191: 
1.11      avankest  192:    <li class=no-num><a href="#acknowledgements">Acknowledgements</a>
1.1       avankest  193:   </ul>
                    194:   <!--end-toc-->
                    195: 
1.11      avankest  196:   <h2 id=introduction><span class=secno>1. </span>Introduction</h2>
1.1       avankest  197: 
                    198:   <p><em>This section is non-normative.</em>
                    199: 
1.20      lhunt     200:   <p>This specification introduces two methods that take a group of selectors
1.24      lhunt     201:    (often simply referred to as a selector) as an argument and return the
1.20      lhunt     202:    matching elements [<cite><a href="#ref-selectors">Selectors</a></cite>].
1.24      lhunt     203:    With these methods, it is easier to match a set of <code>Element</code>
1.20      lhunt     204:    nodes based on specific criteria. So instead of having to filter the
1.22      lhunt     205:    result of a <code>getElementsByTagName()</code> call, authors can directly
1.44      lhunt     206:    “filter” in the query.
1.10      avankest  207: 
1.36      lhunt     208:   <h3 id=examples><span class=secno>1.1 </span>Examples</h3>
1.1       avankest  209: 
                    210:   <p><em>This section is non-normative.</em>
                    211: 
1.24      lhunt     212:   <p>Some ECMAScript [<cite><a href="#ref-ecma262">ECMAScript</a></cite>]
                    213:    examples:
1.1       avankest  214: 
1.14      avankest  215:   <div class=example>
1.41      lhunt     216:    <p>This is an example table written in HTML 4.01.</p>
1.22      lhunt     217: 
1.20      lhunt     218:    <pre>&lt;table id="score">
                    219:   &lt;thead>
                    220:     &lt;tr>
                    221:       &lt;th>Test
                    222:       &lt;th>Result
                    223:   &lt;tfoot>
                    224:     &lt;tr>
                    225:       &lt;th>Average
                    226:       &lt;td>82%
                    227:   &lt;tbody>
                    228:     &lt;tr>
                    229:       &lt;td>A
                    230:       &lt;td>87%
                    231:     &lt;tr>
                    232:       &lt;td>B
                    233:       &lt;td>78%
                    234:     &lt;tr>
                    235:       &lt;td>C
                    236:       &lt;td>81%
                    237: &lt;/table></pre>
                    238: 
                    239:    <p>In order to obtain the cells containing the results in the table, which
                    240:     might be done, for example, to plot the values on a graph, there are at
                    241:     least two approaches that may be taken. Using only the APIs from DOM
                    242:     Level 2, it requires a script like the following that iterates through
                    243:     each <code>tr</code> within each <code>tbody</code> in the
                    244:     <code>table</code> to find the second cell of each row.</p>
                    245: 
                    246:    <pre>var table = document.getElementById("score");
1.23      lhunt     247: var groups = table.tBodies;
                    248: var rows = null;
1.24      lhunt     249: var cells = [];
1.20      lhunt     250: 
1.23      lhunt     251: for (var i = 0; i &lt; groups.length; i++) {
                    252:   rows = groups[i].rows;
                    253:   for (var j = 0; j &lt; rows.length; j++) {
                    254:     cells.push(rows[j].cells[1]);
1.20      lhunt     255:   }
1.36      lhunt     256: }</pre>
1.20      lhunt     257: 
1.46      lhunt     258:    <p>Alternatively, using the <code
                    259:     title=document-selectallelements>querySelectorAll()</code> method, that
                    260:     script becomes much more concise.</p>
1.20      lhunt     261: 
1.36      lhunt     262:    <pre>var cells = document.querySelectorAll("#score&gt;tbody&gt;tr&gt;td:nth-of-type(2)");</pre>
1.41      lhunt     263: 
                    264:    <p>This script will also function correctly for a table written in XHTML
                    265:     markup instead of HTML.</p>
1.20      lhunt     266:   </div>
                    267: 
1.49    ! lhunt     268:   <h2 id=conformance><span class=secno>2. </span>Conformance Requirements</h2>
1.1       avankest  269: 
1.21      lhunt     270:   <p>All diagrams, examples and notes in this specification are
1.20      lhunt     271:    non-normative, as are all sections explicitly marked non-normative.
                    272:    Everything else in this specification is normative.
1.1       avankest  273: 
1.23      lhunt     274:   <p>The key words <em class=ct>must</em>, <em class=ct>should</em>, and <em
                    275:    class=ct>may</em> in the normative parts of this document are to be
1.24      lhunt     276:    interpreted as described in RFC 2119 [<cite><a
1.1       avankest  277:    href="#ref-rfc2119">RFC2119</a></cite>].
                    278: 
1.11      avankest  279:   <p>The following conformance classes are defined (and considered) by this
                    280:    specification:
1.1       avankest  281: 
                    282:   <dl>
1.22      lhunt     283:    <dt><dfn id=conforming>conforming user agent</dfn>
1.1       avankest  284: 
1.48      lhunt     285:    <dd>A user agent that implements the <code><a
1.49    ! lhunt     286:     href="#dom-document-selector">NodeSelector</a></code> and
1.48      lhunt     287:     <code>NodeList</code> interfaces described in this specification and
                    288:     conforms to all <em class=ct>must</em>-level criteria that apply to
                    289:     implementations.
1.22      lhunt     290: 
                    291:    <dt><dfn id=conforming0>conforming namespace resolver</dfn>
                    292: 
                    293:    <dd>An object implementing the <code><a
                    294:     href="#nsresolver">NSResolver</a></code> interface which conforms to all
1.36      lhunt     295:     <em class=ct>must</em>-level criteria that apply to the <code><a
1.23      lhunt     296:     href="#nsresolver">NSResolver</a></code> interface.
1.22      lhunt     297: 
                    298:    <dt><dfn id=conforming1>conforming application</dfn>
                    299: 
                    300:    <dd>An application that uses the interfaces defined in this specification
1.36      lhunt     301:     and conforms to all <em class=ct>must</em>-level criteria that apply to
                    302:     applications.
1.1       avankest  303:   </dl>
                    304: 
1.49    ! lhunt     305:   <h3 id=terminology><span class=secno>2.1 </span>Terminology and Conventions</h3>
1.16      avankest  306: 
1.23      lhunt     307:   <p>The terminology used in this specification is that from Selectors
                    308:    [<cite><a href="#ref-selectors">Selectors</a></cite>].
1.16      avankest  309: 
1.23      lhunt     310:   <p>Conformance requirements phrased as algorithms or specific steps may be
                    311:    implemented in any manner, so long as the end result is equivalent. In
1.39      lhunt     312:    doing so, implementations <em class=ct>may</em> assume that the
                    313:    <code>lookupNamespaceURI()</code> method of the object implementing the
                    314:    <code><a href="#nsresolver">NSResolver</a></code> interface (or ECMAScript
                    315:    <code>Function</code> being used in place of such an object) returns
                    316:    consistent results when invoked.
1.16      avankest  317: 
1.23      lhunt     318:   <p>The construction "<code>Foo</code> object", where <code>Foo</code> is
                    319:    actually an interface, is sometimes used instead of the more accurate
1.24      lhunt     320:    "object implementing the <code>Foo</code> interface".
1.23      lhunt     321: 
1.46      lhunt     322:   <p>The IDL used in this specification uses the syntax defined in Language
                    323:    Bindings for DOM Specifications [<cite><a
                    324:    href="#ref-bindings">Bindings</a></cite>].
                    325: 
1.49    ! lhunt     326:   <h2 id=interoperability><span class=secno>3. </span>Interoperability
        !           327:    Considerations</h2>
1.16      avankest  328: 
1.23      lhunt     329:   <p><em>This section is non-normative.</em>
                    330: 
1.36      lhunt     331:   <p>Since implementations may optimise the algorithms described in this
1.23      lhunt     332:    specification, and because some may invoke the <code><a
                    333:    href="#nsresolver">NSResolver</a></code> object more than others,
1.36      lhunt     334:    interoperability concerns may arise if the <code><a
                    335:    href="#nsresolver">NSResolver</a></code> object causes side effects or
                    336:    returns inconsistent results each time it is invoked.
1.16      avankest  337: 
1.19      avankest  338:   <div class=example>
1.22      lhunt     339:    <p>In the following example, the <code><a
                    340:     href="#nsresolver">NSResolver</a></code> causes a side effect when
                    341:     executed.</p>
                    342: 
                    343:    <pre>function resolver(prefix) {
1.21      lhunt     344:   sideEffect();
1.24      lhunt     345:   if (prefix == "test") {
                    346:     return "http://example.org/test";
                    347:   }
1.22      lhunt     348: }
                    349: 
1.36      lhunt     350: var x = document.querySelectorAll("test|*:empty > test|p", resolver)</pre>
1.22      lhunt     351: 
1.36      lhunt     352:    <p>Some implementations may not need to <span>resolve the namespace
1.22      lhunt     353:     prefix</span> in order to determine that the selector cannot match any
                    354:     elements, in which case <code>sideEffect()</code> will not be invoked.</p>
                    355: 
                    356:    <p>The following example could return inconsistent results each time it is
                    357:     invoked, depending on the order in which namespace prefixes are resolved
                    358:     and the number of times the resolver is invoked to for the
                    359:     <code>foo</code> prefix.</p>
                    360: 
                    361:    <pre>var i = 0;
                    362: function resolver(prefix) {
                    363: 
                    364:   var ns = ["http://example.org/foo",
                    365:             "http://example.org/bar",
1.24      lhunt     366:             "http://example.org/baz"];
1.22      lhunt     367:   return ns[i++];
                    368: }
                    369: 
1.41      lhunt     370: var x = document.querySelectorAll("foo|x, foo|y, bar|z", resolver);</pre>
1.22      lhunt     371: 
                    372:    <p>This could result in selecting <code>x</code>, <code>y</code> and
                    373:     <code>z</code> elements from almost any combination of namespaces that
                    374:     may be returned. The result is unpredictable and different results may
1.36      lhunt     375:     occur in different implementations.</p>
1.19      avankest  376:   </div>
                    377: 
1.49    ! lhunt     378:   <h3 id=extensibility><span class=secno>3.1 </span>Extensibility</h3>
1.5       avankest  379: 
                    380:   <p><em>This section is non-normative.</em>
                    381: 
                    382:   <p>Extensions of the APIs defined in this specification are <em>strongly
1.36      lhunt     383:    discouraged</em>. Implementors, Working Groups and other interested
                    384:    parties should discuss extensions on a relevant public forum, such as <a
1.5       avankest  385:    href="mailto:public-webapi@w3.org">public-webapi@w3.org</a>.
                    386: 
1.49    ! lhunt     387:   <h2 id=security><span class=secno>4. </span>Security Considerations</h2>
1.11      avankest  388: 
                    389:   <p>It is expected that implementing this specification introduces no new
1.21      lhunt     390:    security risks for users.
                    391: 
1.36      lhunt     392:   <p>Implementations <em class=ct>should</em> ensure they do not crash or
1.46      lhunt     393:    behave erratically when facing when facing a misbehaving or hostile
                    394:    <code><a href="#nsresolver">NSResolver</a></code> object. Since the
                    395:    <code><a href="#nsresolver">NSResolver</a></code> object may be
                    396:    implemented by application authors, its behaviour could be unpredictable.
                    397:    Such behaviour could include:
1.21      lhunt     398: 
                    399:   <ul>
                    400:    <li>Returning inconsistent results;
                    401: 
                    402:    <li>Hanging;
                    403: 
                    404:    <li>Changing the DOM during the operation.
                    405:   </ul>
                    406: 
1.49    ! lhunt     407:   <h2 id=privacy><span class=secno>5. </span>Privacy Considerations</h2>
1.11      avankest  408: 
1.20      lhunt     409:   <p>History theft is a potential privacy issue because the
1.24      lhunt     410:    <code>:visited</code> pseudo-class in Selectors [<cite><a
1.20      lhunt     411:    href="#ref-selectors">Selectors</a></cite>] allows authors to query which
                    412:    links have been visited.
                    413: 
                    414:   <p class=note>This is not a new problem, as it can already be exploited
                    415:    using existing CSS and DOM APIs, such as <code>getComputedStyle()</code>
                    416:    [<cite><a href="#ref-dom2style">DOM2Style</a></cite>].
                    417: 
                    418:   <div class=example>
1.36      lhunt     419:    <p>In this example, <var>vlinks</var> will acquire a list of links that
                    420:     the user has visited. The author can then obtain the URIs and potentially
1.20      lhunt     421:     exploit this knowledge.</p>
                    422: 
1.27      lhunt     423:    <pre>var vlinks = document.querySelectorAll(":visited");
1.20      lhunt     424: for (var i = 0; i &lt; vlinks.length; i++) {
                    425:   doSomethingEvil(vlinks[i].href);
                    426: }</pre>
                    427:   </div>
                    428: 
                    429:   <p>As <a href="http://www.w3.org/TR/css3-selectors/#link">defined in
1.24      lhunt     430:    <cite>Selectors</cite></a> ([<cite><a
                    431:    href="#ref-selectors">Selectors</a></cite>], section 6.6.1), user agents
1.47      lhunt     432:    <em class=ct>may</em> treat all links as unvisited links. It is <em
                    433:    class=ct>recommended</em> that implementations behave consistently with
                    434:    other uses of Selectors supported by the user agent.
1.11      avankest  435: 
1.49    ! lhunt     436:   <h2 id=nodeselector><span class=secno>6. </span>The <code
1.27      lhunt     437:    title="">querySelector()</code> and <code
                    438:    title="">querySelectorAll()</code> Methods</h2>
1.16      avankest  439: 
1.46      lhunt     440:   <p>Objects implementing either the <code>Document</code>,
                    441:    <code>DocumentFragment</code> or <code>Element</code> interfaces, as
1.36      lhunt     442:    defined in DOM Level 3 Core, <em class=ct>must</em> also implement the
1.46      lhunt     443:    <code><a href="#dom-document-selector">NodeSelector</a></code> interface.
                    444:    [<cite><a href="#ref-dom3core">DOM3Core</a></cite>]
1.1       avankest  445: 
1.46      lhunt     446:   <pre class=idl>interface <dfn id=dom-document-selector>NodeSelector</dfn> {
                    447:   Element   <span title=document-selectelement>querySelector</span>([NoNull] in DOMString selectors) raises (NAMESPACE_ERR, SYNTAX_ERR);
                    448:   Element   <span title=document-selectelement>querySelector</span>([NoNull] in DOMString selectors, in <a href="#nsresolver">NSResolver</a> nsresolver) raises (NAMESPACE_ERR, SYNTAX_ERR);
                    449:   <span>NodeList</span>  <span title=document-selectallelements>querySelectorAll</span>([NoNull] in DOMString selectors) raises (NAMESPACE_ERR, SYNTAX_ERR);
                    450:   <span>NodeList</span>  <span title=document-selectallelements>querySelectorAll</span>([NoNull] in DOMString selectors, in <a href="#nsresolver">NSResolver</a> nsresolver) raises (NAMESPACE_ERR, SYNTAX_ERR);
1.16      avankest  451: };
1.46      lhunt     452: </pre>
1.36      lhunt     453: 
                    454:   <p>The term <dfn id=first>first</dfn> used in the definitions of the
                    455:    <code>querySelector</code> methods means <em>first in document order</em>.
                    456:    The term <dfn id=document>document order</dfn> means a depth-first
                    457:    pre-order traversal of the DOM tree or subtree in question. The term <dfn
1.47      lhunt     458:    id=context>context node</dfn> refers to the node upon which the method was
                    459:    invoked. The term <dfn id=nodes>node’s subtree</dfn> refers to the tree
                    460:    of elements that are descendants of the <a href="#context">context
                    461:    node</a>. The term <dfn id=matching>matching <code>Element</code>
                    462:    node</dfn> refers to an <code>Element</code> node that matches the group
                    463:    of selectors (<var>selectors</var>) that was passed to the method,
                    464:    according to the rules for matching elements defined in Selectors
                    465:    ([<cite><a href="#ref-selectors">Selectors</a></cite>]).
1.1       avankest  466: 
1.21      lhunt     467:   <p>The <dfn id=element-selectelement
1.27      lhunt     468:    title=element-selectelement><code>querySelector()</code></dfn> methods on
1.46      lhunt     469:    the <code><a href="#dom-document-selector">NodeSelector</a></code>
1.36      lhunt     470:    interface <em class=ct>must</em>, when invoked, return the first matching
1.46      lhunt     471:    <code>Element</code> node within the node’s subtree. If there is no such
                    472:    node, the methods <em class=ct>must</em> return <code>null</code>.
1.1       avankest  473: 
1.21      lhunt     474:   <p>The <dfn id=element-selectallelements
1.27      lhunt     475:    title=element-selectallelements><code>querySelectorAll()</code></dfn>
1.20      lhunt     476:    methods on the <code><a
1.46      lhunt     477:    href="#dom-document-selector">NodeSelector</a></code> interface <em
1.43      lhunt     478:    class=ct>must</em>, when invoked, return a <code>NodeList</code>
1.47      lhunt     479:    containing all of the matching <code>Element</code> nodes within the
1.46      lhunt     480:    node’s subtree, in document order. If there are no such nodes, the
1.43      lhunt     481:    methods <em class=ct>must</em> return an empty <code>NodeList</code>.
                    482: 
                    483:   <p>The <code>NodeList</code> object returned by the
                    484:    <code>querySelectorAll()</code> methods <em class=ct>must</em> be static,
                    485:    not <a href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#td-live"
                    486:    title="Document Object Model Core">live</a>. ([<cite><a
                    487:    href="#ref-dom3core">DOM3Core</a></cite>], section 1.1.1) Subsequent
                    488:    changes to the structure of the underlying document <em class=ct>must
                    489:    not</em> be reflected in the <code>NodeList</code> object. This means that
                    490:    the object will instead contain a list of matching <code>Element</code>
                    491:    nodes that were in the document at the time the list was created.
1.20      lhunt     492: 
1.46      lhunt     493:   <p>Both <code title=document-selectelement>querySelector()</code> and <code
                    494:    title=document-selectallelements>querySelectorAll()</code> take a group of
                    495:    selectors (<var>selectors</var>) as the first argument and optionally an
                    496:    <code><a href="#nsresolver">NSResolver</a></code> (<var
1.47      lhunt     497:    title="">nsresolver</var>) as the second. The implementation <em
                    498:    class=ct>must</em> process the <var title="">selectors</var> according to
1.46      lhunt     499:    <a href="http://www.w3.org/TR/css3-selectors/#w3cselgrammar">the grammar
                    500:    of Selectors</a> ([<cite><a href="#ref-selectors">Selectors</a></cite>],
                    501:    section 10). Selectors are evaluated against a given element in the
                    502:    context the entire DOM tree in which the element is located.
                    503: 
                    504:   <p>If the user agent also supports some level of CSS, the implementation
                    505:    <em class=ct>should</em> support the same set of selectors in both these
                    506:    APIs and CSS.</p>
                    507:   <!-- XXX Can't remember what this note was here for, but it doesn't seem to make sense now
                    508:                <p class="note">This is to ensure that any DOM modifications performed by a
                    509:                   misbehaving namespace resolver have occurred prior to matching any
                    510:                   elements.</p>
                    511:        -->
1.36      lhunt     512: 
                    513:   <p class=note>Authors are advised that while the use of pseudo-elements in
                    514:    selectors is permitted, they will not match any elements in the document,
                    515:    and thus would not result in any elements being returned. Therefore,
1.46      lhunt     516:    authors should avoid the use of pseudo-elements in selectors that are
                    517:    passed to the methods defined in this specification.
1.36      lhunt     518: 
                    519:   <p>Implementations <em class=ct>must</em> use the <var
                    520:    title="">nsresolver</var> argument to <a href="#get-the">get the default
                    521:    namespace</a> for the group of selectors and, if the selectors include
                    522:    namespace prefixes, the implementation must also use it to <a
                    523:    href="#resolve" title=resolve-prefixes>resolve those prefixes</a>.
                    524: 
                    525:   <p>If the group of selectors uses namespace prefixes, or if the default
                    526:    namespace for the group of selectors is not the <code>null</code>
                    527:    namespace, the caller <em class=ct>must</em> pass in an <code><a
                    528:    href="#nsresolver">NSResolver</a></code> object. Otherwise, the caller <em
                    529:    class=ct>may</em> set the argument to <code>null</code> or omit it if the
                    530:    language binding permits.
                    531: 
                    532:   <p>If the given group of selectors is <a
                    533:    href="http://www.w3.org/TR/css3-selectors/#Conformance">invalid</a>
                    534:    ([<cite><a href="#ref-selectors">Selectors</a></cite>], section 13), the
                    535:    implementation <em class=ct>must</em> <a
1.25      lhunt     536:    href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#DOMException-SYNTAX_ERR">raise
                    537:    a <code>SYNTAX_ERR</code> exception</a> ([<cite><a
                    538:    href="#ref-dom3core">DOM3Core</a></cite>], section 1.4).
1.20      lhunt     539: 
1.22      lhunt     540:   <p>An <dfn id=unresolvable>unresolvable namespace</dfn> is a namespace that
                    541:    cannot be resolved because there was no <code><a
1.21      lhunt     542:    href="#nsresolver">NSResolver</a></code> provided or the given <code><a
1.36      lhunt     543:    href="#nsresolver">NSResolver</a></code> returns an empty string (<a
                    544:    href="#convert-value">or equivalent</a>) for a prefix. When an <a
                    545:    href="#unresolvable">unresolvable namespace</a> is encountered, the
                    546:    implementation <em class=ct>must</em> <a
1.25      lhunt     547:    href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#DOMException-NAMESPACE_ERR">raise
                    548:    a <code>NAMESPACE_ERR</code> exception</a> ([<cite><a
                    549:    href="#ref-dom3core">DOM3Core</a></cite>], section 1.4).
1.1       avankest  550: 
1.36      lhunt     551:   <p class=note>Unresolvable namespaces can only occur for namespace
                    552:    prefixes. The default namespace will always resolve as either the null
                    553:    namespace or the value returned by the <code><a
                    554:    href="#nsresolver">NSResolver</a></code>.
1.14      avankest  555: 
1.26      lhunt     556:   <p>If an exception is raised by the <code><a
                    557:    href="#nsresolver">NSResolver</a></code> while resolving namespaces,
1.37      lhunt     558:    processing <em class=ct>must</em> be aborted and the exception propagated
                    559:    to the caller.
1.26      lhunt     560: 
1.36      lhunt     561:   <p>For languages that support optional arguments for methods, such as
1.22      lhunt     562:    ECMAScript, if the <var title="">nsresolver</var> argument is omitted, the
1.36      lhunt     563:    implementation <em class=ct>must</em> act as if the <var
1.22      lhunt     564:    title="">nsresolver</var> argument was set to <code>null</code>.
1.1       avankest  565: 
1.22      lhunt     566:   <div class=example>
                    567:    <p>The methods accept a group of selectors (comma separated) as the
                    568:     argument. The following example would select all <code>p</code> elements
1.36      lhunt     569:     in the document that have a class of either "<code>error</code>" or
                    570:     "<code>warning</code>".</p>
1.22      lhunt     571: 
1.27      lhunt     572:    <pre>var alerts = document.querySelectorAll("p.warning, p.error");</pre>
1.22      lhunt     573: 
1.46      lhunt     574:    <p>The <code title=document-selectelement>querySelector()</code> methods
                    575:     also accept a group of selectors and they will return the first element
                    576:     (if any) that matches any of the selectors in the group.</p>
1.22      lhunt     577: 
1.27      lhunt     578:    <pre>var x = document.querySelector("#foo, #bar");</pre>
1.22      lhunt     579: 
                    580:    <p><var>x</var> would contain the first element in the document with an ID
                    581:     of either <code>foo</code> or <code>bar</code> (or both).</p>
                    582: 
                    583:    <p>The methods can also be invoked on elements. In this example, the
                    584:     method is invoked on an element that is the target of an event listener.</p>
                    585: 
                    586:    <pre>function handle(evt) {
1.41      lhunt     587:   var x = evt.target.querySelector("span");
1.22      lhunt     588:   ...
1.23      lhunt     589:   // Do something with x
1.22      lhunt     590: }</pre>
                    591:   </div>
                    592: 
1.49    ! lhunt     593:   <h3 id=nsresolver-interface><span class=secno>6.1 </span>The <code
1.16      avankest  594:    title="">NSResolver</code> Interface</h3>
1.6       avankest  595: 
1.14      avankest  596:   <p>The <code><a href="#nsresolver">NSResolver</a></code> interface allows
1.39      lhunt     597:    prefixes to be bound to a namespace URI.
1.7       avankest  598: 
1.20      lhunt     599:   <pre class=idl>interface <dfn id=nsresolver>NSResolver</dfn> {
1.39      lhunt     600:   DOMString       <span>lookupNamespaceURI</span>(in DOMString prefix);
1.6       avankest  601: };</pre>
1.1       avankest  602: 
1.39      lhunt     603:   <p class=note>The object implementing this interface is expected to be
                    604:    implemented by applications. Other specifications may introduce methods
1.44      lhunt     605:    that return an object implementing the <code><a
                    606:    href="#nsresolver">NSResolver</a></code> interface, in which case it would
                    607:    be implemented by the user agent. When the object is implemented by the
1.39      lhunt     608:    application, some conformance requirements are relaxed in order to make it
                    609:    easier for authors to write a conforming implementation.
                    610: 
                    611:   <p>When the <code>lookupNamespaceURI()</code> method is invoked, the
                    612:    implementation <em class=ct>must</em> perform the following algorithm:
1.22      lhunt     613: 
                    614:   <ul>
1.39      lhunt     615:    <li>If the given prefix is an empty string, then:
                    616:     <ul>
                    617:      <li>If there is a default namespace, return the default namespace URI.
                    618: 
                    619:      <li>If there is no default namespace, return an empty string.
                    620:     </ul>
                    621: 
                    622:    <li>Otherwise, look up the namespace URI associated with the given prefix
                    623:     and:
                    624:     <ul>
                    625:      <li>If the prefix has an associated namespace URI, return the URI.
                    626: 
                    627:      <li>If the prefix has no associated namespace URI, return an empty
                    628:       string.
                    629:     </ul>
                    630:   </ul>
1.22      lhunt     631: 
1.39      lhunt     632:   <p>If the <code>lookupNamespaceURI()</code> method is passed a value of
                    633:    <code>null</code> or <code>undefined</code>, then the implementation <em
                    634:    class=ct>must</em> act as if an empty string had been passed instead. If
                    635:    the method is passed an object other than a string, the object <em
                    636:    class=ct>must</em> be converted to a string and the implementation <em
                    637:    class=ct>must</em> act as if the resulting string had been passed instead.
                    638:    The method to convert an object to a string is dependent upon the language
                    639:    used. For example, in ECMAScript, this is achieved by invoking the
                    640:    <code>toString()</code> method on the object.
                    641: 
                    642:   <p>If the <code><a href="#nsresolver">NSResolver</a></code> object is
                    643:    implemented by the application instead of the user agent, then, where the
                    644:    algorithm says to return an empty string, the implementation <em
                    645:    class=ct>may</em> return <code>null</code> or <code>undefined</code>
1.40      lhunt     646:    instead. Additionally, the implementation is not required to handle being
1.39      lhunt     647:    passed <code>null</code>, <code>undefined</code> or non-string objects as
                    648:    the prefix parameter.
1.22      lhunt     649: 
1.39      lhunt     650:   <p>The <code>lookupNamespaceURI()</code> <em class=ct>must</em> return
                    651:    consistent results each time it is invoked.
1.22      lhunt     652: 
                    653:   <p class=note>Authors are <em>strongly discouraged</em> from writing an
                    654:    <code><a href="#nsresolver">NSResolver</a></code> that returns
                    655:    inconsistent results.
                    656: 
1.36      lhunt     657:   <p>To <dfn id=resolve title=resolve-prefixes>resolve namespace
                    658:    prefixes</dfn>, if there was an <code><a
                    659:    href="#nsresolver">NSResolver</a></code> object provided, implementations
1.39      lhunt     660:    <em class=ct>must</em> pass the prefix, preserving case, to the
                    661:    <code>lookupNamespaceURI()</code> method. Implementations <em
                    662:    class=ct>must</em> handle prefixes case sensitively. If there was no
                    663:    <code><a href="#nsresolver">NSResolver</a></code> object provided, or if
                    664:    the method returns an empty string, the prefix represents an <a
                    665:    href="#unresolvable">unresolvable namespace</a>. Otherwise, the return
                    666:    value is the namespace for the given prefix.
1.21      lhunt     667: 
1.44      lhunt     668:   <p class=note>Although prefixes are case sensitive, there are no
                    669:    restrictions imposed by this specification on how the implementation of
                    670:    the <code><a href="#nsresolver">NSResolver</a></code> object may handle
                    671:    them internally. Thus, while the resolver may handle them case
                    672:    insensitively, such an approach is effectively the same as declaring all
                    673:    case variants of the prefix with the same namespace URI.
1.20      lhunt     674: 
1.36      lhunt     675:   <p>To <dfn id=get-the>get the default namespace</dfn>, if there was an
                    676:    <code><a href="#nsresolver">NSResolver</a></code> object provided,
1.39      lhunt     677:    implementations <em class=ct>must</em> invoke the
                    678:    <code>lookupNamespaceURI()</code> method with the empty string as the
                    679:    argument. If there is no <code><a href="#nsresolver">NSResolver</a></code>
                    680:    object provided, or if the method returns an empty string, then there is
                    681:    no default namespace. Otherwise, the return value is the default
                    682:    namespace.
1.36      lhunt     683: 
                    684:   <p id=convert-value>While resolving either a prefix or the default
1.39      lhunt     685:    namespace, if the <code>lookupNamespaceURI()</code> method returns
1.47      lhunt     686:    <code>null</code> or <code>undefined</code>, the implementation <em
1.38      lhunt     687:    class=ct>must</em> act as if an empty string had been returned. If the
                    688:    method returns an object other than a string, the object <em
                    689:    class=ct>must</em> be converted to a string and the implementation must
1.36      lhunt     690:    act as if the resulting string had been returned instead. The method to
                    691:    convert an object to a string is dependent upon the language used. For
                    692:    example, in ECMAScript, this is achieved by invoking the
                    693:    <code>toString()</code> method on the object.
                    694: 
                    695:   <p>For ECMAScript, this object <em class=ct>may</em> be implemented as a
                    696:    <code>Function</code> and implementations <em class=ct>must</em> support
                    697:    the <code><a href="#nsresolver">NSResolver</a></code> being implemented as
                    698:    either a <code>Function</code> or an <code>Object</code>. In the
                    699:    algorithms to <span>resolve namespace prefixes</span> or to <a
                    700:    href="#get-the">get the default namespace</a>, if the implementation is
                    701:    passed a <code>Function</code> instead of an object implementing the
                    702:    <code><a href="#nsresolver">NSResolver</a></code> interface, it must
1.39      lhunt     703:    instead invoke the function itself instead of the
                    704:    <code>lookupNamespaceURI()</code> method.
1.36      lhunt     705: 
                    706:   <p>The number of times that an implementation <em class=ct>may</em> invoke
1.39      lhunt     707:    the <code>lookupNamespaceURI()</code> method with each prefix and the
                    708:    order in which prefixes are resolved is not defined by this specification.
1.20      lhunt     709: 
1.22      lhunt     710:   <div class=example>
1.36      lhunt     711:    <p>The following examples make use of this sample document.</p>
1.20      lhunt     712: 
1.22      lhunt     713:    <pre>&lt;html xmlns="http://www.w3.org/1999/xhtml"
                    714:       xmlns:svg="http://www.w3.org/2000/svg"
                    715:       xml:lang="en">
                    716:   &lt;head>
                    717:     &lt;title&gt;Selectors API Example&lt;/title&gt;
                    718:   &lt;/head>
                    719:   &lt;body>
1.44      lhunt     720:     &lt;div>&lt;svg:svg … > … &lt;/svg:svg>&lt;/div&gt;
                    721:     &lt;p>&lt;svg:svg … > … &lt;/svg:svg>&lt;/p&gt;
                    722:     &lt;p>&lt;svg xmlns="http://www.w3.org/2000/svg" … > … &lt;/svg>&lt;/p>
1.22      lhunt     723:   &lt;/body>
1.36      lhunt     724: &lt;/html></pre>
1.20      lhunt     725: 
1.21      lhunt     726:    <p>The <code><a href="#nsresolver">NSResolver</a></code> may be
1.39      lhunt     727:     implemented as an object with a <code>lookupNamespaceURI</code> method.</p>
1.21      lhunt     728: 
                    729:    <pre>var resolver = {
                    730:   lookupNamespaceURI: function(prefix) {
                    731:     var ns = {
                    732:       "xh"  :"http://www.w3.org/1999/xhtml",
1.22      lhunt     733:       "xbl" :"http://www.w3.org/ns/xbl",
1.21      lhunt     734:       "svg" :"http://www.w3.org/2000/svg",
                    735:       "math":"http://www.w3.org/1998/Math/MathML"
                    736:     };
1.23      lhunt     737:     return ns[prefix];
1.21      lhunt     738:   }
                    739: }</pre>
                    740: 
                    741:    <p>For convenience, in ECMAScript, the <code><a
                    742:     href="#nsresolver">NSResolver</a></code> may also be implemented as a
                    743:     function. In the above example, namespace prefixes are resolved case
1.36      lhunt     744:     sensitively. If case insensitive namespace prefixes are desired, it is
1.21      lhunt     745:     possible to implement this in the resolver by altering the case of the
                    746:     prefix before comparison.</p>
                    747: 
                    748:    <pre>function resolver(prefix) {
                    749:   var ns = {
                    750:     "xh"  :"http://www.w3.org/1999/xhtml",
1.22      lhunt     751:     "xbl" :"http://www.w3.org/ns/xbl",
1.21      lhunt     752:     "svg" :"http://www.w3.org/2000/svg",
                    753:     "math":"http://www.w3.org/1998/Math/MathML"
                    754:   };
1.23      lhunt     755:   return ns[prefix.toLowerCase()];
1.22      lhunt     756: }
                    757: 
1.41      lhunt     758: var svgImages = document.querySelectorAll("xh|p SVG|svg", resolver);</pre>
1.21      lhunt     759: 
1.44      lhunt     760:    <p class=note>This is not a proper implementation of caseless matching in
                    761:     Unicode, which requires both strings that are being compared to be case
                    762:     folded prior to performing a binary comparison [<cite><a
                    763:     href="#ref-casemap">CaseMap</a></cite>]. However, this is a sufficient
                    764:     approximation for prefixes that are restricted to characters, such as
                    765:     those in the US-ASCII subset, for which simply converting both strings to
                    766:     lowercase and comparing returns the correct result. If desired, an
1.46      lhunt     767:     implementation of the full case mapping algorithm is left as an exercise
1.44      lhunt     768:     for the reader.</p>
                    769: 
1.21      lhunt     770:    <p>Using the case insensitive namespace resolver, the prefix
                    771:     <code>xh</code> will be resolved as
                    772:     <code>http://www.w3.org/1999/xhtml</code> and <code>SVG</code> as
1.22      lhunt     773:     <code>http://www.w3.org/2000/svg</code>. The <var>svgImages</var>
1.43      lhunt     774:     variable will be a <code>NodeList</code> containing the two
1.22      lhunt     775:     <code>svg</code> elements that are descendents of the <code>p</code>
                    776:     elements.</p>
                    777: 
                    778:    <p>The default namespace can be specified so that prefixes do not need to
                    779:     be used for selecting elements in a specific namespace. In the following
                    780:     example, the default namespace has been set to the XHTML namespace.</p>
                    781: 
                    782:    <pre>function resolver(prefix) {
                    783:   var ns = {
1.23      lhunt     784:     ""    :"http://www.w3.org/1999/xhtml", // Default namespace
1.22      lhunt     785:     "xbl" :"http://www.w3.org/ns/xbl",
                    786:     "svg" :"http://www.w3.org/2000/svg",
                    787:     "math":"http://www.w3.org/1998/Math/MathML"
                    788:   };
1.23      lhunt     789:   return ns[prefix];
1.36      lhunt     790: }</pre>
1.22      lhunt     791: 
                    792:    <p>Using this resolver, the previous example can be rewritten without
                    793:     explicitly declaring the XHTML namespace using the <code>xh</code>
                    794:     prefix.</p>
                    795: 
1.41      lhunt     796:    <pre>var svgImages = document.querySelectorAll("p svg|svg", resolver);</pre>
1.21      lhunt     797: 
                    798:    <p>In the following example, the <code>foo</code> namespace is undefined
                    799:     and will result in an unresolvable namespace. The
                    800:     <code>NAMESPACE_ERR</code> exception can be handled using a
                    801:     <code>try</code>/<code>catch</code> block.</p>
                    802: 
                    803:    <pre>try {
1.41      lhunt     804:   document.querySelectorAll("foo|bar", resolver);
1.21      lhunt     805: } catch (e) {
                    806:   switch (e.code) {
                    807:   case DOMException.NAMESPACE_ERR:
                    808:     // Namespace error
                    809:     break;
                    810:   case DOMException.SYNTAX_ERR:
                    811:     // Syntax error
                    812:     break;
                    813:   default:
                    814:     // Unknown error
                    815:   }
                    816: }</pre>
1.14      avankest  817: 
1.21      lhunt     818:    <p>This example demonstrates how to iterate through the items in a
1.43      lhunt     819:     <code>NodeList</code>.</p>
1.21      lhunt     820: 
1.41      lhunt     821:    <pre>var lis = document.querySelectorAll("ul.nav&gt;li");
1.24      lhunt     822: for (var i = 0; i &lt; lis.length; i++) {
1.21      lhunt     823:   process(lis.item(i));
                    824: }</pre>
                    825: 
                    826:    <p>In ECMAScript, the language binding also allows <code>NodeList</code>s
1.43      lhunt     827:     and to be addressed using the array notation, so that loop could be
                    828:     rewritten like this:</p>
1.21      lhunt     829: 
1.24      lhunt     830:    <pre>for (var i = 0; i &lt; lis.length; i++) {
1.21      lhunt     831:   process(lis[i]);
                    832: }</pre>
                    833: 
1.43      lhunt     834:    <p>Since the <code>NodeList</code> objects returned by these methods are
1.22      lhunt     835:     not live, changes to the DOM do not affect the content of the list.
1.21      lhunt     836:     Consider the <code>process()</code> function called in the previous
                    837:     examples is defined as follows:</p>
                    838: 
1.36      lhunt     839:    <pre>function process(elmt) {
1.21      lhunt     840:   elmt.parentNode.removeChild(elmt);
                    841: }</pre>
                    842: 
                    843:    <p>This would cause each selected element to be removed from the DOM, but
1.43      lhunt     844:     each element will remain in the <code>NodeList</code>. If the list were a
1.21      lhunt     845:     live <code>NodeList</code>, removing an item from the DOM would also
                    846:     remove the element from the list and adjust the indexes of subsequent
                    847:     elements. That would have adverse effects upon the loop because not all
                    848:     selected elements would be processed.</p>
                    849:   </div>
                    850: 
1.11      avankest  851:   <h2 class=no-num id=references>References</h2>
1.1       avankest  852: 
                    853:   <dl>
1.23      lhunt     854:    <dt id=ref-casemap>[CaseMap]
1.1       avankest  855: 
1.24      lhunt     856:    <dd>(Non-normative) <cite><a
1.21      lhunt     857:     href="http://unicode.org/reports/tr21/tr21-5.html">Unicode Standard Annex
1.24      lhunt     858:     #21 Case Mappings</a></cite>, M. Davis, editor. Unicode Consortium, March
                    859:     2001.
1.1       avankest  860: 
1.23      lhunt     861:    <dt id=ref-css21>[CSS21]
                    862: 
                    863:    <dd>(Non-normative) <cite><a href="http://www.w3.org/TR/CSS21">Cascading
1.44      lhunt     864:     Style Sheets, level 2 revision 1</a></cite>, B. Bos, T. Çelik, I.
                    865:     Hickson, H. Wium Lie, editors. World Wide Web Consortium, June 2005.
1.23      lhunt     866: 
1.22      lhunt     867:    <dt id=ref-dom2style>[DOM2Style]
1.20      lhunt     868: 
1.23      lhunt     869:    <dd>(Non-normative) <cite><a
1.20      lhunt     870:     href="http://www.w3.org/TR/DOM-Level-2-Style/">Document Object Model
1.44      lhunt     871:     (DOM) Level 2 Style Specification</a></cite>, C. Wilson, P. Le Hégaret,
                    872:     V. Apparao, editors. World Wide Web Consortium, November 2000.
1.20      lhunt     873: 
1.11      avankest  874:    <dt id=ref-dom3core>[DOM3Core]
1.1       avankest  875: 
                    876:    <dd><cite><a href="http://www.w3.org/TR/DOM-Level-3-Core">Document Object
                    877:     Model (DOM) Level 3 Core Specification</a></cite>, A. Le Hors, P. Le
1.44      lhunt     878:     Hégaret, L. Wood, G. Nicol, J. Robie, M. Champion, S. Byrne, editors.
1.1       avankest  879:     World Wide Web Consortium, April 2004.
                    880: 
1.11      avankest  881:    <dt id=ref-ecma262>[ECMAScript]
1.1       avankest  882: 
                    883:    <dd><cite><a
                    884:     href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMAScript
                    885:     Language Specification</a></cite>, Third Edition. ECMA, December 1999.
                    886: 
1.25      lhunt     887:    <dt id=ref-rfc2119>[RFC2119]
                    888: 
                    889:    <dd><cite><a href="http://ietf.org/rfc/rfc2119">RFC 2119: Key words for
                    890:     use in RFCs to Indicate Requirement Levels</a></cite>, S. Bradner. IETF,
                    891:     March 1997.
                    892: 
1.11      avankest  893:    <dt id=ref-selectors>[Selectors]
1.1       avankest  894: 
                    895:    <dd><cite><a
                    896:     href="http://www.w3.org/TR/css3-selectors">Selectors</a></cite>, D.
1.44      lhunt     897:     Glazman, T. Çelik, I. Hickson, P. Linss, J. Williams, editors. World
                    898:     Wide Web Consortium, December 2005.
1.1       avankest  899:   </dl>
                    900: 
1.11      avankest  901:   <h2 class=no-num id=acknowledgements>Acknowledgements</h2>
1.1       avankest  902: 
1.45      avankest  903:   <p>The editors would like to thank to the following people who have
1.7       avankest  904:    contributed to this specification (ordered on first name):
                    905: 
1.1       avankest  906:   <ul>
1.19      avankest  907:    <li>Alex Russell
                    908: 
1.44      lhunt     909:    <li>Björn Höhrmann
1.1       avankest  910: 
1.36      lhunt     911:    <li>Boris Zbarsky
                    912: 
1.1       avankest  913:    <li>Cameron McCormack
                    914: 
1.19      avankest  915:    <li>Charles McCathieNevile
                    916: 
                    917:    <li>Chris Wilson
                    918: 
1.1       avankest  919:    <li>Daniel Schierbeck
                    920: 
1.19      avankest  921:    <li>Dave Massy
                    922: 
1.44      lhunt     923:    <li>David Håsäther
1.36      lhunt     924: 
1.1       avankest  925:    <li>Dean Jackson
                    926: 
1.44      lhunt     927:    <li>Erik Dahlström
1.36      lhunt     928: 
1.1       avankest  929:    <li>Ian Hickson
                    930: 
                    931:    <li>Jim Ley
                    932: 
                    933:    <li>Jonas Sicking
                    934: 
1.11      avankest  935:    <li>Jorgen Horstink
                    936: 
1.5       avankest  937:    <li>Karl Dubost
                    938: 
1.1       avankest  939:    <li>Maciej Stachowiak
                    940: 
                    941:    <li>Mohamed Zergaoui
                    942: 
1.24      lhunt     943:    <li>Philip Taylor
                    944: 
1.19      avankest  945:    <li>Robert Sayre
                    946: 
1.1       avankest  947:    <li>Robin Berjon
1.14      avankest  948: 
1.19      avankest  949:    <li>Simon Pieters
                    950: 
1.36      lhunt     951:    <li>Steven Pemberton
                    952: 
1.14      avankest  953:    <li>Tarquin Wilton-Jones
1.21      lhunt     954: 
                    955:    <li>Travis Leithead
1.1       avankest  956:   </ul>
                    957: 
                    958:   <p>Thanks to all those who have helped to improve this specification by
                    959:    sending suggestions and corrections. (Please, keep bugging us with your
                    960:    issues!)

Webmaster