diff --git a/src/manipulation.js b/src/manipulation.js
index 49426dbe6..3031c4f73 100644
--- a/src/manipulation.js
+++ b/src/manipulation.js
@@ -104,12 +104,13 @@ function getAll( context, tag ) {
// Manipulating tables requires a tbody
function manipulationTarget( elem, content ) {
- return jQuery.nodeName( elem, "table" ) &&
- jQuery.nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ?
+ if ( jQuery.nodeName( elem, "table" ) &&
+ jQuery.nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ) {
- elem.getElementsByTagName("tbody")[0] ||
- elem.appendChild( elem.ownerDocument.createElement("tbody") ) :
- elem;
+ return elem.getElementsByTagName( "tbody" )[ 0 ] || elem;
+ }
+
+ return elem;
}
// Replace/restore the type attribute of script elements for safe DOM manipulation
diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js
index 9ea53b8ab..d9e9e40b3 100644
--- a/test/unit/manipulation.js
+++ b/test/unit/manipulation.js
@@ -2485,6 +2485,81 @@ test( "Make sure jQuery.fn.remove can work on elements in documentFragment", 1,
equal( fragment.childNodes.length, 0, "div element was removed from documentFragment" );
});
+test( "Make sure tr element will be appended to tbody element of table when present", function() {
+ expect( 1 );
+
+ var html,
+ table = document.createElement( "table" );
+
+ table.appendChild( document.createElement( "tbody" ) );
+ document.getElementById( "qunit-fixture" ).appendChild( table );
+
+ jQuery( table ).append( "
| test |
" );
+
+ // Lowercase and replace spaces to remove possible browser inconsistencies
+ html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
+
+ strictEqual( html, "| test |
" );
+});
+
+test( "Make sure tr elements will be appended to tbody element of table when present", function() {
+ expect( 1 );
+
+ var html,
+ table = document.createElement( "table" );
+
+ table.appendChild( document.createElement( "tbody" ) );
+ document.getElementById( "qunit-fixture" ).appendChild( table );
+
+ jQuery( table ).append( "| 1 |
| 2 |
" );
+
+ // Lowercase and replace spaces to remove possible browser inconsistencies
+ html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
+
+ strictEqual( html, "| 1 |
| 2 |
" );
+});
+
+test( "Make sure tfoot element will not be appended to tbody element of table when present", function() {
+ expect( 1 );
+
+ var html,
+ table = document.createElement( "table" );
+
+ table.appendChild( document.createElement( "tbody" ) );
+ document.getElementById( "qunit-fixture" ).appendChild( table );
+
+ jQuery( table ).append( "" );
+
+ // Lowercase and replace spaces to remove possible browser inconsistencies
+ html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
+
+ strictEqual( html, "" );
+});
+
+test( "Make sure document fragment will be appended to tbody element of table when present", function() {
+ expect( 1 );
+
+ var html,
+ fragment = document.createDocumentFragment(),
+ table = document.createElement( "table" ),
+ tr = document.createElement( "tr" ),
+ td = document.createElement( "td" );
+
+ table.appendChild( document.createElement( "tbody" ) );
+ document.getElementById( "qunit-fixture" ).appendChild( table );
+
+ fragment.appendChild( tr );
+ tr.appendChild( td );
+ td.innerHTML = "test";
+
+ jQuery( table ).append( fragment );
+
+ // Lowercase and replace spaces to remove possible browser inconsistencies
+ html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
+
+ strictEqual( html, "| test |
" );
+});
+
test( "Make sure col element is appended correctly", function() {
expect( 1 );