Bug 104096

Summary: HTMLCollections namedItem() methods should return null than undefined for empty collections.
Product: WebKit Reporter: Aleksei Yu. Semenov <a.semenov>
Component: DOMAssignee: Vineet Chaudhary (vineetc) <code.vineet>
Status: RESOLVED FIXED    
Severity: Normal CC: abarth, ap, code.vineet, dglazkov, haraken, japhet, webcomponents-bugzilla, webkit.review.bot
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: Unspecified   
OS: Unspecified   
URL: http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interfaces.html#htmlallcollection
Attachments:
Description Flags
proposed_patch none

Description Aleksei Yu. Semenov 2012-12-05 00:50:07 PST
HTML5 specification ( http://www.w3.org/TR/html5/the-form-element.html#the-form-element )
define for element 'form' property 'elements' of type HTMLFormControlsCollection.
The HTMLFormControlsCollection is defined as follows ( http://www.w3.org/TR/html5/common-dom-interfaces.html#htmlformcontrolscollection-0 ):

interface HTMLFormControlsCollection : HTMLCollection {
  // inherits length and item()
  legacycaller getter object? namedItem(DOMString name); // overrides inherited namedItem()
};

"The namedItem(name) method must act according to the following algorithm:

1. If, at the time the method is called, there is exactly one node in the collection that has either an id attribute or a name attribute equal to name, then return that node and stop the algorithm.
2. Otherwise, if there are no nodes in the collection that have either an id attribute or a name attribute equal to name, then return null and stop the algorithm.
3. Otherwise, create a new RadioNodeList object representing a live view of the HTMLFormControlsCollection object, further filtered so that the only nodes in the RadioNodeList object are those that have either an id attribute or a name attribute equal to name. The nodes in the RadioNodeList object must be sorted in tree order.
4. Return that RadioNodeList object."

However, namedItem() returns undefined result instead of null for the case #2.

Here is sample code:

<html>
<head>
<script type="text/javascript">

function test() {
	    var form = document.createElement('form');
	    document.body.appendChild(form);

	    document.write('form.elements.namedItem("non-existing-id"): ' + form.elements.namedItem('non-existing-id') + ' (expected: null)');
}
</script>
</head>
<body onload="test()">
</body>
</html>

The problem is found in Google Chrome 23.0.1271.91 (Official build 167501) m
Comment 1 Vineet Chaudhary (vineetc) 2013-02-13 14:39:17 PST
Created attachment 188187 [details]
proposed_patch
Comment 2 Hajime Morrita 2013-02-13 15:59:48 PST
Comment on attachment 188187 [details]
proposed_patch

The change looks good.
Could you make the test a bit more comprehensive?
It's worth covering some neighboring cases in same test.
Comment 3 Kentaro Hara 2013-02-13 16:09:15 PST
Comment on attachment 188187 [details]
proposed_patch

The change looks good. What is the behavior of other browsers?
Comment 4 Vineet Chaudhary (vineetc) 2013-02-13 16:35:12 PST
(In reply to comment #2)
> (From update of attachment 188187 [details])
> The change looks good.
> Could you make the test a bit more comprehensive?
> It's worth covering some neighboring cases in same test.

Actually most of the namedItem() tests are already covered in like :
fast/dom/collection-item.html
collection-nameditem-move-between-documents.html


> The change looks good. What is the behavior of other browsers?
All other browsers behaves as expected ie. returns null.
Comment 5 Kentaro Hara 2013-02-13 16:42:26 PST
Comment on attachment 188187 [details]
proposed_patch

Thanks!
Comment 6 Vineet Chaudhary (vineetc) 2013-02-13 17:06:33 PST
Comment on attachment 188187 [details]
proposed_patch

Thank you!!
Comment 7 WebKit Review Bot 2013-02-13 17:32:55 PST
Comment on attachment 188187 [details]
proposed_patch

Clearing flags on attachment: 188187

Committed r142831: <http://trac.webkit.org/changeset/142831>
Comment 8 WebKit Review Bot 2013-02-13 17:32:59 PST
All reviewed patches have been landed.  Closing bug.
Comment 9 Vineet Chaudhary (vineetc) 2013-02-13 19:53:27 PST
(In reply to comment #7)
> (From update of attachment 188187 [details])
> Clearing flags on attachment: 188187
> 
> Committed r142831: <http://trac.webkit.org/changeset/142831>

Sorry this seems caused regression bug 109780.