Skip to content

Commit 939e755

Browse files
committed
Manipulation: don't auto-insert tbody
Fixes gh-1835 Closes gh-2021 Ref e984d1c
1 parent b744a50 commit 939e755

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed

src/manipulation.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,13 @@ function getAll( context, tag ) {
104104

105105
// Manipulating tables requires a tbody
106106
function manipulationTarget( elem, content ) {
107-
return jQuery.nodeName( elem, "table" ) &&
108-
jQuery.nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ?
107+
if ( jQuery.nodeName( elem, "table" ) &&
108+
jQuery.nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ) {
109109

110-
elem.getElementsByTagName("tbody")[0] ||
111-
elem.appendChild( elem.ownerDocument.createElement("tbody") ) :
112-
elem;
110+
return elem.getElementsByTagName( "tbody" )[ 0 ] || elem;
111+
}
112+
113+
return elem;
113114
}
114115

115116
// 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
@@ -2485,6 +2485,81 @@ test( "Make sure jQuery.fn.remove can work on elements in documentFragment", 1,
24852485
equal( fragment.childNodes.length, 0, "div element was removed from documentFragment" );
24862486
});
24872487

2488+
test( "Make sure tr element will be appended to tbody element of table when present", function() {
2489+
expect( 1 );
2490+
2491+
var html,
2492+
table = document.createElement( "table" );
2493+
2494+
table.appendChild( document.createElement( "tbody" ) );
2495+
document.getElementById( "qunit-fixture" ).appendChild( table );
2496+
2497+
jQuery( table ).append( "<tr><td>test</td></tr>" );
2498+
2499+
// Lowercase and replace spaces to remove possible browser inconsistencies
2500+
html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2501+
2502+
strictEqual( html, "<tbody><tr><td>test</td></tr></tbody>" );
2503+
});
2504+
2505+
test( "Make sure tr elements will be appended to tbody element of table when present", function() {
2506+
expect( 1 );
2507+
2508+
var html,
2509+
table = document.createElement( "table" );
2510+
2511+
table.appendChild( document.createElement( "tbody" ) );
2512+
document.getElementById( "qunit-fixture" ).appendChild( table );
2513+
2514+
jQuery( table ).append( "<tr><td>1</td></tr><tr><td>2</td></tr>" );
2515+
2516+
// Lowercase and replace spaces to remove possible browser inconsistencies
2517+
html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2518+
2519+
strictEqual( html, "<tbody><tr><td>1</td></tr><tr><td>2</td></tr></tbody>" );
2520+
});
2521+
2522+
test( "Make sure tfoot element will not be appended to tbody element of table when present", function() {
2523+
expect( 1 );
2524+
2525+
var html,
2526+
table = document.createElement( "table" );
2527+
2528+
table.appendChild( document.createElement( "tbody" ) );
2529+
document.getElementById( "qunit-fixture" ).appendChild( table );
2530+
2531+
jQuery( table ).append( "<tfoot/>" );
2532+
2533+
// Lowercase and replace spaces to remove possible browser inconsistencies
2534+
html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2535+
2536+
strictEqual( html, "<tbody></tbody><tfoot></tfoot>" );
2537+
});
2538+
2539+
test( "Make sure document fragment will be appended to tbody element of table when present", function() {
2540+
expect( 1 );
2541+
2542+
var html,
2543+
fragment = document.createDocumentFragment(),
2544+
table = document.createElement( "table" ),
2545+
tr = document.createElement( "tr" ),
2546+
td = document.createElement( "td" );
2547+
2548+
table.appendChild( document.createElement( "tbody" ) );
2549+
document.getElementById( "qunit-fixture" ).appendChild( table );
2550+
2551+
fragment.appendChild( tr );
2552+
tr.appendChild( td );
2553+
td.innerHTML = "test";
2554+
2555+
jQuery( table ).append( fragment );
2556+
2557+
// Lowercase and replace spaces to remove possible browser inconsistencies
2558+
html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2559+
2560+
strictEqual( html, "<tbody><tr><td>test</td></tr></tbody>" );
2561+
});
2562+
24882563
test( "Make sure col element is appended correctly", function() {
24892564
expect( 1 );
24902565

0 commit comments

Comments
 (0)