]> source.dussan.org Git - jquery.git/commitdiff
Make sure that dynamically-created elements don't have a parent. Fixes #5638.
authorJohn Resig <jeresig@gmail.com>
Fri, 11 Dec 2009 01:01:04 +0000 (17:01 -0800)
committerJohn Resig <jeresig@gmail.com>
Fri, 11 Dec 2009 01:01:04 +0000 (17:01 -0800)
src/core.js
test/unit/core.js

index 756117c091db84c5e20da26954195653ce3eb751..7be151217ea34ddbf568faf7aacd10a0ba5a2d3c 100644 (file)
@@ -50,7 +50,7 @@ var jQuery = function( selector, context ) {
 
 jQuery.fn = jQuery.prototype = {
        init: function( selector, context ) {
-               var match, elem, ret, doc;
+               var match, elem, ret, doc, parent;
 
                // Handle $(""), $(null), or $(undefined)
                if ( !selector ) {
@@ -85,7 +85,12 @@ jQuery.fn = jQuery.prototype = {
 
                                        } else {
                                                ret = buildFragment( [ match[1] ], [ doc ] );
-                                               selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
+                                               parent = ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment;
+                                               selector = [];
+
+                                               while ( parent.firstChild ) {
+                                                       selector.push( parent.removeChild( parent.firstChild ) );
+                                               }
                                        }
 
                                // HANDLE: $("#id")
index ba7fd171e2d6f57f77ca72a5bd9074c686a2c685..185f77aa3b87783be996b672e474067387553846 100644 (file)
@@ -12,7 +12,7 @@ test("Basic requirements", function() {
 });
 
 test("jQuery()", function() {
-       expect(12);
+       expect(15);
 
        // Basic constructor's behavior
 
@@ -51,10 +51,13 @@ test("jQuery()", function() {
 
        var code = jQuery("<code/>");
        equals( code.length, 1, "Correct number of elements generated for code" );
+       equals( code.parent().length, 0, "Make sure that the generated HTML has no parent." );
        var img = jQuery("<img/>");
        equals( img.length, 1, "Correct number of elements generated for img" );
+       equals( img.parent().length, 0, "Make sure that the generated HTML has no parent." );
        var div = jQuery("<div/><hr/><code/><b/>");
        equals( div.length, 4, "Correct number of elements generated for div hr code b" );
+       equals( div.parent().length, 0, "Make sure that the generated HTML has no parent." );
 
        equals( jQuery([1,2,3]).get(1), 2, "Test passing an array to the factory" );