1 // Will speed up references to window, and allows munging its name.
4 // Will speed up references to undefined, and allows munging its name.
7 // Map over jQuery in case of overwrite
8 _jQuery = window.jQuery,
10 // Map over the $ in case of overwrite
13 // Define a local copy of jQuery
16 // A central reference to the root jQuery(document)
19 jQuery = window.jQuery = window.$ = function( selector, context ) {
20 // The jQuery object is actually just the init constructor 'enhanced'
21 return arguments.length === 0 ?
23 new jQuery.fn.init( selector, context );
26 // A simple way to check for HTML strings or ID strings
27 // (both of which we optimize for)
28 quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-]+)$/,
30 // Is it a simple selector
31 isSimple = /^.[^:#\[\.,]*$/,
33 // Keep a UserAgent string for use with jQuery.browser
34 userAgent = navigator.userAgent.toLowerCase(),
36 // Save a reference to the core toString method
37 toString = Object.prototype.toString;
39 jQuery.fn = jQuery.prototype = {
40 init: function( selector, context ) {
43 // Handle $(""), $(null), or $(undefined)
49 // Handle $(DOMElement)
50 if ( selector.nodeType ) {
53 this.context = selector;
57 // Handle HTML strings
58 if ( typeof selector === "string" ) {
59 // Are we dealing with HTML string or an ID?
60 match = quickExpr.exec( selector );
62 // Verify a match, and that no context was specified for #id
63 if ( match && (match[1] || !context) ) {
65 // HANDLE: $(html) -> $(array)
67 selector = jQuery.clean( [ match[1] ], context );
71 elem = document.getElementById( match[3] );
73 // Handle the case where IE and Opera return items
74 // by name instead of ID
75 if ( elem && elem.id !== match[3] ) {
76 return rootjQuery.find( selector );
79 // Otherwise, we inject the element directly into the jQuery object
80 ret = jQuery( elem || null );
81 ret.context = document;
82 ret.selector = selector;
86 // HANDLE: $(expr, $(...))
87 } else if ( !context || context.jquery ) {
88 return (context || rootjQuery).find( selector );
90 // HANDLE: $(expr, context)
91 // (which is just equivalent to: $(context).find(expr)
93 return jQuery( context ).find( selector );
96 // HANDLE: $(function)
97 // Shortcut for document ready
98 } else if ( jQuery.isFunction( selector ) ) {
99 return rootjQuery.ready( selector );
102 // Make sure that old selector state is passed along
103 if ( selector.selector && selector.context ) {
104 this.selector = selector.selector;
105 this.context = selector.context;
108 return this.setArray(jQuery.isArray( selector ) ?
110 jQuery.makeArray(selector));
113 // Start with an empty selector
116 // The current version of jQuery being used
119 // The number of elements contained in the matched element set
124 // Get the Nth element in the matched element set OR
125 // Get the whole matched element set as a clean array
126 get: function( num ) {
129 // Return a 'clean' array
130 Array.prototype.slice.call( this ) :
132 // Return just the object
136 // Take an array of elements and push it onto the stack
137 // (returning the new matched element set)
138 pushStack: function( elems, name, selector ) {
139 // Build a new jQuery matched element set
140 var ret = jQuery( elems || null );
142 // Add the old object onto the stack (as a reference)
143 ret.prevObject = this;
145 ret.context = this.context;
147 if ( name === "find" ) {
148 ret.selector = this.selector + (this.selector ? " " : "") + selector;
150 ret.selector = this.selector + "." + name + "(" + selector + ")";
153 // Return the newly-formed element set
157 // Force the current matched set of elements to become
158 // the specified array of elements (destroying the stack in the process)
159 // You should use pushStack() in order to do this, but maintain the stack
160 setArray: function( elems ) {
161 // Resetting the length to 0, then using the native Array push
162 // is a super-fast way to populate an object with array-like properties
164 Array.prototype.push.apply( this, elems );
169 // Execute a callback for every element in the matched set.
170 // (You can seed the arguments with an array of args, but this is
171 // only used internally.)
172 each: function( callback, args ) {
173 return jQuery.each( this, callback, args );
176 // Determine the position of an element within
177 // the matched set of elements
178 index: function( elem ) {
179 if ( !elem || typeof elem === "string" ) {
180 return jQuery.inArray( this[0],
181 // If it receives a string, the selector is used
182 // If it receives nothing, the siblings are used
183 elem ? jQuery( elem ) : this.parent().children() );
185 // Locate the position of the desired element
186 return jQuery.inArray(
187 // If it receives a jQuery object, the first element is used
188 elem.jquery ? elem[0] : elem, this );
191 is: function( selector ) {
192 return !!selector && jQuery.multiFilter( selector, this ).length > 0;
195 // For internal use only.
196 // Behaves like an Array's method, not like a jQuery method.
202 // Give the init function the jQuery prototype for later instantiation
203 jQuery.fn.init.prototype = jQuery.fn;
205 jQuery.extend = jQuery.fn.extend = function() {
206 // copy reference to target object
207 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
209 // Handle a deep copy situation
210 if ( typeof target === "boolean" ) {
212 target = arguments[1] || {};
213 // skip the boolean and the target
217 // Handle case when target is a string or something (possible in deep copy)
218 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
222 // extend jQuery itself if only one argument is passed
223 if ( length === i ) {
228 for ( ; i < length; i++ ) {
229 // Only deal with non-null/undefined values
230 if ( (options = arguments[ i ]) != null ) {
231 // Extend the base object
232 for ( name in options ) {
233 src = target[ name ];
234 copy = options[ name ];
236 // Prevent never-ending loop
237 if ( target === copy ) {
241 // Recurse if we're merging object values
242 if ( deep && copy && typeof copy === "object" && !copy.nodeType ) {
243 target[ name ] = jQuery.extend( deep,
244 // Never move original objects, clone them
245 src || ( copy.length != null ? [ ] : { } ), copy );
247 // Don't bring in undefined values
248 } else if ( copy !== undefined ) {
249 target[ name ] = copy;
255 // Return the modified object
260 noConflict: function( deep ) {
264 window.jQuery = _jQuery;
270 // See test/unit/core.js for details concerning isFunction.
271 // Since version 1.3, DOM methods and functions like alert
272 // aren't supported. They return false on IE (#2968).
273 isFunction: function( obj ) {
274 return toString.call(obj) === "[object Function]";
277 isArray: function( obj ) {
278 return toString.call(obj) === "[object Array]";
281 // check if an element is in a (or is an) XML document
282 isXMLDoc: function( elem ) {
283 return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
284 !!elem.ownerDocument && elem.ownerDocument.documentElement.nodeName !== "HTML";
287 // Evalulates a script in a global context
288 globalEval: function( data ) {
289 if ( data && /\S/.test(data) ) {
290 // Inspired by code by Andrea Giammarchi
291 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
292 var head = document.getElementsByTagName("head")[0] || document.documentElement,
293 script = document.createElement("script");
295 script.type = "text/javascript";
296 if ( jQuery.support.scriptEval ) {
297 script.appendChild( document.createTextNode( data ) );
302 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
303 // This arises when a base node is used (#2709).
304 head.insertBefore( script, head.firstChild );
305 head.removeChild( script );
309 nodeName: function( elem, name ) {
310 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
313 // args is for internal usage only
314 each: function( object, callback, args ) {
315 var name, i = 0, length = object.length;
318 if ( length === undefined ) {
319 for ( name in object ) {
320 if ( callback.apply( object[ name ], args ) === false ) {
325 for ( ; i < length; ) {
326 if ( callback.apply( object[ i++ ], args ) === false ) {
332 // A special, fast, case for the most common use of each
334 if ( length === undefined ) {
335 for ( name in object ) {
336 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
341 for ( var value = object[0];
342 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
349 trim: function( text ) {
350 return (text || "").replace( /^\s+|\s+$/g, "" );
353 makeArray: function( array ) {
356 if ( array != null ) {
359 // The window, strings (and functions) also have 'length'
360 if ( i == null || typeof array === "string" || jQuery.isFunction(array) || array.setInterval ) {
372 inArray: function( elem, array ) {
373 for ( var i = 0, length = array.length; i < length; i++ ) {
374 if ( array[ i ] === elem ) {
382 merge: function( first, second ) {
383 // We have to loop this way because IE & Opera overwrite the length
384 // expando of getElementsByTagName
385 var i = 0, elem, pos = first.length;
387 // Also, we need to make sure that the correct elements are being returned
388 // (IE returns comment nodes in a '*' query)
389 if ( !jQuery.support.getAll ) {
390 while ( (elem = second[ i++ ]) != null ) {
391 if ( elem.nodeType !== 8 ) {
392 first[ pos++ ] = elem;
397 while ( (elem = second[ i++ ]) != null ) {
398 first[ pos++ ] = elem;
405 unique: function( array ) {
406 var ret = [], done = {}, id;
409 for ( var i = 0, length = array.length; i < length; i++ ) {
410 id = jQuery.data( array[ i ] );
414 ret.push( array[ i ] );
424 grep: function( elems, callback, inv ) {
427 // Go through the array, only saving the items
428 // that pass the validator function
429 for ( var i = 0, length = elems.length; i < length; i++ ) {
430 if ( !inv !== !callback( elems[ i ], i ) ) {
431 ret.push( elems[ i ] );
438 map: function( elems, callback ) {
441 // Go through the array, translating each of the items to their
442 // new value (or values).
443 for ( var i = 0, length = elems.length; i < length; i++ ) {
444 value = callback( elems[ i ], i );
446 if ( value != null ) {
447 ret[ ret.length ] = value;
451 return ret.concat.apply( [], ret );
454 // Use of jQuery.browser is deprecated.
455 // It's included for backwards compatibility and plugins,
456 // although they should work to migrate away.
458 version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
459 safari: /webkit/.test( userAgent ),
460 opera: /opera/.test( userAgent ),
461 msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
462 mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
466 // All jQuery objects should point back to these
467 rootjQuery = jQuery(document);
469 function evalScript( i, elem ) {
477 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
480 if ( elem.parentNode ) {
481 elem.parentNode.removeChild( elem );
486 return (new Date).getTime();