diff --git a/.changeset/ui-inner-html-notify.md b/.changeset/ui-inner-html-notify.md
new file mode 100644
index 0000000..4ae01ed
--- /dev/null
+++ b/.changeset/ui-inner-html-notify.md
@@ -0,0 +1,5 @@
+---
+'@clack/ui': patch
+---
+
+Improves `innerHTML` performance by skipping render notifications for nodes built before they are inserted
diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts
index cbd6e6e..5823a36 100644
--- a/packages/ui/src/dom.ts
+++ b/packages/ui/src/dom.ts
@@ -37,6 +37,12 @@ export let createParsedElement: (
namespaceURI: string,
) => Element;
+/**
+ * Runs `build` without change notifications, for nodes built detached. Layout replays only elements it has
+ * already opened, so marks on new nodes go unused; inserting the result notifies as usual.
+ */
+export let buildDetached: (document: Document, build: () => T) => T;
+
const NODE_TYPE_NAMES = [
'ELEMENT_NODE',
'ATTRIBUTE_NODE',
@@ -356,10 +362,20 @@ export class Document extends Node {
readonly #notify: (nodes: Iterable) => void;
#factory: (...params: ConstructorParameters) => Element;
+ #buildingDetached = false;
static {
createParsedElement = (document, localName, namespaceURI) =>
document.#createElement(localName, { namespaceURI, prefix: null });
+ buildDetached = (document, build) => {
+ const previous = document.#buildingDetached;
+ document.#buildingDetached = true;
+ try {
+ return build();
+ } finally {
+ document.#buildingDetached = previous;
+ }
+ };
}
constructor(
@@ -373,7 +389,9 @@ export class Document extends Node {
}
override notify(nodes: Iterable): void {
- this.#notify(nodes);
+ if (!this.#buildingDetached) {
+ this.#notify(nodes);
+ }
}
get nodeType(): number {
diff --git a/packages/ui/src/layout.test.ts b/packages/ui/src/layout.test.ts
index db9156f..8aaabc4 100644
--- a/packages/ui/src/layout.test.ts
+++ b/packages/ui/src/layout.test.ts
@@ -2,6 +2,7 @@ import { close, createTerm, open, pack, text, type Op } from '@bomb.sh/tty';
import { assert, describe, expect, it, vi } from 'vitest';
import { createHTMLDocument } from './dom.ts';
import { Layout } from './layout.ts';
+import { RenderApi } from './render.ts';
describe('Layout', () => {
it('replays unchanged subtrees without reading their children', async () => {
@@ -52,6 +53,44 @@ describe('Layout', () => {
expect(term.render(ops).errors).toEqual([]);
});
+ it('lays out content replaced by innerHTML and replays its untouched siblings', async () => {
+ const controller = new AbortController();
+ const render = new RenderApi(controller.signal);
+ const frames = render[Symbol.asyncIterator]();
+ const document = createHTMLDocument((nodes) => render.request(nodes));
+ const root = document.createElement('document');
+ const kept = document.createElement('document');
+ const host = document.createElement('document');
+ kept.appendChild(document.createTextNode('kept'));
+ host.innerHTML = 'old';
+ root.appendChild(kept);
+ root.appendChild(host);
+ document.body!.appendChild(root);
+ const layout = new Layout(root);
+ const term = await createTerm({ width: 80, height: 24 });
+ const reference = await createTerm({ width: 80, height: 24 });
+ await frames.next();
+ const initial = [...layout.layout()];
+ let result = term.render(initial);
+ reference.render(initial);
+
+ for (const markup of [
+ 'new nestedsibling',
+ 'again',
+ ]) {
+ host.innerHTML = markup;
+ const { value: changes } = await frames.next();
+ assert(changes);
+ const ops = [...layout.layout({ changes, info: result.info })];
+ expect(ops).toContainEqual(result.info.get('DOCUMENT: 1')!.replay);
+ result = term.render(ops);
+ const expected = reference.render([...layout.layout()]);
+ expect(result.errors).toEqual([]);
+ expect(result.output).toEqual(expected.output);
+ }
+ controller.abort();
+ });
+
it('lays out uncached elements and falls back when render info is missing', async () => {
const document = createHTMLDocument(() => {});
const root = document.createElement('document');
diff --git a/packages/ui/src/parse.test.ts b/packages/ui/src/parse.test.ts
index c653fec..38885a7 100644
--- a/packages/ui/src/parse.test.ts
+++ b/packages/ui/src/parse.test.ts
@@ -25,14 +25,20 @@ describe('innerHTML setter', () => {
expect(element.attributes[0]).toMatchObject({ name: 'a:b', prefix: null });
});
- it('notifies the document so a connected UI re-renders', () => {
+ it('notifies for the swap only, not for nodes built while detached', () => {
const notify = vi.fn<(nodes: Iterable) => void>();
const document = createHTMLDocument(notify);
const host = document.body!;
+ host.innerHTML = 'old
';
notify.mockClear();
- host.innerHTML = 'hi';
- const notified = notify.mock.calls.flatMap(([nodes]) => [...nodes]);
- expect(notified).toContain(host);
+ host.innerHTML = 'hi
';
+ const notified = new Set(notify.mock.calls.flatMap(([nodes]) => [...nodes]));
+ const top = host.firstChild!;
+ const nested = top.firstChild!;
+ expect(notified.has(host)).toBe(true);
+ expect(notified.has(top)).toBe(true);
+ expect(notified.has(nested)).toBe(false);
+ expect(notified.has(nested.firstChild!)).toBe(false);
});
it('treats null as the empty string', () => {
diff --git a/packages/ui/src/parse.ts b/packages/ui/src/parse.ts
index 99163e5..def1f9e 100644
--- a/packages/ui/src/parse.ts
+++ b/packages/ui/src/parse.ts
@@ -10,6 +10,7 @@ import {
import { asciiLowercase } from './ascii.ts';
import {
Attr,
+ buildDetached,
createParsedElement,
type Document,
type DocumentFragment,
@@ -25,10 +26,8 @@ export function parseFragment(context: Element, html: string): DocumentFragment
if (document instanceof XMLDocument) {
throw new DOMException('XML fragment parsing is not supported', 'NotSupportedError');
}
- return parseHTMLFragment(html, {
- context: fragmentContext(context),
- sink: createSink(document),
- }) as DocumentFragment;
+ const options = { context: fragmentContext(context), sink: createSink(document) };
+ return buildDetached(document, () => parseHTMLFragment(html, options)) as DocumentFragment;
}
function fragmentContext(element: Element): FragmentContext {