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 = /^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-]+)$/,
25 // Is it a simple selector
26 isSimple = /^.[^:#\[\.,]*$/,
28 // Keep a UserAgent string for use with jQuery.browser
29 userAgent = navigator.userAgent.toLowerCase(),
31 // Save a reference to the core toString method
32 toString = Object.prototype.toString;
34 // Expose jQuery to the global object
35 window.jQuery = window.$ = jQuery;
37 jQuery.fn = jQuery.prototype = {
38 init: function( selector, context ) {
41 // Handle $(""), $(null), or $(undefined)
47 // Handle $(DOMElement)
48 if ( selector.nodeType ) {
51 this.context = selector;
55 // Handle HTML strings
56 if ( typeof selector === "string" ) {
57 // Are we dealing with HTML string or an ID?
58 match = quickExpr.exec( selector );
60 // Verify a match, and that no context was specified for #id
61 if ( match && (match[1] || !context) ) {
63 // HANDLE: $(html) -> $(array)
65 selector = jQuery.clean( [ match[1] ], context );
69 elem = document.getElementById( match[3] );
71 // Handle the case where IE and Opera return items
72 // by name instead of ID
73 if ( elem && elem.id !== match[3] ) {
74 return rootjQuery.find( selector );
77 // Otherwise, we inject the element directly into the jQuery object
78 this.length = elem ? 1 : 0;
82 this.context = document;
83 this.selector = selector;
87 // HANDLE: $(expr, $(...))
88 } else if ( !context || context.jquery ) {
89 return (context || rootjQuery).find( selector );
91 // HANDLE: $(expr, context)
92 // (which is just equivalent to: $(context).find(expr)
94 return jQuery( context ).find( selector );
97 // HANDLE: $(function)
98 // Shortcut for document ready
99 } else if ( jQuery.isFunction( selector ) ) {
100 return rootjQuery.ready( selector );
103 // Make sure that old selector state is passed along
104 if ( selector.selector && selector.context ) {
105 this.selector = selector.selector;
106 this.context = selector.context;
109 return this.setArray(jQuery.isArray( selector ) ?
111 jQuery.makeArray(selector));
114 // Start with an empty selector
117 // The current version of jQuery being used
120 // The number of elements contained in the matched element set
125 // Get the Nth element in the matched element set OR
126 // Get the whole matched element set as a clean array
127 get: function( num ) {
130 // Return a 'clean' array
131 Array.prototype.slice.call( this ) :
133 // Return just the object
137 // Take an array of elements and push it onto the stack
138 // (returning the new matched element set)
139 pushStack: function( elems, name, selector ) {
140 // Build a new jQuery matched element set
141 var ret = jQuery( elems || null );
143 // Add the old object onto the stack (as a reference)
144 ret.prevObject = this;
146 ret.context = this.context;
148 if ( name === "find" ) {
149 ret.selector = this.selector + (this.selector ? " " : "") + selector;
151 ret.selector = this.selector + "." + name + "(" + selector + ")";
154 // Return the newly-formed element set
158 // Force the current matched set of elements to become
159 // the specified array of elements (destroying the stack in the process)
160 // You should use pushStack() in order to do this, but maintain the stack
161 setArray: function( elems ) {
162 // Resetting the length to 0, then using the native Array push
163 // is a super-fast way to populate an object with array-like properties
165 Array.prototype.push.apply( this, elems );
170 // Execute a callback for every element in the matched set.
171 // (You can seed the arguments with an array of args, but this is
172 // only used internally.)
173 each: function( callback, args ) {
174 return jQuery.each( this, callback, args );
177 // Determine the position of an element within
178 // the matched set of elements
179 index: function( elem ) {
180 if ( !elem || typeof elem === "string" ) {
181 return jQuery.inArray( this[0],
182 // If it receives a string, the selector is used
183 // If it receives nothing, the siblings are used
184 elem ? jQuery( elem ) : this.parent().children() );
186 // Locate the position of the desired element
187 return jQuery.inArray(
188 // If it receives a jQuery object, the first element is used
189 elem.jquery ? elem[0] : elem, this );
192 is: function( selector ) {
193 return !!selector && jQuery.multiFilter( selector, this ).length > 0;
196 // For internal use only.
197 // Behaves like an Array's method, not like a jQuery method.
203 // Give the init function the jQuery prototype for later instantiation
204 jQuery.fn.init.prototype = jQuery.fn;
206 jQuery.extend = jQuery.fn.extend = function() {
207 // copy reference to target object
208 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
210 // Handle a deep copy situation
211 if ( typeof target === "boolean" ) {
213 target = arguments[1] || {};
214 // skip the boolean and the target
218 // Handle case when target is a string or something (possible in deep copy)
219 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
223 // extend jQuery itself if only one argument is passed
224 if ( length === i ) {
229 for ( ; i < length; i++ ) {
230 // Only deal with non-null/undefined values
231 if ( (options = arguments[ i ]) != null ) {
232 // Extend the base object
233 for ( name in options ) {
234 src = target[ name ];
235 copy = options[ name ];
237 // Prevent never-ending loop
238 if ( target === copy ) {
242 // Recurse if we're merging object values
243 if ( deep && copy && typeof copy === "object" && !copy.nodeType ) {
244 target[ name ] = jQuery.extend( deep,
245 // Never move original objects, clone them
246 src || ( copy.length != null ? [ ] : { } ), copy );
248 // Don't bring in undefined values
249 } else if ( copy !== undefined ) {
250 target[ name ] = copy;
256 // Return the modified object
261 noConflict: function( deep ) {
265 window.jQuery = _jQuery;
271 // See test/unit/core.js for details concerning isFunction.
272 // Since version 1.3, DOM methods and functions like alert
273 // aren't supported. They return false on IE (#2968).
274 isFunction: function( obj ) {
275 return toString.call(obj) === "[object Function]";
278 isArray: function( obj ) {
279 return toString.call(obj) === "[object Array]";
282 // check if an element is in a (or is an) XML document
283 isXMLDoc: function( elem ) {
284 return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
285 !!elem.ownerDocument && elem.ownerDocument.documentElement.nodeName !== "HTML";
288 // Evalulates a script in a global context
289 globalEval: function( data ) {
290 if ( data && /\S/.test(data) ) {
291 // Inspired by code by Andrea Giammarchi
292 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
293 var head = document.getElementsByTagName("head")[0] || document.documentElement,
294 script = document.createElement("script");
296 script.type = "text/javascript";
297 if ( jQuery.support.scriptEval ) {
298 script.appendChild( document.createTextNode( data ) );
303 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
304 // This arises when a base node is used (#2709).
305 head.insertBefore( script, head.firstChild );
306 head.removeChild( script );
310 nodeName: function( elem, name ) {
311 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
314 // args is for internal usage only
315 each: function( object, callback, args ) {
317 length = object.length,
318 isObj = length === undefined || jQuery.isFunction(object);
322 for ( name in object ) {
323 if ( callback.apply( object[ name ], args ) === false ) {
328 for ( ; i < length; ) {
329 if ( callback.apply( object[ i++ ], args ) === false ) {
335 // A special, fast, case for the most common use of each
338 for ( name in object ) {
339 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
344 for ( var value = object[0];
345 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
352 trim: function( text ) {
353 return (text || "").replace( /^\s+|\s+$/g, "" );
356 makeArray: function( array ) {
359 if ( array != null ) {
362 // The window, strings (and functions) also have 'length'
363 if ( i == null || typeof array === "string" || jQuery.isFunction(array) || array.setInterval ) {
375 inArray: function( elem, array ) {
376 for ( var i = 0, length = array.length; i < length; i++ ) {
377 if ( array[ i ] === elem ) {
385 merge: function( first, second ) {
386 // We have to loop this way because IE & Opera overwrite the length
387 // expando of getElementsByTagName
388 var i = 0, elem, pos = first.length;
390 // Also, we need to make sure that the correct elements are being returned
391 // (IE returns comment nodes in a '*' query)
392 if ( !jQuery.support.getAll ) {
393 while ( (elem = second[ i++ ]) != null ) {
394 if ( elem.nodeType !== 8 ) {
395 first[ pos++ ] = elem;
400 while ( (elem = second[ i++ ]) != null ) {
401 first[ pos++ ] = elem;
408 unique: function( array ) {
409 var ret = [], done = {}, id;
412 for ( var i = 0, length = array.length; i < length; i++ ) {
413 id = jQuery.data( array[ i ] );
417 ret.push( array[ i ] );
427 grep: function( elems, callback, inv ) {
430 // Go through the array, only saving the items
431 // that pass the validator function
432 for ( var i = 0, length = elems.length; i < length; i++ ) {
433 if ( !inv !== !callback( elems[ i ], i ) ) {
434 ret.push( elems[ i ] );
441 map: function( elems, callback ) {
444 // Go through the array, translating each of the items to their
445 // new value (or values).
446 for ( var i = 0, length = elems.length; i < length; i++ ) {
447 value = callback( elems[ i ], i );
449 if ( value != null ) {
450 ret[ ret.length ] = value;
454 return ret.concat.apply( [], ret );
457 // Use of jQuery.browser is deprecated.
458 // It's included for backwards compatibility and plugins,
459 // although they should work to migrate away.
461 version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
462 safari: /webkit/.test( userAgent ),
463 opera: /opera/.test( userAgent ),
464 msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
465 mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
469 // All jQuery objects should point back to these
470 rootjQuery = jQuery(document);
472 function evalScript( i, elem ) {
480 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
483 if ( elem.parentNode ) {
484 elem.parentNode.removeChild( elem );
489 return (new Date).getTime();