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
TheDocument class (DOM/Document.h, DOM/Document.cpp) is the root of the DOM tree:
- 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 fromNode:
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 byText 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:- Capturing phase: From document to target
- At target phase: On the target element
- 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 withCustomEvent (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):
Attributes
Element attributes are managed throughAttr 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 withTreeWalker 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
Integration with other systems
With CSS
With Layout
With JavaScript
Related components
LibWeb
Overall rendering engine
CSS
Styling the DOM tree
HTML
HTML-specific elements and behaviors