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
32 rtrim = /(\s|\u00A0)+|(\s|\u00A0)+$/g,
34 // Match a standalone tag
35 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
37 // Keep a UserAgent string for use with jQuery.browser
38 userAgent = navigator.userAgent.toLowerCase(),
40 // Save a reference to some core methods
41 toString = Object.prototype.toString,
42 hasOwnProperty = Object.prototype.hasOwnProperty,
43 push = Array.prototype.push,
44 slice = Array.prototype.slice,
45 indexOf = Array.prototype.indexOf;
47 jQuery.fn = jQuery.prototype = {
48 init: function( selector, context ) {
49 var match, elem, ret, doc;
51 // Handle $(""), $(null), or $(undefined)
56 // Handle $(DOMElement)
57 if ( selector.nodeType ) {
58 this.context = this[0] = selector;
63 // Handle HTML strings
64 if ( typeof selector === "string" ) {
65 // Are we dealing with HTML string or an ID?
66 match = quickExpr.exec( selector );
68 // Verify a match, and that no context was specified for #id
69 if ( match && (match[1] || !context) ) {
71 // HANDLE: $(html) -> $(array)
73 doc = (context ? context.ownerDocument || context : document);
75 // If a single string is passed in and it's a single tag
76 // just do a createElement and skip the rest
77 ret = rsingleTag.exec( selector );
80 selector = [ doc.createElement( ret[1] ) ];
83 ret = buildFragment( [ match[1] ], [ doc ] );
84 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
89 elem = document.getElementById( match[2] );
92 // Handle the case where IE and Opera return items
93 // by name instead of ID
94 if ( elem.id !== match[2] ) {
95 return rootjQuery.find( selector );
98 // Otherwise, we inject the element directly into the jQuery object
103 this.context = document;
104 this.selector = selector;
109 } else if ( !context && /^\w+$/.test( selector ) ) {
110 this.selector = selector;
111 this.context = document;
112 selector = document.getElementsByTagName( selector );
114 // HANDLE: $(expr, $(...))
115 } else if ( !context || context.jquery ) {
116 return (context || rootjQuery).find( selector );
118 // HANDLE: $(expr, context)
119 // (which is just equivalent to: $(context).find(expr)
121 return jQuery( context ).find( selector );
124 // HANDLE: $(function)
125 // Shortcut for document ready
126 } else if ( jQuery.isFunction( selector ) ) {
127 return rootjQuery.ready( selector );
130 if (selector.selector !== undefined) {
131 this.selector = selector.selector;
132 this.context = selector.context;
135 return jQuery.isArray( selector ) ?
136 this.setArray( selector ) :
137 jQuery.makeArray( selector, this );
140 // Start with an empty selector
143 // The current version of jQuery being used
146 // The default length of a jQuery object is 0
149 // The number of elements contained in the matched element set
155 return slice.call( this, 0 );
158 // Get the Nth element in the matched element set OR
159 // Get the whole matched element set as a clean array
160 get: function( num ) {
163 // Return a 'clean' array
166 // Return just the object
167 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
170 // Take an array of elements and push it onto the stack
171 // (returning the new matched element set)
172 pushStack: function( elems, name, selector ) {
173 // Build a new jQuery matched element set
174 var ret = jQuery( elems || null );
176 // Add the old object onto the stack (as a reference)
177 ret.prevObject = this;
179 ret.context = this.context;
181 if ( name === "find" ) {
182 ret.selector = this.selector + (this.selector ? " " : "") + selector;
184 ret.selector = this.selector + "." + name + "(" + selector + ")";
187 // Return the newly-formed element set
191 // Force the current matched set of elements to become
192 // the specified array of elements (destroying the stack in the process)
193 // You should use pushStack() in order to do this, but maintain the stack
194 setArray: function( elems ) {
195 // Resetting the length to 0, then using the native Array push
196 // is a super-fast way to populate an object with array-like properties
198 push.apply( this, elems );
203 // Execute a callback for every element in the matched set.
204 // (You can seed the arguments with an array of args, but this is
205 // only used internally.)
206 each: function( callback, args ) {
207 return jQuery.each( this, callback, args );
210 // Determine the position of an element within
211 // the matched set of elements
212 index: function( elem ) {
213 if ( !elem || typeof elem === "string" ) {
214 return jQuery.inArray( this[0],
215 // If it receives a string, the selector is used
216 // If it receives nothing, the siblings are used
217 elem ? jQuery( elem ) : this.parent().children() );
219 // Locate the position of the desired element
220 return jQuery.inArray(
221 // If it receives a jQuery object, the first element is used
222 elem.jquery ? elem[0] : elem, this );
225 is: function( selector ) {
226 return !!selector && jQuery.filter( selector, this ).length > 0;
229 // For internal use only.
230 // Behaves like an Array's method, not like a jQuery method.
236 // Give the init function the jQuery prototype for later instantiation
237 jQuery.fn.init.prototype = jQuery.fn;
239 jQuery.extend = jQuery.fn.extend = function() {
240 // copy reference to target object
241 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
243 // Handle a deep copy situation
244 if ( typeof target === "boolean" ) {
246 target = arguments[1] || {};
247 // skip the boolean and the target
251 // Handle case when target is a string or something (possible in deep copy)
252 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
256 // extend jQuery itself if only one argument is passed
257 if ( length === i ) {
262 for ( ; i < length; i++ ) {
263 // Only deal with non-null/undefined values
264 if ( (options = arguments[ i ]) != null ) {
265 // Extend the base object
266 for ( name in options ) {
267 src = target[ name ];
268 copy = options[ name ];
270 // Prevent never-ending loop
271 if ( target === copy ) {
275 // Recurse if we're merging object literal values
276 if ( deep && copy && jQuery.isObjectLiteral(copy) ) {
277 // Don't extend not object literals
278 var clone = src && jQuery.isObjectLiteral(src) ? src : {};
280 // Never move original objects, clone them
281 target[ name ] = jQuery.extend( deep, clone, copy );
283 // Don't bring in undefined values
284 } else if ( copy !== undefined ) {
285 target[ name ] = copy;
291 // Return the modified object
296 noConflict: function( deep ) {
300 window.jQuery = _jQuery;
306 // See test/unit/core.js for details concerning isFunction.
307 // Since version 1.3, DOM methods and functions like alert
308 // aren't supported. They return false on IE (#2968).
309 isFunction: function( obj ) {
310 return toString.call(obj) === "[object Function]";
313 isArray: function( obj ) {
314 return toString.call(obj) === "[object Array]";
317 isObjectLiteral: function( obj ) {
318 if ( toString.call(obj) !== "[object Object]" || typeof obj.nodeType === "number" ) {
322 // not own constructor property must be Object
324 && !hasOwnProperty.call(obj, "constructor")
325 && !hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf") ) {
329 //own properties are iterated firstly,
330 //so to speed up, we can test last one if it is own or not
333 for ( key in obj ) {}
335 return key === undefined || hasOwnProperty.call( obj, key );
338 isEmptyObject: function( obj ) {
339 for ( var name in obj ) {
345 // check if an element is in a (or is an) XML document
346 isXMLDoc: function( elem ) {
347 // documentElement is verified for cases where it doesn't yet exist
348 // (such as loading iframes in IE - #4833)
349 var documentElement = (elem ? elem.ownerDocument || elem : 0).documentElement;
350 return documentElement ? documentElement.nodeName !== "HTML" : false;
353 // Evalulates a script in a global context
354 globalEval: function( data ) {
355 if ( data && rnotwhite.test(data) ) {
356 // Inspired by code by Andrea Giammarchi
357 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
358 var head = document.getElementsByTagName("head")[0] || document.documentElement,
359 script = document.createElement("script");
361 script.type = "text/javascript";
363 if ( jQuery.support.scriptEval ) {
364 script.appendChild( document.createTextNode( data ) );
369 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
370 // This arises when a base node is used (#2709).
371 head.insertBefore( script, head.firstChild );
372 head.removeChild( script );
376 nodeName: function( elem, name ) {
377 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
380 // args is for internal usage only
381 each: function( object, callback, args ) {
383 length = object.length,
384 isObj = length === undefined || jQuery.isFunction(object);
388 for ( name in object ) {
389 if ( callback.apply( object[ name ], args ) === false ) {
394 for ( ; i < length; ) {
395 if ( callback.apply( object[ i++ ], args ) === false ) {
401 // A special, fast, case for the most common use of each
404 for ( name in object ) {
405 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
410 for ( var value = object[0];
411 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
418 trim: function( text ) {
419 return (text || "").replace( rtrim, "" );
422 // results is for internal usage only
423 makeArray: function( array, results ) {
424 var ret = results || [];
426 if ( array != null ) {
427 // The window, strings (and functions) also have 'length'
428 if ( array.length == null || typeof array === "string" || jQuery.isFunction(array) || array.setInterval ) {
429 push.call( ret, array );
431 jQuery.merge( ret, array );
438 inArray: function( elem, array ) {
439 if ( array.indexOf ) {
440 return array.indexOf( elem );
443 for ( var i = 0, length = array.length; i < length; i++ ) {
444 if ( array[ i ] === elem ) {
452 merge: function( first, second ) {
453 var pos, i = second.length;
455 // We have to get length this way when IE & Opera overwrite the length
456 // expando of getElementsByTagName
457 if ( i && i.nodeType ) {
458 for ( i = 0; second[i]; ++i ) {}
461 pos = i + first.length;
463 // Correct length for non Arrays
467 first[ --pos ] = second[ --i ];
473 grep: function( elems, callback, inv ) {
476 // Go through the array, only saving the items
477 // that pass the validator function
478 for ( var i = 0, length = elems.length; i < length; i++ ) {
479 if ( !inv !== !callback( elems[ i ], i ) ) {
480 ret.push( elems[ i ] );
487 map: function( elems, callback ) {
490 // Go through the array, translating each of the items to their
491 // new value (or values).
492 for ( var i = 0, length = elems.length; i < length; i++ ) {
493 value = callback( elems[ i ], i );
495 if ( value != null ) {
496 ret[ ret.length ] = value;
500 return ret.concat.apply( [], ret );
503 // Use of jQuery.browser is deprecated.
504 // It's included for backwards compatibility and plugins,
505 // although they should work to migrate away.
507 version: (/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/.exec(userAgent) || [0,'0'])[1],
508 safari: /webkit/.test( userAgent ),
509 opera: /opera/.test( userAgent ),
510 msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
511 mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
516 jQuery.inArray = function( elem, array ) {
517 return indexOf.call( array, elem );
521 // All jQuery objects should point back to these
522 rootjQuery = jQuery(document);
524 function evalScript( i, elem ) {
532 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
535 if ( elem.parentNode ) {
536 elem.parentNode.removeChild( elem );
540 // Mutifunctional method to get and set values to a collection
541 // The value/s can be optionally by executed if its a function
542 function access( elems, key, value, exec, fn ) {
543 var l = elems.length;
545 // Setting many attributes
546 if ( typeof key === "object" ) {
548 access(elems, k, key[k], exec, fn);
553 // Setting one attribute
554 if (value !== undefined) {
555 // Optionally, function values get executed if exec is true
556 exec = exec && jQuery.isFunction(value);
558 for (var i = 0; i < l; i++) {
560 val = exec ? value.call(elem, i) : value;
566 // Getting an attribute
567 return l ? fn(elems[0], key) : null;
571 return (new Date).getTime();