prototypejs / prototype

Prototype JavaScript framework
http://prototypejs.org/
Other
3.54k stars 639 forks source link

Array.prototype.filter does not work when applied to a NodeList #293

Closed codefactor closed 9 years ago

codefactor commented 9 years ago
 [].filter.apply(document.body.childNodes, function(el){return true})

The implementation of Array.prototype.filter assumes this points to an Array. The native implementation of Array.prototype.filter works fine, but Prototype's version does not.

codefactor commented 9 years ago

I've found a similar usage in another open source library html2canvas - I just thought I'd bring this issue up here.

Should functions on the Array.prototype being overridden by Prototype support cases where this is not an Array?

savetheclocktower commented 9 years ago

The way this is written means that if Array.prototype.filter is present in the browser, we'll effectively be overriding it with itself (Array.prototype.filter = Array.prototype.filter). If it isn't present, we'll be using our version, which is adapted from the MDN polyfill.

Which browser exhibits this problem? And — just to be clear — you're using version 1.7.2, right?

savetheclocktower commented 9 years ago

BTW, on a sample page with 1.7.2 included, this works as I expect:

 [].filter.call(document.body.childNodes, function(el){ return true });

Your example likely does not work because apply expects an array of args as a second argument.

lmazuel commented 9 years ago

Hi,

I think I have similar issue here: https://github.com/n1k0/casperjs/issues/1212

Your filter uses each which is not available in HTMLOptionsCollection

> [].each
function c(y,x){var w=0;try{this._each(function(A){y.call(x,A,w++)})}catch(z){if(z!=$break){throw z}}return this}
> select.options.each
undefined
> select.options.toString()
"[object HTMLOptionsCollection]"

Then it's not possible to filter HTMLOptionsCollection object.

The website I try to use uses Prototype 1.7. I don't know if it's working on 1.7.2, I'm just a QA tester which makes a few JS, not a web developer. If you have the time to confirm (or not) if it's working on 1.7.2, it will help me a lot and gives the opportunity to open a library update requirement on my dev team.

Thank for your help!

(PhantomJS 1.9.0 and Chrome 41.0.2272.118)

lmazuel commented 9 years ago

Finally, I have understood how to test it in Prototype 1.7.2. It works in 1.7.2, the filter function is not overridden.

I hope my messages will help however,