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

(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.

    http://simon.html5.org/test/html/dom/htmlcollections/item-vs-namedItem/

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:

    .elements["010"]
    .elements["0.0"]
    .elements["0."]

...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:

    .elements[".0"]
    .elements["0e0"]

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

We also have this case:

    .elements["0.."]

...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:

    .elements[["0","1"]]

...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]]  
method...

   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  
steps.

   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