1

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.

Oriol
  • 274,082
  • 63
  • 437
  • 513
  • `tbody` is part of the standard HTML definition (see section 4.9.5 in th elink you gave for the spec), but its use is not required. It's so that rending engines can distinguish between heading and body (see `` as well). – lurker Jun 16 '15 at 14:20
  • @lurker Yes, I know `tbody` is not required (the content model of `table` allows `tr` without `tbody`). However, if you don't include it, most browsers will insert it. Can I rely on that? – Oriol Jun 16 '15 at 14:21
  • @lurker you should post that as an answer – Andrea Ligios Jun 16 '15 at 14:30
  • @lurker I have included a snippet which demonstrates that the `tbody` is **literally** inserted when the table is parsed into the DOM. That happens on Firefox, Chrome and IE8. – Oriol Jun 16 '15 at 14:32
  • Ah thanks for the snippet. I see what you mean. To me, it's not literal unless it changes your HTML file. Other than that, it's still an "internal" rendering. ;) Semantic difference... :) – lurker Jun 16 '15 at 14:34
  • Here's a [related question](http://stackoverflow.com/questions/7490364/why-do-browsers-still-inject-tbody-in-html5). More directly, here's the HTML5 documentation ([The "In Table" Insertion Mode](http://www.w3.org/html/wg/drafts/html/master/syntax.html#parsing-main-intable)) that discusses this scenario. – lurker Jun 16 '15 at 14:42
  • @lurker Thanks, that answers the question. – Oriol Jun 16 '15 at 14:47

1 Answers1

0

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:

Community
  • 1
  • 1
Oriol
  • 274,082
  • 63
  • 437
  • 513