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