How does the browser parse HTML?
Show answer
It decodes bytes into characters using the declared encoding, tokenizes them into start tags, end tags, text and comments, and a tree builder turns that token stream into the DOM. The parser never throws: HTML5 specifies exact error-recovery rules, so every browser builds the same DOM even from broken markup, for example auto-closing a p when a div starts, or moving stray text out of a table. The doctype only picks the rendering mode: <!doctype html> means standards mode, no doctype means quirks mode. Parsing is incremental, so the browser can start fetching resources and rendering before the whole file has arrived, and a classic <script> pauses it.