]> source.dussan.org Git - jquery.git/commitdiff
Manipulation: don't auto-insert tbody
authorOleg Gaidarenko <markelog@gmail.com>
Wed, 21 Jan 2015 18:47:03 +0000 (21:47 +0300)
committerOleg Gaidarenko <markelog@gmail.com>
Wed, 11 Feb 2015 13:49:59 +0000 (16:49 +0300)
Fixes gh-1835
Closes gh-2021

src/manipulation.js
test/unit/manipulation.js

index b11f560747da89fea94d1c332248a7acb3af16e1..7c9f5049a2db6f100046529636308a934fe3e694 100644 (file)
@@ -57,14 +57,14 @@ wrapMap.optgroup = wrapMap.option;
 wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
 wrapMap.th = wrapMap.td;
 
-// 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
index 70047622db0f966a04a9b0faf7f18974b2f0af08..5c73a192cdda66cf8bc9b36418532fe3ebeef761 100644 (file)
@@ -2452,6 +2452,81 @@ test( "Validate creation of multiple quantities of certain elements (#13818)", 4
        });
 });
 
+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( "<tr><td>test</td></tr>" );
+
+       // Lowercase and replace spaces to remove possible browser inconsistencies
+       html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
+
+       strictEqual( html, "<tbody><tr><td>test</td></tr></tbody>" );
+});
+
+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( "<tr><td>1</td></tr><tr><td>2</td></tr>" );
+
+       // Lowercase and replace spaces to remove possible browser inconsistencies
+       html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
+
+       strictEqual( html, "<tbody><tr><td>1</td></tr><tr><td>2</td></tr></tbody>" );
+});
+
+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( "<tfoot/>" );
+
+       // Lowercase and replace spaces to remove possible browser inconsistencies
+       html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
+
+       strictEqual( html, "<tbody></tbody><tfoot></tfoot>" );
+});
+
+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, "<tbody><tr><td>test</td></tr></tbody>" );
+});
+
 test( "Make sure col element is appended correctly", function() {
        expect( 1 );