8000 fix(isArrayLike): recognize empty instances of an Array subclass · angular/angular.js@87d2c44 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 87d2c44

Browse files
committed
fix(isArrayLike): recognize empty instances of an Array subclass
Fixes #13560
1 parent f7eab8d commit 87d2c44

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/Angular.js

Lines changed: 2 additions & 1 deletion
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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,17 +1218,34 @@ 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+
1248+
expect(isArrayLike(arrLike)).toBe(true);
12321249
});
12331250
});
12341251

< 38AB /code>

0 commit comments

Comments
 (0)
0