mirror of
https://github.com/jquery/jquery.git
synced 2024-11-23 02:54:22 +00:00
Core: .each/.map should accept an undefined/null value
(cherry-picked from bf48c21d22
)
Fixes gh-2267
Closes gh-2363
This commit is contained in:
parent
9c373c3141
commit
15f48047bc
20
src/core.js
20
src/core.js
@ -294,11 +294,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;
|
||||
@ -327,7 +326,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
|
||||
@ -405,14 +404,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 );
|
||||
|
||||
@ -493,13 +491,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 ) ) {
|
||||
|
@ -1182,6 +1182,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 );
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user