Is the insertion of <tbody> in HTML tables standard?

579 Views Asked by At

In HTML, when parsing a table element with tr elements inside, a tbody is usually inserted:

<tbody> is inserted inside <table> (http://jsfiddle.net/ed9msnfp/)

function log(el, ctx) {
  var li = document.createElement('li');
  li.appendChild(document.createTextNode(el.tagName.toLowerCase()));
  var ul = document.createElement('ul');
  for(var i=0; i<el.children.length; ++i)
    log(el.children[i], ul);
  li.appendChild(ul);
  ctx.appendChild(li);
}
log(document.querySelector('table'), document.getElementById('dom-tree'));
table {
  display: none;
}
<table>
  <tr><td>1</td></tr>
  <tr><td>2</td></tr>
</table>
The table is parsed as:
<ul id="dom-tree"></ul>

Is this behavior standard?

I couldn't find it described in the spec, but it makes sense, because the insertRow DOM method automatically creates tbody elements too:

tr = table . insertRow( [ index ] )

Creates a tr element, along with a tbody if required, inserts them into the table at the position given by the argument, and returns the tr.

1

There are 1 best solutions below

0
On

Yes, it's standard. It's described in Parsing HTML documents (instead of Tabular data):

If node is a table element, then switch the insertion mode to "in table"

8.2.5.4.9 The "in table" insertion mode

When the user agent is to apply the rules for the "in table" insertion mode, the user agent must handle the token as follows: