Improve replaceWith #1038

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+9 −23
Split
View
@@ -273,38 +273,24 @@ jQuery.fn.extend({
var self = this,
isFunc = jQuery.isFunction( value );
- // Make sure that the elements are removed from the DOM before they are inserted
- // this can help fix replacing a parent with child elements
- if ( !isFunc && typeof value !== "string" ) {
- value = jQuery( value ).detach();
- }
-
- this.each( function( i ) {
- var next = this.nextSibling,
- parent = this.parentNode,
- // HTML argument replaced by "this" element
- // 1. There were no supporting tests
- // 2. There was no internal code relying on this
- // 3. There was no documentation of an html argument
+ return this.each(function( i ) {
+ var parsed,
val = !isFunc ? value : value.call( this, i, this );
if ( isDisconnected( this ) ) {
// for disconnected elements, we replace with the new content in the set. We use
// clone here to ensure that each replaced instance is unique
- self[ i ] = jQuery( val ).clone()[ 0 ];
- return;
+ return self[ i ] = jQuery( val ).clone()[ 0 ];
@rwaldron

rwaldron Nov 20, 2012

Member

Why this changed behaviour? There are no tests that support a newly appointed return value...

@markelog

markelog Nov 20, 2012

Member

This behaviour isn't changed, look closely

@rwaldron

rwaldron Nov 21, 2012

Member

Ugh. Github's diff doesn't show enough lines to give context to the change. Sorry for the noise.

}
- jQuery( this ).remove();
-
- if ( next ) {
- jQuery( next ).before( val );
- } else {
- jQuery( parent ).append( val );
+ if ( !val || !val.nodeType ) {
+ parsed = jQuery.buildFragment( [ val ], this );
+ val = parsed.cacheable ? jQuery.clone( parsed.fragment ) : parsed.fragment;
}
- });
- return this;
+ jQuery.cleanData( getAll( this ) );
+ this.parentNode.replaceChild( val, this );
+ });
},
detach: function( selector ) {