Spaces and newlines are totally valid characters, like letters and digits. They form text nodes and become a part of the DOM. So, for instance, in the example above the <head> tag contains some spaces before <title>, and that text becomes a #text node (it contains a newline and some spaces only).
There are only two top-level exclusions:
- Spaces and newlines before
<head>are ignored for historical reasons. - If we put something after
</body>, then that is automatically moved inside thebody, at the end, as the HTML spec requires that all content must be inside<body>. So there can’t be any spaces after</body>.
