Friday, September 27, 2019

html - 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):



"abcdefhijinnerklm"


Querying nsIDOMNSHTMLElement.innerHTML on the Body element using gives:



"abcdefhijinnerklm"



Iterating through Body child elements gives:



Text : "abc"
Element : "def"
Text : "hijinn"
Element : "e"
Text : "rklm"



Example HTML Document 2 (text node moved inside body but title isn't):



"defhijinnerklm"


Querying nsIDOMNSHTMLElement.innerHTML on the Body element using gives:



"hijinnerklm"



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?


Answer



The basic answer is "because the HTML5 parsing algorithm says so". To be more specific, only certain tags are allowed outside of , and everything else gets put inside even though it wasn't there in the original data stream.



No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...