1 import arr from "./var/arr.js";
2 import getProto from "./var/getProto.js";
3 import slice from "./var/slice.js";
4 import flat from "./var/flat.js";
5 import push from "./var/push.js";
6 import indexOf from "./var/indexOf.js";
7 import class2type from "./var/class2type.js";
8 import toString from "./var/toString.js";
9 import hasOwn from "./var/hasOwn.js";
10 import fnToString from "./var/fnToString.js";
11 import ObjectFunctionString from "./var/ObjectFunctionString.js";
12 import support from "./var/support.js";
13 import isArrayLike from "./core/isArrayLike.js";
14 import DOMEval from "./core/DOMEval.js";
16 var version = "@VERSION",
18 rhtmlSuffix = /HTML$/i,
20 // Define a local copy of jQuery
21 jQuery = function( selector, context ) {
23 // The jQuery object is actually just the init constructor 'enhanced'
24 // Need init if jQuery is called (just allow error to be thrown if not included)
25 return new jQuery.fn.init( selector, context );
28 jQuery.fn = jQuery.prototype = {
30 // The current version of jQuery being used
35 // The default length of a jQuery object is 0
39 return slice.call( this );
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 ) {
46 // Return all the elements in a clean array
48 return slice.call( this );
51 // Return just the one element from the set
52 return num < 0 ? this[ num + this.length ] : this[ num ];
55 // Take an array of elements and push it onto the stack
56 // (returning the new matched element set)
57 pushStack: function( elems ) {
59 // Build a new jQuery matched element set
60 var ret = jQuery.merge( this.constructor(), elems );
62 // Add the old object onto the stack (as a reference)
63 ret.prevObject = this;
65 // Return the newly-formed element set
69 // Execute a callback for every element in the matched set.
70 each: function( callback ) {
71 return jQuery.each( this, callback );
74 map: function( callback ) {
75 return this.pushStack( jQuery.map( this, function( elem, i ) {
76 return callback.call( elem, i, elem );
81 return this.pushStack( slice.apply( this, arguments ) );
93 return this.pushStack( jQuery.grep( this, function( _elem, i ) {
99 return this.pushStack( jQuery.grep( this, function( _elem, i ) {
105 var len = this.length,
106 j = +i + ( i < 0 ? len : 0 );
107 return this.pushStack( j >= 0 && j < len ? [ this[ j ] ] : [] );
111 return this.prevObject || this.constructor();
115 jQuery.extend = jQuery.fn.extend = function() {
116 var options, name, src, copy, copyIsArray, clone,
117 target = arguments[ 0 ] || {},
119 length = arguments.length,
122 // Handle a deep copy situation
123 if ( typeof target === "boolean" ) {
126 // Skip the boolean and the target
127 target = arguments[ i ] || {};
131 // Handle case when target is a string or something (possible in deep copy)
132 if ( typeof target !== "object" && typeof target !== "function" ) {
136 // Extend jQuery itself if only one argument is passed
137 if ( i === length ) {
142 for ( ; i < length; i++ ) {
144 // Only deal with non-null/undefined values
145 if ( ( options = arguments[ i ] ) != null ) {
147 // Extend the base object
148 for ( name in options ) {
149 copy = options[ name ];
151 // Prevent Object.prototype pollution
152 // Prevent never-ending loop
153 if ( name === "__proto__" || target === copy ) {
157 // Recurse if we're merging plain objects or arrays
158 if ( deep && copy && ( jQuery.isPlainObject( copy ) ||
159 ( copyIsArray = Array.isArray( copy ) ) ) ) {
160 src = target[ name ];
162 // Ensure proper type for the source value
163 if ( copyIsArray && !Array.isArray( src ) ) {
165 } else if ( !copyIsArray && !jQuery.isPlainObject( src ) ) {
172 // Never move original objects, clone them
173 target[ name ] = jQuery.extend( deep, clone, copy );
175 // Don't bring in undefined values
176 } else if ( copy !== undefined ) {
177 target[ name ] = copy;
183 // Return the modified object
189 // Unique for each copy of jQuery on the page
190 expando: "jQuery" + ( version + Math.random() ).replace( /\D/g, "" ),
192 // Assume jQuery is ready without the ready module
195 error: function( msg ) {
196 throw new Error( msg );
201 isPlainObject: function( obj ) {
204 // Detect obvious negatives
205 // Use toString instead of jQuery.type to catch host objects
206 if ( !obj || toString.call( obj ) !== "[object Object]" ) {
210 proto = getProto( obj );
212 // Objects with no prototype (e.g., `Object.create( null )`) are plain
217 // Objects with prototype are plain iff they were constructed by a global Object function
218 Ctor = hasOwn.call( proto, "constructor" ) && proto.constructor;
219 return typeof Ctor === "function" && fnToString.call( Ctor ) === ObjectFunctionString;
222 isEmptyObject: function( obj ) {
225 for ( name in obj ) {
231 // Evaluates a script in a provided context; falls back to the global one
233 globalEval: function( code, options, doc ) {
234 DOMEval( code, { nonce: options && options.nonce }, doc );
237 each: function( obj, callback ) {
240 if ( isArrayLike( obj ) ) {
242 for ( ; i < length; i++ ) {
243 if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
249 if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
259 // Retrieve the text value of an array of DOM nodes
260 text: function( elem ) {
264 nodeType = elem.nodeType;
268 // If no nodeType, this is expected to be an array
269 while ( ( node = elem[ i++ ] ) ) {
271 // Do not traverse comment nodes
272 ret += jQuery.text( node );
274 } else if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) {
275 return elem.textContent;
276 } else if ( nodeType === 3 || nodeType === 4 ) {
277 return elem.nodeValue;
280 // Do not include comment or processing instruction nodes
286 // results is for internal usage only
287 makeArray: function( arr, results ) {
288 var ret = results || [];
291 if ( isArrayLike( Object( arr ) ) ) {
293 typeof arr === "string" ?
297 push.call( ret, arr );
304 inArray: function( elem, arr, i ) {
305 return arr == null ? -1 : indexOf.call( arr, elem, i );
308 isXMLDoc: function( elem ) {
309 var namespace = elem && elem.namespaceURI,
310 docElem = elem && ( elem.ownerDocument || elem ).documentElement;
312 // Assume HTML when documentElement doesn't yet exist, such as inside
313 // document fragments.
314 return !rhtmlSuffix.test( namespace || docElem && docElem.nodeName || "HTML" );
317 merge: function( first, second ) {
318 var len = +second.length,
322 for ( ; j < len; j++ ) {
323 first[ i++ ] = second[ j ];
331 grep: function( elems, callback, invert ) {
335 length = elems.length,
336 callbackExpect = !invert;
338 // Go through the array, only saving the items
339 // that pass the validator function
340 for ( ; i < length; i++ ) {
341 callbackInverse = !callback( elems[ i ], i );
342 if ( callbackInverse !== callbackExpect ) {
343 matches.push( elems[ i ] );
350 // arg is for internal usage only
351 map: function( elems, callback, arg ) {
356 // Go through the array, translating each of the items to their new values
357 if ( isArrayLike( elems ) ) {
358 length = elems.length;
359 for ( ; i < length; i++ ) {
360 value = callback( elems[ i ], i, arg );
362 if ( value != null ) {
367 // Go through every key on the object,
370 value = callback( elems[ i ], i, arg );
372 if ( value != null ) {
378 // Flatten any nested arrays
382 // A global GUID counter for objects
385 // jQuery.support is not used in Core but other projects attach their
386 // properties to it so it needs to exist.
390 if ( typeof Symbol === "function" ) {
391 jQuery.fn[ Symbol.iterator ] = arr[ Symbol.iterator ];
394 // Populate the class2type map
395 jQuery.each( "Boolean Number String Function Array Date RegExp Object Error Symbol".split( " " ),
396 function( _i, name ) {
397 class2type[ "[object " + name + "]" ] = name.toLowerCase();
400 export default jQuery;