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

Commit 323f9ab

Browse files
gkalpakpetebacondarwin
authored andcommitted
fix(isArrayLike): recognize empty instances of an Array subclass
Fixes #13560 Closes #13708
1 parent 5ba4419 commit 323f9ab

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
@@ -1119,17 +1119,38 @@ describe('angular', function() {
11191119
});
11201120

11211121
it('should return true if passed a nodelist', function() {
1122-
var nodes = document.body.childNodes;
1123-
expect(isArrayLike(nodes)).toBe(true);
1122+
var nodes1 = document.body.childNodes;
1123+
expect(isArrayLike(nodes1)).toBe(true);
1124+
1125+
var nodes2 = document.getElementsByTagName('nonExistingTagName');
1126+
expect(isArrayLike(nodes2)).toBe(true);
11241127
});
11251128

11261129
it('should return false for objects with `length` but no matching indexable items', function() {
1127-
var obj = {
1130+
var obj1 = {
11281131
a: 'a',
11291132
b:'b',
11301133
length: 10
11311134
};
1132-
expect(isArrayLike(obj)).toBe(false);
1135+
expect(isArrayLike(obj1)).toBe(false);
1136+
1137+
var obj2 = {
1138+
length: 0
1139+
};
1140+
expect(isArrayLike(obj2)).toBe(false);
1141+
});
1142+
1143+
it('should return true for empty instances of an Array subclass', function() {
1144+
function ArrayLike() {}
1145+
ArrayLike.prototype = Array.prototype;
1146+
1147+
var arrLike = new ArrayLike();
1148+
expect(arrLike.length).toBe(0);
1149+
expect(isArrayLike(arrLike)).toBe(true);
1150+
1151+
arrLike.push(1, 2, 3);
1152+
expect(arrLike.length).toBe(3);
1153+
expect(isArrayLike(arrLike)).toBe(true);
11331154
});
11341155
});
11351156

0 commit comments

Comments
 (0)