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

fix(isArrayLike): recognize empty instances of an Array subclass #13708

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ function isArrayLike(obj) {
// NodeList objects (with `item` method) and
// other objects with suitable length characteristics are array-like
return isNumber(length) &&
(length >= 0 && (length - 1) in obj || typeof obj.item == 'function');
(length >= 0 && ((length - 1) in obj || obj instanceof Array) || typeof obj.item == 'function');

}

/**
Expand Down
29 changes: 25 additions & 4 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1218,17 +1218,38 @@ describe('angular', function() {
});

it('should return true if passed a nodelist', function() {
var nodes = document.body.childNodes;
expect(isArrayLike(nodes)).toBe(true);
var nodes1 = document.body.childNodes;
expect(isArrayLike(nodes1)).toBe(true);

var nodes2 = document.getElementsByTagName('nonExistingTagName');
expect(isArrayLike(nodes2)).toBe(true);
});

it('should return false for objects with `length` but no matching indexable items', function() {
var obj = {
var obj1 = {
a: 'a',
b:'b',
length: 10
};
expect(isArrayLike(obj)).toBe(false);
expect(isArrayLike(obj1)).toBe(false);

var obj2 = {
length: 0
};
expect(isArrayLike(obj2)).toBe(false);
});

it('should return true for empty instances of an Array subclass', function() {
function ArrayLike() {}
ArrayLike.prototype = Array.prototype;

var arrLike = new ArrayLike();
expect(arrLike.length).toBe(0);
expect(isArrayLike(arrLike)).toBe(true);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually the only test that fails without this change, but I added the other exectations to help catch future regressions.


arrLike.push(1, 2, 3);
expect(arrLike.length).toBe(3);
expect(isArrayLike(arrLike)).toBe(true);
});
});

Expand Down