Re: Unsafe Names for HTML Form Controls
- From: dhtml <dhtmlkitchen@xxxxxxxxx>
- Date: Thu, 30 Oct 2008 23:36:22 -0700
Richard Cornford wrote:
dhtml wrote:RobG wrote:<snip>On Oct 26, 4:38 pm, dhtml wrote:I have written an article "Unsafe Names for HTML Form Controls".I did not agree with all of your feedback though.
You probably won't agree with mine either, but we will have to wait as it will be a while before i have time to write them all down.
I'll try to get to these comments over the weekend.
<snip>In the section “What kind of Collection is a Form”, I have
serious reservations about calling a form a collection. It
isn’t, it a DOM object with certain properties. You can't
access the controls as form[i], whereas you can use
form.elements[i] because its elements property really is
a collection.
It's right in the spec.
No it is not.
spec says it is:
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-40002357
| The FORM element encompasses behavior similar to a collection and
| an element.
And we can see that a form control can be referenced right off the form:-
document.forms[0][0]
If something has behavior of a collection, then it can be called a collection. Not an "HTMLCollection" but some sort of collection that is vaguely described by the spec.
of course that's not the only thing a form is. It's also an EventTarget in most modern browsers.
<snip>“The FORM...provides direct access to the contained input
elements”
Is misleading, a form has direct access to all named form
controls, not just input elements. Also, this point has been
made earlier when stating that references to named controls
are added as properties of the form. Further, the elements
collection gives access to all controls, not just the named
ones.
The quote is from DOM 1.
http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-40002357
I don’t think there should be any reference to the form as a
collection - since you can’t access the named properties by index, it
isn’t any kind of collection (mystery solved).
Not calling a form a collection because it doesn't allow access by index is not a good argument. First of all, it isn't true.
The DOM specification says that a form encompasses behavior of a collection.
You are reading more into the text than it actually says. In the spec, the ECMAScript bindings documents make assertions about the mapping of bracket notation property accessors to collection interface methods. No such assertions are made about the HTMLFormElement interface, and it also has no methods that could be mapped to the property accessors. Thus there is no DOM specified behaviour for - formElement[0] - or - formElement['someName'] -.
You seem to interpret that as the property accessors map to either item or namedItem method. But we can see that when using a string as a property, the indexed element is returned.
That is a true statement. Named properties are accessed by name. Indexed properties, by index. That's two types of ways
to index a form control.
But neither are defined in the (ECMAScript bindings for) the specification.
I really don't think I'm reading more into the spec. This is what it says:-
| Functions of objects that implement the HTMLCollection interface:
|
| item(index)
| This *function* returns an object that implements the Node
| interface.
| The index parameter is a Number.
| Note: This *object* can also be dereferenced using square
| bracket notation (e.g. obj[1]). Dereferencing with an integer
| index is equivalent to invoking the item function with that
| index.
| namedItem(name)
| This function returns an object that implements the Node
| interface.
| The name parameter is a String.
| Note: 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.
Where the spec says "Note: This *object* can also be dereferenced using square bracket notation (e.g. obj[1])", that does define how property accessors work on a form, even though they defined it wrong.
The square bracket property accessors do not do type checking. Not on native objects and not when used on DOM elements. The Expression is converted to a string.
That's why I pointed out:
http://jibbering.com/faq/names/extra_props.html#StandardWrong
And the example shows that
document.forms[0]["0"] is the same as document.forms[0][0];
There is no equivalent of "namedItem" being called. It doesn't work that way.
Second, a map, such as NamedNodeMap, can be a collection.<snip>
Not all collections are indexed.
That would depend on how you defined a collection (the DOM specification doesn't).
Right.
The term 'collection', in this context, comes from early Netscape browser documentation, and was also adopted by Microsoft to describe corresponding structures/objects. The HTML DOM formalises (most of) those 'collection' objects as the HTMLCollection interface, but a NamedNodeMap is not an HTMLCollection.
Right. Its a collection in the general sense. For example, a java.util.Map is part of the "Collections Framework". Granted, we're not talking about Java, but in the general sense of the word "collection" a map fits the description.
Richard.
--
comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
.
- Follow-Ups:
- Re: Unsafe Names for HTML Form Controls
- From: Richard Cornford
- Re: Unsafe Names for HTML Form Controls
- References:
- Unsafe Names for HTML Form Controls
- From: dhtml
- Re: Unsafe Names for HTML Form Controls
- From: RobG
- Re: Unsafe Names for HTML Form Controls
- From: dhtml
- Re: Unsafe Names for HTML Form Controls
- From: Richard Cornford
- Unsafe Names for HTML Form Controls
- Prev by Date: Re: About (function(){})()
- Next by Date: Re: Unsafe Names for HTML Form Controls
- Previous by thread: Re: Unsafe Names for HTML Form Controls
- Next by thread: Re: Unsafe Names for HTML Form Controls
- Index(es):
Relevant Pages
|