Skip to content

Commit efdb8a4

Browse files
authored
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 efdb8a4

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/manipulation.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ var
4747
rscriptTypeMasked = /^true\/(.*)/,
4848
rcleanScript = /^\s*<!(?:\[CDATA\[|--)|(?:\]\]|--)>\s*$/g;
4949

50+
// Prefer a tbody over its parent table for containing new rows
5051
function manipulationTarget( elem, content ) {
5152
if ( jQuery.nodeName( elem, "table" ) &&
5253
jQuery.nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ) {
5354

54-
return elem.getElementsByTagName( "tbody" )[ 0 ] || elem;
55+
return jQuery( ">tbody", elem )[ 0 ] || elem;
5556
}
5657

5758
return elem;

test/unit/manipulation.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2730,6 +2730,26 @@ QUnit.test( "Make sure col element is appended correctly", function( assert ) {
27302730
assert.strictEqual( table.find( "td" ).width(), 150 );
27312731
} );
27322732

2733+
QUnit.test( "Make sure tr is not appended to the wrong tbody (gh-3439)", function( assert ) {
2734+
assert.expect( 1 );
2735+
2736+
var htmlOut,
2737+
htmlIn =
2738+
"<thead><tr><td>" +
2739+
"<table><tbody><tr><td>nested</td></tr></tbody></table>" +
2740+
"</td></tr></thead>",
2741+
newRow = "<tr><td>added</td></tr>",
2742+
htmlExpected = htmlIn.replace( "</thead>", "</thead>" + newRow ),
2743+
table = supportjQuery( "<table/>" ).html( htmlIn ).appendTo( "#qunit-fixture" )[ 0 ];
2744+
2745+
jQuery( table ).append( newRow );
2746+
2747+
// Lowercase and replace spaces to remove possible browser inconsistencies
2748+
htmlOut = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2749+
2750+
assert.strictEqual( htmlOut, htmlExpected );
2751+
} );
2752+
27332753
QUnit.test( "Insert script with data-URI (gh-1887)", 1, function( assert ) {
27342754
Globals.register( "testFoo" );
27352755
Globals.register( "testSrcFoo" );

0 commit comments

Comments
 (0)