]> source.dussan.org Git - jquery.git/commitdiff
Manipulation: Restrict the tbody search to child nodes
authorRichard Gibson <richard.gibson@gmail.com>
Mon, 9 Jan 2017 19:37:12 +0000 (11:37 -0800)
committerGitHub <noreply@github.com>
Mon, 9 Jan 2017 19:37:12 +0000 (11:37 -0800)
For performance, use a querySelectorAll path instead of Javascript iteration.
http://codepen.io/anon/pen/vywJjx?editors=1010

Fixes gh-3439
Closes gh-3463

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

index 9b4f5e451d2d1783acd20299d398a4d1bb4f2010..4f79f8998aae2c49a201e3e5bd5825ce5342c0a8 100644 (file)
@@ -47,11 +47,12 @@ var
        rscriptTypeMasked = /^true\/(.*)/,
        rcleanScript = /^\s*<!(?:\[CDATA\[|--)|(?:\]\]|--)>\s*$/g;
 
+// Prefer a tbody over its parent table for containing new rows
 function manipulationTarget( elem, content ) {
        if ( jQuery.nodeName( elem, "table" ) &&
                jQuery.nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ) {
 
-               return elem.getElementsByTagName( "tbody" )[ 0 ] || elem;
+               return jQuery( ">tbody", elem )[ 0 ] || elem;
        }
 
        return elem;
index 911852451b6fae4a8fa085c11936c9a03db4a94e..b1c04dbd5c4e9adb8ba3d643a5a3b231c7fa858d 100644 (file)
@@ -2730,6 +2730,26 @@ QUnit.test( "Make sure col element is appended correctly", function( assert ) {
        assert.strictEqual( table.find( "td" ).width(), 150 );
 } );
 
+QUnit.test( "Make sure tr is not appended to the wrong tbody (gh-3439)", function( assert ) {
+       assert.expect( 1 );
+
+       var htmlOut,
+               htmlIn =
+                       "<thead><tr><td>" +
+                               "<table><tbody><tr><td>nested</td></tr></tbody></table>" +
+                       "</td></tr></thead>",
+               newRow = "<tr><td>added</td></tr>",
+               htmlExpected = htmlIn.replace( "</thead>", "</thead>" + newRow ),
+               table = supportjQuery( "<table/>" ).html( htmlIn ).appendTo( "#qunit-fixture" )[ 0 ];
+
+       jQuery( table ).append( newRow );
+
+       // Lowercase and replace spaces to remove possible browser inconsistencies
+       htmlOut = table.innerHTML.toLowerCase().replace( /\s/g, "" );
+
+       assert.strictEqual( htmlOut, htmlExpected );
+} );
+
 QUnit.test( "Insert script with data-URI (gh-1887)", 1, function( assert ) {
        Globals.register( "testFoo" );
        Globals.register( "testSrcFoo" );