Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 274a673

Browse files
committed
fix(forEach): allow looping over result of querySelectorAll in IE8
In IE8 the result object of calling `node.querySelectorAll` does not have a `hasOwnPropery` function. However, it should be usable with `forEach`. Related to #5400.
1 parent f0e3dfd commit 274a673

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Angular.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ function forEach(obj, iterator, context) {
213213
if (obj) {
214214
if (isFunction(obj)){
215215
for (key in obj) {
216-
if (key != 'prototype' && key != 'length' && key != 'name' && obj.hasOwnProperty(key)) {
216+
// Need to check if hasOwnProperty exists,
217+
// as on IE8 the result of querySelectorAll is an object without a hasOwnProperty function
218+
if (key != 'prototype' && key != 'length' && key != 'name' && (!obj.hasOwnProperty || obj.hasOwnProperty(key))) {
217219
iterator.call(context, obj[key], key);
218220
}
219221
}

test/AngularSpec.js

+15
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,21 @@ describe('angular', function() {
504504
expect(log).toEqual(['0:a', '1:c']);
505505
});
506506

507+
if (document.querySelectorAll) {
508+
it('should handle the result of querySelectorAll in IE8 as it has no hasOwnProperty function', function() {
509+
document.body.innerHTML = "<p>" +
510+
"<a name='x'>a</a>" +
511+
"<a name='y'>b</a>" +
512+
"<a name='x'>c</a>" +
513+
"</p>";
514+
515+
var htmlCollection = document.querySelectorAll('[name="x"]'),
516+
log = [];
517+
518+
forEach(htmlCollection, function(value, key) { log.push(key + ':' + value.innerHTML)});
519+
expect(log).toEqual(['0:a', '1:c']);
520+
});
521+
}
507522

508523
it('should handle arguments objects like arrays', function() {
509524
var args,

0 commit comments

Comments
 (0)