Sanitised HTML, cross-shadow selection and Temporal snapshots
I wanted to test three newer browser APIs directly alongside a drawable 2D canvas: inserting untrusted markup with Element.setHTML(), reading a cross-shadow selection with getSelection().getComposedRanges(), and stamping captureElementImage() frames with Temporal.
1. Element.setHTML() into a drawable canvas
Edit the untrusted HTML below, toggle which safe tags the Sanitizer keeps, and press Sanitise and draw. The clean markup goes straight into a drawable child of a 2D canvas, a scan line sweeps across the new DOM, and the diff lists what setHTML() stripped and what stayed.
Ready.
Removed by setHTML()
Kept in the drawable DOM
Show the code setHTML(), Sanitizer config, scan-line paint
- The sanitised target is a drawable child of a 2D canvas.
- Apply
Element.setHTML()with optional removed elements and diff against a parsed inert copy. - Paint the clean DOM at its CSS size and sweep a 700 ms scan line over it.
2. Shadow-crossing selection with getComposedRanges()
Drag a selection from the top paragraph through the framed shadow-root canvas into the bottom paragraph, or use the buttons below on a phone. The canvas hides the browser's native selection colour inside the shadow root and paints a glowing ribbon across the range returned by getSelection().getComposedRanges().
This paragraph starts in the light DOM and runs straight into an open shadow root drawn by a 2D canvas.
The passage finishes back in the light DOM here, so a selection can cross both shadow boundaries in either direction.
getSelection().getRangeAt(0)
- state
- no selection
getComposedRanges({ shadowRoots })
- state
- no selection
Show the code shadow-root canvas, getComposedRanges(), ribbon highlight
- A custom element sits inline in the passage between two light-DOM paragraphs.
- The custom element keeps a drawable 2D canvas in an open shadow root and paints the ribbon under the text.
- Measure client rectangles from the cross-shadow
StaticRangereturned bygetComposedRanges().
3. Temporal timestamps on captured DOM snapshots
Edit the note card inside the canvas or press Mark edit, then capture single frames or a 1-second burst of 4 frames. Each capture calls canvas.captureElementImage(), stamps the snapshot with Temporal.Now.instant() and Temporal.Now.zonedDateTimeISO(), and draws the last 4 frames along the filmstrip while closing every evicted ElementImage.
Open snapshots: 0 / 4. Evicted and closed: 0.
Show the code captureElementImage(), Temporal stamps, ring eviction
- The editable note card is a drawable child of the same 2D canvas that draws the filmstrip.
- Stamp each snapshot with
Temporal.Nowand callclose()on evicted frames and onpagehide. - Draw the frozen filmstrip frames first and the live note card last so clicks land on the live inputs.
How it works
Every canvas on this page uses a 2D context scaled to its backing device pixels via a ResizeObserver. Each ctx.drawElementImage() call passes explicit CSS width and height so elements stay at 1:1 size on high-density screens instead of scaling by devicePixelRatio twice.
In the first experiment, Element.setHTML() parses and sanitises untrusted markup directly into the drawable <article> child. A separate inert DOMParser parse of the raw string computes the tag and attribute diff without running scripts or fetching images. For 700 ms after each sanitisation, requestAnimationFrame calls requestPaint() so the scan line sweeps across the newly painted DOM and then stops.
In the second experiment, <shadow-sel-box> puts its drawable canvas inside an open shadow root. When selectionchange fires, the page compares getSelection().getRangeAt(0) with getSelection().getComposedRanges({ shadowRoots: [shadow] }). Because a StaticRange has no getClientRects() method and a single live Range can't span a shadow boundary, the script slices the composed range against the shadow root's text nodes to collect line rectangles and paints a ribbon behind the text before calling drawElementImage().
In the third experiment, pressing Capture frame sets a pending flag and requests a paint so captureElementImage() runs right after the latest edit paints. The filmstrip holds at most 4 ElementImage objects; adding a fifth calls close() on the oldest snapshot, and pagehide closes every remaining snapshot. During paint, the filmstrip thumbnails draw first and the live note card draws last, because the last drawElementImage() call for an element determines its hit box.
Live checks
Each check builds its own fixture on the off-screen test stage and leaves the three demos above alone. Press the button below to run them in this browser.
| Status | Check | Detail |
|---|
Findings
- Not verified yet in headless Chromium 157 or on Chrome for Android: run the live checks on this page to verify
Element.setHTML(),getSelection().getComposedRanges(),selection.directionandTemporal.NowalongsidedrawElementImage()andcaptureElementImage()in your browser. StaticRangefromgetComposedRanges()doesn't exposegetClientRects(), so painting a canvas highlight across a shadow boundary still requires projecting the composed endpoints onto liveRangeinstances inside each tree.- Drawing captured
ElementImagethumbnails before the live editable card on the same canvas keeps clicks and typing aligned with the live controls while avoiding a second canvas context. - Calling
close()on every evictedElementImageand onpagehidecaps open snapshots at 4 during normal use and returns open count to 0 when cleared.