This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 14743 - Indexing NodeList out of range should return undefined
Summary: Indexing NodeList out of range should return undefined
Status: RESOLVED INVALID
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: DOM (show other bugs)
Version: unspecified
Hardware: All All
: P2 normal
Target Milestone: ---
Assignee: Anne
QA Contact: public-webapps-bugzilla
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-09 18:47 UTC by Aryeh Gregor
Modified: 2011-11-09 20:04 UTC (History)
2 users (show)

See Also:


Attachments

Description Aryeh Gregor 2011-11-09 18:47:16 UTC
Try this on Live DOM Viewer:

<!DOCTYPE html>
<script>
try {w(document.childNodes[-1])} catch(e) {w(e)}
try {w(document.childNodes.item(-1))} catch(e) {w(e)}
try {w(document.childNodes[20])} catch(e) {w(e)}
try {w(document.childNodes.item(20))} catch(e) {w(e)}
</script>

IE9: undefined, null, undefined, null
Firefox 9.0a2: INDEX_SIZE_ERR, null, undefined, null
Chrome 16 dev: undefined, INDEX_SIZE_ERR, undefined, null
Opera 12.00: undefined, null, undefined, null

Correct behavior is clearly IE/Opera, IMO.  Throwing anywhere here is annoying because it means you can't use if (document.childNodes[i]) to test whether the child exists.

The current spec says NodeList.prototype.item is a getter, and therefore as far as I can tell its behavior has to match actual indexing per WebIDL.  The correct fix is probably to make item() not a getter, and define separate getting behavior that returns undefined.
Comment 1 Aryeh Gregor 2011-11-09 20:04:04 UTC
Hixie pointed me to the correct part of WebIDL.  I think it contradicts the parts I was looking at, so I'll file some WebIDL bugs, but DOM4 is correct, anyway.