Skip to content

Commit 726fda0

Browse files
committed
Make sure the fragment isn't used if it's not the same set we're working with. Fixes #6068.
1 parent a7dc66b commit 726fda0

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/manipulation.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ jQuery.fn.extend({
300300
},
301301

302302
domManip: function( args, table, callback ) {
303-
var results, first, value = args[0], scripts = [], fragment;
303+
var results, first, value = args[0], scripts = [], fragment, parent;
304304

305305
// We can't cloneNode fragments that contain checked, in WebKit
306306
if ( !jQuery.support.checkClone && arguments.length === 3 && typeof value === "string" && rchecked.test( value ) ) {
@@ -318,9 +318,12 @@ jQuery.fn.extend({
318318
}
319319

320320
if ( this[0] ) {
321+
parent = value && value.parentNode;
322+
321323
// If we're in a fragment, just use that instead of building a new one
322-
if ( args[0] && args[0].parentNode && args[0].parentNode.nodeType === 11 ) {
323-
results = { fragment: args[0].parentNode };
324+
if ( parent && parent.nodeType === 11 && parent.childNodes.length === this.length ) {
325+
results = { fragment: parent };
326+
324327
} else {
325328
results = buildFragment( args, this, scripts );
326329
}
@@ -429,9 +432,10 @@ jQuery.each({
429432
replaceAll: "replaceWith"
430433
}, function( name, original ) {
431434
jQuery.fn[ name ] = function( selector ) {
432-
var ret = [], insert = jQuery( selector );
435+
var ret = [], insert = jQuery( selector ),
436+
parent = this.length === 1 && this[0].parentNode;
433437

434-
if ( this.length === 1 && this[0].parentNode && this[0].parentNode.nodeType === 11 && insert.length === 1 ) {
438+
if ( parent && parent.nodeType === 11 && parent.childNodes.length === 1 && insert.length === 1 ) {
435439
insert[ original ]( this[0] );
436440
return this;
437441

test/unit/manipulation.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ test("append(Function) with incoming value", function() {
376376
});
377377

378378
test("appendTo(String|Element|Array<Element>|jQuery)", function() {
379-
expect(12);
379+
expect(13);
380380
var defaultText = 'Try them out:'
381381
jQuery('<b>buga</b>').appendTo('#first');
382382
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() {
424424
ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
425425

426426
reset();
427+
428+
div = jQuery("<div/>");
429+
jQuery("<span>a</span><b>b</b>").filter("span").appendTo( div );
430+
431+
equals( div.children().length, 1, "Make sure the right number of children were inserted." );
427432
});
428433

429434
var testPrepend = function(val) {

0 commit comments

Comments
 (0)