W3C home > Mailing lists > Public > public-html@w3.org > July 2007

HTMLCollection item() vs. namedItem() with [] syntax (detailed review of the DOM)

From: Simon Pieters <simonp@opera.com>
Date: Mon, 09 Jul 2007 11:50:09 +0200
To: public-html <public-html@w3.org>
Message-ID: <op.tu6yxvfcidj3kv@hp-a0a83fcd39d2.palace.opera.no>

(This is part of my detailed review of the Document Object Model section.)

For the HTMLCollection[1], the spec says:

    In ECMAScript implementations, objects that implement the HTMLCollection
    interface must also have a [[Get]] method that, when invoked with a
    property name that is a number, acts like the item() method would when
    invoked with that argument, and when invoked with a property name that
    is a string, acts like the namedItem() method would when invoked with
    that argument.

...and for HTMLFormControlsCollection and HTMLOptionsCollection, it says:

    In the ECMAScript DOM binding, objects implementing the
    HTMLFormControlsCollection interface must support being dereferenced
    using the square bracket notation, such that dereferencing with an
    integer index is equivalent to invoking the item() method with that
    index, and such that dereferencing with a string index is equivalent to
    invoking the namedItem() method with that index.

Do these paragraphs mean the same thing? Why are they phrased differently?

In any case, they don't quite match reality.


There are some points of non-interop here.

When a string is used that looks like an integer in the base 8, or looks  
like a number with decimals:


...then it is equivalent to .item() in Safari and IE7, and equivalent to  
.namedItem() in Firefox and Opera.

Another case is where the string starts with a dot, or looks like a float  
with an exponent:


The above are equivalent to .item() in Safari and .namedItem() in the  

We also have this case:


...which is equivalent to .item(0) in IE and .namedItem("0..") in the  
others. (What comes after the first dot doesn't matter.)

Finally, when an array is used with more than one item:


...then IE uses the first item of the array whereas the others use the  
array's .toString().

Below is what IE7 seems to do, where "obj" is what you pass to the [[Get]]  

   1. If obj is an array, let obj be the array's first item.

   2. If obj is an integer, a float or Infinity, then pass that to .item()
      and abort these steps.

   3. If obj is a string, check whether the string matches the regex
      /^(\d+)(\..*)?$/. If it does, pass $1 to .item() and abort these  

   4. Pass obj.toString() to .namedItem().

[1] http://www.whatwg.org/specs/web-apps/current-work/#htmlcollection
Simon Pieters
Opera Software
Received on Monday, 9 July 2007 09:50:15 UTC

This archive was generated by hypermail 2.3.1 : Thursday, 29 October 2015 10:15:24 UTC