Skip to main content
The Document Object Model (DOM) is the foundation of Ladybird’s web rendering. It represents HTML and XML documents as a tree of nodes, providing the structure that CSS styles and JavaScript manipulates.

Overview

LibWeb’s DOM implementation (LibWeb/DOM/) provides a complete, spec-compliant DOM with:

Node hierarchy

Elements, text, comments, and document nodes

Event system

Event dispatch, bubbling, and capturing

Tree manipulation

Insert, remove, and move nodes

Live collections

Dynamic element and node lists

Document and nodes

Document

The Document class (DOM/Document.h, DOM/Document.cpp) is the root of the DOM tree:
Key responsibilities:
  • URL and origin: Security and resource loading
  • Quirks mode: Compatibility mode detection
  • Style scopes: Stylesheet management
  • Layout invalidation: Trigger re-layout when needed
  • Event dispatch: Root of event propagation

Node hierarchy

All DOM objects inherit from Node:
The node tree is implemented using TreeNode<Node> from LibWeb/TreeNode.h, providing efficient tree traversal and manipulation.

Element

Elements are the most common node type (DOM/Element.h, DOM/Element.cpp):

Text nodes

Text content is represented by Text nodes (DOM/CharacterData.h, DOM/Text.h):

Comment nodes

Comments in HTML/XML (DOM/Comment.h):

ParentNode and ChildNode

Mixin interfaces for nodes with children:

ParentNode

ChildNode

Event system

The DOM event system enables interactive web pages:

EventTarget

Base class for objects that can receive events (DOM/EventTarget.h):

Event

Base event class (DOM/Event.h, DOM/Event.idl):

Event dispatch algorithm

Events propagate through the DOM tree in three phases:
  1. Capturing phase: From document to target
  2. At target phase: On the target element
  3. Bubbling phase: From target back to document
Not all events bubble. For example, focus and blur events don’t bubble, but focusin and focusout do.

Custom events

Create custom events with CustomEvent (DOM/CustomEvent.h):

DOM collections

HTMLCollection

Live collection of elements (DOM/HTMLCollection.h):

NodeList

List of nodes, can be live or static (DOM/NodeList.h):

DOMTokenList

Manage space-separated tokens like classes (DOM/DOMTokenList.h):

Ranges and selections

Range

Represents a portion of the document (DOM/Range.h):

Selection

User’s current selection (Selection/Selection.h):

Mutation observers

Observe changes to the DOM (DOM/MutationObserver.h):
Mutation observers are perfect for reacting to DOM changes without polling. They’re used internally by frameworks and can be very powerful for custom components.

Attributes

Element attributes are managed through Attr nodes:

Attr

NamedNodeMap

Collection of attributes (DOM/NamedNodeMap.h):

Document fragments

Lightweight containers for nodes (DOM/DocumentFragment.h):
Using DocumentFragment for batch insertions is much more efficient than inserting nodes one at a time, as it triggers only one layout invalidation.

Shadow DOM

Encapsulated DOM subtrees (DOM/ShadowRoot.h):

Abort signals

Cancel asynchronous operations (DOM/AbortSignal.h, DOM/AbortController.h):

Tree traversal

Efficient tree walking with TreeWalker and NodeIterator:

Accessibility tree

The accessibility tree (DOM/AccessibilityTreeNode.h) provides structure for assistive technologies:

Update and invalidation

The document tracks what needs updating:

Layout invalidation

Update reasons

Frequent layout invalidation can cause performance issues. Batch DOM changes when possible and avoid reading layout properties (like offsetHeight) during animations.

Integration with other systems

With CSS

With Layout

With JavaScript

LibWeb

Overall rendering engine

CSS

Styling the DOM tree

HTML

HTML-specific elements and behaviors