Make sure fragment caching is only happening on nodes in the main document. Fixes...
[jquery.git] / src / manipulation.js
index 01c6b0b..88da6de 100644 (file)
@@ -296,7 +296,7 @@ jQuery.fn.extend({
        },
 
        domManip: function( args, table, callback ) {
-               var results, first, value = args[0], scripts = [];
+               var results, first, value = args[0], scripts = [], fragment;
 
                // We can't cloneNode fragments that contain checked, in WebKit
                if ( !jQuery.support.checkClone && arguments.length === 3 && typeof value === "string" && rchecked.test( value ) ) {
@@ -320,8 +320,14 @@ jQuery.fn.extend({
                        } else {
                                results = buildFragment( args, this, scripts );
                        }
-
-                       first = results.fragment.firstChild;
+                       
+                       fragment = results.fragment;
+                       
+                       if ( fragment.childNodes.length === 1 ) {
+                               first = fragment = fragment.firstChild;
+                       } else {
+                               first = fragment.firstChild;
+                       }
 
                        if ( first ) {
                                table = table && jQuery.nodeName( first, "tr" );
@@ -331,14 +337,14 @@ jQuery.fn.extend({
                                                table ?
                                                        root(this[i], first) :
                                                        this[i],
-                                               results.cacheable || this.length > 1 || i > 0 ?
-                                                       results.fragment.cloneNode(true) :
-                                                       results.fragment
+                                               i > 0 || results.cacheable || this.length > 1  ?
+                                                       fragment.cloneNode(true) :
+                                                       fragment
                                        );
                                }
                        }
 
-                       if ( scripts ) {
+                       if ( scripts.length ) {
                                jQuery.each( scripts, evalScript );
                        }
                }
@@ -378,10 +384,15 @@ function cloneCopyEvent(orig, ret) {
 }
 
 function buildFragment( args, nodes, scripts ) {
-       var fragment, cacheable, cacheresults, doc;
+       var fragment, cacheable, cacheresults,
+               doc = (nodes && nodes[0] ? nodes[0].ownerDocument || nodes[0] : document);
+
+       // Only cache "small" (1/2 KB) strings that are associated with the main document
+       // Cloning options loses the selected state, so don't cache them
+       // Also, WebKit does not clone 'checked' attributes on cloneNode, so don't cache
+       if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && doc === document &&
+               args[0].indexOf("<option") < 0 && (jQuery.support.checkClone || !rchecked.test( args[0] )) ) {
 
-       // webkit does not clone 'checked' attribute of radio inputs on cloneNode, so don't cache if string has a checked
-       if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 && (jQuery.support.checkClone || !rchecked.test( args[0] )) ) {
                cacheable = true;
                cacheresults = jQuery.fragments[ args[0] ];
                if ( cacheresults ) {
@@ -392,7 +403,6 @@ function buildFragment( args, nodes, scripts ) {
        }
 
        if ( !fragment ) {
-               doc = (nodes && nodes[0] ? nodes[0].ownerDocument || nodes[0] : document);
                fragment = doc.createDocumentFragment();
                jQuery.clean( args, doc, fragment, scripts );
        }