X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fmanipulation.js;h=b026672df7e789acfc103757d90f0cdccd027d3d;hb=0c4418bbc4022ed5cb746af364ff1c0d7c331209;hp=ce9b917916b83b276f898c37b6e6f151d7604290;hpb=9476530fa1e39ce1324e98f4dbb79576895372e5;p=jquery.git diff --git a/src/manipulation.js b/src/manipulation.js index ce9b917..b026672 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 ( 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 ( scripts ) + if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 ) { + 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 ) { + 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 ); + } + + 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", @@ -186,8 +219,7 @@ jQuery.each({ remove: function( selector ) { if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) { if ( this.nodeType === 1 ) { - cleanData( this.getElementsByTagName("*") ); - cleanData( [this] ); + cleanData( jQuery("*", this).add(this) ); } if ( this.parentNode ) { @@ -199,7 +231,7 @@ jQuery.each({ empty: function() { // Remove element nodes and prevent memory leaks if ( this.nodeType === 1 ) { - cleanData( this.getElementsByTagName("*") ); + cleanData( jQuery("*", this) ); } // Remove any remaining nodes @@ -306,7 +338,7 @@ jQuery.extend({ // IE completely kills leading whitespace when innerHTML is used if ( !jQuery.support.leadingWhitespace && /^\s/.test( elem ) ) div.insertBefore( context.createTextNode( elem.match(/^\s*/)[0] ), div.firstChild ); - + elem = jQuery.makeArray( div.childNodes ); } @@ -327,7 +359,7 @@ jQuery.extend({ fragment.appendChild( ret[i] ); } } - + return scripts; }