Architecture
Layers, core building blocks, and design invariants.
# DOM++ Architecture (High Level)
flowchart TB
subgraph Entry["Entry"]
A[installDOMPP / createDOMPP]
B[window.DOMPP]
end
subgraph Builders["Builders (src/builders)"]
EB[createElementBuilder]
CB[createComponentBuilder]
CB2[createBuilders]
end
subgraph Enhancements["Enhancements (src/enhancements)"]
FE[fragment.js]
EE[element.js]
DE[document.js]
end
subgraph Core["Core Engine"]
EB2[ElementBuilder]
CB3[ComponentBuilder]
REC[Reconciliation]
NORM[normalizeChildren]
end
A --> CB2
CB2 --> EB
CB2 --> CB
EB --> EB2
CB --> CB3
CB2 --> FE
CB2 --> EE
CB2 --> DE
FE --> EB2
EE --> EB2
DE --> EB2
DE --> CB3
EB2 --> REC
CB3 --> REC
REC --> NORM
Layers
- Entry:
installDOMPP(doc)installs builders and enhancements onto a document; browser entry auto-installswindow.DOMPP. - Builders: pure factories (
createElementBuilder,createComponentBuilder,createBuilders) with no global dependency. - Enhancements: patch
Document,Element, andDocumentFragmentprototypes to delegate to builders. - Core Engine:
ElementBuilder(virtual descriptor + reconciliation),ComponentBuilder(render/rerender boundary).
Key Invariants
- DOM nodes are real browser nodes; no virtual DOM wrapper.
ElementBuilderholds astructdescriptor and a livenode.- Component render returns the rendered result directly (element or flat array) with a non-enumerable
.rerender. - Fragments and array-returning components produce flat child arrays (per-slot reconciliation).
Setters
Chainable setters, child reconciliation, and attribute safety.
# Setters Flow
Chainable Setters
All setters return the same ElementBuilder for fluent composition.
flowchart LR
C[createElement] --> EB[ElementBuilder]
EB --> SA[setAttributes]
EB --> SS[setStyles]
EB --> SE[setEvents]
EB --> SC[setChildren]
EB --> ST[setText]
SA --> EB
SS --> EB
SE --> EB
SC --> EB
ST --> EB
setChildren Reconciliation
flowchart TD
A[setChildren invoked] --> B[normalizeChildren]
B --> C{node exists?}
C -- no --> D[store in struct.children]
C -- yes --> E[_reconcileChildren]
E --> F{previous child?}
F -- ElementBuilder + same tag --> G[patch in place]
F -- ElementBuilder + diff tag --> H[replace node]
F -- text node --> I[update nodeValue]
F -- none --> J[create & insert]
G --> K[update children array]
H --> K
I --> K
J --> K
setText
setText(text) is setChildren([text]) — normalizes to a single text child and reconciles.
Security Guards (setAttributes)
flowchart TD
A[setAttributes] --> B{attr name?}
B -- on* --> C[throw: inline events disabled]
B -- srcdoc --> D[throw: srcdoc disabled]
B -- style --> E[throw: use setStyles]
B -- URL attr --> F[validate protocol]
B -- other --> G[setAttribute]
F -- unsafe --> H[throw]
F -- safe --> G
Enhancement
Installation flow and how DOM APIs resolve builders.
# Enhancement Flow (High Level)
Installation
flowchart TD
A[installDOMPP doc] --> B[createBuilders doc]
B --> C[installFragmentEnhancements]
B --> D[installElementEnhancements]
B --> E[installDocumentEnhancements]
C --> F[DocumentFragment.prototype.setChildren/setText]
D --> G[Element.prototype setters]
E --> H[Document.prototype.createComponent]
E --> I[Document.prototype.createElement override]
E --> J[Document.prototype.hydrateElement]
E --> K[Document.prototype.createDocumentFragment override]
B --> L[return API object]
Per-Document Builder Resolution
Enhancements store builders on doc.__domppBuilders. Prototype methods resolve builders from target.ownerDocument (or the document itself), so the same prototypes work across documents.
sequenceDiagram
participant U as User code
participant D as Document
participant P as Prototype method
participant B as Builders
U->>D: document.createElement(tag, props, ...children)
D->>P: createElement override
P->>D: getBuilders(this)
D-->>P: builders
P->>B: elementBuilder(...)
B-->>U: ElementBuilder
createElement Routing
flowchart TD
A[createElement tagName, options, rest] --> B{builders installed?}
B -- no --> C[native createElement]
B -- yes --> D{tagName function?}
D -- yes --> E[elementBuilder function component]
D -- no --> F{tagName is element node?}
F -- yes --> G[elementBuilder adopt + reconcile children]
F -- no --> H{options plain object without is?}
H -- yes --> I[elementBuilder with props]
H -- no --> J{rest.length > 0?}
J -- yes --> K[elementBuilder with children]
J -- no --> C
hydrateElement Routing
flowchart TD
A[hydrateElement node, props, ...children] --> B{builders installed?}
B -- no --> C[error: builders not installed]
B -- yes --> D{node is element node?}
D -- no --> E[throw TypeError]
D -- yes --> F[adoptElement(node)]
F --> G[apply props via applyElementProps]
G --> H{children?}
H -- yes --> I[_attachChildren (non-destructive)]
H -- no --> J[return ElementBuilder]
I --> J
createComponent
document.createComponent(builder) returns a rendered ElementBuilder (or flat array) extended with .rerender(callback?).
flowchart TD
A[createComponent builder] --> B[new ComponentBuilder]
B --> C[render immediately]
C --> D{result is array?}
D -- yes --> E[materializeArray]
D -- no --> F[return ElementBuilder]
E --> G[flat child array + .rerender]
F --> H[.rerender attached]
Component
Render, rerender, root changes, and array reconciliation.
# Component Rerender Flow
Render and Rerender
sequenceDiagram
participant U as User
participant C as ComponentBuilder
participant R as Render fn
participant EB as ElementBuilder
participant DOM as DOM
U->>C: createComponent(builder)
C->>R: builder(rerender)
R-->>C: tree
C->>EB: create/reconcile
EB->>DOM: create nodes
C-->>U: rendered result + .rerender
U->>C: result.rerender(callback)
C->>R: apply callback
C->>R: builder(rerender) -> next tree
C->>EB: reconcile (patch in place)
EB->>DOM: patch nodes
C-->>U: same identity
Element Root Tag Change
When a component's root tag changes between renders, identity is preserved by replacing the node but keeping the same ElementBuilder instance.
flowchart TD
A[rerender] --> B{same node?}
B -- yes --> C[sync struct/events]
B -- no --> D{same tag?}
D -- yes --> E[_patchFrom]
D -- no --> F[create nextNode]
F --> G[replaceChild in DOM]
G --> H[update node/struct/events]
H --> I[keep identity]
C --> I
E --> I
Array Component Reconciliation
flowchart TD
A[materializeArray next] --> B[flatten next items]
B --> C[for each slot]
C --> D{nextChild null?}
D -- yes --> E[remove old node]
D -- no --> F[reconcileSlot prev, next, oldNode]
F --> G[update slot node]
C --> H[remove extra old nodes]
H --> I[reorder nodes in DOM]
I --> J[update flat array in place]