@@ -201,7 +201,7 @@ jQuery.extend({
201201 return clone ;
202202 } ,
203203
204- buildFragment : function ( elems , context , scripts , selection ) {
204+ buildFragment : function ( elems , context , scripts , selection , ignored ) {
205205 var elem , tmp , tag , wrap , contains , j ,
206206 fragment = context . createDocumentFragment ( ) ,
207207 nodes = [ ] ,
@@ -257,9 +257,11 @@ jQuery.extend({
257257 i = 0 ;
258258 while ( ( elem = nodes [ i ++ ] ) ) {
259259
260- // #4087 - If origin and destination elements are the same, and this is
261- // that element, do not do anything
260+ // Skip elements already in the context collection (trac-4087)
262261 if ( selection && jQuery . inArray ( elem , selection ) > - 1 ) {
262+ if ( ignored ) {
263+ ignored . push ( elem ) ;
264+ }
263265 continue ;
264266 }
265267
@@ -446,28 +448,28 @@ jQuery.fn.extend({
446448 } ,
447449
448450 replaceWith : function ( ) {
449- var arg = arguments [ 0 ] ;
450-
451- // Make the changes, replacing each context element with the new content
452- this . domManip ( arguments , function ( elem ) {
453- arg = this . parentNode ;
451+ var ignored = [ ] ;
454452
455- jQuery . cleanData ( getAll ( this ) ) ;
453+ // Make the changes, replacing each non-ignored context element with the new content
454+ return this . domManip ( arguments , function ( elem ) {
455+ var parent = this . parentNode ;
456456
457- if ( arg ) {
458- arg . replaceChild ( elem , this ) ;
457+ if ( jQuery . inArray ( this , ignored ) < 0 ) {
458+ jQuery . cleanData ( getAll ( this ) ) ;
459+ if ( parent ) {
460+ parent . replaceChild ( elem , this ) ;
461+ }
459462 }
460- } ) ;
461463
462- // Force removal if there was no new content (e.g., from empty arguments)
463- return arg && ( arg . length || arg . nodeType ) ? this : this . remove ( ) ;
464+ // Force callback invocation
465+ } , ignored ) ;
464466 } ,
465467
466468 detach : function ( selector ) {
467469 return this . remove ( selector , true ) ;
468470 } ,
469471
470- domManip : function ( args , callback ) {
472+ domManip : function ( args , callback , ignored ) {
471473
472474 // Flatten any nested arrays
473475 args = concat . apply ( [ ] , args ) ;
@@ -489,19 +491,20 @@ jQuery.fn.extend({
489491 if ( isFunction ) {
490492 args [ 0 ] = value . call ( this , index , self . html ( ) ) ;
491493 }
492- self . domManip ( args , callback ) ;
494+ self . domManip ( args , callback , ignored ) ;
493495 } ) ;
494496 }
495497
496498 if ( l ) {
497- fragment = jQuery . buildFragment ( args , this [ 0 ] . ownerDocument , false , this ) ;
499+ fragment = jQuery . buildFragment ( args , this [ 0 ] . ownerDocument , false , this , ignored ) ;
498500 first = fragment . firstChild ;
499501
500502 if ( fragment . childNodes . length === 1 ) {
501503 fragment = first ;
502504 }
503505
504- if ( first ) {
506+ // Require either new content or an interest in ignored elements to invoke the callback
507+ if ( first || ignored ) {
505508 scripts = jQuery . map ( getAll ( fragment , "script" ) , disableScript ) ;
506509 hasScripts = scripts . length ;
507510
0 commit comments