@opentf/afw 0.0.0-stage → 0.33.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 +123 -2
- package/components/Link.d.ts +12 -0
- package/components/Link.jsx +54 -0
- package/core/context.d.ts +7 -0
- package/core/context.js +60 -0
- package/core/dev.js +27 -0
- package/core/errors.d.ts +14 -0
- package/core/errors.js +48 -0
- package/core/html.js +10 -0
- package/core/metadata.js +186 -0
- package/core/reactive.d.ts +5 -0
- package/core/reactive.js +190 -0
- package/core/render-context.js +11 -0
- package/core/signals.d.ts +8 -0
- package/core/signals.js +264 -0
- package/index.d.ts +5 -0
- package/index.js +13 -0
- package/jsx-runtime.d.ts +37 -0
- package/package.json +66 -4
- package/runtime/clipboard.d.ts +7 -0
- package/runtime/clipboard.js +47 -0
- package/runtime/code-block.d.ts +14 -0
- package/runtime/code-block.js +76 -0
- package/runtime/context.browser.js +163 -0
- package/runtime/context.d.ts +13 -0
- package/runtime/context.js +44 -0
- package/runtime/dom.browser.js +423 -0
- package/runtime/dom.d.ts +142 -0
- package/runtime/dom.js +670 -0
- package/runtime/error-boundary.browser.js +140 -0
- package/runtime/error-boundary.d.ts +19 -0
- package/runtime/error-boundary.js +123 -0
- package/runtime/events.browser.js +29 -0
- package/runtime/events.d.ts +1 -0
- package/runtime/events.js +17 -0
- package/runtime/head.js +29 -0
- package/runtime/hmr.js +199 -0
- package/runtime/hydrate.browser.js +144 -0
- package/runtime/hydrate.d.ts +179 -0
- package/runtime/hydrate.js +584 -0
- package/runtime/index.d.ts +14 -0
- package/runtime/index.js +24 -0
- package/runtime/lifecycle.d.ts +6 -0
- package/runtime/lifecycle.js +14 -0
- package/runtime/mount.d.ts +20 -0
- package/runtime/mount.js +88 -0
- package/runtime/portal.browser.js +85 -0
- package/runtime/portal.d.ts +12 -0
- package/runtime/portal.js +74 -0
- package/runtime/raw-html.d.ts +11 -0
- package/runtime/raw-html.js +55 -0
- package/runtime/resource.d.ts +9 -0
- package/runtime/resource.js +139 -0
- package/runtime/route-data.d.ts +15 -0
- package/runtime/route-data.js +64 -0
- package/runtime/router.d.ts +149 -0
- package/runtime/router.js +807 -0
- package/server/adapters/node.d.ts +28 -0
- package/server/adapters/node.js +74 -0
- package/server/api.d.ts +175 -0
- package/server/api.js +233 -0
- package/server/builtins.js +42 -0
- package/server/cookies.d.ts +44 -0
- package/server/cookies.js +122 -0
- package/server/head.js +9 -0
- package/server/index.d.ts +155 -0
- package/server/index.js +31 -0
- package/server/loader.js +201 -0
- package/server/middleware.js +119 -0
- package/server/render-context.js +30 -0
- package/server/render.js +175 -0
- package/server/ssg-runtime.js +297 -0
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import { describe, expect, test } from "../../afw-test/browser-runner.js";
|
|
2
|
+
|
|
3
|
+
import { signal } from "../core/signals.js";
|
|
4
|
+
import { bindAttr, bindChild, bindList, bindText, mount, setAttr, setProp, spread, toText } from "./index.js";
|
|
5
|
+
|
|
6
|
+
describe("toText", () => {
|
|
7
|
+
test("renders nullish and false as empty string", () => {
|
|
8
|
+
expect(toText(null)).toBe("");
|
|
9
|
+
expect(toText(undefined)).toBe("");
|
|
10
|
+
expect(toText(false)).toBe("");
|
|
11
|
+
expect(toText(0)).toBe("0");
|
|
12
|
+
expect(toText("hi")).toBe("hi");
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe("setAttr", () => {
|
|
17
|
+
test("sets style objects, properties, attributes, and removes nullish", () => {
|
|
18
|
+
const el = document.createElement("div");
|
|
19
|
+
|
|
20
|
+
setAttr(el, "style", { color: "red" });
|
|
21
|
+
expect(el.style.color).toBe("red");
|
|
22
|
+
|
|
23
|
+
// Numbers get px appended; unitless properties don't; custom props go through.
|
|
24
|
+
setAttr(el, "style", { width: 8, opacity: 0.5, "--gap": 4 });
|
|
25
|
+
expect(el.style.width).toBe("8px");
|
|
26
|
+
expect(el.style.opacity).toBe("0.5");
|
|
27
|
+
expect(el.style.getPropertyValue("--gap")).toBe("4");
|
|
28
|
+
|
|
29
|
+
setAttr(el, "id", "x"); // plain attribute
|
|
30
|
+
expect(el.getAttribute("id")).toBe("x");
|
|
31
|
+
|
|
32
|
+
const input = document.createElement("input");
|
|
33
|
+
setAttr(input, "value", "hello"); // property-backed
|
|
34
|
+
expect(input.value).toBe("hello");
|
|
35
|
+
|
|
36
|
+
setAttr(el, "title", null); // removed
|
|
37
|
+
expect(el.hasAttribute("title")).toBe(false);
|
|
38
|
+
|
|
39
|
+
setAttr(el, "hidden", true); // boolean attribute present
|
|
40
|
+
expect(el.getAttribute("hidden")).toBe("");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("writes enumerated and ARIA attributes as 'true'/'false' keywords", () => {
|
|
44
|
+
const el = document.createElement("div");
|
|
45
|
+
|
|
46
|
+
setAttr(el, "draggable", true); // enumerated: needs the literal keyword
|
|
47
|
+
expect(el.getAttribute("draggable")).toBe("true");
|
|
48
|
+
|
|
49
|
+
setAttr(el, "draggable", false); // not removed — keyword "false"
|
|
50
|
+
expect(el.getAttribute("draggable")).toBe("false");
|
|
51
|
+
|
|
52
|
+
setAttr(el, "aria-expanded", true);
|
|
53
|
+
expect(el.getAttribute("aria-expanded")).toBe("true");
|
|
54
|
+
|
|
55
|
+
setAttr(el, "aria-hidden", null); // nullish still removes
|
|
56
|
+
expect(el.hasAttribute("aria-hidden")).toBe(false);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("wires on* handlers as properties", () => {
|
|
60
|
+
const el = document.createElement("button");
|
|
61
|
+
let clicks = 0;
|
|
62
|
+
setAttr(el, "onClick", () => clicks++);
|
|
63
|
+
el.click();
|
|
64
|
+
expect(clicks).toBe(1);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe("bindText", () => {
|
|
69
|
+
test("updates a text node reactively", () => {
|
|
70
|
+
const name = signal("a");
|
|
71
|
+
const node = document.createTextNode("");
|
|
72
|
+
bindText(node, () => name.value);
|
|
73
|
+
expect(node.data).toBe("a");
|
|
74
|
+
name.value = "b";
|
|
75
|
+
expect(node.data).toBe("b");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("dispose stops updates", () => {
|
|
79
|
+
const n = signal(1);
|
|
80
|
+
const node = document.createTextNode("");
|
|
81
|
+
const dispose = bindText(node, () => n.value);
|
|
82
|
+
dispose();
|
|
83
|
+
n.value = 2;
|
|
84
|
+
expect(node.data).toBe("1");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("elides the write when the rendered text is unchanged", () => {
|
|
88
|
+
const tick = signal(0);
|
|
89
|
+
const node = document.createTextNode("");
|
|
90
|
+
let writes = 0;
|
|
91
|
+
Object.defineProperty(node, "data", {
|
|
92
|
+
get() { return this._d ?? ""; },
|
|
93
|
+
set(v) { writes++; this._d = v; },
|
|
94
|
+
});
|
|
95
|
+
bindText(node, () => { tick.value; return "constant"; });
|
|
96
|
+
expect(node.data).toBe("constant");
|
|
97
|
+
expect(writes).toBe(1);
|
|
98
|
+
tick.value = 1; // re-runs, same text → no write
|
|
99
|
+
expect(writes).toBe(1);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("inserts node-valued expressions (JSX stored as a value) and swaps them", () => {
|
|
103
|
+
const parent = document.createElement("div");
|
|
104
|
+
const anchor = document.createTextNode("");
|
|
105
|
+
parent.appendChild(anchor);
|
|
106
|
+
const a = document.createElement("i");
|
|
107
|
+
const b = document.createElement("b");
|
|
108
|
+
const which = signal(a);
|
|
109
|
+
bindText(anchor, () => which.value);
|
|
110
|
+
expect(parent.querySelector("i")).toBe(a);
|
|
111
|
+
expect(parent.querySelector("b")).toBeNull();
|
|
112
|
+
which.value = b;
|
|
113
|
+
expect(parent.querySelector("i")).toBeNull();
|
|
114
|
+
expect(parent.querySelector("b")).toBe(b);
|
|
115
|
+
// Falling back to text removes the previously inserted node.
|
|
116
|
+
which.value = "hi";
|
|
117
|
+
expect(parent.querySelector("b")).toBeNull();
|
|
118
|
+
expect(parent.textContent).toBe("hi");
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe("setProp", () => {
|
|
123
|
+
test("sets a property when the element exposes one, else an attribute", () => {
|
|
124
|
+
// Custom element with a value property setter (like ContextProvider).
|
|
125
|
+
let received;
|
|
126
|
+
customElements.define(
|
|
127
|
+
"x-setprop",
|
|
128
|
+
class extends HTMLElement {
|
|
129
|
+
set value(v) {
|
|
130
|
+
received = v;
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
);
|
|
134
|
+
const ce = document.createElement("x-setprop");
|
|
135
|
+
|
|
136
|
+
const obj = { a: 1 };
|
|
137
|
+
setProp(ce, "value", obj); // property exists → setter receives the object
|
|
138
|
+
expect(received).toBe(obj);
|
|
139
|
+
|
|
140
|
+
setProp(ce, "class", "card"); // no such property → attribute
|
|
141
|
+
expect(ce.getAttribute("class")).toBe("card");
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe("bindAttr", () => {
|
|
146
|
+
test("updates an attribute reactively", () => {
|
|
147
|
+
const cls = signal("on");
|
|
148
|
+
const el = document.createElement("div");
|
|
149
|
+
bindAttr(el, "class", () => cls.value);
|
|
150
|
+
expect(el.getAttribute("class")).toBe("on");
|
|
151
|
+
cls.value = "off";
|
|
152
|
+
expect(el.getAttribute("class")).toBe("off");
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("elides DOM writes when the primitive value is unchanged", () => {
|
|
156
|
+
// Models a keyed-list row whose class depends on a shared `selected` signal:
|
|
157
|
+
// most rows recompute to the same value and must not touch the DOM.
|
|
158
|
+
const selected = signal(-1);
|
|
159
|
+
const rowId = 7;
|
|
160
|
+
const el = document.createElement("div");
|
|
161
|
+
let writes = 0;
|
|
162
|
+
const orig = el.setAttribute.bind(el);
|
|
163
|
+
el.setAttribute = (...a) => { writes++; return orig(...a); };
|
|
164
|
+
|
|
165
|
+
bindAttr(el, "class", () => (selected.value === rowId ? "danger" : ""));
|
|
166
|
+
expect(writes).toBe(1); // initial write ("")
|
|
167
|
+
|
|
168
|
+
selected.value = 999; // still not this row → recomputes to "" → no write
|
|
169
|
+
expect(writes).toBe(1);
|
|
170
|
+
|
|
171
|
+
selected.value = rowId; // now selected → one real write
|
|
172
|
+
expect(el.getAttribute("class")).toBe("danger");
|
|
173
|
+
expect(writes).toBe(2);
|
|
174
|
+
|
|
175
|
+
selected.value = 999; // deselected → one real write back to ""
|
|
176
|
+
expect(writes).toBe(3);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("always re-applies object values (may have mutated internally)", () => {
|
|
180
|
+
const tick = signal(0);
|
|
181
|
+
const style = { width: 1 };
|
|
182
|
+
const el = document.createElement("div");
|
|
183
|
+
bindAttr(el, "style", () => {
|
|
184
|
+
tick.value; // depend on the signal
|
|
185
|
+
style.width += 1; // same object reference, new contents
|
|
186
|
+
return style;
|
|
187
|
+
});
|
|
188
|
+
expect(el.style.width).toBe("2px");
|
|
189
|
+
tick.value = 1; // re-run: same ref, but must re-apply
|
|
190
|
+
expect(el.style.width).toBe("3px");
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
describe("bindList", () => {
|
|
195
|
+
const render = (sig) => {
|
|
196
|
+
const li = document.createElement("li");
|
|
197
|
+
bindText(li.appendChild(document.createTextNode("")), () => sig.value.name);
|
|
198
|
+
return li;
|
|
199
|
+
};
|
|
200
|
+
const key = (item) => item.id;
|
|
201
|
+
const text = (parent) =>
|
|
202
|
+
Array.from(parent.querySelectorAll("li")).map((li) => li.textContent).join(",");
|
|
203
|
+
|
|
204
|
+
test("renders, reorders (reusing nodes), updates, and removes by key", () => {
|
|
205
|
+
const items = signal([{ id: 1, name: "a" }, { id: 2, name: "b" }]);
|
|
206
|
+
const parent = document.createElement("ul");
|
|
207
|
+
bindList(parent, () => items.value, render, key);
|
|
208
|
+
expect(text(parent)).toBe("a,b");
|
|
209
|
+
|
|
210
|
+
const firstA = parent.querySelector("li");
|
|
211
|
+
|
|
212
|
+
// Insert in the middle.
|
|
213
|
+
items.value = [{ id: 1, name: "a" }, { id: 3, name: "c" }, { id: 2, name: "b" }];
|
|
214
|
+
expect(text(parent)).toBe("a,c,b");
|
|
215
|
+
|
|
216
|
+
// Reorder + drop: the node for id:1 must be the *same* element (keyed reuse).
|
|
217
|
+
items.value = [{ id: 2, name: "b" }, { id: 1, name: "a" }];
|
|
218
|
+
expect(text(parent)).toBe("b,a");
|
|
219
|
+
expect(parent.querySelectorAll("li")[1]).toBe(firstA);
|
|
220
|
+
|
|
221
|
+
// Fine-grained update: same node, new text.
|
|
222
|
+
items.value = [{ id: 2, name: "b" }, { id: 1, name: "A" }];
|
|
223
|
+
expect(text(parent)).toBe("b,A");
|
|
224
|
+
expect(parent.querySelectorAll("li")[1]).toBe(firstA);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test("swapping two rows moves only those two nodes (minimal reconciliation)", () => {
|
|
228
|
+
const data = Array.from({ length: 20 }, (_, i) => ({ id: i, name: `n${i}` }));
|
|
229
|
+
const items = signal(data);
|
|
230
|
+
const parent = document.createElement("ul");
|
|
231
|
+
bindList(parent, () => items.value, render, key);
|
|
232
|
+
|
|
233
|
+
let moves = 0;
|
|
234
|
+
const orig = parent.insertBefore.bind(parent);
|
|
235
|
+
parent.insertBefore = (...a) => { moves++; return orig(...a); };
|
|
236
|
+
|
|
237
|
+
const swapped = data.slice();
|
|
238
|
+
const tmp = swapped[1];
|
|
239
|
+
swapped[1] = swapped[18];
|
|
240
|
+
swapped[18] = tmp;
|
|
241
|
+
items.value = swapped;
|
|
242
|
+
|
|
243
|
+
expect(moves).toBe(2); // not ~16 from a cascading nextSibling fix-up
|
|
244
|
+
expect(text(parent)).toBe("n0,n18,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17,n1,n19");
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test("falls back to index when no keyFn is given", () => {
|
|
248
|
+
const items = signal(["x", "y"]);
|
|
249
|
+
const parent = document.createElement("ul");
|
|
250
|
+
bindList(parent, () => items.value, (sig) => {
|
|
251
|
+
const li = document.createElement("li");
|
|
252
|
+
bindText(li.appendChild(document.createTextNode("")), () => sig.value);
|
|
253
|
+
return li;
|
|
254
|
+
});
|
|
255
|
+
expect(text(parent)).toBe("x,y");
|
|
256
|
+
items.value = ["x", "y", "z"];
|
|
257
|
+
expect(text(parent)).toBe("x,y,z");
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test("evicting a row disposes its bindings (no zombie effects on shared signals)", () => {
|
|
261
|
+
// Every row's class binding reads the shared `selected` signal — the
|
|
262
|
+
// js-framework-benchmark "select row" shape. Evicted rows must unsubscribe,
|
|
263
|
+
// or each rebuild leaks 1 effect per row and every later `selected` write
|
|
264
|
+
// re-runs all of them (this made select-row cost more than create-1k).
|
|
265
|
+
const selected = signal(-1);
|
|
266
|
+
const items = signal([{ id: 1 }, { id: 2 }]);
|
|
267
|
+
const parent = document.createElement("ul");
|
|
268
|
+
let classRuns = 0;
|
|
269
|
+
bindList(
|
|
270
|
+
parent,
|
|
271
|
+
() => items.value,
|
|
272
|
+
(sig) => {
|
|
273
|
+
const li = document.createElement("li");
|
|
274
|
+
bindAttr(li, "class", () => {
|
|
275
|
+
classRuns++;
|
|
276
|
+
return selected.value === sig.value.id ? "on" : "";
|
|
277
|
+
});
|
|
278
|
+
return li;
|
|
279
|
+
},
|
|
280
|
+
(item) => item.id,
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
items.value = [{ id: 3 }, { id: 4 }]; // all-new keys: rows 1,2 evicted
|
|
284
|
+
classRuns = 0;
|
|
285
|
+
selected.value = 3;
|
|
286
|
+
expect(classRuns).toBe(2); // only the 2 live rows, not 4
|
|
287
|
+
expect(parent.querySelector("li").getAttribute("class")).toBe("on");
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
test("dispose stops the list and its per-row bindings", () => {
|
|
291
|
+
const label = signal("a");
|
|
292
|
+
const items = signal([{ id: 1 }]);
|
|
293
|
+
const parent = document.createElement("ul");
|
|
294
|
+
let runs = 0;
|
|
295
|
+
const dispose = bindList(
|
|
296
|
+
parent,
|
|
297
|
+
() => items.value,
|
|
298
|
+
() => {
|
|
299
|
+
const li = document.createElement("li");
|
|
300
|
+
bindText(li.appendChild(document.createTextNode("")), () => {
|
|
301
|
+
runs++;
|
|
302
|
+
return label.value;
|
|
303
|
+
});
|
|
304
|
+
return li;
|
|
305
|
+
},
|
|
306
|
+
(item) => item.id,
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
dispose();
|
|
310
|
+
runs = 0;
|
|
311
|
+
items.value = [{ id: 2 }]; // reconciliation stopped
|
|
312
|
+
label.value = "b"; // row binding disposed
|
|
313
|
+
expect(runs).toBe(0);
|
|
314
|
+
expect(parent.querySelector("li").textContent).toBe("a"); // frozen
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
describe("spread", () => {
|
|
319
|
+
test("applies attributes to elements and properties to components", () => {
|
|
320
|
+
const el = document.createElement("div");
|
|
321
|
+
spread(el, { id: "a", title: "t" }, false);
|
|
322
|
+
expect(el.getAttribute("id")).toBe("a");
|
|
323
|
+
expect(el.getAttribute("title")).toBe("t");
|
|
324
|
+
|
|
325
|
+
const comp = document.createElement("div"); // stand-in for a custom element
|
|
326
|
+
spread(comp, { foo: { nested: 1 }, bar: 2 }, true);
|
|
327
|
+
expect(comp.foo).toEqual({ nested: 1 });
|
|
328
|
+
expect(comp.bar).toBe(2);
|
|
329
|
+
|
|
330
|
+
spread(el, null, false); // nullish is a no-op
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
describe("bindChild", () => {
|
|
335
|
+
test("swaps conditional elements, text, and nothing", () => {
|
|
336
|
+
const which = signal(0);
|
|
337
|
+
const parent = document.createElement("div");
|
|
338
|
+
const anchor = parent.appendChild(document.createComment(""));
|
|
339
|
+
bindChild(anchor, () => {
|
|
340
|
+
if (which.value === 0) return null; // nothing
|
|
341
|
+
if (which.value === 1) {
|
|
342
|
+
const p = document.createElement("p");
|
|
343
|
+
p.textContent = "one";
|
|
344
|
+
return p;
|
|
345
|
+
}
|
|
346
|
+
return "text"; // primitive → text node
|
|
347
|
+
});
|
|
348
|
+
expect(parent.textContent).toBe("");
|
|
349
|
+
expect(parent.querySelector("p")).toBeNull();
|
|
350
|
+
|
|
351
|
+
which.value = 1;
|
|
352
|
+
expect(parent.querySelector("p").textContent).toBe("one");
|
|
353
|
+
|
|
354
|
+
which.value = 2;
|
|
355
|
+
expect(parent.querySelector("p")).toBeNull();
|
|
356
|
+
expect(parent.textContent).toBe("text");
|
|
357
|
+
|
|
358
|
+
which.value = 0; // back to nothing
|
|
359
|
+
expect(parent.textContent).toBe("");
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
test("renders arrays of nodes and keeps anchor position", () => {
|
|
363
|
+
const items = signal(["a", "b"]);
|
|
364
|
+
const parent = document.createElement("div");
|
|
365
|
+
parent.appendChild(document.createTextNode("["));
|
|
366
|
+
const anchor = parent.appendChild(document.createComment(""));
|
|
367
|
+
parent.appendChild(document.createTextNode("]"));
|
|
368
|
+
bindChild(anchor, () => items.value.map((t) => {
|
|
369
|
+
const s = document.createElement("span");
|
|
370
|
+
s.textContent = t;
|
|
371
|
+
return s;
|
|
372
|
+
}));
|
|
373
|
+
expect(parent.textContent).toBe("[ab]");
|
|
374
|
+
items.value = ["x"];
|
|
375
|
+
expect(parent.textContent).toBe("[x]");
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
test("dispose stops updates", () => {
|
|
379
|
+
const on = signal(true);
|
|
380
|
+
const parent = document.createElement("div");
|
|
381
|
+
const anchor = parent.appendChild(document.createComment(""));
|
|
382
|
+
const dispose = bindChild(anchor, () => (on.value ? document.createElement("b") : null));
|
|
383
|
+
expect(parent.querySelector("b")).not.toBeNull();
|
|
384
|
+
dispose();
|
|
385
|
+
on.value = false;
|
|
386
|
+
expect(parent.querySelector("b")).not.toBeNull(); // frozen
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
test("swapping a branch disposes the old branch's bindings", () => {
|
|
390
|
+
const on = signal(true);
|
|
391
|
+
const label = signal("a");
|
|
392
|
+
const parent = document.createElement("div");
|
|
393
|
+
const anchor = parent.appendChild(document.createComment(""));
|
|
394
|
+
let runs = 0;
|
|
395
|
+
bindChild(anchor, () => {
|
|
396
|
+
if (!on.value) return null;
|
|
397
|
+
const p = document.createElement("p");
|
|
398
|
+
bindText(p.appendChild(document.createTextNode("")), () => {
|
|
399
|
+
runs++;
|
|
400
|
+
return label.value;
|
|
401
|
+
});
|
|
402
|
+
return p;
|
|
403
|
+
});
|
|
404
|
+
expect(parent.textContent).toBe("a");
|
|
405
|
+
|
|
406
|
+
on.value = false; // branch swapped out — its text binding must die with it
|
|
407
|
+
runs = 0;
|
|
408
|
+
label.value = "b";
|
|
409
|
+
expect(runs).toBe(0);
|
|
410
|
+
});
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
describe("mount", () => {
|
|
414
|
+
test("mounts a factory and a node", () => {
|
|
415
|
+
const root = document.createElement("div");
|
|
416
|
+
mount(() => {
|
|
417
|
+
const el = document.createElement("span");
|
|
418
|
+
el.textContent = "hi";
|
|
419
|
+
return el;
|
|
420
|
+
}, root);
|
|
421
|
+
expect(root.innerHTML).toBe("<span>hi</span>");
|
|
422
|
+
});
|
|
423
|
+
});
|
package/runtime/dom.d.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Apply a `style` object to an element. Numbers get "px" appended unless the
|
|
3
|
+
* property is unitless; CSS custom properties (`--foo`) go through setProperty;
|
|
4
|
+
* nullish values clear the property. A string value is treated as raw cssText.
|
|
5
|
+
*/
|
|
6
|
+
export function applyStyle(el: any, value: any): void;
|
|
7
|
+
/** Render a reactive value to its text form: null/undefined/false → "". */
|
|
8
|
+
export function toText(value: any): string;
|
|
9
|
+
/**
|
|
10
|
+
* Compile a static subtree once, then stamp copies of it.
|
|
11
|
+
*
|
|
12
|
+
* CSR codegen hoists one `const t = template("<p …>…</p>")` per distinct static
|
|
13
|
+
* subtree in a module and calls `t()` where it would otherwise have emitted a
|
|
14
|
+
* `createElement` + `setAttribute` + `appendChild` for every node. Parsing happens
|
|
15
|
+
* on the first call and `cloneNode(true)` — a single engine-side copy — on every
|
|
16
|
+
* call, including the first.
|
|
17
|
+
*
|
|
18
|
+
* The parse is deferred rather than done at module scope so importing a compiled
|
|
19
|
+
* module never touches `document`: the hydrate bundle is a browser artifact, but
|
|
20
|
+
* module evaluation order is not something a runtime helper should depend on.
|
|
21
|
+
*
|
|
22
|
+
* The compiler only emits this for markup it has proven the HTML parser rebuilds
|
|
23
|
+
* exactly as `createElement` would (`codegen::static_tree::template_html`) — a
|
|
24
|
+
* template is *not* a general-purpose `innerHTML`, and passing it author markup
|
|
25
|
+
* would reintroduce every reparenting rule that analysis exists to avoid.
|
|
26
|
+
*/
|
|
27
|
+
export function template(html: any): () => any;
|
|
28
|
+
/**
|
|
29
|
+
* clsx-style class normalization. Falsy entries are skipped; strings/numbers are
|
|
30
|
+
* kept; arrays recurse; objects contribute the keys whose values are truthy. Lets
|
|
31
|
+
* JSX write `class={["btn", active && "on", { lg: size === "lg" }]}`.
|
|
32
|
+
*/
|
|
33
|
+
export function clsx(value: any): any;
|
|
34
|
+
/**
|
|
35
|
+
* Apply a single static prop/attribute. Handles `style` objects, `on*` event
|
|
36
|
+
* handlers, property-backed attributes, and boolean/nullish removal; everything
|
|
37
|
+
* else falls back to `setAttribute`.
|
|
38
|
+
*/
|
|
39
|
+
export function setAttr(el: any, name: any, value: any): void;
|
|
40
|
+
/**
|
|
41
|
+
* Apply a named prop to a *child component* element. Set it as a property when the
|
|
42
|
+
* element exposes one (custom-element prop setters, `value`, `checked`, …) so rich
|
|
43
|
+
* values (objects, signals) reach the component's setter; otherwise fall back to an
|
|
44
|
+
* attribute so plain ones (`class`, `data-*`, `aria-*`) still land correctly. This
|
|
45
|
+
* mirrors how JSX libraries target custom elements. If the element is not yet
|
|
46
|
+
* upgraded `name in el` is false, so the value lands as an attribute and the
|
|
47
|
+
* component picks it up from `getAttribute` when it upgrades.
|
|
48
|
+
*/
|
|
49
|
+
export function setProp(el: any, name: any, value: any): void;
|
|
50
|
+
/**
|
|
51
|
+
* Apply every own enumerable key of `obj` to `el` (`<div {...obj}/>`, SPEC §5.5).
|
|
52
|
+
* `asProps` sets element properties (for components) instead of attributes (for
|
|
53
|
+
* host elements). Keys are applied in object order; the caller wraps this in an
|
|
54
|
+
* effect so a reactive source re-applies.
|
|
55
|
+
*/
|
|
56
|
+
export function spread(el: any, obj: any, asProps: any): void;
|
|
57
|
+
/**
|
|
58
|
+
* Wire a text node to a reactive expression. Primitive values update the text in
|
|
59
|
+
* place; a DOM node (or array of them) — e.g. JSX stored as a value, `{iconMap[k]}`
|
|
60
|
+
* — is inserted before the text node, which stays as a stable anchor. Returns the
|
|
61
|
+
* effect disposer so the caller (component lifecycle) can stop updates.
|
|
62
|
+
*/
|
|
63
|
+
export function bindText(node: any, fn: any): () => void;
|
|
64
|
+
/** A JSX value from a hydrate build: builds fresh nodes, or adopts server-rendered ones. */
|
|
65
|
+
export interface HydratableNode {
|
|
66
|
+
build(): Node | Node[] | null;
|
|
67
|
+
adopt(cursor: { node: Node | null }, nodes: Node[]): Node | Node[] | null;
|
|
68
|
+
}
|
|
69
|
+
/** Wrap a JSX value's build and adopt functions so a receiving hole can hydrate it. */
|
|
70
|
+
export function hydratableNode(
|
|
71
|
+
build: HydratableNode["build"],
|
|
72
|
+
adopt: HydratableNode["adopt"],
|
|
73
|
+
): HydratableNode;
|
|
74
|
+
/** Whether `value` is a {@link hydratableNode}. */
|
|
75
|
+
export function isHydratable(value: unknown): value is HydratableNode;
|
|
76
|
+
/** Children read as a value during hydration: keeps the server nodes it adopts. */
|
|
77
|
+
export function adoptedChildren(): HydratableNode;
|
|
78
|
+
/**
|
|
79
|
+
* Wire an element attribute/prop to a reactive expression. Returns the disposer.
|
|
80
|
+
*
|
|
81
|
+
* No-op writes are elided for primitive values: when the expression recomputes
|
|
82
|
+
* to the same primitive it last wrote, the DOM write is skipped. This matters
|
|
83
|
+
* when many elements share one dependency — e.g. a keyed list where every row's
|
|
84
|
+
* `class={selected === row.id ? "danger" : ""}` subscribes to a single
|
|
85
|
+
* `selected` signal. Changing it re-runs every row's effect, but only the two
|
|
86
|
+
* rows whose class actually changes should touch the DOM; the rest would
|
|
87
|
+
* otherwise call `setAttribute` with an unchanged value and trigger needless
|
|
88
|
+
* style invalidation. Object values (style/class objects, spreads) may have
|
|
89
|
+
* mutated internally, so they are always re-applied.
|
|
90
|
+
*/
|
|
91
|
+
export function bindAttr(el: any, name: any, fn: any): () => void;
|
|
92
|
+
/**
|
|
93
|
+
* Wire a dynamic child region (conditional/element-valued holes like
|
|
94
|
+
* `{cond && <p/>}` or `{cond ? <a/> : <b/>}`) to a reactive expression. The
|
|
95
|
+
* region is delimited by `anchor` (a comment); on every change the previous
|
|
96
|
+
* nodes are replaced with the new ones, inserted just before the anchor.
|
|
97
|
+
* Returns the effect disposer.
|
|
98
|
+
*/
|
|
99
|
+
export function bindChild(anchor: any, fn: any): () => void;
|
|
100
|
+
/**
|
|
101
|
+
* Hydrate a server-rendered conditional / dynamic-node region (docs/HYDRATION.md §3.1,
|
|
102
|
+
* 2.1b). The cursor `cur` is at the `<!--[-->` marker; between it and `<!--]-->` sits the
|
|
103
|
+
* one branch the server rendered (or nothing). `adoptFn(cur)` evaluates the same branch
|
|
104
|
+
* expression with *adopt* calls — only the taken branch's builder runs, claiming its nodes
|
|
105
|
+
* off the shared cursor — so the adopted nodes become the region's initial content with no
|
|
106
|
+
* rebuild. `buildFn()` is the CSR version (build calls) the effect swaps to when a later
|
|
107
|
+
* reactive change selects a different branch. The closing `<!--]-->` is the swap anchor.
|
|
108
|
+
* Returns the effect disposer.
|
|
109
|
+
*
|
|
110
|
+
* `depsFn()` is the same branch expression with **every branch replaced by `null`**. The
|
|
111
|
+
* region effect has to run something on first paint to subscribe to the deps it should react
|
|
112
|
+
* to — the *condition* — but running the real `buildFn` there constructs a whole subtree only
|
|
113
|
+
* to discard it, and when a branch re-slots the component's `{children}`, `appendChild`
|
|
114
|
+
* **moves** those live server nodes into the throwaway tree, silently emptying the slot (the
|
|
115
|
+
* `<Card>` → `<Link>{children}</Link>` shape lost its children this way). Evaluating the
|
|
116
|
+
* condition with null branches subscribes to exactly the same reads without building anything;
|
|
117
|
+
* reactivity *inside* a branch is wired by that branch's own bindText/bindAttr effects, so it
|
|
118
|
+
* is not a dependency of this region. Older codegen passes no `depsFn` — fall back to `buildFn`.
|
|
119
|
+
*/
|
|
120
|
+
export function hydrateChild(cur: any, adoptFn: any, buildFn: any, depsFn: any): () => void;
|
|
121
|
+
/**
|
|
122
|
+
* Render a keyed list (`array.map(...)`, SPEC §5.4.4) into `parent`, reconciling
|
|
123
|
+
* by key on every change to `sourceFn`'s dependencies.
|
|
124
|
+
*
|
|
125
|
+
* - `sourceFn()` returns the current array (plain data).
|
|
126
|
+
* - `renderItem(itemSignal, index)` builds a node for a new item; the item's
|
|
127
|
+
* value is a signal so per-item bindings update fine-grained on data changes.
|
|
128
|
+
* - `keyFn(item, index)` returns a stable key; falls back to `index` when omitted.
|
|
129
|
+
*
|
|
130
|
+
* Returns the effect disposer (stops reconciliation; used by component cleanup).
|
|
131
|
+
*/
|
|
132
|
+
export function bindList(parent: any, sourceFn: any, renderItem: any, keyFn: any): () => void;
|
|
133
|
+
/**
|
|
134
|
+
* Hydrate a server-rendered list region (docs/HYDRATION.md §3.1/2.1). The cursor `cur` is
|
|
135
|
+
* positioned at the `<!--[-->` marker; the region holds one root node per item, then
|
|
136
|
+
* `<!--]-->`. Adopts each item's server node via `adoptItem(cur, itemSignal, index)`
|
|
137
|
+
* (claiming off the shared cursor, which advances to the next item), seeds the reconcile
|
|
138
|
+
* cache with the adopted `{sig, node}` pairs, then wires the *same* keyed-reconcile effect
|
|
139
|
+
* `bindList` uses — so a later data change builds/moves/removes with no first-paint flash.
|
|
140
|
+
* The closing `<!--]-->` becomes the reconcile anchor. Returns the effect disposer.
|
|
141
|
+
*/
|
|
142
|
+
export function hydrateList(cur: any, sourceFn: any, adoptItem: any, renderItem: any, keyFn: any): () => void;
|