diff options
author | Richard Gibson <richard.gibson@gmail.com> | 2017-01-09 11:37:12 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-09 11:37:12 -0800 |
commit | efdb8a46e4213dcf69f792c42c234c6b112ba471 (patch) | |
tree | 72aee4a6e87dd7dcf8cc71262e0bb11ef9f87442 | |
parent | 9d822bc1c13dd3393b418210a99537c22c06f2c3 (diff) | |
download | jquery-efdb8a46e4213dcf69f792c42c234c6b112ba471.tar.gz jquery-efdb8a46e4213dcf69f792c42c234c6b112ba471.zip |
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
-rw-r--r-- | src/manipulation.js | 3 | ||||
-rw-r--r-- | test/unit/manipulation.js | 20 |
2 files changed, 22 insertions, 1 deletions
diff --git a/src/manipulation.js b/src/manipulation.js index 9b4f5e451..4f79f8998 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -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; diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index 911852451..b1c04dbd5 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -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" ); |