Thomas 'PointedEars' Lahn wrote:
> Henry wrote:
>> On Mar 6, 7:24 pm, Rex the Strange wrote:
>>> You would think that you could get away with this:
>>>
>>> var page_element = document.getElementById
>>> ("parent_element_name");
>>>
>>> var table = document.createElement ("table");
>>> var row = document.createElement ("tr");
>>> var cell = document.createElement ("td");
>>>
>>> page_element.appendChild (table);
>>> table.appendChild (row);
>>
>> No, you absolutely should not expect to be able to "get
>> away with" that in an HTML DOM. The structural constraints
>> of HTML do not allow a TR element to be the direct child
>> of any type of element except a table section element
>> (TBODY, THEAD or TFOOT). It makes as little sense to
>> attempt to make a TR a direct child of a TABLE as it would
>> to attempt to make one a direct child of a SELECT element.
>
> The restriction enforced by the DOM is based on
>
> http://www.w3.org/TR/html4/struct/tables.html#h-11.2.1
> http://www.w3.org/TR/html4/sgml/loosedtd.html
>
> However, it should be noted that the TBODY element is not
> necessary to write in HTML 4.01 markup because both its
> start tag and its end tag are optional in all relevant DTDs.
That still results in a TBODY existing. Except in XHTML or course,
where TR may be direct children of TABLE (not a good idea but
understandable given XHTML having some desire to be 'compatible'
with HTML while not tolerating optional tags or implied elements).
> Therefore it makes rather little sense to me to enforce its
> creation in DOM scripting instead of having it automatically
> generated where needed, but there it is.
While it is (obviously as it already happens) possible to 'fix'
attempts to create erroneous DOM structures that programmers attempt
to create by inserting omitted elements the attempt is not without
its issues. First there is what you would do in an XHTML DOM, where
the intervening table section elements is not required, but can be
used; do you add one regardless or always omit them. Then there is
the situation in HTML; if a TR is appended to a table and the table
already has a TBODY do you instead append to the existing TBODY or
add a new one, and if a second TR is appended does it get added to
an existing TBODY (possibly the one added by the last appended TR)
or does it get a new TBODY of its own. Without a very clear
standard on how this situation it to be handled you are likely to
get different outcomes from each and every browser. Followed by lots
of complaints that browsers have bugs because they are not doing
precisely the same as other browsers.
On the whole it is probably better to have the browser create the
DOM structure exactly as the programmer asks it to, and let the
programmer be responsible for building a structure that makes
sense in the type of DOM they are using.