Reduced the number of function calls required for .find() (single element root),...
[jquery.git] / src / core.js
index c7d10f3..a67b5f0 100644 (file)
@@ -22,7 +22,16 @@ var
 jQuery.fn = jQuery.prototype = {
        init: function( selector, context ) {
                // Make sure that a selection was provided
-               selector = selector || document;
+               if ( selector === undefined ) {
+                       selector = document;
+               }
+
+               // Handle "", null
+               if ( !selector ) {
+                       this.length = 0;
+                       this.context = document;
+                       return this;
+               }
 
                // Handle $(DOMElement)
                if ( selector.nodeType ) {
@@ -31,6 +40,7 @@ jQuery.fn = jQuery.prototype = {
                        this.context = selector;
                        return this;
                }
+
                // Handle HTML strings
                if ( typeof selector === "string" ) {
                        // Are we dealing with HTML string or an ID?
@@ -40,17 +50,18 @@ jQuery.fn = jQuery.prototype = {
                        if ( match && (match[1] || !context) ) {
 
                                // HANDLE: $(html) -> $(array)
-                               if ( match[1] )
+                               if ( match[1] ) {
                                        selector = jQuery.clean( [ match[1] ], context );
 
                                // HANDLE: $("#id")
-                               else {
+                               } else {
                                        var elem = document.getElementById( match[3] );
 
                                        // Handle the case where IE and Opera return items
                                        // by name instead of ID
-                                       if ( elem && elem.id != match[3] )
+                                       if ( elem && elem.id != match[3] ) {
                                                return jQuery().find( selector );
+                                       }
 
                                        // Otherwise, we inject the element directly into the jQuery object
                                        var ret = jQuery( elem || [] );
@@ -61,13 +72,21 @@ jQuery.fn = jQuery.prototype = {
 
                        // HANDLE: $(expr, [context])
                        // (which is just equivalent to: $(content).find(expr)
-                       } else
-                               return jQuery( context ).find( selector );
+                       } else if ( !context || context.nodeType ) {
+                               this[0] = context || document;
+                               this.length = 1;
+                               this.context = context;
+                               return this.find( selector );
+
+                       } else {
+                               return (context.jquery ? context : jQuery( context )).find( selector );
+                       }
 
                // HANDLE: $(function)
                // Shortcut for document ready
-               } else if ( jQuery.isFunction( selector ) )
+               } else if ( jQuery.isFunction( selector ) ) {
                        return jQuery( document ).ready( selector );
+               }
 
                // Make sure that old selector state is passed along
                if ( selector.selector && selector.context ) {
@@ -274,8 +293,7 @@ jQuery.fn = jQuery.prototype = {
 
        find: function( selector ) {
                if ( this.length === 1 ) {
-                       var ret = this.pushStack( [], "find", selector );
-                       ret.length = 0;
+                       var ret = this.pushStack( "", "find", selector );
                        jQuery.find( selector, this[0], ret );
                        return ret;
                } else {
@@ -502,13 +520,13 @@ jQuery.fn = jQuery.prototype = {
                if ( this[0] ) {
                        var fragment = (this[0].ownerDocument || this[0]).createDocumentFragment(),
                                scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment ),
-                               first = fragment.firstChild,
-                               extra = this.length > 1 ? fragment.cloneNode(true) : fragment;
+                               first = fragment.firstChild;
 
                        if ( first )
                                for ( var i = 0, l = this.length; i < l; i++ )
-                                       callback.call( root(this[i], first), i > 0 ? extra.cloneNode(true) : fragment );
-                       
+                                       callback.call( root(this[i], first), this.length > 1 || i > 0 ?
+                                                       fragment.cloneNode(true) : fragment );
+               
                        if ( scripts )
                                jQuery.each( scripts, evalScript );
                }
@@ -1189,13 +1207,16 @@ jQuery.each({
        insertAfter: "after",
        replaceAll: "replaceWith"
 }, function(name, original){
-       jQuery.fn[ name ] = function() {
-               var args = arguments;
+       jQuery.fn[ name ] = function( selector ) {
+               var ret = [], insert = jQuery( selector );
 
-               return this.each(function(){
-                       for ( var i = 0, length = args.length; i < length; i++ )
-                               jQuery( args[ i ] )[ original ]( this );
-               });
+               for ( var i = 0, l = insert.length; i < l; i++ ) {
+                       var elems = (i > 0 ? this.clone(true) : this).get();
+                       jQuery.fn[ original ].apply( jQuery(insert[i]), elems );
+                       ret = ret.concat( elems );
+               }
+
+               return this.pushStack( ret, name, selector );
        };
 });
 
@@ -1221,24 +1242,28 @@ jQuery.each({
        },
 
        remove: function( selector ) {
-               if ( !selector || jQuery.filter( selector, [ this ] ).length ) {
-                       // Prevent memory leaks
-                       jQuery( "*", this ).add([this]).each(function(){
-                               jQuery.event.remove(this);
-                               jQuery.removeData(this);
-                       });
-                       if (this.parentNode)
+               if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) {
+                       if ( this.nodeType === 1 ) {
+                               cleanData( this.getElementsByTagName("*") );
+                               cleanData( [this] );
+                       }
+
+                       if ( this.parentNode ) {
                                this.parentNode.removeChild( this );
+                       }
                }
        },
 
        empty: function() {
                // Remove element nodes and prevent memory leaks
-               jQuery(this).children().remove();
+               if ( this.nodeType === 1 ) {
+                       cleanData( this.getElementsByTagName("*") );
+               }
 
                // Remove any remaining nodes
-               while ( this.firstChild )
+               while ( this.firstChild ) {
                        this.removeChild( this.firstChild );
+               }
        }
 }, function(name, fn){
        jQuery.fn[ name ] = function(){
@@ -1246,6 +1271,15 @@ jQuery.each({
        };
 });
 
+function cleanData( elems ) {
+       for ( var i = 0, l = elems.length; i < l; i++ ) {
+               var id = elems[i][expando];
+               if ( id ) {
+                       delete jQuery.cache[ id ];
+               }
+       }
+}
+
 // Helper function used by the dimensions and offset modules
 function num(elem, prop) {
        return elem[0] && parseInt( jQuery.curCSS(elem[0], prop, true), 10 ) || 0;