The browser receives HTML as a text string from the server. To build the DOM and render the page, the browser needs to convert this text into a structured tree. Raw HTML isn’t programmable.
HTML parsing tokenizes HTML text and constructs the DOM tree. The parser is lenient—it corrects malformed HTML automatically (missing closing tags, mismatched tags, etc.).
- Tokenization: Break HTML into tokens (
<div>,class=, text content,</div>) - Tree construction: Assemble tokens into DOM tree
↓ DOM: html → body → h1 → “Hello”<html> <body> <h1>Hello</h1> - Error recovery: Fix common HTML errors automatically
The HTML parser can be paused by <script> tags (unless async/defer).
- Lenient parsing: fixes errors automatically
- Scripts block parsing (unless async/defer)
- Incremental parsing: starts before full HTML arrives
- Output is the DOM tree
- Builds into: DOM Tree — HTML parsing creates the DOM
- Related: Browser Rendering — HTML parsing is step 1
- Related: CSS Parsing — parallel but separate process
- Contrasts with: XML Parsing — HTML is lenient, XML is strict
<script>withoutasync/deferblocks parsingdocument.write()during parsing can break things- Malformed HTML gets “fixed” (may not match intent)
- Table parsing is especially complex