🌐 AI搜索 & 代理 主页
Skip to content

Commit 8db967e

Browse files
committed
Added caching to domManip. Fixes #4883.
1 parent fd2a2fe commit 8db967e

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

src/manipulation.js

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,64 @@ jQuery.fn.extend({
137137
},
138138

139139
domManip: function( args, table, callback ) {
140+
var fragment, scripts, cacheable, cached, cacheresults, first;
141+
140142
if ( this[0] ) {
141-
var fragment = (this[0].ownerDocument || this[0]).createDocumentFragment(),
142-
scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment ),
143-
first = fragment.firstChild;
143+
if ( args.length === 1 && typeof args[0] === "string" ) {
144+
cacheable = true;
145+
cacheresults = jQuery.fragments[ args[0] ];
146+
if ( cacheresults ) {
147+
if ( cacheresults !== 1 ) {
148+
fragment = cacheresults;
149+
}
150+
cached = true;
151+
}
152+
}
153+
154+
if ( !fragment ) {
155+
fragment = (this[0].ownerDocument || this[0]).createDocumentFragment();
156+
scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment );
157+
}
158+
159+
first = fragment.firstChild;
144160

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

150-
if ( scripts )
164+
for ( var i = 0, l = this.length; i < l; i++ ) {
165+
callback.call(
166+
table ?
167+
root(this[i], first) :
168+
this[i],
169+
cacheable || this.length > 1 || i > 0 ?
170+
fragment.cloneNode(true) :
171+
fragment
172+
);
173+
}
174+
}
175+
176+
if ( scripts ) {
151177
jQuery.each( scripts, evalScript );
178+
}
179+
180+
if ( cacheable ) {
181+
jQuery.fragments[ args[0] ] = cacheresults ? fragment : 1;
182+
}
152183
}
153184

154185
return this;
155186

156187
function root( elem, cur ) {
157-
return table && jQuery.nodeName(elem, "table") && jQuery.nodeName(cur, "tr") ?
188+
return jQuery.nodeName(elem, "table") ?
158189
(elem.getElementsByTagName("tbody")[0] ||
159190
elem.appendChild(elem.ownerDocument.createElement("tbody"))) :
160191
elem;
161192
}
162193
}
163194
});
164195

196+
jQuery.fragments = {};
197+
165198
jQuery.each({
166199
appendTo: "append",
167200
prependTo: "prepend",

0 commit comments

Comments
 (0)