My Digital Garden

JPNAS.NET / Digital Garden

🌱 SeedLast updated:

Inside a Browser Engine

Early notes for a closer look at the engine itself: how HTML becomes a tree, how CSS decides what every box looks like, how layout, paint and the JavaScript engine work – and a tiny engine of my own in Python to see it from the inside.

Open in the interactive Digital Garden

In How a Web Browser Works the engine was one box with an arrow in and an arrow out. This note is meant to open that box. It is still a seed, so for now these are mostly my notes and plans.

The Idea

  • Step by step, with small examples. One short HTML page, followed from the first byte to the last pixel.
  • Build a toy engine myself. Nothing teaches like writing it – a few hundred lines of Python that understand a small part of HTML and CSS and draw boxes and text into an image.
  • Compare with the real ones. What Blink, Gecko, WebKit and Ladybird's LibWeb do at each step, and where they differ.
  • Stay readable. No need to know C++ – the ideas are the same in any language.

What I Want to Cover

  • Parsing HTML: the two stages from the HTML standard – tokenization and tree construction – and why the standard spells out what to do even with broken HTML, so that browsers in practice build the same DOM from it.
  • CSS: selectors, the cascade and specificity – how the engine decides which of many rules wins for every element.
  • Layout: block and inline boxes, line breaking, and the newer flexbox and grid.
  • Paint and composite: display lists, layers, and why some animations are smooth and others stutter.
  • The JavaScript engine: from an interpreter to several levels of JIT compilers (in Chrome's V8 these are the interpreter Ignition and the compilers Sparkplug, Maglev and TurboFan, from quick-and-simple to slow-and-very-fast), the garbage collector, and the event loop that keeps a page responsive.
  • Testing: how the Web Platform Tests keep the engines honest.
  • Example: the toy engine in Python, step by step.
  • Example: finding the same steps in Ladybird's source code.

What I'm reading for this: the book Web Browser Engineering by Pavel Panchekha and Chris Harrelson, which builds a small browser in Python (the whole text is on its website), and Matt Brubeck's seven-part series Let's build a browser engine! from 2014, with a toy engine in Rust called robinson. More background on compilers is in From Source Code to Silicon.

← All notes