core.js (10181B)
1 /* global Symbol */ 2 // Defining this global in .eslintrc.json would create a danger of using the global 3 // unguarded in another place, it seems safer to define global only for this module 4 5 define( [ 6 "./var/arr", 7 "./var/getProto", 8 "./var/slice", 9 "./var/flat", 10 "./var/push", 11 "./var/indexOf", 12 "./var/class2type", 13 "./var/toString", 14 "./var/hasOwn", 15 "./var/fnToString", 16 "./var/ObjectFunctionString", 17 "./var/support", 18 "./var/isFunction", 19 "./var/isWindow", 20 "./core/DOMEval", 21 "./core/toType" 22 ], function( arr, getProto, slice, flat, push, indexOf, 23 class2type, toString, hasOwn, fnToString, ObjectFunctionString, 24 support, isFunction, isWindow, DOMEval, toType ) { 25 26 "use strict"; 27 28 var version = "3.7.1", 29 30 rhtmlSuffix = /HTML$/i, 31 32 // Define a local copy of jQuery 33 jQuery = function( selector, context ) { 34 35 // The jQuery object is actually just the init constructor 'enhanced' 36 // Need init if jQuery is called (just allow error to be thrown if not included) 37 return new jQuery.fn.init( selector, context ); 38 }; 39 40 jQuery.fn = jQuery.prototype = { 41 42 // The current version of jQuery being used 43 jquery: version, 44 45 constructor: jQuery, 46 47 // The default length of a jQuery object is 0 48 length: 0, 49 50 toArray: function() { 51 return slice.call( this ); 52 }, 53 54 // Get the Nth element in the matched element set OR 55 // Get the whole matched element set as a clean array 56 get: function( num ) { 57 58 // Return all the elements in a clean array 59 if ( num == null ) { 60 return slice.call( this ); 61 } 62 63 // Return just the one element from the set 64 return num < 0 ? this[ num + this.length ] : this[ num ]; 65 }, 66 67 // Take an array of elements and push it onto the stack 68 // (returning the new matched element set) 69 pushStack: function( elems ) { 70 71 // Build a new jQuery matched element set 72 var ret = jQuery.merge( this.constructor(), elems ); 73 74 // Add the old object onto the stack (as a reference) 75 ret.prevObject = this; 76 77 // Return the newly-formed element set 78 return ret; 79 }, 80 81 // Execute a callback for every element in the matched set. 82 each: function( callback ) { 83 return jQuery.each( this, callback ); 84 }, 85 86 map: function( callback ) { 87 return this.pushStack( jQuery.map( this, function( elem, i ) { 88 return callback.call( elem, i, elem ); 89 } ) ); 90 }, 91 92 slice: function() { 93 return this.pushStack( slice.apply( this, arguments ) ); 94 }, 95 96 first: function() { 97 return this.eq( 0 ); 98 }, 99 100 last: function() { 101 return this.eq( -1 ); 102 }, 103 104 even: function() { 105 return this.pushStack( jQuery.grep( this, function( _elem, i ) { 106 return ( i + 1 ) % 2; 107 } ) ); 108 }, 109 110 odd: function() { 111 return this.pushStack( jQuery.grep( this, function( _elem, i ) { 112 return i % 2; 113 } ) ); 114 }, 115 116 eq: function( i ) { 117 var len = this.length, 118 j = +i + ( i < 0 ? len : 0 ); 119 return this.pushStack( j >= 0 && j < len ? [ this[ j ] ] : [] ); 120 }, 121 122 end: function() { 123 return this.prevObject || this.constructor(); 124 }, 125 126 // For internal use only. 127 // Behaves like an Array's method, not like a jQuery method. 128 push: push, 129 sort: arr.sort, 130 splice: arr.splice 131 }; 132 133 jQuery.extend = jQuery.fn.extend = function() { 134 var options, name, src, copy, copyIsArray, clone, 135 target = arguments[ 0 ] || {}, 136 i = 1, 137 length = arguments.length, 138 deep = false; 139 140 // Handle a deep copy situation 141 if ( typeof target === "boolean" ) { 142 deep = target; 143 144 // Skip the boolean and the target 145 target = arguments[ i ] || {}; 146 i++; 147 } 148 149 // Handle case when target is a string or something (possible in deep copy) 150 if ( typeof target !== "object" && !isFunction( target ) ) { 151 target = {}; 152 } 153 154 // Extend jQuery itself if only one argument is passed 155 if ( i === length ) { 156 target = this; 157 i--; 158 } 159 160 for ( ; i < length; i++ ) { 161 162 // Only deal with non-null/undefined values 163 if ( ( options = arguments[ i ] ) != null ) { 164 165 // Extend the base object 166 for ( name in options ) { 167 copy = options[ name ]; 168 169 // Prevent Object.prototype pollution 170 // Prevent never-ending loop 171 if ( name === "__proto__" || target === copy ) { 172 continue; 173 } 174 175 // Recurse if we're merging plain objects or arrays 176 if ( deep && copy && ( jQuery.isPlainObject( copy ) || 177 ( copyIsArray = Array.isArray( copy ) ) ) ) { 178 src = target[ name ]; 179 180 // Ensure proper type for the source value 181 if ( copyIsArray && !Array.isArray( src ) ) { 182 clone = []; 183 } else if ( !copyIsArray && !jQuery.isPlainObject( src ) ) { 184 clone = {}; 185 } else { 186 clone = src; 187 } 188 copyIsArray = false; 189 190 // Never move original objects, clone them 191 target[ name ] = jQuery.extend( deep, clone, copy ); 192 193 // Don't bring in undefined values 194 } else if ( copy !== undefined ) { 195 target[ name ] = copy; 196 } 197 } 198 } 199 } 200 201 // Return the modified object 202 return target; 203 }; 204 205 jQuery.extend( { 206 207 // Unique for each copy of jQuery on the page 208 expando: "jQuery" + ( version + Math.random() ).replace( /\D/g, "" ), 209 210 // Assume jQuery is ready without the ready module 211 isReady: true, 212 213 error: function( msg ) { 214 throw new Error( msg ); 215 }, 216 217 noop: function() {}, 218 219 isPlainObject: function( obj ) { 220 var proto, Ctor; 221 222 // Detect obvious negatives 223 // Use toString instead of jQuery.type to catch host objects 224 if ( !obj || toString.call( obj ) !== "[object Object]" ) { 225 return false; 226 } 227 228 proto = getProto( obj ); 229 230 // Objects with no prototype (e.g., `Object.create( null )`) are plain 231 if ( !proto ) { 232 return true; 233 } 234 235 // Objects with prototype are plain iff they were constructed by a global Object function 236 Ctor = hasOwn.call( proto, "constructor" ) && proto.constructor; 237 return typeof Ctor === "function" && fnToString.call( Ctor ) === ObjectFunctionString; 238 }, 239 240 isEmptyObject: function( obj ) { 241 var name; 242 243 for ( name in obj ) { 244 return false; 245 } 246 return true; 247 }, 248 249 // Evaluates a script in a provided context; falls back to the global one 250 // if not specified. 251 globalEval: function( code, options, doc ) { 252 DOMEval( code, { nonce: options && options.nonce }, doc ); 253 }, 254 255 each: function( obj, callback ) { 256 var length, i = 0; 257 258 if ( isArrayLike( obj ) ) { 259 length = obj.length; 260 for ( ; i < length; i++ ) { 261 if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) { 262 break; 263 } 264 } 265 } else { 266 for ( i in obj ) { 267 if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) { 268 break; 269 } 270 } 271 } 272 273 return obj; 274 }, 275 276 277 // Retrieve the text value of an array of DOM nodes 278 text: function( elem ) { 279 var node, 280 ret = "", 281 i = 0, 282 nodeType = elem.nodeType; 283 284 if ( !nodeType ) { 285 286 // If no nodeType, this is expected to be an array 287 while ( ( node = elem[ i++ ] ) ) { 288 289 // Do not traverse comment nodes 290 ret += jQuery.text( node ); 291 } 292 } 293 if ( nodeType === 1 || nodeType === 11 ) { 294 return elem.textContent; 295 } 296 if ( nodeType === 9 ) { 297 return elem.documentElement.textContent; 298 } 299 if ( nodeType === 3 || nodeType === 4 ) { 300 return elem.nodeValue; 301 } 302 303 // Do not include comment or processing instruction nodes 304 305 return ret; 306 }, 307 308 // results is for internal usage only 309 makeArray: function( arr, results ) { 310 var ret = results || []; 311 312 if ( arr != null ) { 313 if ( isArrayLike( Object( arr ) ) ) { 314 jQuery.merge( ret, 315 typeof arr === "string" ? 316 [ arr ] : arr 317 ); 318 } else { 319 push.call( ret, arr ); 320 } 321 } 322 323 return ret; 324 }, 325 326 inArray: function( elem, arr, i ) { 327 return arr == null ? -1 : indexOf.call( arr, elem, i ); 328 }, 329 330 isXMLDoc: function( elem ) { 331 var namespace = elem && elem.namespaceURI, 332 docElem = elem && ( elem.ownerDocument || elem ).documentElement; 333 334 // Assume HTML when documentElement doesn't yet exist, such as inside 335 // document fragments. 336 return !rhtmlSuffix.test( namespace || docElem && docElem.nodeName || "HTML" ); 337 }, 338 339 // Support: Android <=4.0 only, PhantomJS 1 only 340 // push.apply(_, arraylike) throws on ancient WebKit 341 merge: function( first, second ) { 342 var len = +second.length, 343 j = 0, 344 i = first.length; 345 346 for ( ; j < len; j++ ) { 347 first[ i++ ] = second[ j ]; 348 } 349 350 first.length = i; 351 352 return first; 353 }, 354 355 grep: function( elems, callback, invert ) { 356 var callbackInverse, 357 matches = [], 358 i = 0, 359 length = elems.length, 360 callbackExpect = !invert; 361 362 // Go through the array, only saving the items 363 // that pass the validator function 364 for ( ; i < length; i++ ) { 365 callbackInverse = !callback( elems[ i ], i ); 366 if ( callbackInverse !== callbackExpect ) { 367 matches.push( elems[ i ] ); 368 } 369 } 370 371 return matches; 372 }, 373 374 // arg is for internal usage only 375 map: function( elems, callback, arg ) { 376 var length, value, 377 i = 0, 378 ret = []; 379 380 // Go through the array, translating each of the items to their new values 381 if ( isArrayLike( elems ) ) { 382 length = elems.length; 383 for ( ; i < length; i++ ) { 384 value = callback( elems[ i ], i, arg ); 385 386 if ( value != null ) { 387 ret.push( value ); 388 } 389 } 390 391 // Go through every key on the object, 392 } else { 393 for ( i in elems ) { 394 value = callback( elems[ i ], i, arg ); 395 396 if ( value != null ) { 397 ret.push( value ); 398 } 399 } 400 } 401 402 // Flatten any nested arrays 403 return flat( ret ); 404 }, 405 406 // A global GUID counter for objects 407 guid: 1, 408 409 // jQuery.support is not used in Core but other projects attach their 410 // properties to it so it needs to exist. 411 support: support 412 } ); 413 414 if ( typeof Symbol === "function" ) { 415 jQuery.fn[ Symbol.iterator ] = arr[ Symbol.iterator ]; 416 } 417 418 // Populate the class2type map 419 jQuery.each( "Boolean Number String Function Array Date RegExp Object Error Symbol".split( " " ), 420 function( _i, name ) { 421 class2type[ "[object " + name + "]" ] = name.toLowerCase(); 422 } ); 423 424 function isArrayLike( obj ) { 425 426 // Support: real iOS 8.2 only (not reproducible in simulator) 427 // `in` check used to prevent JIT error (gh-2145) 428 // hasOwn isn't used here due to false negatives 429 // regarding Nodelist length in IE 430 var length = !!obj && "length" in obj && obj.length, 431 type = toType( obj ); 432 433 if ( isFunction( obj ) || isWindow( obj ) ) { 434 return false; 435 } 436 437 return type === "array" || length === 0 || 438 typeof length === "number" && length > 0 && ( length - 1 ) in obj; 439 } 440 441 return jQuery; 442 } );