• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

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.

Core Idea

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.).

How It Works

  1. Tokenization: Break HTML into tokens (<div>, class=, text content, </div>)
  2. Tree construction: Assemble tokens into DOM tree
    <html>
      <body>
        <h1>Hello</h1>
    
    ↓ DOM: html → body → h1 → “Hello”
  3. Error recovery: Fix common HTML errors automatically

The HTML parser can be paused by <script> tags (unless async/defer).

Visual Explanation

G HTML HTML Text <html><body><h1>Hello</h1> Tokens Tokens <html>, <body>, <h1>, Hello, </h1> HTML->Tokens DOM DOM Tree (html → body → h1 → 'Hello') Tokens->DOM

Key Properties

  • Lenient parsing: fixes errors automatically
  • Scripts block parsing (unless async/defer)
  • Incremental parsing: starts before full HTML arrives
  • Output is the DOM tree

Connections

Edge Cases & Gotchas

  • <script> without async/defer blocks parsing
  • document.write() during parsing can break things
  • Malformed HTML gets “fixed” (may not match intent)
  • Table parsing is especially complex