Re: John Resig has a new idea
- From: Garrett Smith <dhtmlkitchen@xxxxxxxxx>
- Date: Fri, 27 Mar 2009 12:37:33 -0800
Richard Cornford wrote:
Danny wrote:Actually, this analysis is largely wrong. If the collection is
empty, the object[0] will throw the error,
So in what sense is asserting that the - value = object[0] - expression will throw an error wrong? Your test-case has no style sheets and so the - document.styleSheets - collection will be empty in its case.
Yep. And object.item(0) will throw the error, too.
[...]
That is just for native objects. Host objects are allowed to implement their [[Put]] methods in any way they want, including such that exceptions are thrown on assignment. Thus when you are dealing with a host object (or don't know what type of object you are dealing with) the only sensible approach is to assume the worst, which includes not accessing the 'array index' members of array-like objects outside of the range with the upper limit (x.length - 1).
The error is not caused by using property access [] operator (to get the out-of-index property).
The error occurs with IE trying to get the out of index property. The error occurs whether by property access, by calling the item method, or by calling the collection directly (which IE allows).
The item() method in IE and does not return null when called on various collections (but not all collections) in IE.
Some examples of out-of-bounds item returning null are node.all.item(99999) and styleSheet.media.item(99999). Both correctly return null in IE.
The w3c does not define a standard "collection" interface with an item method. IE documents its item method as being something that operates on "various collections". This is fine. What is not fine is IE's design and testing.
In reality the most restrictive standard imposing a constraint on behaviour here is the W3C DOM spec (and ECMAScript bindings) for the StyleSheetList interface and its - item - method, and if taken literally that document requires that reading of an 'out of bounds' index should result in the - null - value (with no exceptions thrown). That position is arguable (hinges on the interpretation of "integer index" in context), but if accepted IE is not conforming with that specification, but then no other browsers do either so it becomes a little hypocritical to point the finger at IE alone. (There is no binding of IDL null to any ECMAScript primitive in the W3C specs, but if you expect - getElementById - to return null for a non-existing ID value, rather than the undefined value, then you also have to expect - item - to return null)
I am not sure you mean by "no other browsers conform to the specification".
javascript: alert(document.styleSheets.item(999999))
"null" in Safari, Firefox, Opera.
These browsers do not return undefined for an 'out of bounds' index.
However, the item() method in IE throws the same error when the index is out of range:-
javascript: alert( document.styleSheets.item(999999) )
The w3c HTML DOM mentions the use of property accessors for HTMLCollection. However, DOM Level 2 Style makes no mention of property accessors behavior is not standardized for a StyleSheetList.
The w3c did not define a generalized "collection" interface. Such design would seem to avoid w3c specification shotgun surgery.
Garrett
.
- Follow-Ups:
- Re: John Resig has a new idea
- From: Richard Cornford
- Re: John Resig has a new idea
- From: Garrett Smith
- Re: John Resig has a new idea
- References:
- John Resig has a new idea
- From: beegee
- Re: John Resig has a new idea
- From: Richard Cornford
- Re: John Resig has a new idea
- From: Danny
- Re: John Resig has a new idea
- From: Richard Cornford
- John Resig has a new idea
- Prev by Date: Re: window.focus() issue
- Next by Date: Re: John Resig has a new idea
- Previous by thread: Re: John Resig has a new idea
- Next by thread: Re: John Resig has a new idea
- Index(es):
Relevant Pages
|