jQuery = window.jQuery = window.$ = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
- return new jQuery.fn.init( selector, context );
+ return selector === undefined ?
+ rootjQuery :
+ new jQuery.fn.init( selector, context );
},
// A simple way to check for HTML strings or ID strings
jQuery.fn = jQuery.prototype = {
init: function( selector, context ) {
- // Make sure that a selection was provided
- if ( selector === undefined ) {
- selector = document;
- }
-
- // Handle "", null
+ // Handle $("") or $(null)
if ( !selector ) {
this.length = 0;
- this.context = document;
return this;
}
// Handle the case where IE and Opera return items
// by name instead of ID
if ( elem && elem.id != match[3] ) {
- return jQuery().find( selector );
+ return rootjQuery.find( selector );
}
// Otherwise, we inject the element directly into the jQuery object
- var ret = jQuery( elem || [] );
+ var ret = jQuery( elem || null );
ret.context = document;
ret.selector = selector;
return ret;
}
- // HANDLE: $(expr, [context])
- // (which is just equivalent to: $(content).find(expr)
- } else if ( !context || context.nodeType ) {
- this[0] = context || document;
- this.length = 1;
- this.context = context;
- return this.find( selector );
+ // HANDLE: $(expr, $(...))
+ } else if ( !context || context.jquery ) {
+ return (context || rootjQuery).find( selector );
+ // HANDLE: $(expr, context)
+ // (which is just equivalent to: $(content).find(expr)
} else {
- return (context.jquery ? context : jQuery( context )).find( selector );
+ return jQuery( context ).find( selector );
}
// HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) {
- return jQuery( document ).ready( selector );
+ return rootjQuery.ready( selector );
}
// Make sure that old selector state is passed along
// (returning the new matched element set)
pushStack: function( elems, name, selector ) {
// Build a new jQuery matched element set
- var ret = jQuery( elems );
+ var ret = jQuery( elems || null );
// Add the old object onto the stack (as a reference)
ret.prevObject = this;
},
end: function() {
- return this.prevObject || jQuery( [] );
+ return this.prevObject || jQuery(null);
},
- // For internal use only.
- // Behaves like an Array's method, not like a jQuery method.
- push: [].push,
- sort: [].sort,
- splice: [].splice,
-
find: function( selector ) {
- if ( this.length === 1 ) {
- var ret = this.pushStack( "", "find", selector );
- jQuery.find( selector, this[0], ret );
- return ret;
- } else {
- return this.pushStack( jQuery.unique(jQuery.map(this, function(elem){
- return jQuery.find( selector, elem );
- })), "find", selector );
+ var ret = this.pushStack( "", "find", selector ), length = 0,
+ splice = Array.prototype.splice;
+
+ for ( var i = 0, l = this.length; i < l; i++ ) {
+ length = ret.length;
+ jQuery.find( selector, this[i], ret );
+
+ if ( i > 0 ) {
+ // Make sure that the results are unique
+ for ( var n = length; n < ret.length; n++ ) {
+ for ( var r = 0; r < length; r++ ) {
+ if ( ret[r] === ret[n] ) {
+ splice.call(ret, n--, 1);
+ break;
+ }
+ }
+ }
+ }
}
+
+ return ret;
},
clone: function( events ) {
}
});
+// All jQuery objects should point back to these
+var rootjQuery = jQuery(document);
+
// Use of jQuery.browser is deprecated.
// It's included for backwards compatibility and plugins,
// although they should work to migrate away.