CHAPTER 10 – How HTML Works
Checking engine…

10.3 — The Live Tree Architecture

The DOM is not a file. It is a live, interconnected graph of C++ objects residing in the browser’s memory. Understanding its internal node types, whitespace rules, and dynamic nature is essential for debugging layout and writing performant code.

Diagram showing inheritance: Node base class → Element, Text, Comment subclasses
Figure 10.10: The DOM node hierarchy – every object inherits from the base Node class.

Element Nodes – The Heavy Containers

An Element node represents an HTML tag: <div>, <section>, <input>. It stores tag names, attributes, namespace info, and pointers to its parent, children, and siblings. However, it does not store raw text. The actual character content is delegated to separate Text nodes.

Diagram showing an <p> element node with a child Text node containing the sentence
Figure 10.11: Element nodes own no text directly; text lives in separate Text node objects.

Text Nodes – The Lean Character Containers

A Text node is a slim object that holds a raw string and links to a parent element. It exists solely to store the characters that appear between tags. The browser creates one Text node for every contiguous run of character data.

Comment Nodes – Invisible but Real

HTML comments (<!-- … -->) are not rendered visually, yet the parser instantiates a Comment node object, allocates memory for its string, and grafts it into the DOM tree. It remains fully accessible via scripting.

Diagram of an indented list with whitespace Text nodes inserted between each <li> element
Figure 10.12: Whitespace characters between tags become literal Text nodes in the DOM, causing visual gaps in inline layouts.

Whitespace Text Nodes – The Hidden Gaps

Spaces, tabs, and line feeds between HTML elements are not ignored. The tokenizer treats them as character data and the tree constructor turns them into real Text nodes. In inline or inline‑block layouts, these invisible nodes create visible gaps on screen, often the 4‑px space between elements. Removing all whitespace between tags can eliminate these gaps.

Side-by-side: raw source file (unclosed tags) vs. the live DOM tree (browser-corrected, fully closed)
Figure 10.13: The static source file vs. the dynamic, error‑corrected live DOM tree.

Source Code vs. Live DOM – The Great Divergence

Your raw .html file is a static string that never changes. The live DOM is a reactive graph that begins to diverge the moment the page loads. Browser extensions inject nodes, JavaScript frameworks dynamically create or destroy elements, and the parser’s error recovery silently repairs broken markup – all visible only in the Elements panel.

Diagram showing a basic DOM tree being modified by a script adding a new element and by a browser extension injecting a banner
Figure 10.14: The live DOM tree is constantly altered by scripts, extensions, and runtime events.
TAKEAWAY

The DOM is a living C++ object hierarchy, not a text file. Element nodes provide structure, Text nodes hold characters, and Comment nodes preserve metadata – all inheriting from a common Node class. Whitespace becomes real nodes, and the live tree diverges from source code the instant it’s built. Use the Elements panel to see the actual runtime graph; never trust View Source alone. This is the reality your CSS and JavaScript interact with.

Checking engine…