kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Memoise the SafeHTML vnode tree

Open pcenov opened this issue 10 months ago • 2 comments

Overview

SafeHTML rebuilds its whole vnode tree on every render: the functional component re-runs DOMPurify.sanitize and remaps the resulting DOM fragment even when the html prop has not changed, so every parent re-render re-diffs the entire article. It should build the tree once per html value and reuse it — also the suspected fix for content being lost around images in the report below.

Complexity: Medium Target branch: develop

Context

packages/kolibri-common/components/SafeHTML/index.js. createSafeHTML returns a functional component whose render re-runs DOMPurify.sanitize(props.html, { RETURN_DOM_FRAGMENT: true }) on every render and maps the fresh fragment through mapChildren — Array.from(childNodes).map(mapNode).filter(Boolean) — yielding raw strings for text nodes and vnodes for elements and components.

The functional, array-returning shape is a holdover from when SafeHTML rendered granularly; the current design maps the whole sanitised tree in one component, so there is no longer a reason to avoid an instance.

The video in the original report shows the same block of the same page load rendering correctly and then corrupted, which places the fault in re-rendering rather than in the source HTML:

  • ~12s — Hummingbird Viewing Tips → canary paragraph complete → Creating a Hummingbird Garden → style canary → Hummingbird Gallery 2 → image.
  • ~20s — image → tag was not properly removed by the sanitizer. with its leading text gone → Creating a Hummingbird Garden → style canary → Hummingbird Gallery 2 → image.

Not reproduced on demand. Studio's Kolibri QA Channel is v161, matching the imported copy, and that kpub has a single gallery section and a single sanitization-test section — no repeated gallery/canary blocks, and no text adjacent to an image within a section. On develop, scrolling, resizing, 60s idle, six reloads, and $forceUpdate() on both ContentViewer and SafeHtml5RendererIndex all left the DOM intact; one sample did show 10 <section>s reduced to 6 with all 8 embedded viewers gone, trigger unidentified.

Original report (pcenov, 2026-01-26)

Observed behavior

Observed while testing https://github.com/learningequality/kolibri/pull/14089 but it's also present in Kolibri 0.19.1:

Under the images there's the following text: tag was not properly removed by the sanitizer.

See the following video where I'm comparing the current version against the official 0.19 Kolibri build:

https://github.com/user-attachments/assets/df416f31-d7f2-4091-8b06-7ce790c7e165

Note that I'm not seeing this issue in Microsoft's Edge browser where everything is working fine.

Expected behavior

No error message.

Steps to reproduce the issue

  1. Install the build from https://github.com/learningequality/kolibri/pull/14089 or Kolibri 0.19.1
  2. Enable the safe html5 viewer plugin: kolibri plugin enable kolibri.plugins.safe_html5_viewer
  3. Download the QA Channel and observe the 'All About Hummingbirds (long article)'

Usage Details

Windows 11, Ubuntu 22, Mac OS 15.5 - Chrome, Firefox, Safari

The Change

  • SafeHTML should become a stateful component that derives its vnode tree from a computed on the html prop, so DOMPurify.sanitize and the DOM-to-vnode mapping run once per html value rather than on every render.
  • A re-render with unchanged html should reuse the same vnode objects, so Vue's patch short-circuits at oldVnode === vnode (vue.runtime.common.dev.js:6807) instead of re-diffing the whole article.
  • A stateful render function cannot return an array of roots, so the sanitised content should render inside a single wrapper element. All three consumers already place SafeHTML in a block context, so the extra nesting level is absorbed, but SafeHTML/style.scss selectors that assume the current flattening need checking.

How to Get There

  • Kolibri QA Channel → All About Hummingbirds (long article).
  • QTI consumers: /en/learn/#/qti_sandbox/:itemId? — registered from kolibri/plugins/learn/frontend/routes/index.js:189 in non-production builds only.

Out of Scope

  • Keying SafeHTML's mapped children. With the tree memoised the diff does not run, and an unkeyed mismatch causes wrong component reuse rather than lost text.
  • Reproducing the original report; the change does not depend on a repro.

Acceptance Criteria

  • [ ] SafeHTML is a stateful component whose vnode tree comes from a computed on the html prop.
  • [ ] DOMPurify.sanitize and the DOM-to-vnode mapping run once per html value, not once per render.
  • [ ] A parent re-render with unchanged html leaves the rendered DOM untouched — the same element references are still in place afterwards.
  • [ ] Changing html renders the new content.
  • [ ] The sanitised content renders inside a single wrapper element, with SafeHTML/style.scss adjusted wherever the added nesting level breaks a selector.
  • [ ] The three consumers render unchanged: the safe_html5_viewer article, the QTI item body with its custom components, and QTI hints.
  • [ ] Tests cover re-render with unchanged html preserving the DOM, and html changing producing new content.

AI usage

Used Claude Code to investigate this and draft the issue. It pulled frames from pcenov's video to establish that the text is lost on re-render rather than being wrong in the source, checked the claims about Vue's patching against the [email protected] dist source rather than asserting them from memory, and tried and failed to reproduce the defect against a local devserver. Its first diagnosis — unkeyed children losing the text — I rejected, since patch converges on the new tree regardless; memoisation is what survived that. I directed the structure and reviewed each section.

pcenov avatar Jan 26 '26 10:01 pcenov

@radinamatic

pcenov avatar Jan 26 '26 10:01 pcenov

Plan: SafeHTML builds its vnode tree once per html value instead of on every render.

  • [ ] Capture the pre-change render baseline
  • [ ] Memoise the vnode tree behind a single wrapper root
  • [ ] Confirm the three consumers render unchanged
  • [ ] Self-review the produced code

@rtibblesbot's comments are generated by an LLM, and should be evaluated accordingly

How was this generated?
  • Implemented code following the pre-approved plan using test-driven development (red-green-refactor)
  • Ran the project's full test suite and linter before committing
  • For UI changes: captured live screenshots to verify rendering
  • For backend changes: verified with example API/CLI output

rtibblesbot avatar Aug 15 '26 00:08 rtibblesbot