Skip to content

Commit bf48c21

Browse files
mr21mgol
authored andcommitted
Core: .each/.map should accept an undefined/null value
Fixes gh-2267 Closes gh-2363
1 parent d242753 commit bf48c21

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/core.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,10 @@ jQuery.extend({
268268
},
269269

270270
each: function( obj, callback ) {
271-
var i = 0,
272-
length = obj.length,
273-
isArray = isArraylike( obj );
271+
var length, i = 0;
274272

275-
if ( isArray ) {
273+
if ( isArrayLike( obj ) ) {
274+
length = obj.length;
276275
for ( ; i < length; i++ ) {
277276
if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
278277
break;
@@ -301,7 +300,7 @@ jQuery.extend({
301300
var ret = results || [];
302301

303302
if ( arr != null ) {
304-
if ( isArraylike( Object(arr) ) ) {
303+
if ( isArrayLike( Object( arr ) ) ) {
305304
jQuery.merge( ret,
306305
typeof arr === "string" ?
307306
[ arr ] : arr
@@ -355,14 +354,13 @@ jQuery.extend({
355354

356355
// arg is for internal usage only
357356
map: function( elems, callback, arg ) {
358-
var value,
357+
var length, value,
359358
i = 0,
360-
length = elems.length,
361-
isArray = isArraylike( elems ),
362359
ret = [];
363360

364361
// Go through the array, translating each of the items to their new values
365-
if ( isArray ) {
362+
if ( isArrayLike( elems ) ) {
363+
length = elems.length;
366364
for ( ; i < length; i++ ) {
367365
value = callback( elems[ i ], i, arg );
368366

@@ -441,13 +439,13 @@ function(i, name) {
441439
class2type[ "[object " + name + "]" ] = name.toLowerCase();
442440
});
443441

444-
function isArraylike( obj ) {
442+
function isArrayLike( obj ) {
445443

446444
// Support: iOS 8.2 (not reproducible in simulator)
447445
// `in` check used to prevent JIT error (gh-2145)
448446
// hasOwn isn't used here due to false negatives
449447
// regarding Nodelist length in IE
450-
var length = "length" in obj && obj.length,
448+
var length = !!obj && "length" in obj && obj.length,
451449
type = jQuery.type( obj );
452450

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

test/unit/core.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,18 @@ test("jQuery.each(Object,Function)", function() {
11981198
equal( i, document.styleSheets.length, "Iteration over document.styleSheets" );
11991199
});
12001200

1201+
test("jQuery.each/map(undefined/null,Function)", 1, function() {
1202+
try {
1203+
jQuery.each( undefined, jQuery.noop );
1204+
jQuery.each( null, jQuery.noop );
1205+
jQuery.map( undefined, jQuery.noop );
1206+
jQuery.map( null, jQuery.noop );
1207+
ok( true, "jQuery.each/map( undefined/null, function() {} );" );
1208+
} catch ( e ) {
1209+
ok( false, "each/map must accept null and undefined values" );
1210+
}
1211+
});
1212+
12011213
test( "JIT compilation does not interfere with length retrieval (gh-2145)", function() {
12021214
expect( 4 );
12031215

0 commit comments

Comments
 (0)