Why do browsers still introduce <tbody> in HTML5?
Both IE9 and Chrome14 log TBODY as the tagName element inside the <table>
The HTML5 specification on <table> clearly states:
followed by either zero or more tbody elements or one or more tr elements
Further. The HTML5 specification on <tr> clearly states:
As a child of the table, after any header elements, colgroup and thead, but only if there are no tbody elements that are children of the table element.
Why do browsers damage my DOM and enter <tbody> when
- I did not ask for one
- This is absolutely true without one.
The "backward compatibility" answer is absolutely null because I specifically chose doctype for HTML5.
The “backward compatibility” answer is absolutely null because I specifically chose an HTML5 document.
However, browsers do not distinguish between HTML versions. HTML documents with HTML5 doctype and HTML4 doctype (with a few exceptions to a transition document such as HTML4 without a URL in FPI) are parsed and displayed the same way.
I will provide the corresponding part of the HTML5 parser description :
8.2.5.4.9 Input mode "in the table"
...
A start tag whose tag name is one of the following: "td", "th", "tr"
Act as if the start tag token with the tag name "tbody" was then process the current token.
The part of the HTML5 specification that defines how the tree is built is completely missing.
The specification allows you to write table without the tbody element, as it meant. Just as if you missed the html , head or body tags of opening or closing, your page could still be displayed correctly.
I assume that you want the DOM to contain a body for your content if for some reason it is not taken into account. The same goes for tbody . He added, because he clearly suggests that you forgot to add it yourself.
A start tag whose tag name is one of the following: "td", "th", "tr"
Act as if the start tag token with the tag name "tbody" was seen, and then processed the current token.
In my experience, browsers do not distinguish between HTML5 and HTML4 documents. They behave the same for both. <!doctype html> does not cause any special actions in browsers.
And also <!doctype html> not reserved for "HTML5 documents" - it's just the easiest type possible that launches standards mode.
Much happens because HTML5 combines the successor in HTML 4 and XHTML 1.x into one specification.
When XHTML 1.0 was introduced and browsers started experimenting with XML parsing, they ran into a problem. The authors wrote <table> without <tbody> s. Since the XML parser is not allowed to output tags such as HTML parsers, the best way to help authors migrate to XHTML (which at the time seemed like a good idea) was to make the tables display correctly by allowing <tr> be direct children <table> inside the DOM. (The DOM is as large as possible, regardless of whether it came from HTML syntax or XML parsing.) Thus, browsers implemented support for this.
The HTML5 content model is now split between serializing HTML and XHTML HTML5, so it should consider both devices, that is, with or without tbody.
On the other hand, in the section "HTML Syntax" (which does not apply to the XML parser), it clearly shows that HTML parsing will output tbody tags.
When <table><tr><td>my text</td></tr></table> served as text/html , the table structure created in the DOM will have tr as a direct child of tbody, which is a direct child table element. The HTML5 content model says this is normal.
When <table><tr><td>my text</td></tr></table> served as application/xhtml+xml , the table structure created in the DOM will have tr as a direct child of the table. The HTML5 content model says this is normal too.
It is also possible to create tr as a direct child of the table through scripts. For the same reason, browsers will see this as a table row, as most people expect.
This is for “historical reasons” (i.e. backward compatibility, which is very important for HTML):
For historical reasons, some elements have additional restrictions besides even the restrictions set by their content model.
ElementA
tableshould not containtrelements, although these elements are technically permitted insidetableelements in accordance with the content model described in this description. (If thetrelement is placed inside thetablein the markup, this actually meanstbodystart the tag in front of it.)
Please note that this quote is from "HTML Syntax" . This section applies only to documents, development tools, and markup generators, and clearly not to conformance checking tools (which should use the HTML parsing algorithm ).
So: the specification states that using tr outside tbody allowed according to the content model and the parsing specification, but everything that generates HTML (including YOU) should use tbody .
Backward compatibility is not only about doctype, scripts can rely on the tbody element.