]> source.dussan.org Git - jquery.git/commitdiff
Fixes #11338, .replaceWith should work on detached nodes.
authorOleg <markelog@gmail.com>
Fri, 30 Mar 2012 15:56:11 +0000 (19:56 +0400)
committerDave Methvin <dave.methvin@gmail.com>
Thu, 5 Apr 2012 16:53:36 +0000 (12:53 -0400)
src/manipulation.js
test/unit/manipulation.js

index 7a1cc3b173c0c77da5e2f233e03734414c5dc553..43bf37746194b0c2a891ee6e7eb99b0628f3a97f 100644 (file)
@@ -46,7 +46,7 @@ wrapMap.optgroup = wrapMap.option;
 wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
 wrapMap.th = wrapMap.td;
 
-// IE6-8 can't serialize link, script, style, or any html5 (NoScope) tags, 
+// IE6-8 can't serialize link, script, style, or any html5 (NoScope) tags,
 // unless wrapped in a div with non-breaking characters in front of it.
 if ( !jQuery.support.htmlSerialize ) {
        wrapMap._default = [ 1, "X<div>", "</div>" ];
@@ -222,7 +222,7 @@ jQuery.fn.extend({
                        }
 
                        // See if we can take a shortcut and just use innerHTML
-                       if ( typeof value === "string" && !rnoInnerhtml.test( value ) && 
+                       if ( typeof value === "string" && !rnoInnerhtml.test( value ) &&
                                ( jQuery.support.htmlSerialize || !rnoshimcache.test( value )  ) &&
                                ( jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value ) ) &&
                                !wrapMap[ ( rtagName.exec( value ) || ["", ""] )[1].toLowerCase() ] ) {
@@ -252,7 +252,7 @@ jQuery.fn.extend({
        },
 
        replaceWith: function( value ) {
-               if ( this[0] && this[0].parentNode ) {
+               if ( this[0] && this[0].parentNode && this[0].parentNode.nodeType != 11 ) {
                        // 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 ( jQuery.isFunction( value ) ) {
@@ -278,11 +278,11 @@ jQuery.fn.extend({
                                        jQuery(parent).append( value );
                                }
                        });
-               } else {
-                       return this.length ?
-                               this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value ) :
-                               this;
                }
+
+               return this.length ?
+                       this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value ) :
+                       this;
        },
 
        detach: function( selector ) {
index 8b891f7b5ab3de28aa8a6ddd987b8129269a7dd7..6776795911ae612827bee9a5e27d52bd8ccf4111 100644 (file)
@@ -915,7 +915,7 @@ test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {
 });
 
 var testReplaceWith = function(val) {
-       expect(21);
+       expect(22);
        jQuery("#yahoo").replaceWith(val( "<b id='replace'>buga</b>" ));
        ok( jQuery("#replace")[0], "Replace element with string" );
        ok( !jQuery("#yahoo")[0], "Verify that original element is gone, after string" );
@@ -976,6 +976,9 @@ var testReplaceWith = function(val) {
        equal( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
        equal( set.length, 1, "Replace the disconnected node." );
 
+       // #11338
+       ok( jQuery("<div>1</div>").replaceWith( val("<span/>") ).is("span"), "#11338, Make sure disconnected node with content is replaced");
+
        var non_existant = jQuery("#does-not-exist").replaceWith( val("<b>should not throw an error</b>") );
        equal( non_existant.length, 0, "Length of non existant element." );
 
@@ -1006,7 +1009,7 @@ test("replaceWith(String|Element|Array&lt;Element&gt;|jQuery)", function() {
 test("replaceWith(Function)", function() {
        testReplaceWith(functionReturningObj);
 
-       expect(22);
+       expect(23);
 
        var y = jQuery("#yahoo")[0];