Permalink
Browse files

Manipulation: Restrict the tbody search to child nodes

For performance, use a querySelectorAll path instead of Javascript iteration.
http://codepen.io/anon/pen/vywJjx?editors=1010

Fixes gh-3439
Closes gh-3463
1 parent 9d822bc commit efdb8a46e4213dcf69f792c42c234c6b112ba471 @gibson042 gibson042 committed on GitHub Jan 9, 2017
Showing with 22 additions and 1 deletion.
  1. +2 −1 src/manipulation.js
  2. +20 −0 test/unit/manipulation.js
View
@@ -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;
@@ -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" );

0 comments on commit efdb8a4

Please sign in to comment.