Make sure the fragment isn't used if it's not the same set we're working with. Fixes...
authorjeresig <jeresig@gmail.com>
Sat, 13 Feb 2010 07:49:04 +0000 (02:49 -0500)
committerjeresig <jeresig@gmail.com>
Sat, 13 Feb 2010 07:49:04 +0000 (02:49 -0500)
src/manipulation.js
test/unit/manipulation.js

index b2a9df9..1453f98 100644 (file)
@@ -300,7 +300,7 @@ jQuery.fn.extend({
        },
 
        domManip: function( args, table, callback ) {
-               var results, first, value = args[0], scripts = [], fragment;
+               var results, first, value = args[0], scripts = [], fragment, parent;
 
                // We can't cloneNode fragments that contain checked, in WebKit
                if ( !jQuery.support.checkClone && arguments.length === 3 && typeof value === "string" && rchecked.test( value ) ) {
@@ -318,9 +318,12 @@ jQuery.fn.extend({
                }
 
                if ( this[0] ) {
+                       parent = value && value.parentNode;
+
                        // If we're in a fragment, just use that instead of building a new one
-                       if ( args[0] && args[0].parentNode && args[0].parentNode.nodeType === 11 ) {
-                               results = { fragment: args[0].parentNode };
+                       if ( parent && parent.nodeType === 11 && parent.childNodes.length === this.length ) {
+                               results = { fragment: parent };
+
                        } else {
                                results = buildFragment( args, this, scripts );
                        }
@@ -429,9 +432,10 @@ jQuery.each({
        replaceAll: "replaceWith"
 }, function( name, original ) {
        jQuery.fn[ name ] = function( selector ) {
-               var ret = [], insert = jQuery( selector );
+               var ret = [], insert = jQuery( selector ),
+                       parent = this.length === 1 && this[0].parentNode;
                
-               if ( this.length === 1 && this[0].parentNode && this[0].parentNode.nodeType === 11 && insert.length === 1 ) {
+               if ( parent && parent.nodeType === 11 && parent.childNodes.length === 1 && insert.length === 1 ) {
                        insert[ original ]( this[0] );
                        return this;
                        
index e6e6c8d..2492ca5 100644 (file)
@@ -376,7 +376,7 @@ test("append(Function) with incoming value", function() {
 });
 
 test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
-       expect(12);
+       expect(13);
        var defaultText = 'Try them out:'
        jQuery('<b>buga</b>').appendTo('#first');
        equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
@@ -424,6 +424,11 @@ test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
        ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
 
        reset();
+
+       div = jQuery("<div/>");
+       jQuery("<span>a</span><b>b</b>").filter("span").appendTo( div );
+
+       equals( div.children().length, 1, "Make sure the right number of children were inserted." );
 });
 
 var testPrepend = function(val) {