Re: John Resig has a new idea



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
.



Relevant Pages

  • Re: two complaints by Intel Fortran
    ... Intel consider two interfaces incompatible ... accepts an empty ... generic interface, but unlike ... forms that the standard doesn't allow to be empty. ...
    (comp.lang.fortran)
  • Re: IBindingList AllowNew is FALSE!!!
    ... It is possible because - and now the fun starts - AddNew is called by the ... interface. ... > collections of other business objects up to three or four levels deep. ... > empty the CurrencyManager keeps trying to call the AddNew method of the ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Purpose of an empty Interface ??
    ... Interface, "javax.jms.ConnectionFactory". ... This interface was empty. ... This question does not apply to JAVA but to general OOP. ... There are two different schools of thought on whether empty interfaces ...
    (comp.programming)
  • Re: Purpose of an empty Interface ??
    ... Interface, "javax.jms.ConnectionFactory". ... This interface was empty. ... "empty" abstract classes and interfaces in other languages. ... There are two different schools of thought on whether empty interfaces ...
    (comp.programming)
  • Re: Determining if ActiveX is enabled
    ... > Is there a property in the DOM or the Request object that indicates ... whether or not the user's browser has been set to enable, disable, or notify ... I want to use one of two different style sheets based on this setting. ...
    (microsoft.public.scripting.vbscript)