Closed Bug 818548 Opened 12 years ago Closed 6 years ago

getElementsByAttribute returns an HTMLCollection, not a NodeList

Categories

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

17 Branch
x86
Linux
defect

Tracking

()

RESOLVED WONTFIX

People

(Reporter: moises.campos, Unassigned)

Details

Attachments

(1 file)

User Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20100101 Firefox/17.0
Build ID: 20121128204232

Steps to reproduce:

if we run this code: 
<vbox id="list">
	<button id="one" source="source1" label="1"/>
	<button id="two" source="source2" label="2"/>
	<button id="three" label="3"/>
	<button id="four" source="source4" label="4"/>
</vbox>
<button label="Go!" oncommand="test()"/>
<script>
var ConsSrv = Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService);
function test() {
	var list = document.getElementById('list').getElementsByAttribute('source', '*');
	for each(var obj in list) {
		ConsSrv.logStringMessage('|'+obj.tagName+'|'+obj.id+'|');
	}
}
</script>

Note: This code is loaded through document.loadOverlay invocation 



Actual results:

On Firefox v17.0.1 we get this result on console: 

|button|one|
 ----------
|button|two|
 ----------
|button|four|
 ----------
 ----------
Warning: ReferenceError: reference to undefined property obj.tagName
Source file: (...) 
Line: 29
 ----------
|undefined|undefined|
 ----------
|undefined|undefined|
 ----------
|undefined|undefined|
 ----------
|undefined|undefined|

On Aurora 19.0a2 we get this result: 
|button|one|
 ----------
|button|two|
 ----------
|button|four|
 ----------
|button|one|
 ----------
|button|two|
 ----------
|button|four|
 ----------
Warning: ReferenceError: reference to undefined property obj.tagName
Source file: (...)
Line: 29
 ----------
|undefined|undefined|
 ----------
|undefined|undefined|
 ----------
|undefined|undefined|
 ----------
|undefined|undefined|

The returned NodeList has duplicates obj's in it. 
This happens on Linux (Fedora 16) and also tested on MWindows 



Expected results:

I think that the correct result should be: 
|button|one|
 ----------
|button|two|
 ----------
|button|four|
Moisés, could you please attach a testcase that can reproduce this issue?
Flags: needinfo?(moises.campos)
Component: Untriaged → DOM
Product: Firefox → Core
Attached file XUL page with testcase
The output is sent to console.
Flags: needinfo?(moises.campos)
> for each(var obj in list) {

That's the wrong way to iterate over a list, and it's what's biting you...

In this case, GetElementsByAttribute returns an HTMLCollection, not a NodeList (which is perhaps a bug in its own right, but a preexisting one I suspect).  And what changed is that enumeration of an HTMLCollection now also enumerates the named properties, as the spec requires.  So when you enumerate all properties, as you do above, you get the following set of property names: { "0", "1", "2", "one", "two", "four" }.  Then you look them up on the list object, and get the result you get.

I don't know whether the compat hit from enumerating the names is worse than the compat hit from making GetElementsByAttribute return a NodeList and thus hiding the names completely, given that I believe we've exposed the names on it for a decade or so if you look for them...
Also note that you'd have similar problems if anyone added enumerable properties to HTMLCollection.prototype or Object.prototype...
Summary: getElementsByAttribute returns wrong NodeList → getElementsByAttribute returns an HTMLCollection, not a NodeList
Peter, thoughts?
Well noticed! Wrong webpage reference... Iteration in the returned Array works fine in Aurora 19.0a2 and Firefox v17.0.1. 
Thanks for the inconvenience
|for (var obj of list)| works.
https://bugzilla.mozilla.org/show_bug.cgi?id=1472046

Move all DOM bugs that haven’t been updated in more than 3 years and has no one currently assigned to P5.

If you have questions, please contact :mdaly.
Priority: -- → P5
Wontfix.  We're not planning to change this behavior.  The IDL will be updated to say HTMLCollection.
Status: UNCONFIRMED → RESOLVED
Closed: 6 years ago
Resolution: --- → WONTFIX
Component: DOM → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: