# The snapshot algorithm
**Normative companion to [The Malleable HTML File Specification](specification.txt), section 2.** Version 1, draft. July 2026. Public domain (CC0).
The main specification names the steps; this page fixes their exact behavior and order. Two independently written clients that both follow this page produce the same bytes from the same DOM. When prose and fixtures disagree, the conformance fixtures win.
## The two artifacts
One capture pipeline produces two artifacts:
- the **snapshot**: the full serialized state of the live page (steps 1 through 6),
- the **document**: the snapshot taken through the save-time steps (7 and 8). This is the only artifact that reaches disk.
## The algorithm
A client must perform these steps in this order.
**1. Settle.** Give the page a chance to reach a clean boundary before capture. A client that batches undo history must flush any open batch here, so the captured state never straddles an undo boundary. A client with no such machinery skips this step.
**2. Clone.** Deep-clone `document.documentElement`. Never mutate the live DOM during capture; every following step operates on the clone. If cloning can trigger page-defined side effects (custom element callbacks, clone guards), the client must suppress them for the duration of the clone.
**3. Sync form state into markup.** Live form values exist only in JavaScript object properties and would not survive serialization. Reflect them into the clone's attributes and content:
| Control | Rule |
|---|---|
| `` and other textual inputs | set the `value` attribute to the live `.value` |
| `` and `type=radio` | set or remove the `checked` attribute per the live `.checked` |
| `