# The Parsing Phase

> How the browser converts raw bytes of HTML into a functional Document Object Model.

Subject: Internet | Lesson 24 of 35 | 5 min read
Interactive lesson: https://codewithanimation.in/internet/parsing-phase
Previous: [Edge Caching vs. Browser Caching](https://codewithanimation.in/internet/edge-caching.md) | Next: [DOM Construction](https://codewithanimation.in/internet/dom-construction.md)

## What it is

When a browser receives a response from a server, it receives raw network bytes. The **Parsing Phase** is the browser's engine converting those 1s and 0s into the DOM (Document Object Model).

This process happens in four strict steps: **Bytes -> Characters -> Tokens -> Nodes -> DOM**.

## Why it matters

Understanding parsing is the foundation of web performance. The browser cannot render the page until it constructs the DOM.

If the parser encounters a synchronous `<script>` tag, it completely pauses DOM construction to download and execute the script. This is known as a **render-blocking resource**.

## How it works

1. **Bytes & Characters**: The browser reads the raw bytes and translates them into characters (e.g., UTF-8).

2. **Tokens**: The tokenizer reads characters and looks for HTML tags (like `<html>`, `<body>`), converting them into distinct tokens.

3. **Nodes**: As tokens are emitted, they are converted into Node objects containing properties and rules.

4. **DOM Tree**: Because HTML tags are nested, the nodes are linked together into a tree structure capturing the parent-child relationships.

## Common mistake: The 'Wait For Download' Myth

A huge misconception is that the browser waits for the entire HTML document to download before it begins parsing.

In reality, browsers process HTML **incrementally**. The parser starts building the DOM as soon as the first chunks of data arrive over the network, allowing the browser to paint the top of a page while the bottom is still downloading.

## Tip: Pro tip

**The Tokenizer is a State Machine**: It literally reads your HTML character by character.

When it sees a `<`, it enters the 'Tag open' state. If you accidentally forget to close a string or a tag, the state machine will try to auto-correct it, but it might swallow large chunks of your document.

## Try it: Try it

Watch the tokenizer convert raw HTML into DOM nodes.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/parsing-phase

## Check yourself

### Question 1

What is the correct sequence of steps a browser takes to parse HTML?

- A. Tokens -> Bytes -> DOM -> Nodes -> Characters
- B. Bytes -> Tokens -> Characters -> DOM -> Nodes
- C. Bytes -> Characters -> Tokens -> Nodes -> DOM
- D. Characters -> Nodes -> Tokens -> Bytes -> DOM

Correct answer: C

The browser receives Bytes, decodes to Characters, tokenizes them, turns them into Node objects, and links them into the DOM tree.

- Why not A: It always starts with raw network bytes.
- Why not B: Tokens are generated from characters, not the other way around.
- Why not D: Nodes are built from Tokens.

### Question 2

What happens when the HTML parser encounters a standard `<script src='app.js'></script>` tag in the middle of the `<body>`?

- A. It continues parsing the DOM and downloads the script in the background.
- B. It pauses DOM construction entirely until the script is downloaded and executed.
- C. It aborts parsing and throws a render error.
- D. It builds a shadow DOM for the script to execute in.

Correct answer: B

Standard scripts are 'parser-blocking'. The browser assumes the script might use document.write() to alter the HTML, so it halts all DOM construction to process the script first.

- Why not A: This requires the 'async' or 'defer' attribute.
- Why not C: Scripts don't cause fatal parse aborts.
- Why not D: Scripts run against the main document DOM.

### Question 3

True or False: A browser must download the entire HTML file before it begins building the DOM.

- A. True, because it needs to know all tags to close the <html> root node.
- B. False, browsers parse HTML incrementally as network chunks arrive.
- C. True, to prevent XSS attacks from incomplete scripts.
- D. False, but only if the server sends a special 'Stream-HTML' header.

Correct answer: B

Browsers are heavily optimized to parse HTML incrementally. The moment the first few kilobytes of data arrive, the parser begins generating tokens and nodes.

- Why not A: Browsers don't wait.
- Why not C: Security is handled independently of the streaming parser.
- Why not D: No special header is needed. HTML is inherently streamable.

## Remember this

- The parsing pipeline is: Bytes -> Characters -> Tokens -> Nodes -> DOM.
- Synchronous scripts block the HTML parser.
- Browsers parse HTML incrementally as chunks stream over the network.
