]> source.dussan.org Git - jquery.git/commitdiff
Ref 0ed497b. Close gh-1125.
authorOleg <markelog@gmail.com>
Wed, 9 Jan 2013 09:52:15 +0000 (13:52 +0400)
committerRichard Gibson <richard.gibson@gmail.com>
Fri, 11 Jan 2013 18:00:37 +0000 (13:00 -0500)
src/manipulation.js
test/unit/manipulation.js

index ce05fd63c4b283aac228a669f6ddfcb590d4d997..984f80307f0bfba42de79775806ab75a1462ee56 100644 (file)
@@ -33,7 +33,7 @@ var nodeNames = "abbr|article|aside|audio|bdi|canvas|data|datalist|details|figca
                legend: [ 1, "<fieldset>" ],
                area: [ 1, "<map>" ],
                param: [ 1, "<object>" ],
-               thead: [ 1, "<table>"  ],
+               thead: [ 1, "<table>" ],
                tr: [ 2, "<table><tbody>" ],
                col: [ 2, "<table><tbody></tbody><colgroup>", "</table>" ],
                td: [ 3, "<table><tbody><tr>" ],
@@ -308,9 +308,8 @@ jQuery.fn.extend({
                        });
                }
 
-               if ( this[0] ) {
-                       doc = this[0].ownerDocument;
-                       fragment = jQuery.buildFragment( args, doc, false, this );
+               if ( l ) {
+                       fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, this );
                        first = fragment.firstChild;
 
                        if ( fragment.childNodes.length === 1 ) {
@@ -619,21 +618,20 @@ jQuery.extend({
        },
 
        buildFragment: function( elems, context, scripts, selection ) {
-               var elem, j, tmp, tag, wrap, tbody,
-                       nodes = [],
-                       i = 0,
+               var contains, elem, tag, tmp, wrap, tbody, j,
                        l = elems.length,
-                       fragment = context.createDocumentFragment(),
-                       safe = context === document && safeFragment;
 
-               // Ensure that context is a document
-               if ( !context || typeof context.createDocumentFragment === "undefined" ) {
-                       context = document;
-               }
+                       // Ensure a safe fragment
+                       safe = createSafeFragment( context ),
+
+                       nodes = [],
+                       i = 0;
 
                for ( ; i < l; i++ ) {
                        elem = elems[ i ];
+
                        if ( elem || elem === 0 ) {
+
                                // Add nodes directly
                                if ( jQuery.type( elem ) === "object" ) {
                                        jQuery.merge( nodes, elem.nodeType ? [ elem ] : elem );
@@ -644,8 +642,6 @@ jQuery.extend({
 
                                // Convert html into DOM nodes
                                } else {
-                                       // Ensure a safe container
-                                       safe = safe || createSafeFragment( context );
                                        tmp = tmp || safe.appendChild( context.createElement("div") );
 
                                        // Deserialize a standard representation
@@ -700,7 +696,7 @@ jQuery.extend({
                        }
                }
 
-               // Fix #11356: Clear elements from safeFragment
+               // Fix #11356: Clear elements from fragment
                if ( tmp ) {
                        safe.removeChild( tmp );
                }
@@ -713,18 +709,20 @@ jQuery.extend({
 
                i = 0;
                while ( (elem = nodes[ i++ ]) ) {
-                       safe = jQuery.contains( elem.ownerDocument, elem );
 
-                       // Append to fragment
                        // #4087 - If origin and destination elements are the same, and this is
-                       // that element, do not append to fragment
-                       if ( !selection || jQuery.inArray( elem, selection ) === -1 ) {
-                               fragment.appendChild( elem );
+                       // that element, do not do anything
+                       if ( selection && jQuery.inArray( elem, selection ) !== -1 ) {
+                               continue;
                        }
-                       tmp = getAll( elem, "script" );
+
+                       contains = jQuery.contains( elem.ownerDocument, elem );
+
+                       // Append to fragment
+                       tmp = getAll( safe.appendChild( elem ), "script" );
 
                        // Preserve script evaluation history
-                       if ( safe ) {
+                       if ( contains ) {
                                setGlobalEval( tmp );
                        }
 
@@ -739,9 +737,9 @@ jQuery.extend({
                        }
                }
 
-               elem = tmp = safe = null;
+               tmp = null;
 
-               return fragment;
+               return safe;
        },
 
        cleanData: function( elems, /* internal */ acceptData ) {
index 74e482a47c6bc2f4c0747943df80666eb1f556b6..1d95a73d72cb5104a64825b929e27df5e2a4b3ad 100644 (file)
@@ -395,13 +395,13 @@ var testAppendForObject = function( valueObj, isFragment ) {
 
 var testAppend = function( valueObj ) {
 
-       expect( 60 );
+       expect( 78 );
 
        testAppendForObject( valueObj, false );
        testAppendForObject( valueObj, true );
 
        var defaultText, result, message, iframe, iframeDoc, j, d,
-               $input, $radioChecked, $radioUnchecked, $radioParent;
+               $input, $radioChecked, $radioUnchecked, $radioParent, $map, $table;
 
        defaultText = "Try them out:";
        result = jQuery("#first").append( valueObj("<b>buga</b>") );
@@ -446,18 +446,29 @@ var testAppend = function( valueObj ) {
        jQuery("<fieldset/>").appendTo("#form").append( valueObj("<legend id='legend'>test</legend>") );
        t( "Append legend", "#legend", [ "legend" ] );
 
+       $map = jQuery("<map/>").append( valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>") );
+
+       equal( $map[ 0 ].childNodes.length, 1, "The area was inserted." );
+       equal( $map[ 0 ].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
+
        jQuery("#select1").append( valueObj("<OPTION>Test</OPTION>") );
        equal( jQuery("#select1 option:last").text(), "Test", "Appending OPTION (all caps)" );
 
-       jQuery("#table").append( valueObj("<colgroup></colgroup>") );
-       equal( jQuery("#table colgroup").length, 1, "Append colgroup" );
+       jQuery("#select1").append( valueObj("<optgroup label='optgroup'><option>optgroup</option></optgroup>") );
+       equal( jQuery("#select1 optgroup").attr("label"), "optgroup", "Label attribute in newly inserted optgroup is correct" );
+       equal( jQuery("#select1 option:last").text(), "optgroup", "Appending optgroup" );
+
+       $table = jQuery("#table").empty();
+
+       jQuery.each( "thead tbody tfoot colgroup caption tr th td".split(" "), function( i, name ) {
+               $table.append( valueObj( "<" + name + "/>" ) );
+               ok( $table.find( name ).length >= 1, "Append " + name );
+               ok( jQuery.parseHTML( "<" + name + "/>" ).length, name + " wrapped correctly" );
+       });
 
        jQuery("#table colgroup").append( valueObj("<col/>") );
        equal( jQuery("#table colgroup col").length, 1, "Append col" );
 
-       jQuery("#table").append( valueObj("<caption></caption>") );
-       equal( jQuery("#table caption").length, 1, "Append caption" );
-
        jQuery("#form")
                .append( valueObj("<select id='appendSelect1'></select>") )
                .append( valueObj("<select id='appendSelect2'><option>Test</option></select>") );
@@ -2229,9 +2240,19 @@ test( "insertAfter, insertBefore, etc do not work when destination is original e
 });
 
 test( "Index for function argument should be received (#13094)", 2, function() {
-    var i = 0;
+       var i = 0;
+
+       jQuery("<div/><div/>").before(function( index ) {
+               equal( index, i++, "Index should be correct" );
+       });
+
+});
+
+test( "Make sure jQuery.fn.remove can work on elements in documentFragment", 1, function() {
+       var fragment = document.createDocumentFragment(),
+               div = fragment.appendChild( document.createElement("div") );
+
+       jQuery( div ).remove();
 
-    jQuery("<div/><div/>").before(function( index ) {
-        equal( index, i++, "Index should be correct" );
-    });
+       equal( fragment.childNodes.length, 0, "div element was removed from documentFragment" );
 });