AUDIO
Source snapshot · 11 November 2026

Browser text editing

Revisions, edits and history

snapshot() returns a frozen {value, selection, revision} view. Keep the exact revision object for an operation; matches(revision) compares object identity. Foreign editors, stale snapshots and serialized or cloned revision objects reject with TextEditorError.code === 'StaleRevision'. The visible sequence is an ordinal, not an ownership token. maximumRevision optionally bounds that ordinal from 1 through Number.MAX_SAFE_INTEGER, which is also the default; exhaustion rejects changes without wrapping. Exact no-ops remain legal at the limit. Tokens are local JavaScript objects, independent of Rust EditRevision, and are not security credentials.

The methods replace(revision, {start, end}, text), undo(revision) and redo(revision) notify onEdit(snapshot, kind) after the committed state and DOM agree. The replace range must be ordered and grapheme-safe. History restores source and directional selection together under a fresh revision, evicts the oldest undo snapshot at capacity, and clears redo for a new source edit. Zero history retains edits without snapshots. An empty undo/redo or source-identical programmatic replace preserves revision, selection and redo.

setSelection(revision, selection) quietly changes only selection and preserves history. update(revision, {value, selection}) quietly resynchronizes source and clears both history directions when the source changes; omitted selection retains the existing offsets and must remain legal for the supplied source. Quiet methods allow consumer acknowledgements without a notification loop. Exact state no-ops preserve revision. Public invalid source, capacity, stale revision and exhaustion errors preserve committed state and history.

Native arrow and pointer selection remain browser behavior, followed by legal selection synchronization. Recognized beforeinput insertion, newline and basic backward/forward deletion use complete graphemes. Unknown or noncancelable input falls back to reconciliation of the actual DOM on input, before notification. Source-identical native typing can collapse the native selection and publish a selection revision while preserving history. Control/Command Z, Shift Z and Control Y use the helper's undo/redo history. Tab leaves the field using normal browser navigation. Hidden, inert, disabled and readonly fields reject native source edits. Programmatic methods can update such fields; they still require a connected field in its original live document.

Notifications execute synchronously under an edit guard. Reentrant mutating calls throw Busy; reading snapshots and disposing are allowed. A callback exception leaves an already committed edit intact. Native event handlers record validation errors or Callback in lastError() and restore invalid native candidates; direct method calls throw to the caller. Dispose is idempotent, cancels pending work, detaches listeners/observers and restores the exact original maxlength attribute. It retains committed source in the consumer's field. Removal or adoption into a different document disposes after the mutation observer runs; immediate public updates reject Unavailable. Disposed operations reject Disposed.