Why can/does FF DOM regard nodes as children of BODY even though they are before the BODY tag?
I have observed how firefox can move text and element nodes, listed before the BODY element in a HTML document, to become direct children of the BODY element.
I'm using xulrunner 2.0.1 (Firefox 4.0), Although I have observed IE also moving text, but not elements.
Here are some examples of FF doing this:
Example HTML Document 1 (text nodes and title element moved inside body):
"<html>abc<title>def</title>hij<body>inn<span>e</span>r</body>klm</html>"
Querying nsIDOMNSHTMLElement.innerHTML on the Body element using gives:
"abc<title>def</title>hijinn<span>e</span>rklm"
Iterating through Body child elements gi开发者_开发知识库ves:
Text : "abc"
Element : "def"
Text : "hijinn"
Element : "e"
Text : "rklm"
Example HTML Document 2 (text node moved inside body but title isn't):
"<html><title>def</title>hij<body>inn<span>e</span>r</body>klm</html>"
Querying nsIDOMNSHTMLElement.innerHTML on the Body element using gives:
"hijinn<span>e</span>rklm"
Iterating through Body child elements gives:
Text : "hijinn"
Elemnt : "e"
Text : "rklm"
My question is why is this happening? I would have expected innerHTML just to display what between the two body tags?
This is because the BODY tag is optional, and the parser assumes that it was omitted. If the tag itself is omitted, then the BODY element still shows up in the DOM, because its existance is implied.
The HTML 4.01 specification states that both the start and end tags are optional for the BODY element.
The basic answer is "because the HTML5 parsing algorithm says so". To be more specific, only certain tags are allowed outside of <body>
, and everything else gets put inside <body>
even though it wasn't there in the original data stream.
Because you gave it invalid HTML code. HTML parsers will always correct invalid HTML code. This means in particular that the only allowed children of the <html>
tag are <head>
and <body>
. <title>
tags will be moved into <head>
where they belong, text nodes into <body>
. If you are interested in how browsers handle this "tag soup", there is a detailed specification under http://dev.w3.org/html5/spec/Overview.html#parsing.
精彩评论