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

Commit 93c7251

Browse files
gkalpakpetebacondarwin
authored andcommittedJan 7, 2016
fix(isArrayLike): recognize empty instances of an Array subclass
Fixes #13560 Closes #13708
1 parent e1def1b commit 93c7251

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed
 

‎src/Angular.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ function isArrayLike(obj) {
215215
// NodeList objects (with `item` method) and
216216
// other objects with suitable length characteristics are array-like
217217
return isNumber(length) &&
218-
(length >= 0 && (length - 1) in obj || typeof obj.item == 'function');
218+
(length >= 0 && ((length - 1) in obj || obj instanceof Array) || typeof obj.item == 'function');
219+
219220
}
220221

221222
/**

‎test/AngularSpec.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -1218,17 +1218,38 @@ describe('angular', function() {
12181218
});
12191219

12201220
it('should return true if passed a nodelist', function() {
1221-
var nodes = document.body.childNodes;
1222-
expect(isArrayLike(nodes)).toBe(true);
1221+
var nodes1 = document.body.childNodes;
1222+
expect(isArrayLike(nodes1)).toBe(true);
1223+
1224+
var nodes2 = document.getElementsByTagName('nonExistingTagName');
1225+
expect(isArrayLike(nodes2)).toBe(true);
12231226
});
12241227

12251228
it('should return false for objects with `length` but no matching indexable items', function() {
1226-
var obj = {
1229+
var obj1 = {
12271230
a: 'a',
12281231
b:'b',
12291232
length: 10
12301233
};
1231-
expect(isArrayLike(obj)).toBe(false);
1234+
expect(isArrayLike(obj1)).toBe(false);
1235+
1236+
var obj2 = {
1237+
length: 0
1238+
};
1239+
expect(isArrayLike(obj2)).toBe(false);
1240+
});
1241+
1242+
it('should return true for empty instances of an Array subclass', function() {
1243+
function ArrayLike() {}
1244+
ArrayLike.prototype = Array.prototype;
1245+
1246+
var arrLike = new ArrayLike();
1247+
expect(arrLike.length).toBe(0);
1248+
expect(isArrayLike(arrLike)).toBe(true);
1249+
1250+
arrLike.push(1, 2, 3);
1251+
expect(arrLike.length).toBe(3);
1252+
expect(isArrayLike(arrLike)).toBe(true);
12321253
});
12331254
});
12341255

0 commit comments

Comments
 (0)
This repository has been archived.