3 ms·
> What does "adjacent" mean here? And what happens if the white space is adjacent to text on both sides? I think it is the simplest sense of adjacent: if the c
by tsimionescu 2y ago
> What does "adjacent" mean here? And what happens if the white space is adjacent to text on both sides?
I think it is the simplest sense of adjacent: if the character at the previous or next position in the bytestream is considered text, than the whitespace character is part of the same text node; if it is considered anything else, it's a separate text node. This applies recursively, since whitespace is text itself. If you wanted to specify it very formally, you probably need to include some extra verbiage for escape sequences which represent text characters, but that's the only ambiguity I can think of.
So for example the following HTML:
<div>
ABC Def
Hj<h
<a href="ABC"> <br> </a>cde
</div>
Has the following structure:
DivNode
TextNode (starts with "\n A", ends with "<h\n")
AddressNode
TextNode (3 spaces)
BrNode
TextNode (space)
TextNode ("cde\n")
Any collapsing, un collapsing, ignoring, and so an are handled by manipulating these nodes further. But this is the semantics of the HTML itself.
- lmm 2y ago> Any collapsing, un collapsing, ignoring, and so an are handled by manipulating these nodes further. But this is the semantics of the HTML itself. Well the post I replied to talked about the DOM, and as a description of the behaviour of the DOM I don't think your description is accurate - when you write "<a href="ABC">foo </a> bar" you only end up with one space character contained in a DOM text node, not two.
- tsimionescu 2y agoYou can try it out and see that I'm right. I created a page like this: <div><a href="#">foo </a> bar</div> <script> divElement = document.getElementsByTagName("div")[0] console.log('"' + divElement.childNodes[0].firstChild.data + '"') console.log('"' + divElement.childNodes[1].data + '"') </script> And you'll see in the console: "foo " " bar" So yes, the DOM itself contains what I and the poster before mentioned. It's the presentation layer that decides how to do space collapse. As a bonus, you can also see that the DOM of the page has four children of `body`: `<div>`, then a text node with the content "\n\n", `<script>`, and another text node containing "\n". Tested all this with a simple HTML I saved on disk and opened in Firefox.