Skip to content

Commit 15f4804

Browse files
mr21mgol
authored andcommitted
Core: .each/.map should accept an undefined/null value
(cherry-picked from bf48c21) Fixes gh-2267 Closes gh-2363
1 parent 9c373c3 commit 15f4804

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/core.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,10 @@ jQuery.extend({
294294
},
295295

296296
each: function( obj, callback ) {
297-
var i = 0,
298-
length = obj.length,
299-
isArray = isArraylike( obj );
297+
var length, i = 0;
300298

301-
if ( isArray ) {
299+
if ( isArrayLike( obj ) ) {
300+
length = obj.length;
302301
for ( ; i < length; i++ ) {
303302
if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
304303
break;
@@ -327,7 +326,7 @@ jQuery.extend({
327326
var ret = results || [];
328327

329328
if ( arr != null ) {
330-
if ( isArraylike( Object(arr) ) ) {
329+
if ( isArrayLike( Object( arr ) ) ) {
331330
jQuery.merge( ret,
332331
typeof arr === "string" ?
333332
[ arr ] : arr
@@ -405,14 +404,13 @@ jQuery.extend({
405404

406405
// arg is for internal usage only
407406
map: function( elems, callback, arg ) {
408-
var value,
407+
var length, value,
409408
i = 0,
410-
length = elems.length,
411-
isArray = isArraylike( elems ),
412409
ret = [];
413410

414411
// Go through the array, translating each of the items to their new values
415-
if ( isArray ) {
412+
if ( isArrayLike( elems ) ) {
413+
length = elems.length;
416414
for ( ; i < length; i++ ) {
417415
value = callback( elems[ i ], i, arg );
418416

@@ -493,13 +491,13 @@ function(i, name) {
493491
class2type[ "[object " + name + "]" ] = name.toLowerCase();
494492
});
495493

496-
function isArraylike( obj ) {
494+
function isArrayLike( obj ) {
497495

498496
// Support: iOS 8.2 (not reproducible in simulator)
499497
// `in` check used to prevent JIT error (gh-2145)
500498
// hasOwn isn't used here due to false negatives
501499
// regarding Nodelist length in IE
502-
var length = "length" in obj && obj.length,
500+
var length = !!obj && "length" in obj && obj.length,
503501
type = jQuery.type( obj );
504502

505503
if ( type === "function" || jQuery.isWindow( obj ) ) {

test/unit/core.js

+12
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,18 @@ test("jQuery.each(Object,Function)", function() {
11821182
equal( i, document.styleSheets.length, "Iteration over document.styleSheets" );
11831183
});
11841184

1185+
test("jQuery.each/map(undefined/null,Function)", 1, function() {
1186+
try {
1187+
jQuery.each( undefined, jQuery.noop );
1188+
jQuery.each( null, jQuery.noop );
1189+
jQuery.map( undefined, jQuery.noop );
1190+
jQuery.map( null, jQuery.noop );
1191+
ok( true, "jQuery.each/map( undefined/null, function() {} );" );
1192+
} catch ( e ) {
1193+
ok( false, "each/map must accept null and undefined values" );
1194+
}
1195+
});
1196+
11851197
test( "JIT compilation does not interfere with length retrieval (gh-2145)", function() {
11861198
expect( 4 );
11871199

0 commit comments

Comments
 (0)