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

Commit

Permalink
fix(isArrayLike): handle jQuery objects of length 0
Browse files Browse the repository at this point in the history
Closes: #13169
Closes: #13171
  • Loading branch information
lgalfaso authored and petebacondarwin committed Oct 26, 2015
1 parent 70edec9 commit d3da55c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,6 @@ var
msie = document.documentMode;


function isNodeList(obj) {
return typeof obj.length == 'number' &&
typeof obj.item == 'function';
}

/**
* @private
* @param {*} obj
Expand All @@ -207,15 +202,20 @@ function isArrayLike(obj) {
// `null`, `undefined` and `window` are not array-like
if (obj == null || isWindow(obj)) return false;

// arrays and strings are array like
if (isArray(obj) || isString(obj)) return true;
// arrays, strings and jQuery/jqLite objects are array like
// * jqLite is either the jQuery or jqLite constructor function
// * we have to check the existance of jqLite first as this method is called
// via the forEach method when constructing the jqLite object in the first place
if (isArray(obj) || isString(obj) || (jqLite && obj instanceof jqLite)) return true;

// Support: iOS 8.2 (not reproducible in simulator)
// "length" in obj used to prevent JIT error (gh-11508)
var length = "length" in Object(obj) && obj.length;

// node lists and objects with suitable length characteristics are array-like
return (isNumber(length) && length >= 0 && (length - 1) in obj) || isNodeList(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');
}

/**
Expand Down
5 changes: 5 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,11 @@ describe('angular', function() {

forEach(jqObject, function(value, key) { log.push(key + ':' + value.innerHTML); });
expect(log).toEqual(['0:s1', '1:s2']);

log = [];
jqObject = jqLite("<pane></pane>");
forEach(jqObject.children(), function(value, key) { log.push(key + ':' + value.innerHTML); });
expect(log).toEqual([]);
});


Expand Down

0 comments on commit d3da55c

Please sign in to comment.