]> source.dussan.org Git - jquery.git/commitdiff
Core: .each/.map should accept an undefined/null value
authorThomas Tortorini <thomastortorini@gmail.com>
Thu, 25 Jun 2015 04:18:04 +0000 (06:18 +0200)
committerMichał Gołębiowski <m.goleb@gmail.com>
Mon, 27 Jul 2015 17:57:34 +0000 (19:57 +0200)
Fixes gh-2267
Closes gh-2363

src/core.js
test/unit/core.js

index ba6eeceb8f7a73381459d89ab1b1ec19b7a31891..e8024aaf109467b7d25d42c2e3e99a2b41c1be17 100644 (file)
@@ -268,11 +268,10 @@ jQuery.extend({
        },
 
        each: function( obj, callback ) {
-               var i = 0,
-                       length = obj.length,
-                       isArray = isArraylike( obj );
+               var length, i = 0;
 
-               if ( isArray ) {
+               if ( isArrayLike( obj ) ) {
+                       length = obj.length;
                        for ( ; i < length; i++ ) {
                                if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
                                        break;
@@ -301,7 +300,7 @@ jQuery.extend({
                var ret = results || [];
 
                if ( arr != null ) {
-                       if ( isArraylike( Object(arr) ) ) {
+                       if ( isArrayLike( Object( arr ) ) ) {
                                jQuery.merge( ret,
                                        typeof arr === "string" ?
                                        [ arr ] : arr
@@ -355,14 +354,13 @@ jQuery.extend({
 
        // arg is for internal usage only
        map: function( elems, callback, arg ) {
-               var value,
+               var length, value,
                        i = 0,
-                       length = elems.length,
-                       isArray = isArraylike( elems ),
                        ret = [];
 
                // Go through the array, translating each of the items to their new values
-               if ( isArray ) {
+               if ( isArrayLike( elems ) ) {
+                       length = elems.length;
                        for ( ; i < length; i++ ) {
                                value = callback( elems[ i ], i, arg );
 
@@ -441,13 +439,13 @@ function(i, name) {
        class2type[ "[object " + name + "]" ] = name.toLowerCase();
 });
 
-function isArraylike( obj ) {
+function isArrayLike( obj ) {
 
        // Support: iOS 8.2 (not reproducible in simulator)
        // `in` check used to prevent JIT error (gh-2145)
        // hasOwn isn't used here due to false negatives
        // regarding Nodelist length in IE
-       var length = "length" in obj && obj.length,
+       var length = !!obj && "length" in obj && obj.length,
                type = jQuery.type( obj );
 
        if ( type === "function" || jQuery.isWindow( obj ) ) {
index 8cfe98f838cfb9b478c5e8564f018833d42f3cb9..1ddf695a652a434cc3953926b949204cb43abe4e 100644 (file)
@@ -1198,6 +1198,18 @@ test("jQuery.each(Object,Function)", function() {
        equal( i, document.styleSheets.length, "Iteration over document.styleSheets" );
 });
 
+test("jQuery.each/map(undefined/null,Function)", 1, function() {
+       try {
+               jQuery.each( undefined, jQuery.noop );
+               jQuery.each( null, jQuery.noop );
+               jQuery.map( undefined, jQuery.noop );
+               jQuery.map( null, jQuery.noop );
+               ok( true, "jQuery.each/map( undefined/null, function() {} );" );
+       } catch ( e ) {
+               ok( false, "each/map must accept null and undefined values" );
+       }
+});
+
 test( "JIT compilation does not interfere with length retrieval (gh-2145)", function() {
        expect( 4 );