Closed Bug 493882 Opened 16 years ago Closed 16 years ago

assigning document.body from head can create a DOM tree with two body elements

Categories

(Core :: DOM: Core & HTML, defect)

x86
Linux
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: zwol, Unassigned)

Details

Attachments

(2 files)

This HTML file <!doctype html ><html><head><script> document.body = document.createElement("body"); document.body.appendElement(document.createElement("p")); document.body.firstChild.appendElement(document.createTextNode("body one")); </script></head><body ><p>body two</p></body></html> produces this DOM tree doctype html head script #text body p #text body p #text -- yes, with two BODY nodes. The rendering of this is perfectly sensible (you get "body one" followed by "body two" but I can easily see it confusing *something* -- page Javascript, if nothing else. It seems to me that maybe the content sink should detect that a body node already exists and either merge the new body node with it or replace it. (I personally vote for "merge with" because of bug 493881, which would be much easier to fix if we could just create a body node on demand. Although, that may not be feasible, there are framesets to consider as well.)
Attached file demo
Here's a self-contained file demonstrating the phenomenon.
cc-ing hixie.
Is this with hsivonen's HTML5 parser or with the old parser? The HTML5 spec is pretty clear about what should happen here; per spec you get two <body> elements.
This is with the old parser; the behavior is consistent at least as far back as Firefox 3.0. I'm really dubious of the value of allowing multiple <body> at all, but if that's the cross-browser behavior, whatever.
On reading through the current HTML5 a bit, yes, it does seem that you get two <body> elements, but I don't see any discussion of how such a beast is to be rendered.
Rendering is nothing special, <body> is just a regular display:block in CSS. Note that HTML5 doesn't _allow_ multiple <body> elements; a script that does that is creating an invalid document. We can't stop it, though, I mean, people can create any random DOMs they like. You can create an HTML document with a <div> as the root element and five <head>s and six <body>s inside each <head>, if you use the DOM (or XML). It's not valid, but the behaviour is defined.
So this is invalid, right?
As far as I can tell, getting two body elements is entirely logical. I think it makes sense to keep it that way now that we've finally gotten the parsing spec never to require the parser to read back from the DOM (which is desirable for off-the-main-thread parsing). A document with two body elements violates conformance criteria that applies to authoring and is, therefore, 'invalid'.
I think Olli was asking whether the _bug_ could be closed as invalid. From the discussion it sounds like the current behavior is in fact the intended behavior, so I am gonna go ahead and do that.
Status: NEW → RESOLVED
Closed: 16 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: