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
      1. The sanitised target is a drawable child of a 2D canvas.
      2. Apply Element.setHTML() with optional removed elements and diff against a parsed inert copy.
      3. 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
      1. A custom element sits inline in the passage between two light-DOM paragraphs.
      2. The custom element keeps a drawable 2D canvas in an open shadow root and paints the ribbon under the text.
      3. Measure client rectangles from the cross-shadow StaticRange returned by getComposedRanges().

      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
        1. The editable note card is a drawable child of the same 2D canvas that draws the filmstrip.
        2. Stamp each snapshot with Temporal.Now and call close() on evicted frames and on pagehide.
        3. 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.

        StatusCheckDetail

        Findings