Did some light reordering of the jQuery definition. Moved window and undefined to...
[jquery.git] / src / core.js
1 // Define a local copy of jQuery
2 var jQuery = function( selector, context ) {
3                 // The jQuery object is actually just the init constructor 'enhanced'
4                 return arguments.length === 0 ?
5                         rootjQuery :
6                         new jQuery.fn.init( selector, context );
7         },
8
9         // Map over jQuery in case of overwrite
10         _jQuery = window.jQuery,
11
12         // Map over the $ in case of overwrite
13         _$ = window.$,
14
15         // A central reference to the root jQuery(document)
16         rootjQuery,
17
18         // A simple way to check for HTML strings or ID strings
19         // (both of which we optimize for)
20         quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-]+)$/,
21
22         // Is it a simple selector
23         isSimple = /^.[^:#\[\.,]*$/,
24
25         // Keep a UserAgent string for use with jQuery.browser
26         userAgent = navigator.userAgent.toLowerCase(),
27
28         // Save a reference to the core toString method
29         toString = Object.prototype.toString;
30
31 // Expose jQuery to the global object
32 window.jQuery = window.$ = jQuery;
33
34 jQuery.fn = jQuery.prototype = {
35         init: function( selector, context ) {
36                 var match, elem, ret;
37
38                 // Handle $(""), $(null), or $(undefined)
39                 if ( !selector ) {
40                         this.length = 0;
41                         return this;
42                 }
43
44                 // Handle $(DOMElement)
45                 if ( selector.nodeType ) {
46                         this[0] = selector;
47                         this.length = 1;
48                         this.context = selector;
49                         return this;
50                 }
51
52                 // Handle HTML strings
53                 if ( typeof selector === "string" ) {
54                         // Are we dealing with HTML string or an ID?
55                         match = quickExpr.exec( selector );
56
57                         // Verify a match, and that no context was specified for #id
58                         if ( match && (match[1] || !context) ) {
59
60                                 // HANDLE: $(html) -> $(array)
61                                 if ( match[1] ) {
62                                         selector = jQuery.clean( [ match[1] ], context );
63
64                                 // HANDLE: $("#id")
65                                 } else {
66                                         elem = document.getElementById( match[3] );
67
68                                         // Handle the case where IE and Opera return items
69                                         // by name instead of ID
70                                         if ( elem && elem.id !== match[3] ) {
71                                                 return rootjQuery.find( selector );
72                                         }
73
74                                         // Otherwise, we inject the element directly into the jQuery object
75                                         ret = jQuery( elem || null );
76                                         ret.context = document;
77                                         ret.selector = selector;
78                                         return ret;
79                                 }
80
81                         // HANDLE: $(expr, $(...))
82                         } else if ( !context || context.jquery ) {
83                                 return (context || rootjQuery).find( selector );
84
85                         // HANDLE: $(expr, context)
86                         // (which is just equivalent to: $(context).find(expr)
87                         } else {
88                                 return jQuery( context ).find( selector );
89                         }
90
91                 // HANDLE: $(function)
92                 // Shortcut for document ready
93                 } else if ( jQuery.isFunction( selector ) ) {
94                         return rootjQuery.ready( selector );
95                 }
96
97                 // Make sure that old selector state is passed along
98                 if ( selector.selector && selector.context ) {
99                         this.selector = selector.selector;
100                         this.context = selector.context;
101                 }
102
103                 return this.setArray(jQuery.isArray( selector ) ?
104                         selector :
105                         jQuery.makeArray(selector));
106         },
107
108         // Start with an empty selector
109         selector: "",
110
111         // The current version of jQuery being used
112         jquery: "@VERSION",
113
114         // The number of elements contained in the matched element set
115         size: function() {
116                 return this.length;
117         },
118
119         // Get the Nth element in the matched element set OR
120         // Get the whole matched element set as a clean array
121         get: function( num ) {
122                 return num == null ?
123
124                         // Return a 'clean' array
125                         Array.prototype.slice.call( this ) :
126
127                         // Return just the object
128                         this[ num ];
129         },
130
131         // Take an array of elements and push it onto the stack
132         // (returning the new matched element set)
133         pushStack: function( elems, name, selector ) {
134                 // Build a new jQuery matched element set
135                 var ret = jQuery( elems || null );
136
137                 // Add the old object onto the stack (as a reference)
138                 ret.prevObject = this;
139
140                 ret.context = this.context;
141
142                 if ( name === "find" ) {
143                         ret.selector = this.selector + (this.selector ? " " : "") + selector;
144                 } else if ( name ) {
145                         ret.selector = this.selector + "." + name + "(" + selector + ")";
146                 }
147
148                 // Return the newly-formed element set
149                 return ret;
150         },
151
152         // Force the current matched set of elements to become
153         // the specified array of elements (destroying the stack in the process)
154         // You should use pushStack() in order to do this, but maintain the stack
155         setArray: function( elems ) {
156                 // Resetting the length to 0, then using the native Array push
157                 // is a super-fast way to populate an object with array-like properties
158                 this.length = 0;
159                 Array.prototype.push.apply( this, elems );
160
161                 return this;
162         },
163
164         // Execute a callback for every element in the matched set.
165         // (You can seed the arguments with an array of args, but this is
166         // only used internally.)
167         each: function( callback, args ) {
168                 return jQuery.each( this, callback, args );
169         },
170
171         // Determine the position of an element within
172         // the matched set of elements
173         index: function( elem ) {
174                 if ( !elem || typeof elem === "string" ) {
175                         return jQuery.inArray( this[0],
176                                 // If it receives a string, the selector is used
177                                 // If it receives nothing, the siblings are used
178                                 elem ? jQuery( elem ) : this.parent().children() );
179                 }
180                 // Locate the position of the desired element
181                 return jQuery.inArray(
182                         // If it receives a jQuery object, the first element is used
183                         elem.jquery ? elem[0] : elem, this );
184         },
185
186         is: function( selector ) {
187                 return !!selector && jQuery.multiFilter( selector, this ).length > 0;
188         },
189
190         // For internal use only.
191         // Behaves like an Array's method, not like a jQuery method.
192         push: [].push,
193         sort: [].sort,
194         splice: [].splice
195 };
196
197 // Give the init function the jQuery prototype for later instantiation
198 jQuery.fn.init.prototype = jQuery.fn;
199
200 jQuery.extend = jQuery.fn.extend = function() {
201         // copy reference to target object
202         var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
203
204         // Handle a deep copy situation
205         if ( typeof target === "boolean" ) {
206                 deep = target;
207                 target = arguments[1] || {};
208                 // skip the boolean and the target
209                 i = 2;
210         }
211
212         // Handle case when target is a string or something (possible in deep copy)
213         if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
214                 target = {};
215         }
216
217         // extend jQuery itself if only one argument is passed
218         if ( length === i ) {
219                 target = this;
220                 --i;
221         }
222
223         for ( ; i < length; i++ ) {
224                 // Only deal with non-null/undefined values
225                 if ( (options = arguments[ i ]) != null ) {
226                         // Extend the base object
227                         for ( name in options ) {
228                                 src = target[ name ];
229                                 copy = options[ name ];
230
231                                 // Prevent never-ending loop
232                                 if ( target === copy ) {
233                                         continue;
234                                 }
235
236                                 // Recurse if we're merging object values
237                                 if ( deep && copy && typeof copy === "object" && !copy.nodeType ) {
238                                         target[ name ] = jQuery.extend( deep,
239                                                 // Never move original objects, clone them
240                                                 src || ( copy.length != null ? [ ] : { } ), copy );
241
242                                 // Don't bring in undefined values
243                                 } else if ( copy !== undefined ) {
244                                         target[ name ] = copy;
245                                 }
246                         }
247                 }
248         }
249
250         // Return the modified object
251         return target;
252 };
253
254 jQuery.extend({
255         noConflict: function( deep ) {
256                 window.$ = _$;
257
258                 if ( deep ) {
259                         window.jQuery = _jQuery;
260                 }
261
262                 return jQuery;
263         },
264
265         // See test/unit/core.js for details concerning isFunction.
266         // Since version 1.3, DOM methods and functions like alert
267         // aren't supported. They return false on IE (#2968).
268         isFunction: function( obj ) {
269                 return toString.call(obj) === "[object Function]";
270         },
271
272         isArray: function( obj ) {
273                 return toString.call(obj) === "[object Array]";
274         },
275
276         // check if an element is in a (or is an) XML document
277         isXMLDoc: function( elem ) {
278                 return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
279                         !!elem.ownerDocument && elem.ownerDocument.documentElement.nodeName !== "HTML";
280         },
281
282         // Evalulates a script in a global context
283         globalEval: function( data ) {
284                 if ( data && /\S/.test(data) ) {
285                         // Inspired by code by Andrea Giammarchi
286                         // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
287                         var head = document.getElementsByTagName("head")[0] || document.documentElement,
288                                 script = document.createElement("script");
289
290                         script.type = "text/javascript";
291                         if ( jQuery.support.scriptEval ) {
292                                 script.appendChild( document.createTextNode( data ) );
293                         } else {
294                                 script.text = data;
295                         }
296
297                         // Use insertBefore instead of appendChild  to circumvent an IE6 bug.
298                         // This arises when a base node is used (#2709).
299                         head.insertBefore( script, head.firstChild );
300                         head.removeChild( script );
301                 }
302         },
303
304         nodeName: function( elem, name ) {
305                 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
306         },
307
308         // args is for internal usage only
309         each: function( object, callback, args ) {
310                 var name, i = 0, length = object.length;
311
312                 if ( args ) {
313                         if ( length === undefined ) {
314                                 for ( name in object ) {
315                                         if ( callback.apply( object[ name ], args ) === false ) {
316                                                 break;
317                                         }
318                                 }
319                         } else {
320                                 for ( ; i < length; ) {
321                                         if ( callback.apply( object[ i++ ], args ) === false ) {
322                                                 break;
323                                         }
324                                 }
325                         }
326
327                 // A special, fast, case for the most common use of each
328                 } else {
329                         if ( length === undefined ) {
330                                 for ( name in object ) {
331                                         if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
332                                                 break;
333                                         }
334                                 }
335                         } else {
336                                 for ( var value = object[0];
337                                         i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
338                         }
339                 }
340
341                 return object;
342         },
343
344         trim: function( text ) {
345                 return (text || "").replace( /^\s+|\s+$/g, "" );
346         },
347
348         makeArray: function( array ) {
349                 var ret = [], i;
350
351                 if ( array != null ) {
352                         i = array.length;
353
354                         // The window, strings (and functions) also have 'length'
355                         if ( i == null || typeof array === "string" || jQuery.isFunction(array) || array.setInterval ) {
356                                 ret[0] = array;
357                         } else {
358                                 while ( i ) {
359                                         ret[--i] = array[i];
360                                 }
361                         }
362                 }
363
364                 return ret;
365         },
366
367         inArray: function( elem, array ) {
368                 for ( var i = 0, length = array.length; i < length; i++ ) {
369                         if ( array[ i ] === elem ) {
370                                 return i;
371                         }
372                 }
373
374                 return -1;
375         },
376
377         merge: function( first, second ) {
378                 // We have to loop this way because IE & Opera overwrite the length
379                 // expando of getElementsByTagName
380                 var i = 0, elem, pos = first.length;
381
382                 // Also, we need to make sure that the correct elements are being returned
383                 // (IE returns comment nodes in a '*' query)
384                 if ( !jQuery.support.getAll ) {
385                         while ( (elem = second[ i++ ]) != null ) {
386                                 if ( elem.nodeType !== 8 ) {
387                                         first[ pos++ ] = elem;
388                                 }
389                         }
390
391                 } else {
392                         while ( (elem = second[ i++ ]) != null ) {
393                                 first[ pos++ ] = elem;
394                         }
395                 }
396
397                 return first;
398         },
399
400         unique: function( array ) {
401                 var ret = [], done = {}, id;
402
403                 try {
404                         for ( var i = 0, length = array.length; i < length; i++ ) {
405                                 id = jQuery.data( array[ i ] );
406
407                                 if ( !done[ id ] ) {
408                                         done[ id ] = true;
409                                         ret.push( array[ i ] );
410                                 }
411                         }
412                 } catch( e ) {
413                         ret = array;
414                 }
415
416                 return ret;
417         },
418
419         grep: function( elems, callback, inv ) {
420                 var ret = [];
421
422                 // Go through the array, only saving the items
423                 // that pass the validator function
424                 for ( var i = 0, length = elems.length; i < length; i++ ) {
425                         if ( !inv !== !callback( elems[ i ], i ) ) {
426                                 ret.push( elems[ i ] );
427                         }
428                 }
429
430                 return ret;
431         },
432
433         map: function( elems, callback ) {
434                 var ret = [], value;
435
436                 // Go through the array, translating each of the items to their
437                 // new value (or values).
438                 for ( var i = 0, length = elems.length; i < length; i++ ) {
439                         value = callback( elems[ i ], i );
440
441                         if ( value != null ) {
442                                 ret[ ret.length ] = value;
443                         }
444                 }
445
446                 return ret.concat.apply( [], ret );
447         },
448
449         // Use of jQuery.browser is deprecated.
450         // It's included for backwards compatibility and plugins,
451         // although they should work to migrate away.
452         browser: {
453                 version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
454                 safari: /webkit/.test( userAgent ),
455                 opera: /opera/.test( userAgent ),
456                 msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
457                 mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
458         }
459 });
460
461 // All jQuery objects should point back to these
462 rootjQuery = jQuery(document);
463
464 function evalScript( i, elem ) {
465         if ( elem.src ) {
466                 jQuery.ajax({
467                         url: elem.src,
468                         async: false,
469                         dataType: "script"
470                 });
471         } else {
472                 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
473         }
474
475         if ( elem.parentNode ) {
476                 elem.parentNode.removeChild( elem );
477         }
478 }
479
480 function now() {
481         return (new Date).getTime();
482 }