@@ -274,14 +274,13 @@ jQuery.extend({
274274 return clone ;
275275 } ,
276276
277- buildFragment : function ( elems , context , scripts , selection ) {
277+ buildFragment : function ( elems , context , scripts , selection , ignored ) {
278278 var j , elem , contains ,
279279 tmp , tag , wrap ,
280280 l = elems . length ,
281281
282282 // Ensure a safe fragment
283283 safe = createSafeFragment ( context ) ,
284-
285284 nodes = [ ] ,
286285 i = 0 ;
287286
@@ -343,9 +342,11 @@ jQuery.extend({
343342 i = 0 ;
344343 while ( ( elem = nodes [ i ++ ] ) ) {
345344
346- // #4087 - If origin and destination elements are the same, and this is
347- // that element, do not do anything
345+ // Skip elements already in the context collection (trac-4087)
348346 if ( selection && jQuery . inArray ( elem , selection ) > - 1 ) {
347+ if ( ignored ) {
348+ ignored . push ( elem ) ;
349+ }
349350 continue ;
350351 }
351352
@@ -573,28 +574,28 @@ jQuery.fn.extend({
573574 } ,
574575
575576 replaceWith : function ( ) {
576- var arg = arguments [ 0 ] ;
577-
578- // Make the changes, replacing each context element with the new content
579- this . domManip ( arguments , function ( elem ) {
580- arg = this . parentNode ;
577+ var ignored = [ ] ;
581578
582- jQuery . cleanData ( getAll ( this ) ) ;
579+ // Make the changes, replacing each non-ignored context element with the new content
580+ return this . domManip ( arguments , function ( elem ) {
581+ var parent = this . parentNode ;
583582
584- if ( arg ) {
585- arg . replaceChild ( elem , this ) ;
583+ if ( jQuery . inArray ( this , ignored ) < 0 ) {
584+ jQuery . cleanData ( getAll ( this ) ) ;
585+ if ( parent ) {
586+ parent . replaceChild ( elem , this ) ;
587+ }
586588 }
587- } ) ;
588589
589- // Force removal if there was no new content (e.g., from empty arguments)
590- return arg && ( arg . length || arg . nodeType ) ? this : this . remove ( ) ;
590+ // Force callback invocation
591+ } , ignored ) ;
591592 } ,
592593
593594 detach : function ( selector ) {
594595 return this . remove ( selector , true ) ;
595596 } ,
596597
597- domManip : function ( args , callback ) {
598+ domManip : function ( args , callback , ignored ) {
598599
599600 // Flatten any nested arrays
600601 args = concat . apply ( [ ] , args ) ;
@@ -617,19 +618,20 @@ jQuery.fn.extend({
617618 if ( isFunction ) {
618619 args [ 0 ] = value . call ( this , index , self . html ( ) ) ;
619620 }
620- self . domManip ( args , callback ) ;
621+ self . domManip ( args , callback , ignored ) ;
621622 } ) ;
622623 }
623624
624625 if ( l ) {
625- fragment = jQuery . buildFragment ( args , this [ 0 ] . ownerDocument , false , this ) ;
626+ fragment = jQuery . buildFragment ( args , this [ 0 ] . ownerDocument , false , this , ignored ) ;
626627 first = fragment . firstChild ;
627628
628629 if ( fragment . childNodes . length === 1 ) {
629630 fragment = first ;
630631 }
631632
632- if ( first ) {
633+ // Require either new content or an interest in ignored elements to invoke the callback
634+ if ( first || ignored ) {
633635 scripts = jQuery . map ( getAll ( fragment , "script" ) , disableScript ) ;
634636 hasScripts = scripts . length ;
635637
0 commit comments