@isoloop/dom 0.0.0 → 0.1.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +10 -1
- package/dist/applier-D5wb4gz9.js +200 -0
- package/dist/dom-Bctskbvg.d.ts +316 -0
- package/dist/dom-yLsf0Z4e.js +1140 -0
- package/dist/host.d.ts +29 -0
- package/dist/host.js +3 -0
- package/dist/install-D7m7By8i.js +37 -0
- package/dist/records-OXGckMg9.d.ts +61 -0
- package/dist/records-wVZrPpqr.js +44 -0
- package/dist/testing-worker.d.ts +57 -0
- package/dist/testing-worker.js +217 -0
- package/dist/testing.d.ts +159 -0
- package/dist/testing.js +699 -0
- package/dist/worker.d.ts +12 -0
- package/dist/worker.js +4 -0
- package/package.json +47 -6
package/dist/host.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** `@isoloop/dom/host`: the applier and node-id map (ADR 0011). Internal to `@isoloop/host`. */
|
|
2
|
+
import { a as ISOLOOP_DOM_FMT, c as ROOT_ID, d as isDomBatch, i as DomRecord, l as SerializedNode, n as DomEvent, o as Namespace, r as DomMods, s as NodeId, t as DomBatch, u as SyncedProp } from "./records-OXGckMg9.js";
|
|
3
|
+
/** Decides what the applier may create and write. Supplied by the host; required. */
|
|
4
|
+
interface SanitisePolicy {
|
|
5
|
+
/** The host element to create for an isolate's tag. */
|
|
6
|
+
element(tag: string, ns: Namespace): {
|
|
7
|
+
tag: string;
|
|
8
|
+
ns: Namespace;
|
|
9
|
+
};
|
|
10
|
+
/** The attribute value to write on a host element, or `null` to drop it. */
|
|
11
|
+
attribute(hostTag: string, ns: Namespace, name: string, value: string): string | null;
|
|
12
|
+
/**
|
|
13
|
+
* Whether to set a synced property on this element now (allow-list and
|
|
14
|
+
* host-owned input rules). `force` asks to override a focused input.
|
|
15
|
+
*/
|
|
16
|
+
property(el: Element, name: SyncedProp, value: string | boolean, force: boolean): boolean;
|
|
17
|
+
}
|
|
18
|
+
interface Applier {
|
|
19
|
+
/** Applies one batch. Returns false, changing nothing, if its shape is wrong. */
|
|
20
|
+
apply(batch: unknown): boolean;
|
|
21
|
+
idFor(node: Node): NodeId | undefined;
|
|
22
|
+
nodeFor(id: NodeId): Node | undefined;
|
|
23
|
+
/** The event record for a DOM event, or `null` if no node on its path listens for it. */
|
|
24
|
+
eventFor(e: Event): DomEvent | null;
|
|
25
|
+
/** Removes everything the isolate rendered and forgets every id. */
|
|
26
|
+
clear(): void;
|
|
27
|
+
}
|
|
28
|
+
export declare function createApplier(container: Element, policy: SanitisePolicy): Applier;
|
|
29
|
+
export { type Applier, type DomBatch, type DomEvent, type DomMods, type DomRecord, ISOLOOP_DOM_FMT, type Namespace, type NodeId, ROOT_ID, type SanitisePolicy, type SerializedNode, type SyncedProp, isDomBatch };
|
package/dist/host.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { _ as WText, a as WDocumentFragment, c as WHTMLElement, d as WHTMLOptionElement, f as WHTMLSelectElement, g as WSVGElement, l as WHTMLIFrameElement, m as WNode, n as WComment, o as WElement, p as WHTMLTextAreaElement, r as WDOMTokenList, u as WHTMLInputElement } from "./dom-yLsf0Z4e.js";
|
|
2
|
+
// src/worker/install.ts
|
|
3
|
+
/** The DOM globals the subset provides (ADR 0010 point 2), by their DOM names. */
|
|
4
|
+
const DOM_GLOBALS = {
|
|
5
|
+
Node: WNode,
|
|
6
|
+
Element: WElement,
|
|
7
|
+
HTMLElement: WHTMLElement,
|
|
8
|
+
SVGElement: WSVGElement,
|
|
9
|
+
Text: WText,
|
|
10
|
+
Comment: WComment,
|
|
11
|
+
DocumentFragment: WDocumentFragment,
|
|
12
|
+
HTMLInputElement: WHTMLInputElement,
|
|
13
|
+
HTMLTextAreaElement: WHTMLTextAreaElement,
|
|
14
|
+
HTMLSelectElement: WHTMLSelectElement,
|
|
15
|
+
HTMLOptionElement: WHTMLOptionElement,
|
|
16
|
+
HTMLIFrameElement: WHTMLIFrameElement,
|
|
17
|
+
DOMTokenList: WDOMTokenList
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Exposes `dom` as the worker's `document`, `window` and DOM classes, so
|
|
21
|
+
* frameworks find them where they look. Only for isolates with a surface; a
|
|
22
|
+
* headless isolate never calls this.
|
|
23
|
+
*/
|
|
24
|
+
function install(dom, scope) {
|
|
25
|
+
const define = (name, value) => {
|
|
26
|
+
Object.defineProperty(scope, name, {
|
|
27
|
+
value,
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
define("document", dom.document);
|
|
33
|
+
if (!("window" in scope)) define("window", scope);
|
|
34
|
+
for (const [name, value] of Object.entries(DOM_GLOBALS)) define(name, value);
|
|
35
|
+
dom.document.defaultView = scope;
|
|
36
|
+
}
|
|
37
|
+
export { install as n, DOM_GLOBALS as t };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
declare const ISOLOOP_DOM_FMT = "isoloop-dom@1";
|
|
2
|
+
/** A node id. The worker assigns them at creation; the root is always 0. */
|
|
3
|
+
type NodeId = number;
|
|
4
|
+
declare const ROOT_ID: NodeId;
|
|
5
|
+
type Namespace = "html" | "svg";
|
|
6
|
+
/** Element properties that are synced as properties, not attributes. */
|
|
7
|
+
type SyncedProp = "value" | "checked" | "selected";
|
|
8
|
+
/** A new subtree, as `insert` carries it. */
|
|
9
|
+
type SerializedNode = {
|
|
10
|
+
k: "el";
|
|
11
|
+
id: NodeId;
|
|
12
|
+
tag: string;
|
|
13
|
+
ns: Namespace;
|
|
14
|
+
attrs: [name: string, value: string][];
|
|
15
|
+
props: [name: SyncedProp, value: string | boolean][];
|
|
16
|
+
/** Event types with at least one listener on this node. */
|
|
17
|
+
listen: string[];
|
|
18
|
+
children: SerializedNode[];
|
|
19
|
+
} | {
|
|
20
|
+
k: "text";
|
|
21
|
+
id: NodeId;
|
|
22
|
+
data: string;
|
|
23
|
+
} | {
|
|
24
|
+
k: "comment";
|
|
25
|
+
id: NodeId;
|
|
26
|
+
data: string;
|
|
27
|
+
};
|
|
28
|
+
type DomRecord = ["insert", parent: NodeId, node: SerializedNode, before: NodeId | null] | ["move", parent: NodeId, node: NodeId, before: NodeId | null] | ["remove", node: NodeId] | ["text", node: NodeId, data: string] | ["attr", node: NodeId, name: string, value: string | null] | ["prop", node: NodeId, name: SyncedProp, value: string | boolean, force?: true] | ["listen", node: NodeId, type: string, on: boolean];
|
|
29
|
+
/** One atomic UI change: every record since the last flush, in order. */
|
|
30
|
+
type DomBatch = DomRecord[];
|
|
31
|
+
/** Modifier keys, as an event record carries them. */
|
|
32
|
+
type DomMods = {
|
|
33
|
+
alt: boolean;
|
|
34
|
+
ctrl: boolean;
|
|
35
|
+
meta: boolean;
|
|
36
|
+
shift: boolean;
|
|
37
|
+
};
|
|
38
|
+
/** A DOM event as the host sends it to the worker half (design §4.6). */
|
|
39
|
+
type DomEvent = {
|
|
40
|
+
type: string;
|
|
41
|
+
target: NodeId;
|
|
42
|
+
/** The target and its ancestors that listen for `type`, innermost first. */
|
|
43
|
+
currentTargets: NodeId[];
|
|
44
|
+
value?: string;
|
|
45
|
+
checked?: boolean;
|
|
46
|
+
key?: string;
|
|
47
|
+
code?: string;
|
|
48
|
+
mods: DomMods;
|
|
49
|
+
x?: number;
|
|
50
|
+
y?: number;
|
|
51
|
+
button?: number;
|
|
52
|
+
/** For `resize` on a transferred canvas: its new CSS size (design §4.7). */
|
|
53
|
+
width?: number;
|
|
54
|
+
height?: number;
|
|
55
|
+
/** For `scroll`: the scrolled element's offsets, mirrored on the worker node. */
|
|
56
|
+
scrollTop?: number;
|
|
57
|
+
scrollLeft?: number;
|
|
58
|
+
};
|
|
59
|
+
/** The batch comes from an isolate, so its shape is checked before anything touches the DOM. */
|
|
60
|
+
declare function isDomBatch(x: unknown): x is DomBatch;
|
|
61
|
+
export { ISOLOOP_DOM_FMT as a, ROOT_ID as c, isDomBatch as d, DomRecord as i, SerializedNode as l, DomEvent as n, Namespace as o, DomMods as r, NodeId as s, DomBatch as t, SyncedProp as u };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// src/shared/records.ts
|
|
2
|
+
const ISOLOOP_DOM_FMT = "isoloop-dom@1";
|
|
3
|
+
const ROOT_ID = 0;
|
|
4
|
+
const isSyncedProp = (x) => x === "value" || x === "checked" || x === "selected";
|
|
5
|
+
const field = (x, k) => Reflect.get(x, k);
|
|
6
|
+
const isId = (x) => typeof x === "number" && Number.isInteger(x) && x >= 0;
|
|
7
|
+
const isStr = (x) => typeof x === "string";
|
|
8
|
+
const isIdOrNull = (x) => x === null || isId(x);
|
|
9
|
+
const isPair = (x, value) => Array.isArray(x) && x.length === 2 && isStr(x[0]) && value(x[1]);
|
|
10
|
+
function isSerializedNode(x, depth) {
|
|
11
|
+
if (depth > 512 || typeof x !== "object" || x === null) return false;
|
|
12
|
+
const k = field(x, "k");
|
|
13
|
+
if (k === "text" || k === "comment") return isId(field(x, "id")) && isStr(field(x, "data"));
|
|
14
|
+
if (k !== "el") return false;
|
|
15
|
+
const [id, tag, ns, attrs, props, listen, children] = [
|
|
16
|
+
"id",
|
|
17
|
+
"tag",
|
|
18
|
+
"ns",
|
|
19
|
+
"attrs",
|
|
20
|
+
"props",
|
|
21
|
+
"listen",
|
|
22
|
+
"children"
|
|
23
|
+
].map((f) => field(x, f));
|
|
24
|
+
return isId(id) && isStr(tag) && (ns === "html" || ns === "svg") && Array.isArray(attrs) && attrs.every((a) => isPair(a, isStr)) && Array.isArray(props) && props.every((p) => isPair(p, (v) => isStr(v) || typeof v === "boolean") && Array.isArray(p) && isSyncedProp(p[0])) && Array.isArray(listen) && listen.every(isStr) && Array.isArray(children) && children.every((c) => isSerializedNode(c, depth + 1));
|
|
25
|
+
}
|
|
26
|
+
function isRecord(x) {
|
|
27
|
+
if (!Array.isArray(x)) return false;
|
|
28
|
+
const [k, a, b, c] = x;
|
|
29
|
+
switch (k) {
|
|
30
|
+
case "insert": return x.length === 4 && isId(a) && isSerializedNode(b, 0) && isIdOrNull(c);
|
|
31
|
+
case "move": return x.length === 4 && isId(a) && isId(b) && isIdOrNull(c);
|
|
32
|
+
case "remove": return x.length === 2 && isId(a);
|
|
33
|
+
case "text": return x.length === 3 && isId(a) && isStr(b);
|
|
34
|
+
case "attr": return x.length === 4 && isId(a) && isStr(b) && (c === null || isStr(c));
|
|
35
|
+
case "prop": return (x.length === 4 || x.length === 5 && x[4] === true) && isId(a) && isSyncedProp(b) && (isStr(c) || typeof c === "boolean");
|
|
36
|
+
case "listen": return x.length === 4 && isId(a) && isStr(b) && typeof c === "boolean";
|
|
37
|
+
default: return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** The batch comes from an isolate, so its shape is checked before anything touches the DOM. */
|
|
41
|
+
function isDomBatch(x) {
|
|
42
|
+
return Array.isArray(x) && x.every(isRecord);
|
|
43
|
+
}
|
|
44
|
+
export { ROOT_ID as n, isDomBatch as r, ISOLOOP_DOM_FMT as t };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { t as DomBatch } from "./records-OXGckMg9.js";
|
|
2
|
+
import { l as WorkerDom } from "./dom-Bctskbvg.js";
|
|
3
|
+
/** One framework major's log, as `packages/dom/usage/<project>.json` stores it. */
|
|
4
|
+
export type UsageLog = {
|
|
5
|
+
/** E.g. `preact@10.29.8`. */
|
|
6
|
+
framework: string;
|
|
7
|
+
/** `Class.member` for every member of the subset that was read, written or called. */
|
|
8
|
+
used: string[];
|
|
9
|
+
/** Members looked up that nothing in the subset defines (reads that came back `undefined`). */
|
|
10
|
+
missing: string[];
|
|
11
|
+
/** Names checked with `in` that the subset doesn't define: feature probes, not uses. */
|
|
12
|
+
probed: string[];
|
|
13
|
+
};
|
|
14
|
+
export type UsageTracker = {
|
|
15
|
+
/** The log so far, sorted. */
|
|
16
|
+
report(framework: string): UsageLog;
|
|
17
|
+
};
|
|
18
|
+
/** What a fixture worker posts to the page. */
|
|
19
|
+
export type FixtureMessage = {
|
|
20
|
+
t: "batch";
|
|
21
|
+
batch: DomBatch;
|
|
22
|
+
member?: number;
|
|
23
|
+
} | {
|
|
24
|
+
t: "usage";
|
|
25
|
+
log: UsageLog;
|
|
26
|
+
} | {
|
|
27
|
+
t: "error";
|
|
28
|
+
error: string;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* A pooled fixture (ADR 0015, M2d): one subset document installed as this
|
|
32
|
+
* worker's `document`, with `n` render roots, one per member. Each root's
|
|
33
|
+
* batches are posted with its `member` index, and the page's events for a
|
|
34
|
+
* member go to its root. Pair with `mountMembers()` on the page.
|
|
35
|
+
*/
|
|
36
|
+
export declare function servePoolFixture(n: number): Element[];
|
|
37
|
+
/**
|
|
38
|
+
* Installs the subset as this worker's `document`, posts every batch, and
|
|
39
|
+
* delivers the page's events. Import it before the framework: React decides
|
|
40
|
+
* at load time whether it has a DOM. Uncaught errors are posted with their
|
|
41
|
+
* stack, which a worker `error` event doesn't carry.
|
|
42
|
+
*/
|
|
43
|
+
export declare function serveFixture(): {
|
|
44
|
+
dom: WorkerDom;
|
|
45
|
+
container: Element;
|
|
46
|
+
};
|
|
47
|
+
/** Starts logging. Call before the framework renders; it can't be undone. */
|
|
48
|
+
export declare function trackUsage(): UsageTracker;
|
|
49
|
+
/**
|
|
50
|
+
* Drives the usage fixture the way a user would, through host events: the
|
|
51
|
+
* fixture renders `<button name="toggle">` (hides and shows a conditional
|
|
52
|
+
* child), `<button name="reorder">` (reverses a keyed list), a controlled
|
|
53
|
+
* `<input>` and a `<select>`. `settle` waits for the framework to commit.
|
|
54
|
+
*/
|
|
55
|
+
export declare function driveUsageFixture(dom: WorkerDom, settle: () => Promise<void>): Promise<void>;
|
|
56
|
+
/** Runs the fixture, lets the framework settle, and posts the log (or the error). */
|
|
57
|
+
export declare function reportUsage(tracker: UsageTracker, framework: string, run: () => Promise<void>): Promise<void>;
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { n as install, t as DOM_GLOBALS } from "./install-D7m7By8i.js";
|
|
2
|
+
import { h as WParentNode, i as WDocument, o as WElement, s as WEventTarget, t as WCharacterData, v as createDom } from "./dom-yLsf0Z4e.js";
|
|
3
|
+
// src/testing/worker.ts
|
|
4
|
+
/** Everything a framework sees: the classes `install()` exposes, and `document`. */
|
|
5
|
+
const CLASSES = [
|
|
6
|
+
["EventTarget", WEventTarget],
|
|
7
|
+
["ParentNode", WParentNode],
|
|
8
|
+
["CharacterData", WCharacterData],
|
|
9
|
+
...Object.entries(DOM_GLOBALS).flatMap(([name, C]) => typeof C === "function" ? [[name, C]] : []),
|
|
10
|
+
["Document", WDocument]
|
|
11
|
+
];
|
|
12
|
+
/** React's own markers (`_reactRootContainer`, `__reactFiber$…`), read before they're written, or never. */
|
|
13
|
+
const ownedByFramework = (key) => /^_+react/.test(key);
|
|
14
|
+
const sorted = (xs) => {
|
|
15
|
+
const out = [...xs];
|
|
16
|
+
out.sort();
|
|
17
|
+
return out;
|
|
18
|
+
};
|
|
19
|
+
const post = (msg) => {
|
|
20
|
+
postMessage(msg);
|
|
21
|
+
};
|
|
22
|
+
const fail = (err) => {
|
|
23
|
+
post({
|
|
24
|
+
t: "error",
|
|
25
|
+
error: err instanceof Error ? err.stack ?? err.message : String(err)
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
const isEventMessage = (x) => typeof x === "object" && x !== null && Reflect.get(x, "t") === "event";
|
|
29
|
+
const catchErrors = () => {
|
|
30
|
+
addEventListener("error", (e) => {
|
|
31
|
+
e.preventDefault();
|
|
32
|
+
fail(e.error);
|
|
33
|
+
});
|
|
34
|
+
addEventListener("unhandledrejection", (e) => {
|
|
35
|
+
e.preventDefault();
|
|
36
|
+
fail(e.reason);
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* A pooled fixture (ADR 0015, M2d): one subset document installed as this
|
|
41
|
+
* worker's `document`, with `n` render roots, one per member. Each root's
|
|
42
|
+
* batches are posted with its `member` index, and the page's events for a
|
|
43
|
+
* member go to its root. Pair with `mountMembers()` on the page.
|
|
44
|
+
*/
|
|
45
|
+
function servePoolFixture(n) {
|
|
46
|
+
const dom = createDom({ onBatch: () => {} });
|
|
47
|
+
install(dom, self);
|
|
48
|
+
const roots = Array.from({ length: n }, (_, member) => dom.addRoot({ onBatch: (batch) => post({
|
|
49
|
+
t: "batch",
|
|
50
|
+
batch,
|
|
51
|
+
member
|
|
52
|
+
}) }));
|
|
53
|
+
addEventListener("message", (e) => {
|
|
54
|
+
if (isEventMessage(e.data)) roots[e.data.member ?? 0]?.deliver(e.data.event);
|
|
55
|
+
});
|
|
56
|
+
catchErrors();
|
|
57
|
+
return roots.map((r) => {
|
|
58
|
+
const container = r.root;
|
|
59
|
+
if (!(container instanceof Element)) throw new Error("the subset is not installed");
|
|
60
|
+
return container;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Installs the subset as this worker's `document`, posts every batch, and
|
|
65
|
+
* delivers the page's events. Import it before the framework: React decides
|
|
66
|
+
* at load time whether it has a DOM. Uncaught errors are posted with their
|
|
67
|
+
* stack, which a worker `error` event doesn't carry.
|
|
68
|
+
*/
|
|
69
|
+
function serveFixture() {
|
|
70
|
+
const dom = createDom({ onBatch: (batch) => post({
|
|
71
|
+
t: "batch",
|
|
72
|
+
batch
|
|
73
|
+
}) });
|
|
74
|
+
install(dom, self);
|
|
75
|
+
addEventListener("message", (e) => {
|
|
76
|
+
if (isEventMessage(e.data)) dom.deliver(e.data.event);
|
|
77
|
+
});
|
|
78
|
+
addEventListener("error", (e) => {
|
|
79
|
+
e.preventDefault();
|
|
80
|
+
fail(e.error);
|
|
81
|
+
});
|
|
82
|
+
addEventListener("unhandledrejection", (e) => {
|
|
83
|
+
e.preventDefault();
|
|
84
|
+
fail(e.reason);
|
|
85
|
+
});
|
|
86
|
+
const container = dom.root;
|
|
87
|
+
if (!(container instanceof Element)) throw new Error("the subset is not installed");
|
|
88
|
+
return {
|
|
89
|
+
dom,
|
|
90
|
+
container
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/** Starts logging. Call before the framework renders; it can't be undone. */
|
|
94
|
+
function trackUsage() {
|
|
95
|
+
const used = /* @__PURE__ */ new Set();
|
|
96
|
+
const missing = /* @__PURE__ */ new Set();
|
|
97
|
+
const probed = /* @__PURE__ */ new Set();
|
|
98
|
+
const expandos = /* @__PURE__ */ new Set();
|
|
99
|
+
const seen = /* @__PURE__ */ new Set();
|
|
100
|
+
for (const [name, C] of CLASSES) {
|
|
101
|
+
const proto = C.prototype;
|
|
102
|
+
if (seen.has(proto)) continue;
|
|
103
|
+
seen.add(proto);
|
|
104
|
+
for (const key of Reflect.ownKeys(proto)) {
|
|
105
|
+
if (typeof key !== "string" || key === "constructor" || key.startsWith("iso")) continue;
|
|
106
|
+
const d = Object.getOwnPropertyDescriptor(proto, key);
|
|
107
|
+
if (!d) continue;
|
|
108
|
+
const tag = `${name}.${key}`;
|
|
109
|
+
const wrap = (fn) => function(...args) {
|
|
110
|
+
used.add(tag);
|
|
111
|
+
return Reflect.apply(fn, this, args);
|
|
112
|
+
};
|
|
113
|
+
for (const part of [
|
|
114
|
+
"value",
|
|
115
|
+
"get",
|
|
116
|
+
"set"
|
|
117
|
+
]) {
|
|
118
|
+
const fn = Reflect.get(d, part);
|
|
119
|
+
if (typeof fn === "function") Reflect.set(d, part, wrap(fn));
|
|
120
|
+
}
|
|
121
|
+
Object.defineProperty(proto, key, d);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const base = Object.prototype;
|
|
125
|
+
const trap = new Proxy(base, {
|
|
126
|
+
get(target, key, receiver) {
|
|
127
|
+
if (typeof key === "string" && !(key in target) && !expandos.has(key) && !ownedByFramework(key)) missing.add(key);
|
|
128
|
+
return Reflect.get(target, key, receiver);
|
|
129
|
+
},
|
|
130
|
+
has(target, key) {
|
|
131
|
+
if (typeof key === "string" && !(key in target) && !expandos.has(key) && !ownedByFramework(key)) probed.add(key);
|
|
132
|
+
return Reflect.has(target, key);
|
|
133
|
+
},
|
|
134
|
+
set(target, key, value, receiver) {
|
|
135
|
+
if (typeof key === "string" && !(key in target)) {
|
|
136
|
+
expandos.add(key);
|
|
137
|
+
missing.delete(key);
|
|
138
|
+
probed.delete(key);
|
|
139
|
+
if (typeof receiver !== "object" || receiver === null) return false;
|
|
140
|
+
return Reflect.defineProperty(receiver, key, {
|
|
141
|
+
value,
|
|
142
|
+
writable: true,
|
|
143
|
+
enumerable: true,
|
|
144
|
+
configurable: true
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
return Reflect.set(target, key, value, receiver);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
Object.setPrototypeOf(WEventTarget.prototype, trap);
|
|
151
|
+
return { report: (framework) => ({
|
|
152
|
+
framework,
|
|
153
|
+
used: sorted(used),
|
|
154
|
+
missing: sorted([...missing].filter((k) => !expandos.has(k))),
|
|
155
|
+
probed: sorted([...probed].filter((k) => !expandos.has(k)))
|
|
156
|
+
}) };
|
|
157
|
+
}
|
|
158
|
+
const mods = {
|
|
159
|
+
alt: false,
|
|
160
|
+
ctrl: false,
|
|
161
|
+
meta: false,
|
|
162
|
+
shift: false
|
|
163
|
+
};
|
|
164
|
+
/** The first element under `from` with this tag (and `name` attribute, if given). */
|
|
165
|
+
function find(from, tag, name) {
|
|
166
|
+
for (let n = from.firstChild; n; n = n.nextSibling) {
|
|
167
|
+
if (!(n instanceof WElement)) continue;
|
|
168
|
+
if (n.localName === tag && (name === void 0 || n.getAttribute("name") === name)) return n;
|
|
169
|
+
const deeper = find(n, tag, name);
|
|
170
|
+
if (deeper) return deeper;
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Drives the usage fixture the way a user would, through host events: the
|
|
176
|
+
* fixture renders `<button name="toggle">` (hides and shows a conditional
|
|
177
|
+
* child), `<button name="reorder">` (reverses a keyed list), a controlled
|
|
178
|
+
* `<input>` and a `<select>`. `settle` waits for the framework to commit.
|
|
179
|
+
*/
|
|
180
|
+
async function driveUsageFixture(dom, settle) {
|
|
181
|
+
const send = (tag, name, type, value) => {
|
|
182
|
+
const el = find(dom.root, tag, name);
|
|
183
|
+
if (!el) throw new Error(`usage fixture: no <${tag}${name ? ` name=${name}` : ""}> rendered`);
|
|
184
|
+
dom.deliver({
|
|
185
|
+
type,
|
|
186
|
+
target: dom.idOf(el),
|
|
187
|
+
currentTargets: [],
|
|
188
|
+
mods,
|
|
189
|
+
...value === void 0 ? {} : { value }
|
|
190
|
+
});
|
|
191
|
+
};
|
|
192
|
+
await settle();
|
|
193
|
+
for (const step of [
|
|
194
|
+
() => send("button", "toggle", "click"),
|
|
195
|
+
() => send("button", "reorder", "click"),
|
|
196
|
+
() => send("input", void 0, "input", "typed"),
|
|
197
|
+
() => send("select", void 0, "change", "b"),
|
|
198
|
+
() => send("button", "toggle", "click")
|
|
199
|
+
]) {
|
|
200
|
+
step();
|
|
201
|
+
await settle();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/** Runs the fixture, lets the framework settle, and posts the log (or the error). */
|
|
205
|
+
async function reportUsage(tracker, framework, run) {
|
|
206
|
+
try {
|
|
207
|
+
await run();
|
|
208
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
209
|
+
post({
|
|
210
|
+
t: "usage",
|
|
211
|
+
log: tracker.report(framework)
|
|
212
|
+
});
|
|
213
|
+
} catch (err) {
|
|
214
|
+
fail(err);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
export { driveUsageFixture, reportUsage, serveFixture, servePoolFixture, trackUsage };
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/** `@isoloop/dom/testing`: the bridge conformance suite (ADR 0011). Needs Vitest. */
|
|
2
|
+
import { l as WorkerDom } from "./dom-Bctskbvg.js";
|
|
3
|
+
import { FixtureMessage, UsageLog } from "./testing-worker.js";
|
|
4
|
+
/** An event as a host bridge sends it to the isolate, reduced to what the suite reads. */
|
|
5
|
+
type ConformanceEvent = {
|
|
6
|
+
type: string;
|
|
7
|
+
target: string;
|
|
8
|
+
currentTargets: string[];
|
|
9
|
+
value?: string;
|
|
10
|
+
mods: {
|
|
11
|
+
alt: boolean;
|
|
12
|
+
ctrl: boolean;
|
|
13
|
+
meta: boolean;
|
|
14
|
+
shift: boolean;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
/** The host side of a bridge, as the compositor drives it. */
|
|
18
|
+
interface ConformanceHost {
|
|
19
|
+
mount(root: ShadowRoot, send: (event: ConformanceEvent) => void): void;
|
|
20
|
+
/** Applies one batch. Returns false and changes nothing if the batch is malformed. */
|
|
21
|
+
apply(batch: unknown): boolean;
|
|
22
|
+
unmount(): void;
|
|
23
|
+
}
|
|
24
|
+
/** A node on the isolate side, as the suite sees it. Opaque to the suite. */
|
|
25
|
+
type Handle = {
|
|
26
|
+
readonly __handle: true;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* The isolate side of a bridge, reduced to the DOM operations the suite needs.
|
|
30
|
+
* Each bridge provides its own; the isoloop-dom driver calls the worker-side
|
|
31
|
+
* DOM subset.
|
|
32
|
+
*/
|
|
33
|
+
interface IsolateDriver {
|
|
34
|
+
/** An element with `class="cf-root"` that the suite builds under. Already inserted. */
|
|
35
|
+
readonly root: Handle;
|
|
36
|
+
el(tag: string): Handle;
|
|
37
|
+
text(data: string): Handle;
|
|
38
|
+
/** `document.createComment(data)`. */
|
|
39
|
+
comment(data: string): Handle;
|
|
40
|
+
/** `document.createDocumentFragment()`; `insert` takes it as a parent or a child. */
|
|
41
|
+
fragment(): Handle;
|
|
42
|
+
/** How many children `parent` has, on the isolate side. */
|
|
43
|
+
childCount(parent: Handle): number;
|
|
44
|
+
/** Inserts `child` into `parent` before `before`, or at the end. Moves it if it's already in the tree. */
|
|
45
|
+
insert(parent: Handle, child: Handle, before?: Handle | null): void;
|
|
46
|
+
remove(child: Handle): void;
|
|
47
|
+
/** `null` removes the attribute. */
|
|
48
|
+
setAttr(el: Handle, name: string, value: string | null): void;
|
|
49
|
+
setText(text: Handle, data: string): void;
|
|
50
|
+
/** Sets the `value` property, as a framework does on a controlled input; `force` overrides a focused host input. */
|
|
51
|
+
setValue(el: Handle, value: string, force?: boolean): void;
|
|
52
|
+
listen(el: Handle, type: string): void;
|
|
53
|
+
/** `el.classList`: `add`, `remove`, `toggle` (with `force`) or `contains`; returns whether `el` has the token after. */
|
|
54
|
+
classList(el: Handle, op: "add" | "remove" | "toggle" | "contains", token: string, force?: boolean): boolean;
|
|
55
|
+
/** `el.dataset[key] = value`, or `delete el.dataset[key]` for `null`; returns `el.dataset[key]` after. */
|
|
56
|
+
dataset(el: Handle, key: string, value?: string | null): string | undefined;
|
|
57
|
+
/** `document.getElementById(id)`. */
|
|
58
|
+
byId(id: string): Handle | null;
|
|
59
|
+
/** `a.contains(b)`. */
|
|
60
|
+
contains(a: Handle, b: Handle | null): boolean;
|
|
61
|
+
/** The batches produced since the last flush, in order. */
|
|
62
|
+
flush(): unknown[];
|
|
63
|
+
/** The node id the isolate side uses for `h`, for bridges that expose ids. */
|
|
64
|
+
idOf(h: Handle): string;
|
|
65
|
+
/** The handle an event's `target` or `currentTargets` entry names. */
|
|
66
|
+
handleFor(nodeId: string): Handle | undefined;
|
|
67
|
+
}
|
|
68
|
+
/** Host-side node-id lookups (design §4.5). Optional until every bridge has them. */
|
|
69
|
+
interface NodeIds {
|
|
70
|
+
idFor(node: Node): string | undefined;
|
|
71
|
+
nodeFor(id: string): Element | undefined;
|
|
72
|
+
}
|
|
73
|
+
interface BridgePair {
|
|
74
|
+
host: ConformanceHost & Partial<NodeIds>;
|
|
75
|
+
isolate: IsolateDriver;
|
|
76
|
+
/** Attributes the bridge writes for its own bookkeeping, left out when trees are compared. */
|
|
77
|
+
internalAttr?: (name: string) => boolean;
|
|
78
|
+
}
|
|
79
|
+
type MakeBridgePair = () => BridgePair;
|
|
80
|
+
/** Cases a bridge may declare as known gaps; they run as expected failures. */
|
|
81
|
+
export declare const CASES: {
|
|
82
|
+
readonly create: "creates elements, attributes and text";
|
|
83
|
+
readonly update: "updates attributes and text, and removes attributes";
|
|
84
|
+
readonly move: "moves a node";
|
|
85
|
+
readonly remove: "removes nodes";
|
|
86
|
+
readonly keyed: "keyed list reorders keep node identity and focus";
|
|
87
|
+
readonly sanitise: "sanitises tags, attributes and URLs (I9)";
|
|
88
|
+
readonly ids: "maps node ids both ways";
|
|
89
|
+
readonly events: "registers listeners and round-trips events";
|
|
90
|
+
readonly noListener: "sends nothing for a node without a listener";
|
|
91
|
+
readonly inputs: "host-owned inputs keep the live value while focused";
|
|
92
|
+
readonly styles: "applies inline styles";
|
|
93
|
+
readonly malformed: "rejects a malformed batch and keeps the tree";
|
|
94
|
+
readonly classList: "classList reads and writes the class attribute";
|
|
95
|
+
readonly dataset: "dataset reads and writes data-* attributes";
|
|
96
|
+
readonly getElementById: "getElementById finds rendered elements by id";
|
|
97
|
+
readonly contains: "contains walks the isolate tree";
|
|
98
|
+
readonly fragment: "inserting a fragment inserts its children in order and empties it";
|
|
99
|
+
readonly comment: "comment nodes round-trip, and their data updates";
|
|
100
|
+
readonly forced: "a forced value replaces a focused input’s value, caret at the end";
|
|
101
|
+
};
|
|
102
|
+
type CaseName = keyof typeof CASES;
|
|
103
|
+
/**
|
|
104
|
+
* The suite every render bridge must pass, with no known gaps for the
|
|
105
|
+
* isoloop-dom bridge. Known gaps can be marked per bridge. Batches go through
|
|
106
|
+
* `structuredClone`, as they would through `postMessage`.
|
|
107
|
+
*/
|
|
108
|
+
export declare function runBridgeConformance(name: string, makePair: MakeBridgePair, opts?: {
|
|
109
|
+
knownGaps?: readonly CaseName[];
|
|
110
|
+
}): void;
|
|
111
|
+
/**
|
|
112
|
+
* The isolate side of isoloop-dom for the conformance suite: the real
|
|
113
|
+
* worker-side subset, run in the test page without installing globals.
|
|
114
|
+
* Batches are collected and handed over on `flush()`.
|
|
115
|
+
*/
|
|
116
|
+
export declare function createIsoloopDomDriver(): IsolateDriver & {
|
|
117
|
+
dom: WorkerDom;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Reads the subset doesn't serve, each with why that's fine. A read of an
|
|
121
|
+
* absent member returns `undefined`, which these call sites handle.
|
|
122
|
+
*/
|
|
123
|
+
export declare const TOLERATED_MISSING: Readonly<Record<string, string>>;
|
|
124
|
+
export declare function checkDomUsage(worker: Worker, logPath: string, write: boolean): Promise<UsageLog>;
|
|
125
|
+
type FixtureHost = {
|
|
126
|
+
/** What the worker rendered, under a `<div>` in the document. */
|
|
127
|
+
readonly container: HTMLElement;
|
|
128
|
+
/**
|
|
129
|
+
* Resolves once the worker has rendered and no batch has arrived for
|
|
130
|
+
* `quietMs` (default 30). Rejects if the worker failed, or rendered nothing
|
|
131
|
+
* within 5 s.
|
|
132
|
+
*/
|
|
133
|
+
settled(quietMs?: number): Promise<void>;
|
|
134
|
+
/** Terminates the worker and removes the container. */
|
|
135
|
+
dispose(): void;
|
|
136
|
+
};
|
|
137
|
+
export declare function mountFixture(worker: Worker): FixtureHost;
|
|
138
|
+
/**
|
|
139
|
+
* The page side of `servePoolFixture(n)`: one container per member, each
|
|
140
|
+
* with its own applier, getting only that member's batches and sending its
|
|
141
|
+
* events back for that member (ADR 0015, M2d task 2).
|
|
142
|
+
*/
|
|
143
|
+
export declare function mountMembers(worker: Worker, n: number): HTMLElement[];
|
|
144
|
+
/**
|
|
145
|
+
* The user actions the smoke tests take on the fixture app (the same ones
|
|
146
|
+
* `driveUsageFixture` sends as events), for a container either way: the
|
|
147
|
+
* fixture host's or a real-DOM render's.
|
|
148
|
+
*/
|
|
149
|
+
export declare const SMOKE_STEPS: readonly {
|
|
150
|
+
name: string;
|
|
151
|
+
act(root: Element): void;
|
|
152
|
+
}[];
|
|
153
|
+
/** What the smoke tests compare: the markup, and the controls' live values. */
|
|
154
|
+
export declare function snapshot(root: Element): {
|
|
155
|
+
html: string;
|
|
156
|
+
input: string;
|
|
157
|
+
select: string;
|
|
158
|
+
};
|
|
159
|
+
export type { BridgePair, CaseName, ConformanceEvent, ConformanceHost, FixtureHost, FixtureMessage, Handle, IsolateDriver, MakeBridgePair, NodeIds, UsageLog };
|