Added caching to domManip. Fixes #4883.

This commit is contained in:
John Resig 2009-07-11 13:49:46 +00:00
parent fd2a2fed84
commit 8db967e9d5

View File

@ -137,24 +137,55 @@ jQuery.fn.extend({
}, },
domManip: function( args, table, callback ) { domManip: function( args, table, callback ) {
var fragment, scripts, cacheable, cached, cacheresults, first;
if ( this[0] ) { if ( this[0] ) {
var fragment = (this[0].ownerDocument || this[0]).createDocumentFragment(), if ( args.length === 1 && typeof args[0] === "string" ) {
scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment ), cacheable = true;
first = fragment.firstChild; cacheresults = jQuery.fragments[ args[0] ];
if ( cacheresults ) {
if ( cacheresults !== 1 ) {
fragment = cacheresults;
}
cached = true;
}
}
if ( first ) if ( !fragment ) {
for ( var i = 0, l = this.length; i < l; i++ ) fragment = (this[0].ownerDocument || this[0]).createDocumentFragment();
callback.call( root(this[i], first), this.length > 1 || i > 0 ? scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment );
fragment.cloneNode(true) : fragment ); }
if ( scripts ) first = fragment.firstChild;
if ( first ) {
table = table && jQuery.nodeName( first, "tr" );
for ( var i = 0, l = this.length; i < l; i++ ) {
callback.call(
table ?
root(this[i], first) :
this[i],
cacheable || this.length > 1 || i > 0 ?
fragment.cloneNode(true) :
fragment
);
}
}
if ( scripts ) {
jQuery.each( scripts, evalScript ); jQuery.each( scripts, evalScript );
}
if ( cacheable ) {
jQuery.fragments[ args[0] ] = cacheresults ? fragment : 1;
}
} }
return this; return this;
function root( elem, cur ) { function root( elem, cur ) {
return table && jQuery.nodeName(elem, "table") && jQuery.nodeName(cur, "tr") ? return jQuery.nodeName(elem, "table") ?
(elem.getElementsByTagName("tbody")[0] || (elem.getElementsByTagName("tbody")[0] ||
elem.appendChild(elem.ownerDocument.createElement("tbody"))) : elem.appendChild(elem.ownerDocument.createElement("tbody"))) :
elem; elem;
@ -162,6 +193,8 @@ jQuery.fn.extend({
} }
}); });
jQuery.fragments = {};
jQuery.each({ jQuery.each({
appendTo: "append", appendTo: "append",
prependTo: "prepend", prependTo: "prepend",