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 ?
6 new jQuery.fn.init( selector, context );
9 // Map over jQuery in case of overwrite
10 _jQuery = window.jQuery,
12 // Map over the $ in case of overwrite
15 // Use the correct document accordingly with window argument (sandbox)
16 document = window.document,
18 // A central reference to the root jQuery(document)
21 // A simple way to check for HTML strings or ID strings
22 // (both of which we optimize for)
23 quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$|^#([\w-]+)$/,
25 // Is it a simple selector
26 isSimple = /^.[^:#\[\.,]*$/,
28 // Check if a string has a non-whitespace character in it
31 // Used for trimming whitespace
34 // Keep a UserAgent string for use with jQuery.browser
35 userAgent = navigator.userAgent.toLowerCase(),
37 // Save a reference to some core methods
38 toString = Object.prototype.toString,
39 push = Array.prototype.push,
40 slice = Array.prototype.slice;
42 jQuery.fn = jQuery.prototype = {
43 init: function( selector, context ) {
46 // Handle $(""), $(null), or $(undefined)
51 // Handle $(DOMElement)
52 if ( selector.nodeType ) {
53 this.context = this[0] = selector;
58 // Handle HTML strings
59 if ( typeof selector === "string" ) {
60 // Are we dealing with HTML string or an ID?
61 match = quickExpr.exec( selector );
63 // Verify a match, and that no context was specified for #id
64 if ( match && (match[1] || !context) ) {
66 // HANDLE: $(html) -> $(array)
68 selector = jQuery.clean( [ match[1] ], context );
72 elem = document.getElementById( match[2] );
75 // Handle the case where IE and Opera return items
76 // by name instead of ID
77 if ( elem.id !== match[2] ) {
78 return rootjQuery.find( selector );
81 // Otherwise, we inject the element directly into the jQuery object
86 this.context = document;
87 this.selector = selector;
91 // HANDLE: $(expr, $(...))
92 } else if ( !context || context.jquery ) {
93 return (context || rootjQuery).find( selector );
95 // HANDLE: $(expr, context)
96 // (which is just equivalent to: $(context).find(expr)
98 return jQuery( context ).find( selector );
101 // HANDLE: $(function)
102 // Shortcut for document ready
103 } else if ( jQuery.isFunction( selector ) ) {
104 return rootjQuery.ready( selector );
107 // Make sure that old selector state is passed along
108 if ( selector.selector && selector.context ) {
109 this.selector = selector.selector;
110 this.context = selector.context;
113 return this.setArray(jQuery.isArray( selector ) ?
115 jQuery.makeArray(selector));
118 // Start with an empty selector
121 // The current version of jQuery being used
124 // The default length of a jQuery object is 0
127 // The number of elements contained in the matched element set
133 return slice.call( this, 0 );
136 // Get the Nth element in the matched element set OR
137 // Get the whole matched element set as a clean array
138 get: function( num ) {
141 // Return a 'clean' array
144 // Return just the object
145 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
148 // Take an array of elements and push it onto the stack
149 // (returning the new matched element set)
150 pushStack: function( elems, name, selector ) {
151 // Build a new jQuery matched element set
152 var ret = jQuery( elems || null );
154 // Add the old object onto the stack (as a reference)
155 ret.prevObject = this;
157 ret.context = this.context;
159 if ( name === "find" ) {
160 ret.selector = this.selector + (this.selector ? " " : "") + selector;
162 ret.selector = this.selector + "." + name + "(" + selector + ")";
165 // Return the newly-formed element set
169 // Force the current matched set of elements to become
170 // the specified array of elements (destroying the stack in the process)
171 // You should use pushStack() in order to do this, but maintain the stack
172 setArray: function( elems ) {
173 // Resetting the length to 0, then using the native Array push
174 // is a super-fast way to populate an object with array-like properties
176 push.apply( this, elems );
181 // Execute a callback for every element in the matched set.
182 // (You can seed the arguments with an array of args, but this is
183 // only used internally.)
184 each: function( callback, args ) {
185 return jQuery.each( this, callback, args );
188 // Determine the position of an element within
189 // the matched set of elements
190 index: function( elem ) {
191 if ( !elem || typeof elem === "string" ) {
192 return jQuery.inArray( this[0],
193 // If it receives a string, the selector is used
194 // If it receives nothing, the siblings are used
195 elem ? jQuery( elem ) : this.parent().children() );
197 // Locate the position of the desired element
198 return jQuery.inArray(
199 // If it receives a jQuery object, the first element is used
200 elem.jquery ? elem[0] : elem, this );
203 is: function( selector ) {
204 return !!selector && jQuery.multiFilter( selector, this ).length > 0;
207 // For internal use only.
208 // Behaves like an Array's method, not like a jQuery method.
214 // Give the init function the jQuery prototype for later instantiation
215 jQuery.fn.init.prototype = jQuery.fn;
217 jQuery.extend = jQuery.fn.extend = function() {
218 // copy reference to target object
219 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
221 // Handle a deep copy situation
222 if ( typeof target === "boolean" ) {
224 target = arguments[1] || {};
225 // skip the boolean and the target
229 // Handle case when target is a string or something (possible in deep copy)
230 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
234 // extend jQuery itself if only one argument is passed
235 if ( length === i ) {
240 for ( ; i < length; i++ ) {
241 // Only deal with non-null/undefined values
242 if ( (options = arguments[ i ]) != null ) {
243 // Extend the base object
244 for ( name in options ) {
245 src = target[ name ];
246 copy = options[ name ];
248 // Prevent never-ending loop
249 if ( target === copy ) {
253 // Recurse if we're merging object values
254 if ( deep && copy && typeof copy === "object" && !copy.nodeType ) {
259 } else if ( jQuery.isArray(copy) ) {
261 } else if ( jQuery.isObject(copy) ) {
267 // Never move original objects, clone them
268 target[ name ] = jQuery.extend( deep, clone, copy );
270 // Don't bring in undefined values
271 } else if ( copy !== undefined ) {
272 target[ name ] = copy;
278 // Return the modified object
283 noConflict: function( deep ) {
287 window.jQuery = _jQuery;
293 // See test/unit/core.js for details concerning isFunction.
294 // Since version 1.3, DOM methods and functions like alert
295 // aren't supported. They return false on IE (#2968).
296 isFunction: function( obj ) {
297 return toString.call(obj) === "[object Function]";
300 isArray: function( obj ) {
301 return toString.call(obj) === "[object Array]";
304 isObject: function( obj ) {
305 return this.constructor.call(obj) === Object;
308 isEmptyObject: function( obj ) {
309 for ( var name in obj ) {
315 // check if an element is in a (or is an) XML document
316 isXMLDoc: function( elem ) {
317 // documentElement is verified for cases where it doesn't yet exist
318 // (such as loading iframes in IE - #4833)
319 return ((elem ? elem.ownerDocument || elem : 0).documentElement || 0).nodeName !== "HTML";
322 // Evalulates a script in a global context
323 globalEval: function( data ) {
324 if ( data && rnotwhite.test(data) ) {
325 // Inspired by code by Andrea Giammarchi
326 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
327 var head = document.getElementsByTagName("head")[0] || document.documentElement,
328 script = document.createElement("script");
330 script.type = "text/javascript";
332 if ( jQuery.support.scriptEval ) {
333 script.appendChild( document.createTextNode( data ) );
338 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
339 // This arises when a base node is used (#2709).
340 head.insertBefore( script, head.firstChild );
341 head.removeChild( script );
345 nodeName: function( elem, name ) {
346 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
349 // args is for internal usage only
350 each: function( object, callback, args ) {
352 length = object.length,
353 isObj = length === undefined || jQuery.isFunction(object);
357 for ( name in object ) {
358 if ( callback.apply( object[ name ], args ) === false ) {
363 for ( ; i < length; ) {
364 if ( callback.apply( object[ i++ ], args ) === false ) {
370 // A special, fast, case for the most common use of each
373 for ( name in object ) {
374 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
379 for ( var value = object[0];
380 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
387 trim: function( text ) {
388 return (text || "").replace( rtrim, "" );
391 makeArray: function( array ) {
394 if ( array != null ) {
397 // The window, strings (and functions) also have 'length'
398 if ( i == null || typeof array === "string" || jQuery.isFunction(array) || array.setInterval ) {
410 inArray: function( elem, array ) {
411 for ( var i = 0, length = array.length; i < length; i++ ) {
412 if ( array[ i ] === elem ) {
420 merge: function( first, second ) {
421 // We have to loop this way because IE & Opera overwrite the length
422 // expando of getElementsByTagName
423 var i = 0, elem, pos = first.length;
425 // Also, we need to make sure that the correct elements are being returned
426 // (IE returns comment nodes in a '*' query)
427 if ( !jQuery.support.getAll ) {
428 while ( (elem = second[ i++ ]) != null ) {
429 if ( elem.nodeType !== 8 ) {
430 first[ pos++ ] = elem;
435 while ( (elem = second[ i++ ]) != null ) {
436 first[ pos++ ] = elem;
443 unique: function( array ) {
444 var ret = [], done = {}, id;
447 for ( var i = 0, length = array.length; i < length; i++ ) {
448 id = jQuery.data( array[ i ] );
452 ret.push( array[ i ] );
462 grep: function( elems, callback, inv ) {
465 // Go through the array, only saving the items
466 // that pass the validator function
467 for ( var i = 0, length = elems.length; i < length; i++ ) {
468 if ( !inv !== !callback( elems[ i ], i ) ) {
469 ret.push( elems[ i ] );
476 map: function( elems, callback ) {
479 // Go through the array, translating each of the items to their
480 // new value (or values).
481 for ( var i = 0, length = elems.length; i < length; i++ ) {
482 value = callback( elems[ i ], i );
484 if ( value != null ) {
485 ret[ ret.length ] = value;
489 return ret.concat.apply( [], ret );
492 // Use of jQuery.browser is deprecated.
493 // It's included for backwards compatibility and plugins,
494 // although they should work to migrate away.
496 version: (/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/.exec(userAgent) || [0,'0'])[1],
497 safari: /webkit/.test( userAgent ),
498 opera: /opera/.test( userAgent ),
499 msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
500 mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
504 // All jQuery objects should point back to these
505 rootjQuery = jQuery(document);
507 function evalScript( i, elem ) {
515 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
518 if ( elem.parentNode ) {
519 elem.parentNode.removeChild( elem );
524 return (new Date).getTime();