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

Commit 773efd0

Browse files
lgalfasopetebacondarwin
authored andcommitted
fix(isArrayLike): handle jQuery objects of length 0
Closes: #13169 Closes: #13171
1 parent 8088194 commit 773efd0

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/Angular.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,6 @@ var
191191
msie = document.documentMode;
192192

193193

194-
function isNodeList(obj) {
195-
return typeof obj.length == 'number' &&
196-
typeof obj.item == 'function';
197-
}
198-
199194
/**
200195
* @private
201196
* @param {*} obj
@@ -207,15 +202,20 @@ function isArrayLike(obj) {
207202
// `null`, `undefined` and `window` are not array-like
208203
if (obj == null || isWindow(obj)) return false;
209204

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

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

217-
// node lists and objects with suitable length characteristics are array-like
218-
return (isNumber(length) && length >= 0 && (length - 1) in obj) || isNodeList(obj);
215+
// NodeList objects (with `item` method) and
216+
// other objects with suitable length characteristics are array-like
217+
return isNumber(length) &&
218+
(length >= 0 && (length - 1) in obj || typeof obj.item == 'function');
219219
}
220220

221221
/**

test/AngularSpec.js

+5
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,11 @@ describe('angular', function() {
11421142

11431143
forEach(jqObject, function(value, key) { log.push(key + ':' + value.innerHTML); });
11441144
expect(log).toEqual(['0:s1', '1:s2']);
1145+
1146+
log = [];
1147+
jqObject = jqLite("<pane></pane>");
1148+
forEach(jqObject.children(), function(value, key) { log.push(key + ':' + value.innerHTML); });
1149+
expect(log).toEqual([]);
11451150
});
11461151

11471152

0 commit comments

Comments
 (0)