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

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

From: Simon Pieters <simonp@opera.com>
Date: Mon, 09 Jul 2007 15:00:27 +0200
To: "Maciej Stachowiak" <mjs@apple.com>
Cc: public-html <public-html@w3.org>
Message-ID: <op.tu67q1mbidj3kv@hp-a0a83fcd39d2.palace.opera.no>

On Mon, 09 Jul 2007 12:37:14 +0200, Maciej Stachowiak <mjs@apple.com>  

> They are both subtly wrong because, logically, ECMAScript property names  
> are always strings, and non-string arguments to the [] operator undergo  
> an implicit toString conversion.
> This is why for ECMAScript arrays, 0, 0.0, .0 and "0" all work as  
> numeric subscripts for the built-in Array type, but "0.0" and ".0" do  
> not.
> javascript:alert(["a"][0]) -> a
> javascript:alert(["a"]["0"]) -> a
> javascript:alert(["a"][0.0]) -> a
> javascript:alert(["a"]["0.0"]) -> undefined
> javascript:alert(["a"][.0]) -> a
> javascript:alert(["a"][".0"]) -> undefined
> The model here is that numeric values get converted to string and then  
> string-compared with the appropriate string represetnation of an  
> integer, so it does not matter how they are spelled. But strings undergo  
> no conversion.

Ok. Makes sense.

> [...]
>>  1. If obj is an array, let obj be the array's first item.
> I don't think this is a special rule for arrays, that's just how it  
> handles strings with a comma. I just tired  
> javascript:alert(document.links["0,1"]) on a random page in IE and it  
> gave me the first element of the links array.

Ah. I didn't consider testing commas. Testing further, it seems that IE  
does the same for a lot of characters (but also doesn't for a lot of  


The other browsers don't do this at all. Hopefully that is an indicator  
that it isn't needed for compat with legacy content.

>>  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().
> I think all of these steps are actually operating on strings, not  
> general JavaScript values, so to correctly document what IE does you  
> would have to reformulate purely in terms of strings.


I also realised that I did a mistake in my testing: I used 0/0 when I  
meant 1/0. Infinity isn't treated specially.

Simon Pieters
Opera Software
Received on Monday, 9 July 2007 13:00:45 UTC

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