// Define a local copy of jQuery
var jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
- return new jQuery.fn.init( selector, context );
+ return new jQuery.fn.init( selector, context, rootjQuery );
},
// Map over jQuery in case of overwrite
// (both of which we optimize for)
quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/,
- // Is it a simple selector
- isSimple = /^.[^:#\[\.,]*$/,
-
// Check if a string has a non-whitespace character in it
rnotwhite = /\S/,
- rwhite = /\s/,
// Used for trimming whitespace
trimLeft = /^\s+/,
trimRight = /\s+$/,
- // Check for non-word characters
- rnonword = /\W/,
-
// Check for digits
rdigit = /\d/,
// For matching the engine and version of the browser
browserMatch,
- // Has the ready events already been bound?
- readyBound = false,
-
// The deferred used on DOM ready
readyList,
indexOf = Array.prototype.indexOf,
// [[Class]] -> type pairs
- class2type = {},
-
- // Marker for deferred
- deferredMarker = [];
+ class2type = {};
jQuery.fn = jQuery.prototype = {
- init: function( selector, context ) {
+ constructor: jQuery,
+ init: function( selector, context, rootjQuery ) {
var match, elem, ret, doc;
// Handle $(""), $(null), or $(undefined)
// HANDLE: $(html) -> $(array)
if ( match[1] ) {
+ context = context instanceof jQuery ? context[0] : context;
doc = (context ? context.ownerDocument || context : document);
// If a single string is passed in and it's a single tag
} else {
ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
- selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
+ selector = (ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment).childNodes;
}
return jQuery.merge( this, selector );
return this;
}
- // HANDLE: $("TAG")
- } else if ( !context && !rnonword.test( selector ) ) {
- this.selector = selector;
- this.context = document;
- selector = document.getElementsByTagName( selector );
- return jQuery.merge( this, selector );
-
// HANDLE: $(expr, $(...))
} else if ( !context || context.jquery ) {
return (context || rootjQuery).find( selector );
// HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
} else {
- return jQuery( context ).find( selector );
+ return this.constructor( context ).find( selector );
}
// HANDLE: $(function)
// (returning the new matched element set)
pushStack: function( elems, name, selector ) {
// Build a new jQuery matched element set
- var ret = jQuery();
+ var ret = this.constructor();
if ( jQuery.isArray( elems ) ) {
push.apply( ret, elems );
each: function( callback, args ) {
return jQuery.each( this, callback, args );
},
-
- ready: function() {
+
+ ready: function( fn ) {
// Attach the listeners
jQuery.bindReady();
-
- // Change ready & apply
- return ( jQuery.fn.ready = readyList.then ).apply( this , arguments );
+
+ // Add the callback
+ readyList.done( fn );
+
+ return this;
},
eq: function( i ) {
},
end: function() {
- return this.prevObject || jQuery(null);
+ return this.prevObject || this.constructor(null);
},
// For internal use only.
jQuery.fn.init.prototype = jQuery.fn;
jQuery.extend = jQuery.fn.extend = function() {
- var options, name, src, copy, copyIsArray, clone,
+ var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
}
// If there are functions bound, to execute
- readyList.fire( document , [ jQuery ] );
-
+ readyList.resolveWith( document, [ jQuery ] );
+
// Trigger any bound ready events
if ( jQuery.fn.trigger ) {
jQuery( document ).trigger( "ready" ).unbind( "ready" );
},
bindReady: function() {
- if ( readyBound ) {
+ if ( readyList ) {
return;
}
- readyBound = true;
+ readyList = jQuery._Deferred();
// Catch cases where $(document).ready() is called after the
// browser event has already occurred.
jQuery.error( "Invalid JSON: " + data );
}
},
-
+
// Cross-browser xml parsing
// (xml & tmp used internally)
parseXML: function( data , xml , tmp ) {
xml.async = "false";
xml.loadXML( data );
}
-
+
tmp = xml.documentElement;
-
+
if ( ! tmp || ! tmp.nodeName || tmp.nodeName === "parsererror" ) {
jQuery.error( "Invalid XML: " + data );
}
-
+
return xml;
},
if ( data && rnotwhite.test(data) ) {
// Inspired by code by Andrea Giammarchi
// http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
- var head = document.getElementsByTagName("head")[0] || document.documentElement,
- script = document.createElement("script");
-
- script.type = "text/javascript";
+ var head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement,
+ script = document.createElement( "script" );
- if ( jQuery.support.scriptEval ) {
+ if ( jQuery.support.scriptEval() ) {
script.appendChild( document.createTextNode( data ) );
} else {
script.text = data;
now: function() {
return (new Date()).getTime();
},
-
- // Create a simple deferred (one callbacks list)
- _Deferred: function() {
-
- var // callbacks list
- callbacks = [],
- // stored [ context , args ]
- fired,
- // to avoid firing when already doing so
- firing,
- // flag to know if the deferred has been cancelled
- cancelled,
- // the deferred itself
- deferred = {
-
- // then( f1, f2, ...)
- then: function () {
-
- if ( ! cancelled ) {
-
- var args = arguments,
- i,
- length,
- elem,
- type,
- _fired;
-
- if ( fired ) {
- _fired = fired;
- fired = 0;
- }
-
- for ( i = 0, length = args.length ; i < length ; i++ ) {
- elem = args[ i ];
- type = jQuery.type( elem );
- if ( type === "array" ) {
- deferred.then.apply( deferred , elem );
- } else if ( type === "function" ) {
- callbacks.push( elem );
- }
- }
-
- if ( _fired ) {
- deferred.fire( _fired[ 0 ] , _fired[ 1 ] );
- }
- }
-
- return this;
- },
-
- // resolve with given context and args
- fire: function( context , args ) {
- if ( ! cancelled && ! fired && ! firing ) {
-
- firing = 1;
-
- try {
- while( callbacks[ 0 ] ) {
- callbacks.shift().apply( context , args );
- }
- }
- finally {
- fired = [ context , args ];
- firing = 0;
- }
- }
- return this;
- },
-
- // resolve with this as context and given arguments
- resolve: function() {
- deferred.fire( this , arguments );
- return this;
- },
-
- // Has this deferred been resolved?
- isResolved: function() {
- return !!( firing || fired );
- },
-
- // Cancel
- cancel: function() {
- cancelled = 1;
- callbacks = [];
- return this;
- },
-
- // Has this deferred been cancelled?
- isCancelled: function() {
- return !!cancelled;
- }
- };
-
- // Add the deferred marker
- deferred.then._ = deferredMarker;
-
- return deferred;
- },
-
- // Full fledged deferred (two callbacks list)
- // Typical success/error system
- Deferred: function( func ) {
-
- var errorDeferred = jQuery._Deferred(),
- deferred = jQuery._Deferred(),
- successCancel = deferred.cancel;
-
- // Add errorDeferred methods and redefine cancel
- jQuery.extend( deferred , {
-
- fail: errorDeferred.then,
- fireReject: errorDeferred.fire,
- reject: errorDeferred.resolve,
- isRejected: errorDeferred.isResolved
-
- } );
-
- // Remove cancel related
- delete deferred.cancel;
- delete deferred.isCancelled;
-
- // Make sure only one callback list will be used
- deferred.then( errorDeferred.cancel ).fail( successCancel );
-
- // Call given func if any
- if ( func ) {
- func.call( deferred , deferred );
- }
-
- return deferred;
- },
-
- // Check if an object is a deferred
- isDeferred: function( object , method ) {
- method = method || "then";
- return !!( object && object[ method ] && object[ method ]._ === deferredMarker );
- },
-
- // Deferred helper
- when: function( object , method ) {
- method = method || "then";
- object = jQuery.isDeferred( object , method ) ?
- object :
- jQuery.Deferred().resolve( object );
- object.fail = object.fail || function() { return this; };
- object[ method ] = object[ method ] || object.then;
- object.then = object.then || object[ method ];
- return object;
- },
// Use of jQuery.browser is frowned upon.
// More details: http://docs.jquery.com/Utilities/jQuery.browser
return { browser: match[1] || "", version: match[2] || "0" };
},
+ sub: function() {
+ function jQuerySubclass( selector, context ) {
+ return new jQuerySubclass.fn.init( selector, context );
+ }
+ jQuery.extend( true, jQuerySubclass, this );
+ jQuerySubclass.superclass = this;
+ jQuerySubclass.fn = jQuerySubclass.prototype = this();
+ jQuerySubclass.fn.constructor = jQuerySubclass;
+ jQuerySubclass.subclass = this.subclass;
+ jQuerySubclass.fn.init = function init( selector, context ) {
+ if ( context && context instanceof jQuery && !(context instanceof jQuerySubclass) ) {
+ context = jQuerySubclass(context);
+ }
+
+ return jQuery.fn.init.call( this, selector, context, rootjQuerySubclass );
+ };
+ jQuerySubclass.fn.init.prototype = jQuerySubclass.fn;
+ var rootjQuerySubclass = jQuerySubclass(document);
+ return jQuerySubclass;
+ },
+
browser: {}
});
-// Create readyList deferred
-// also force $.fn.ready to be recognized as a defer
-readyList = jQuery._Deferred();
-jQuery.fn.ready._ = deferredMarker;
-
// Populate the class2type map
jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
class2type[ "[object " + name + "]" ] = name.toLowerCase();
};
}
-// Verify that \s matches non-breaking spaces
-// (IE fails on this test)
-if ( !rwhite.test( "\xA0" ) ) {
+// IE doesn't match non-breaking spaces with \s
+if ( rnotwhite.test( "\xA0" ) ) {
trimLeft = /^[\s\xA0]+/;
trimRight = /[\s\xA0]+$/;
}
jQuery.ready();
}
-// Expose jQuery as an Asynchronous Module
-if ( typeof define === "function" ) {
- define( "jquery", [], function () { return jQuery; } );
-}
-
// Expose jQuery to the global object
-return (window.jQuery = window.$ = jQuery);
+return jQuery;
})();