Build one tree from the token sequence
Use <div><p>A</p><p>B</p></div>. The first p becomes a child of div and contains text A. After it closes, the second p becomes a sibling under that same div. A list of tag names cannot represent this relation. Tree construction creates explicit node kinds and parent-child edges. Screen coordinates and colors are not decided here. The result is a document structure that later CSS processing or scripting can inspect. The demo displays the current token, open elements, and growing tree together.
Write the intermediate states explicitly. After <div>, one edge connects the document to div. After <p>, another connects div to p. Character A adds a text node under p. Reading </p> does not remove p from the tree; it changes the current insertion parent back to div. An end tag is not a command to delete an element from the document or screen. This also explains why one start/end tag pair does not create two element nodes. Inspect source position, current insertion parent, and created nodes together. Those three views reveal what the current state remembers and what the next input can change.
Use a stack of open elements
Begin with the document node. On a start tag, create an element, append it to the current top element, and push it. Character data becomes a child of the current element. A matching end tag pops back to its parent. Before the first </p>, the top is p; afterward it is div. Rewinding must restore both tree and stack to the same step. Updating them independently can attach later nodes to the wrong parent. A complete snapshot or deterministic replay keeps those structures consistent.
| Consumed input | Stack top | Parent of next text |
|---|---|---|
| <div> | div | div |
| <div><p> | p | p |
| <div><p>A</p> | div | div |
Distinguish attributes from text nodes
In <p id="intro">A</p>, id belongs to the p element as an attribute, while A is a child text node. Combining them into one string makes ID lookup and text-only updates difficult. Keep children in an ordered collection. Adjacent character tokens may be combined into one text node, provided order and content remain unchanged. DOM node count need not equal tag count or token count. Tests should therefore inspect node kinds and edges as well as total counts.
A model node can store identifier, kind, element name, attributes, parent identifier, and ordered child identifiers. A text node stores content instead of a tag name. The tree display can turn those data into lists or SVG; it need not regenerate active HTML. An a element may retain an href value without authorizing navigation to that destination. Constructing the node does not require opening a learner-supplied external URL. Keep appending model text separate from injecting executable host-page HTML. This is both an implementation boundary and a useful explanation of the difference between representing a document and letting the host browser execute one.
Give void elements and mismatches explicit rules
A void element such as br does not contain children. Leaving it on the stack would incorrectly make later text its child. The initial model declares supported void elements and appends them without pushing them. Mismatched end tags are errors in its strict fragment language. Real HTML parsing has insertion modes, implied elements, and recovery rules beyond XML-like nesting checks. Keep that difference explicit. An error in this model is not evidence that a production browser must reject the same HTML.
Separate node identity from display position
Later DOM operations may refer to an element again or move it elsewhere. Treating an element as merely the third row in a display breaks identity when another sibling is inserted. Assign stable creation-time node identifiers within a run and store parent and child identifiers separately. Such an identifier is neither a screen coordinate nor an HTML id attribute. Selecting a demo node should reveal source position and attributes. Distinguish stable identity inside one run from identifier reuse after a complete reset.
Check tree invariants: every non-document node has one parent, that parent lists the child identifier, and child order matches insertion order. Do not attach one node to two parents at once or introduce a cycle through parent links. The open-element stack should correspond to the model path of elements currently being constructed. Reusing numeric identifiers after a fresh run must not mix references across runs. Test mutual parent-child links and absence of cycles before comparing screenshots. A tree visualization can otherwise look plausible while hidden model references are inconsistent, and the defect may appear only when the next node is appended or a previous step is restored.
Test the tree independently of painting
Correctness here means expected parent-child relations and text, not a plausible screenshot. Check that div has two p children, that they contain A and B, and that the open-element stack returns to its initial state. Hiding an element with CSS does not remove that DOM node. Render the tree display from the model node data rather than inserting the original HTML into the host page and presenting the host browser result as your own tree builder. This keeps the implementation boundary observable.
Exercise: follow two paragraphs and one line break
Parse <div><p>A<br>B</p><p>C</p></div>. The div has two p children. The first p has text A, a br element, and text B in that order. After br, the parent for B is still p. The second p is not a child of the first. Remove one </p> and record the strict model diagnostic separately from a real browser recovery result. Different outputs are not automatically a model defect: first check which parsing contract each implementation promises.
Build a fixture ladder: an empty fragment, <div></div>, <div><p>A</p></div>, and <p>A<br>B</p>. Expected outputs progress from only a document node, to one empty element, to nested parent identifiers, to a void element that does not increase the stack. Test unsupported tag syntax and mismatched endings as distinct failures. Record which new fixture becomes valid when a feature is added instead of declaring the tree builder complete after one good page. Feed the same tokens in several groups and require the same final tree. That checks that tree construction depends on tokens, not accidentally on how a network read or tokenizer call happened to group them.