Skip to content

Commit e984d1c

Browse files
committed
Manipulation: don't auto-insert tbody
Fixes gh-1835 Closes gh-2021
1 parent 0ea342a commit e984d1c

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

src/manipulation.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ wrapMap.optgroup = wrapMap.option;
5757
wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
5858
wrapMap.th = wrapMap.td;
5959

60-
// Manipulating tables requires a tbody
6160
function manipulationTarget( elem, content ) {
62-
return jQuery.nodeName( elem, "table" ) &&
63-
jQuery.nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ?
61+
if ( jQuery.nodeName( elem, "table" ) &&
62+
jQuery.nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ) {
6463

65-
elem.getElementsByTagName("tbody")[0] ||
66-
elem.appendChild( elem.ownerDocument.createElement("tbody") ) :
67-
elem;
64+
return elem.getElementsByTagName( "tbody" )[ 0 ] || elem;
65+
}
66+
67+
return elem;
6868
}
6969

7070
// Replace/restore the type attribute of script elements for safe DOM manipulation

test/unit/manipulation.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,6 +2452,81 @@ test( "Validate creation of multiple quantities of certain elements (#13818)", 4
24522452
});
24532453
});
24542454

2455+
test( "Make sure tr element will be appended to tbody element of table when present", function() {
2456+
expect( 1 );
2457+
2458+
var html,
2459+
table = document.createElement( "table" );
2460+
2461+
table.appendChild( document.createElement( "tbody" ) );
2462+
document.getElementById( "qunit-fixture" ).appendChild( table );
2463+
2464+
jQuery( table ).append( "<tr><td>test</td></tr>" );
2465+
2466+
// Lowercase and replace spaces to remove possible browser inconsistencies
2467+
html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2468+
2469+
strictEqual( html, "<tbody><tr><td>test</td></tr></tbody>" );
2470+
});
2471+
2472+
test( "Make sure tr elements will be appended to tbody element of table when present", function() {
2473+
expect( 1 );
2474+
2475+
var html,
2476+
table = document.createElement( "table" );
2477+
2478+
table.appendChild( document.createElement( "tbody" ) );
2479+
document.getElementById( "qunit-fixture" ).appendChild( table );
2480+
2481+
jQuery( table ).append( "<tr><td>1</td></tr><tr><td>2</td></tr>" );
2482+
2483+
// Lowercase and replace spaces to remove possible browser inconsistencies
2484+
html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2485+
2486+
strictEqual( html, "<tbody><tr><td>1</td></tr><tr><td>2</td></tr></tbody>" );
2487+
});
2488+
2489+
test( "Make sure tfoot element will not be appended to tbody element of table when present", function() {
2490+
expect( 1 );
2491+
2492+
var html,
2493+
table = document.createElement( "table" );
2494+
2495+
table.appendChild( document.createElement( "tbody" ) );
2496+
document.getElementById( "qunit-fixture" ).appendChild( table );
2497+
2498+
jQuery( table ).append( "<tfoot/>" );
2499+
2500+
// Lowercase and replace spaces to remove possible browser inconsistencies
2501+
html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2502+
2503+
strictEqual( html, "<tbody></tbody><tfoot></tfoot>" );
2504+
});
2505+
2506+
test( "Make sure document fragment will be appended to tbody element of table when present", function() {
2507+
expect( 1 );
2508+
2509+
var html,
2510+
fragment = document.createDocumentFragment(),
2511+
table = document.createElement( "table" ),
2512+
tr = document.createElement( "tr" ),
2513+
td = document.createElement( "td" );
2514+
2515+
table.appendChild( document.createElement( "tbody" ) );
2516+
document.getElementById( "qunit-fixture" ).appendChild( table );
2517+
2518+
fragment.appendChild( tr );
2519+
tr.appendChild( td );
2520+
td.innerHTML = "test";
2521+
2522+
jQuery( table ).append( fragment );
2523+
2524+
// Lowercase and replace spaces to remove possible browser inconsistencies
2525+
html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2526+
2527+
strictEqual( html, "<tbody><tr><td>test</td></tr></tbody>" );
2528+
});
2529+
24552530
test( "Make sure col element is appended correctly", function() {
24562531
expect( 1 );
24572532

0 commit comments

Comments
 (0)