Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does firebug add <tbody> to <table>?

Tags:

html

dom

firebug

I viewed the html source code, there is no <tbody>, but when viewed via firebug in the HTML tab, <tbody> appears. Any idea why?

like image 901
Mask Avatar asked Nov 05 '09 05:11

Mask


People also ask

Why Tbody is added automatically?

The browser has to correct the code in order to create a DOM hieararchy from it. The code is incorrect, but how much depends on the DOCTYPE that you are using. There is no need to specify the tbody element, it's added automatically.

Do you need Tbody in table?

Zero or more <tr> elements. The <tbody> element is not a required child element for a parent <table> element to graphically render. However, it must be present, if the parent <table> element has a <thead> , a <tfoot> or another <tbody> element as a child.

What is a tbody element?

The <tbody> tag is used to group the body content in an HTML table. The <tbody> element is used in conjunction with the <thead> and <tfoot> elements to specify each part of a table (body, header, footer). Browsers can use these elements to enable scrolling of the table body independently of the header and footer.


2 Answers

To summarize the excellent explanations given in the answers and comments by bobince, Kieron, Alohci and others:

  1. Firebug just displays the DOM of the parsed page. Due to complicated HTML parsing rules, the DOM will "differ" (in some sense) from the source HTML.
  2. In this case the TBODY element in the DOM is added by the HTML parser. Note that this weird parsing is limited to text/html documents and in XHTML the DOM corresponds closely to the source XML.
    • This behavior was specified in HTML 4. The content model (allowed children) for table is (CAPTION?, (COL*|COLGROUP*), THEAD?, TFOOT?, TBODY+) -- trs are only allowed in tbody! The spec says that tbody's start tag is optional, which is supposed to mean that if the HTML parser encounters tr directly inside a table it inserts the tbody start tag omitted by the author.
    • To make matters more clear HTML 5 defines very detailed parsing rules, in particular for this case: "When the insertion mode is "in table", tokens must be handled as follows: [...] A start tag whose tag name is one of: "td", "th", "tr" -> Act as if a start tag token with the tag name "tbody" had been seen, then reprocess the current token."
like image 75
Nickolay Avatar answered Oct 06 '22 13:10

Nickolay


Its not firebug, but firefox which does that. This is the way tables are supposed to be written with <TBODY> separate from meta data like <COLGROUP> Firefox simply inserts the <TBODY> tags when it finds them missing.

like image 30
KJ Saxena Avatar answered Oct 06 '22 15:10

KJ Saxena