Architecture overview
LibWeb follows a modular architecture organized by web specifications, with each major component implemented in its own namespace:DOM implementation
Document Object Model, nodes, events, and tree structures
CSS engine
Parsing, cascading, computed styles, and visual formatting
Layout engine
Box model, positioning, flex, grid, and layout calculations
Painting system
Rendering pipeline, display lists, and visual output
Directory structure
LibWeb is organized following a clear pattern where each web specification gets its own subdirectory and C++ namespace:- Code location:
Libraries/LibWeb/[SpecName]/ - Namespace:
Web::[SpecName]
- Lives in
LibWeb/XHR/ - Uses namespace
Web::XHR
This structure makes it easy to locate code related to specific web standards. If you’re looking for Fetch API implementation, check
LibWeb/Fetch/. For Geolocation, look in LibWeb/Geolocation/.Key components
HTML and DOM
The HTML and DOM implementations form the foundation of LibWeb:- Document: The root of the document tree, managing the page lifecycle
- Elements: HTML elements like
<div>,<span>,<img>with their specific behaviors - Node tree: The hierarchical structure of DOM nodes
- Event handling: Event dispatching, bubbling, and capturing
Style computation
LibWeb includes a complete CSS implementation:- CSS parsing: Tokenization and parsing of stylesheets
- Cascade and inheritance: Computing which styles apply to each element
- Computed values: Resolving relative units and special values
- Style scopes: Managing stylesheets and their application
Layout
The layout engine transforms the styled DOM into a layout tree:- Box model calculations
- Positioning (static, relative, absolute, fixed, sticky)
- Flexbox and grid layout
- Text layout and line breaking
Painting
The painting system renders the layout tree to pixels:Painting happens in phases to ensure correct layering. For example, backgrounds are painted before foregrounds, and outlines are painted after everything else.
Code patterns and style
LibWeb follows specific code patterns documented inLibWebPatterns.md:
Spec links and comments
All functions representing web specification operations must include:- Spec link above the function:
- Step-by-step comments matching the specification:
Error handling
LibWeb uses several error types for different purposes:AK::ErrorOr<T>
For propagating OOM errors from AK libraries
WebIDL::ExceptionOr<T>
Most common - for errors that interact with JS bindings
WebIDL::SimpleException
Wrapper around ECMAScript built-in errors
WebIDL::DOMException
For web-specific error types from WebIDL spec
IDL and bindings
LibWeb uses WebIDL files to generate JavaScript bindings:- IDL files: Define the JavaScript-visible interface (
.idl) - Implementation: C++ implementation (
.h,.cpp) - Generated bindings: Automatically created glue code
File placement
For most interfaces, all three files live together:Bindings/.
Integration with LibJS
LibWeb tightly integrates with LibJS for JavaScript execution:- Garbage collection: Uses LibJS’s GC for all heap-allocated objects
- Realms: Each document has an associated JS realm
- Value conversion: Seamless conversion between C++ and JS types
- Promise handling: Native support for async operations
Web standards implementation
LibWeb implements a wide range of web standards:| Specification | Directory | Description |
|---|---|---|
| HTML | LibWeb/HTML/ | Elements, forms, scripting, media |
| CSS | LibWeb/CSS/ | Selectors, properties, layout |
| DOM | LibWeb/DOM/ | Document, nodes, events |
| Fetch | LibWeb/Fetch/ | Network requests and responses |
| WebGL | LibWeb/WebGL/ | 3D graphics API |
| WebAudio | LibWeb/WebAudio/ | Audio processing |
| SVG | LibWeb/SVG/ | Scalable vector graphics |
| Canvas | LibWeb/HTML/ | 2D drawing API |
Performance considerations
LibWeb is designed with performance in mind:- Invalidation tracking: Only re-layout or re-paint when necessary
- Display lists: Optimized rendering with cached display lists
- Lazy evaluation: Compute values only when needed
- Optimizations marked: Special fast paths are clearly documented
Development workflow
When working on LibWeb:- Find the spec: Locate the relevant web specification
- Navigate to the directory: Go to
LibWeb/[SpecName]/ - Read the patterns: Check
Documentation/LibWebPatterns.md - Implement with spec links: Add spec URLs and step comments
- Use proper error types: Choose the right error handling approach
Related components
LibJS
JavaScript engine powering scripts
LibWasm
WebAssembly implementation
CSS implementation
Detailed CSS parsing and properties