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

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

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

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

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.

Ok.

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