]> source.dussan.org Git - jquery.git/commitdiff
Simplify replaceWith method. Closes gh-1276
authorOleg <markelog@gmail.com>
Tue, 17 Sep 2013 09:24:27 +0000 (13:24 +0400)
committerOleg <markelog@gmail.com>
Tue, 17 Sep 2013 10:21:34 +0000 (14:21 +0400)
(cherry picked from commit 642e9a45579cfa90861b8ea71a95dd077775caaf)

src/manipulation.js
test/unit/manipulation.js

index b19edfa3aa5a122b2189c51a96a8c9893e4f1e83..ee1a554c01482ee6d7cf1ec9b9672c41cde404b9 100644 (file)
@@ -609,38 +609,28 @@ jQuery.fn.extend({
        },
 
        replaceWith: function() {
-               var
-                       // Snapshot the DOM in case .domManip sweeps something relevant into its fragment
-                       args = jQuery.map( this, function( elem ) {
-                               return [ elem.nextSibling, elem.parentNode ];
-                       }),
-                       i = 0;
+               var arg = arguments[ 0 ];
 
                // Make the changes, replacing each context element with the new content
                this.domManip( arguments, function( elem ) {
-                       var next = args[ i++ ],
-                               parent = args[ i++ ];
+                       arg = this.parentNode;
 
-                       if ( parent ) {
-                               // Don't use the snapshot next if it has moved (#13810)
-                               if ( next && next.parentNode !== parent ) {
-                                       next = this.nextSibling;
-                               }
-                               jQuery( this ).remove();
-                               parent.insertBefore( elem, next );
+                       jQuery.cleanData( getAll( this ) );
+
+                       if ( arg ) {
+                               arg.replaceChild( elem, this );
                        }
-               // Allow new content to include elements from the context set
-               }, true );
+               });
 
                // Force removal if there was no new content (e.g., from empty arguments)
-               return i ? this : this.remove();
+               return arg && (arg.length || arg.nodeType) ? this : this.remove();
        },
 
        detach: function( selector ) {
                return this.remove( selector, true );
        },
 
-       domManip: function( args, callback, allowIntersection ) {
+       domManip: function( args, callback ) {
 
                // Flatten any nested arrays
                args = concat.apply( [], args );
@@ -663,12 +653,12 @@ jQuery.fn.extend({
                                if ( isFunction ) {
                                        args[0] = value.call( this, index, self.html() );
                                }
-                               self.domManip( args, callback, allowIntersection );
+                               self.domManip( args, callback );
                        });
                }
 
                if ( l ) {
-                       fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, !allowIntersection && this );
+                       fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, this );
                        first = fragment.firstChild;
 
                        if ( fragment.childNodes.length === 1 ) {
index f5a5de853dec4162e3a97a1c7c2a370c56ecccf3..6311682caf14b9f29ca210ce53361c4a0784205e 100644 (file)
@@ -1051,20 +1051,21 @@ test( "replaceWith(string) for more than one element", function() {
        equal(jQuery("#foo p").length, 0, "verify that all the three original element have been replaced");
 });
 
-test( "empty replaceWith (#13401; #13596)", 4, function() {
-       expect( 6 );
-
-       var $el = jQuery("<div/>"),
+test( "Empty replaceWith (#13401; #13596)", 8, function() {
+       var $el = jQuery( "<div/>" ),
                tests = {
                        "empty string": "",
                        "empty array": [],
-                       "empty collection": jQuery("#nonexistent")
+                       "empty collection": jQuery( "#nonexistent" ),
+
+                        // in case of jQuery(...).replaceWith();
+                       "empty undefined": undefined
                };
 
        jQuery.each( tests, function( label, input ) {
-               $el.html("<a/>").children().replaceWith( input );
+               $el.html( "<a/>" ).children().replaceWith( input );
                strictEqual( $el.html(), "", "replaceWith(" + label + ")" );
-               $el.html("<b/>").children().replaceWith(function() { return input; });
+               $el.html( "<b/>" ).children().replaceWith(function() { return input; });
                strictEqual( $el.html(), "", "replaceWith(function returning " + label + ")" );
        });
 });