core.js 9.9 KB

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