Closed Bug 371711 Opened 13 years ago Closed 4 years ago

Assigning into the HTMLCollection returned by document.getElementsByTagName should throw


(Core :: DOM: Core & HTML, enhancement)

Not set





(Reporter: jruderman, Unassigned)


(Keywords: testcase)


(2 files)

Attached file testcase
I think the following code should cause an exception to be thrown:

  document.getElementsByTagName('button')[0] = 'wtf';

I don't think this would break web sites, since it crashed for a while (bug 367589) and I was the first to notice.
Assignee: general → nobody
QA Contact: ian → general
Strangely, Safari and Opera have the same behavior: it doesn't throw.
WebIDL + DOM Core probably mandates one behavior or another here.
Until DOM Core NodeLists are rewritten in Web IDL (probably as part of Web DOM Core) no particular behaviour is mandated at the moment.  But if it were, I think the behaviour should be not to throw, unless in strict mode.  (Because the "0" property that would exist due to the index getter would be [[Writable]]:false.)
Attached file Strict mode test case
AFAICT from WebIDL, the assignments in this test should throw, but nobody does that. Cameron, could you have a look?
Yes, with that test, the first assignment should throw a TypeError (the [[DefineOwnProperty]] algorithm in says to Reject (= throw when in strict mode) if the object supports indexed properties but doesn't have a setter or creator defined on the interface.

The two last w() calls should then write out "[object HTMLButtonElement]" and "undefined".

Note that this is a slightly different case from throwing due to being in strict mode and assigning to a property that exists and is a currently supported property index (*mouthful*).  If everyone just ignores the assignment, I'm open to changing the spec to require that instead of throwing, if people think it's a good idea.  I think it's more consistent with the aims of strict mode to throw in this case, though.
Attachment #571338 - Attachment mime type: text/plain → text/html
Worksforme in strict mode, as expected.
Closed: 4 years ago
Resolution: --- → WORKSFORME
Component: DOM → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.