Last Comment Bug 300519 - Extending the HTMLCollection prototype has no effect
: Extending the HTMLCollection prototype has no effect
Product: Core
Classification: Components
Component: DOM: Core & HTML (show other bugs)
: Trunk
: x86 Windows XP
-- normal with 1 vote (vote)
: ---
Assigned To: Nobody; OK to take it and work on it
: Andrew Overholt [:overholt]
Depends on: 648801
  Show dependency treegraph
Reported: 2005-07-12 13:21 PDT by Sjoerd Visscher
Modified: 2013-04-04 08:14 PDT (History)
9 users (show)
See Also:
Crash Signature:
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---


Description User image Sjoerd Visscher 2005-07-12 13:21:53 PDT
Adding a method to HTMLCollection.prototype should allow that method to be
called on all HTMLCollections, like document.images or document.frames:

HTMLCollection.prototype.test = function() { alert("test"); }

(document.images.constructor == HTMLCollection, so that's not the problem)

I wouldn't have noticed, if there wasn't bug 14869, which makes
document.getElementsByTagName return an HTMLCollection.
Comment 1 User image Boris Zbarsky [:bz] (still a bit busy) 2005-10-31 20:33:31 PST
So all that's going on here is that HTMLCollection.prototype is not on the proto chain of, say, document.images...

The reason, I assume is that HTMLCollection is a proto for things that use the HTMLCollection classinfo.  document.images uses the ContentList classinfo, which goes by the HTMLCollection name, but isn't an HTMLCollection.
Comment 2 User image Mihai Sucan [:msucan] 2009-03-03 08:21:21 PST
Confirmed. This problem just bit me. I was trying to extend the HTMLCollection prototype object for the purpose of changing the new .children property in Firefox 3.1.

HTMLCollection can be extended in Opera, WebKit and Konqueror. No issues there.
Comment 3 User image Boris Zbarsky [:bz] (still a bit busy) 2011-10-11 20:47:57 PDT
Peter, do new list bindings fix this?  Or do you need to do the nodelist/htmlcollection split first?
Comment 4 User image Peter Van der Beken [:peterv] 2011-10-12 03:23:28 PDT
The problem with images is that we have two DOMCIs for HTMLCollection, and whichever is defined first wins. For example starting with images, |HTMLCollection.prototype.test = function() { alert("test"); } document.images.test();| would work, but then |document.createElement("form").elements.test()| would fail. And vice versa. That's fixed with the new DOM bindings. Also, |document.createElement("select").options.test();| now works due to the new DOM bindings. I'm going to mark this fixed, the nodelist/htmlcollection split will just allow us to return the right type of object wherever we currently return a nsContentList (which sort of ends up as an HTMLCollection in JS).

Note You need to log in before you can comment on or make changes to this bug.