Skip to content
Permalink
Browse files

Added caching to domManip. Fixes #4883.

  • Loading branch information
jeresig committed Jul 11, 2009
1 parent fd2a2fe commit 8db967e9d52407c8e76d81b9d472800667f6fa29
Showing with 42 additions and 9 deletions.
  1. +42 −9 src/manipulation.js
@@ -137,31 +137,64 @@ jQuery.fn.extend({
},

domManip: function( args, table, callback ) {
var fragment, scripts, cacheable, cached, cacheresults, first;

if ( this[0] ) {
var fragment = (this[0].ownerDocument || this[0]).createDocumentFragment(),
scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment ),
first = fragment.firstChild;
if ( args.length === 1 && typeof args[0] === "string" ) {
cacheable = true;
cacheresults = jQuery.fragments[ args[0] ];
if ( cacheresults ) {
if ( cacheresults !== 1 ) {
fragment = cacheresults;
}
cached = true;
}
}

if ( !fragment ) {
fragment = (this[0].ownerDocument || this[0]).createDocumentFragment();
scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment );
}

first = fragment.firstChild;

if ( first )
for ( var i = 0, l = this.length; i < l; i++ )
callback.call( root(this[i], first), this.length > 1 || i > 0 ?
fragment.cloneNode(true) : fragment );
if ( first ) {
table = table && jQuery.nodeName( first, "tr" );

if ( scripts )
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 );
}

if ( cacheable ) {
jQuery.fragments[ args[0] ] = cacheresults ? fragment : 1;
}
}

return this;

function root( elem, cur ) {
return table && jQuery.nodeName(elem, "table") && jQuery.nodeName(cur, "tr") ?
return jQuery.nodeName(elem, "table") ?
(elem.getElementsByTagName("tbody")[0] ||
elem.appendChild(elem.ownerDocument.createElement("tbody"))) :
elem;
}
}
});

jQuery.fragments = {};

jQuery.each({
appendTo: "append",
prependTo: "prepend",

0 comments on commit 8db967e

Please sign in to comment.
You can’t perform that action at this time.