Closed Bug 214574 Opened 22 years ago Closed 13 years ago

form.elements['item'] returns a wrong object

Categories

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

defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: tom_m_yeh, Unassigned)

Details

(Keywords: qawanted)

Attachments

(1 file)

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3) Gecko/20030312 Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3) Gecko/20030312 If a form contains an input element called 'item', the following statement returns the item function (I guess it is document.all.item) rather than the element. document.forms['myForm'].elements['item'] Reproducible: Always Steps to Reproduce: 1. 2. 3. Actual Results: The item function. Expected Results: The element called item
Over to DOM0; should the 'item' object really clobber the item() property of the form.elements nodelist?
Assignee: rogerl → dom_bugs
Component: JavaScript Engine → DOM Level 0
QA Contact: pschwartau → ashishbhatt
Keywords: qawanted
QA Contact: ashshbhatt → ian
Attached file testcase
document.forms[0].elements["item"] returns the function while document.forms[0].elements.namedItem("item") returns the element. DOM HTML Appendix D states: "This object can also be dereferenced using square bracket notation (e.g. obj["foo"]). Dereferencing using a string index is equivalent to invoking the namedItem function with that index.", so I'd say Mozilla's behaviour is wrong.
Erik, the square bracket notation returns the function because that's how ECMAScript works (all properties of the object can be accessed via either square bracket notation or '.' notation). That happens before the DOM code is ever called. The real problem here is that whoever wrote the ECMAScript bindings for DOM HTML didn't stop to consider the fact that they were setting up two requirements that could not both be satisfied at the same time.
Well, I understand it is impossible to meet both requirements, so maybe the real question is, is it acceptable to be incompatible with the other browsers? IE and Opera both return the element "item", not the function (Same is true for "namedItem", btw.). Since these methods are rather useless in JS anyway, shouldn't they be hidden by elements of the same name just like any other form property? I know the form and form.elements are two very different objects, but from a scripting point of view it's strange that I can hide the whole elements collection with <input name="elements"> while the elements' methods are protected.
> so maybe the real question is, is it acceptable to be incompatible with the > other browsers That's what this bug is about, yes. Doing that involves adding a bunch of hacky code to the elements collection of the sort that we had for the form element.
This is an automated message, with ID "auto-resolve01". This bug has had no comments for a long time. Statistically, we have found that bug reports that have not been confirmed by a second user after three months are highly unlikely to be the source of a fix to the code. While your input is very important to us, our resources are limited and so we are asking for your help in focussing our efforts. If you can still reproduce this problem in the latest version of the product (see below for how to obtain a copy) or, for feature requests, if it's not present in the latest version and you still believe we should implement it, please visit the URL of this bug (given at the top of this mail) and add a comment to that effect, giving more reproduction information if you have it. If it is not a problem any longer, you need take no action. If this bug is not changed in any way in the next two weeks, it will be automatically resolved. Thank you for your help in this matter. The latest beta releases can be obtained from: Firefox: http://www.mozilla.org/projects/firefox/ Thunderbird: http://www.mozilla.org/products/thunderbird/releases/1.5beta1.html Seamonkey: http://www.mozilla.org/projects/seamonkey/
Confirming to keep on the radar. bz, do we want to WONTFIX? What interfaces do we do this to? What should I say in HTML5?
Status: UNCONFIRMED → NEW
Ever confirmed: true
So... We're not so consistent about this. In particular: 1) On window or document or form objects, the DOM0 named item lookup overrides IDL-defined properties. 2) On HTMLCollection objects, the DOM ["name"] lookup is overridden by IDL-defined properties. Note that IE somehow guesses which object you want, or something; that is, it'll return the element object for nodelist["item"] in this case, and for nodelist.item, but nodelist.item() will call the right function... If we decide to change this behavior to return the element, it's just a matter of implementing an appropriate NewResolve hook for nsNamedArraySH. I bet if we decide we _really_ want to duplicate what IE does we might be able to do that by having a Call hook on the elements we return... The question is how much we want to duplicate it. And if we don't want to duplicate it fully, the question is what breaks more sites -- our current behavior or returning the element for ["item"]. Oh, if only the DOM WG had written a spec that could be implemented here... :(
Well, var m = nodelist.namedItem; var a = m('a'); I don't think we want to go down _that_ road. So at the moment we're doing special lookups only on Window, Document and HTMLFormElement? Interesting. I'd suggest we stick to that and I'll make the HTML5 spec say that's what we should be doing.
> So at the moment we're doing special lookups only on Window, Document and > HTMLFormElement? Lookups that can override DOM props, yes. Note that I think your example doesn't work anyway because it needs the right |this|... and using call() would work right if we had a call hook.
Here's what I propose to do. Make Window, Document, and HTMLFormElement have [[Get]] implementations that override what the IDL says. Make the others have a [[Get]] implementation that defers to the IDL first. Make all of them have no special magic with respect to [[Call]]. Any objections?
Is there a reason you proposed to go with the inconsistent approach? I would prefer making all the [[Get]] implementations be consistent, although I don't care much whether they override the IDL or not. Making them inconsistent is going to be confusing for both implementors and users. The only advantage I can see to having Window override the IDL is to guard against future extensions to Window that conflict with named items on already-existing pages. (i.e. some spec defines window.cnnAd, and cnnAd is a named item on cnn.com causing it to break on a UA that implements the spec). However, the same is much less likely for Document and HTMLFormElement, so I think those at least should be changed to defer to the IDL.
> Is there a reason you proposed to go with the inconsistent approach? It's what browsers do right now.
It's what *some* browsers do right now. In HTMLCollection, Opera overrides the IDL for functions but not properties. (i.e. document.forms.namedItem could return a HTMLFormElement instead of a function). document.cookie can be overriden by a <form name="cookie"> in Opera and Safari. Browsers generally seem to be all over the place on this one.
I ended up just deferring to WebIDL on this.
Assignee: general → nobody
QA Contact: ian → general
If I read WebIDL (<http://dev.w3.org/2006/webapi/WebIDL/#getownproperty>) correctly, our behaviour is correct.
Status: NEW → RESOLVED
Closed: 13 years ago
OS: Windows 2000 → All
Hardware: x86 → All
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: