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

Hi Simon.

Simon Pieters:
> (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?

Yeah, they shouldn’t really be different since they are meant to say the
same thing.  In any case, I expect those sections to replace/removed
once the Language Bindings for DOM Specifications spec is published.
Then, this behaviour can be specified from the IDL. [1]

> In any case, they don't quite match reality.
> 
>    http://simon.html5.org/test/html/dom/htmlcollections/item-vs-namedItem/

I did some similar tests too, though with some “tamer” values:

  http://mcc.id.au/2007/05/binding-tests/

(Specifically, test 036.)

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

That is a strange one!

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

That’s different from what I currently have in the Bindings spec, which
is basically “do a ToUint32() on the property name, and if the result is
a non-negative integer, use the index getter, otherwise use the name
getter”.

Do you think IE7’s rules should be used?

[1] http://dev.w3.org/cvsweb/~checkout~/2006/webapi/Binding4DOM/Overview.html?rev=1.44&content-type=text/html;%20charset=utf-8#get

-- 
Cameron McCormack, http://mcc.id.au/
 xmpp:heycam@jabber.org  ▪  ICQ 26955922  ▪  MSN cam@mcc.id.au

Received on Monday, 9 July 2007 10:50:48 UTC