aboutsummaryrefslogtreecommitdiffstats
path: root/src/manipulation.js
diff options
context:
space:
mode:
authorJohn Resig <jeresig@gmail.com>2009-07-11 13:49:46 +0000
committerJohn Resig <jeresig@gmail.com>2009-07-11 13:49:46 +0000
commit8db967e9d52407c8e76d81b9d472800667f6fa29 (patch)
tree1d012acf24afef6fe74e87ba5ea2c81850278d74 /src/manipulation.js
parentfd2a2fed84b6c32e24a050356a8f422310bae927 (diff)
downloadjquery-8db967e9d52407c8e76d81b9d472800667f6fa29.tar.gz
jquery-8db967e9d52407c8e76d81b9d472800667f6fa29.zip
Added caching to domManip. Fixes #4883.
Diffstat (limited to 'src/manipulation.js')
-rw-r--r--src/manipulation.js51
1 files changed, 42 insertions, 9 deletions
diff --git a/src/manipulation.js b/src/manipulation.js
index 13983fb01..62009be20 100644
--- a/src/manipulation.js
+++ b/src/manipulation.js
@@ -137,24 +137,55 @@ 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;
@@ -162,6 +193,8 @@ jQuery.fn.extend({
}
});
+jQuery.fragments = {};
+
jQuery.each({
appendTo: "append",
prependTo: "prepend",