Optimize for the case where a fragment-bound element is being injected into one other element.

This commit is contained in:
jeresig 2010-01-28 16:30:37 -05:00
parent 98c7248518
commit 0db207da23
2 changed files with 20 additions and 11 deletions

View file

@ -100,6 +100,8 @@ jQuery.fn = jQuery.prototype = {
selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes; selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
} }
return jQuery.merge( this, selector );
// HANDLE: $("#id") // HANDLE: $("#id")
} else { } else {
elem = document.getElementById( match[2] ); elem = document.getElementById( match[2] );

View file

@ -415,13 +415,20 @@ jQuery.each({
}, function( name, original ) { }, function( name, original ) {
jQuery.fn[ name ] = function( selector ) { jQuery.fn[ name ] = function( selector ) {
var ret = [], insert = jQuery( selector ); var ret = [], insert = jQuery( selector );
for ( var i = 0, l = insert.length; i < l; i++ ) { if ( this.length === 1 && this[0].parentNode && this[0].parentNode.nodeType === 11 && insert.length === 1 ) {
var elems = (i > 0 ? this.clone(true) : this).get(); insert[ original ]( this[0] );
jQuery.fn[ original ].apply( jQuery(insert[i]), elems ); return this;
ret = ret.concat( elems );
} else {
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, insert.selector );
} }
return this.pushStack( ret, name, insert.selector );
}; };
}); });
@ -436,13 +443,13 @@ jQuery.extend({
var ret = []; var ret = [];
jQuery.each(elems, function( i, elem ) { for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
if ( typeof elem === "number" ) { if ( typeof elem === "number" ) {
elem += ""; elem += "";
} }
if ( !elem ) { if ( !elem ) {
return; continue;
} }
// Convert html string into DOM nodes // Convert html string into DOM nodes
@ -493,7 +500,7 @@ jQuery.extend({
div.insertBefore( context.createTextNode( rleadingWhitespace.exec(elem)[0] ), div.firstChild ); div.insertBefore( context.createTextNode( rleadingWhitespace.exec(elem)[0] ), div.firstChild );
} }
elem = jQuery.makeArray( div.childNodes ); elem = div.childNodes;
} }
if ( elem.nodeType ) { if ( elem.nodeType ) {
@ -501,13 +508,13 @@ jQuery.extend({
} else { } else {
ret = jQuery.merge( ret, elem ); ret = jQuery.merge( ret, elem );
} }
}
});
if ( fragment ) { if ( fragment ) {
for ( var i = 0; ret[i]; i++ ) { for ( var i = 0; ret[i]; i++ ) {
if ( scripts && jQuery.nodeName( ret[i], "script" ) && (!ret[i].type || ret[i].type.toLowerCase() === "text/javascript") ) { if ( scripts && jQuery.nodeName( ret[i], "script" ) && (!ret[i].type || ret[i].type.toLowerCase() === "text/javascript") ) {
scripts.push( ret[i].parentNode ? ret[i].parentNode.removeChild( ret[i] ) : ret[i] ); scripts.push( ret[i].parentNode ? ret[i].parentNode.removeChild( ret[i] ) : ret[i] );
} else { } else {
if ( ret[i].nodeType === 1 ) { if ( ret[i].nodeType === 1 ) {
ret.splice.apply( ret, [i + 1, 0].concat(jQuery.makeArray(ret[i].getElementsByTagName("script"))) ); ret.splice.apply( ret, [i + 1, 0].concat(jQuery.makeArray(ret[i].getElementsByTagName("script"))) );