]> source.dussan.org Git - jquery.git/commitdiff
Fix #12347 and #12384. Use a fresh div in jQuery.clean each time.
authorDave Methvin <dave.methvin@gmail.com>
Tue, 28 Aug 2012 21:00:12 +0000 (17:00 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Tue, 28 Aug 2012 21:02:01 +0000 (17:02 -0400)
Regression was introduced in 22ad8723ce07569a9b039c7901f29e86ad14523c most likely because the clown who fixed http://bugs.jquery.com/ticket/4011 didn't add a unit test.

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

index f87e3ca94b309cbdc4310b21a6e3ee1b72df9295..aced4567b9c08983c003243411ec0dcc2a29b4e0 100644 (file)
@@ -638,8 +638,8 @@ jQuery.extend({
        },
 
        clean: function( elems, context, fragment, scripts ) {
-               var j, safe, elem, tag, wrap, depth, div, hasBody, tbody, len, handleScript, jsTags,
-                       i = 0,
+               var i, j, elem, tag, wrap, depth, div, hasBody, tbody, len, handleScript, jsTags,
+                       safe = context === document && safeFragment,
                        ret = [];
 
                // Ensure that context is a document
@@ -648,7 +648,7 @@ jQuery.extend({
                }
 
                // Use the already-created safe fragment if context permits
-               for ( safe = context === document && safeFragment; (elem = elems[i]) != null; i++ ) {
+               for ( i = 0; (elem = elems[i]) != null; i++ ) {
                        if ( typeof elem === "number" ) {
                                elem += "";
                        }
@@ -664,7 +664,8 @@ jQuery.extend({
                                } else {
                                        // Ensure a safe container in which to render the html
                                        safe = safe || createSafeFragment( context );
-                                       div = div || safe.appendChild( context.createElement("div") );
+                                       div = context.createElement("div");
+                                       safe.appendChild( div );
 
                                        // Fix "XHTML"-style tags in all browsers
                                        elem = elem.replace(rxhtmlTag, "<$1></$2>");
@@ -707,21 +708,20 @@ jQuery.extend({
 
                                        elem = div.childNodes;
 
-                                       // Remember the top-level container for proper cleanup
-                                       div = safe.lastChild;
+                                       // Take out of fragment container (we need a fresh div each time)
+                                       div.parentNode.removeChild( div );
                                }
                        }
 
                        if ( elem.nodeType ) {
                                ret.push( elem );
                        } else {
-                               ret = jQuery.merge( ret, elem );
+                               jQuery.merge( ret, elem );
                        }
                }
 
                // Fix #11356: Clear elements from safeFragment
                if ( div ) {
-                       safe.removeChild( div );
                        elem = div = safe = null;
                }
 
index d63b3ff86b7a9b12088bb07baaf987585f97ecde..45550e98768282e33654219af84884cbfae48a98 100644 (file)
@@ -1934,6 +1934,20 @@ test("checked state is cloned with clone()", function(){
        equal( jQuery(elem).clone().attr("id","clone")[0].checked, true, "Checked true state correctly cloned" );
 });
 
+test("manipulate mixed jQuery and text (#12384, #12346)", function() {
+       expect(2);
+
+       var div = jQuery("<div>a</div>").append( "&nbsp;", jQuery("<span>b</span>"), "&nbsp;", jQuery("<span>c</span>") ),
+               nbsp = String.fromCharCode(160);
+       equal( div.text(), "a" + nbsp + "b" + nbsp+ "c", "Appending mixed jQuery with text nodes" );
+
+       div = jQuery("<div><div></div></div>")
+               .find("div")
+               .after("<p>a</p>", "<p>b</p>" )
+               .parent();
+       equal( div.find("*").length, 3, "added 2 paragraphs after inner div" );
+});
+
 testIframeWithCallback( "buildFragment works even if document[0] is iframe's window object in IE9/10 (#12266)", "manipulation/iframe-denied.html", function( test ) {
        expect( 1 );