@sigil-dev/compiler 0.9.29 → 0.9.31

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.
Files changed (38) hide show
  1. package/index.ts +6 -4
  2. package/package.json +1 -1
  3. package/src/babel/index.ts +763 -750
  4. package/src/babel/jsx/anchor-mount.ts +338 -333
  5. package/src/babel/jsx/children.ts +116 -116
  6. package/src/babel/jsx/element.ts +1040 -1034
  7. package/src/babel/jsx/fragment.ts +271 -271
  8. package/src/babel/jsx/keyed-list.ts +339 -339
  9. package/src/babel/jsx/utils.ts +356 -356
  10. package/src/babel/util/bind.ts +143 -143
  11. package/src/babel/util/css.ts +208 -208
  12. package/src/babel/util/dead-code.ts +94 -93
  13. package/src/babel/util/links.ts +101 -0
  14. package/src/babel/util/reactive-fns.ts +110 -110
  15. package/src/babel/util/runtime-names.ts +88 -0
  16. package/src/babel/util/state-imports.ts +122 -121
  17. package/src/babel/util/static-assertion.ts +51 -51
  18. package/src/babel/util/static-env.ts +84 -0
  19. package/src/vite/index.ts +61 -61
  20. package/test/bun-plugin.test.ts +45 -45
  21. package/test/context.test.ts +110 -102
  22. package/test/fixtures/stores/index.ts +2 -2
  23. package/test/hydration-analysis.test.ts +133 -133
  24. package/test/hydration.test.ts +315 -313
  25. package/test/jsx.test.ts +254 -254
  26. package/test/links.test.ts +57 -0
  27. package/test/reactive-helpers.test.ts +135 -135
  28. package/test/render-parity.test.ts +909 -907
  29. package/test/runtime-names.test.ts +60 -0
  30. package/test/scoped-css.test.ts +166 -166
  31. package/test/ssr-boolean-attrs.test.ts +56 -56
  32. package/test/ssr-raw-text.test.ts +68 -68
  33. package/test/ssr.test.ts +30 -0
  34. package/test/static-assertion.test.ts +123 -123
  35. package/test/static-env.test.ts +85 -0
  36. package/test/static-skip.test.ts +292 -292
  37. package/test/style-object.test.ts +36 -36
  38. package/test/tree-shaking.test.ts +122 -120
@@ -1,292 +1,292 @@
1
- /**
2
- * Skipping hydration of components the analysis proved static.
3
- *
4
- * Same three renders as render-parity: server HTML, that HTML after hydration,
5
- * and a fresh client render with an empty pool (what navigation runs). The
6
- * difference is what each one calls.
7
- */
8
- import { afterEach, describe, expect, test } from "bun:test";
9
- import { transformSync } from "@babel/core";
10
- import * as runtime from "../../runtime/index.ts";
11
- import sigilPlugin from "../src/babel/index.ts";
12
-
13
- type Mode = "ssr" | "hydrate" | "dom";
14
-
15
- function compile(source: string, mode: Mode, dev = false): () => unknown {
16
- const code = transformSync(source, {
17
- plugins: ["@babel/plugin-syntax-jsx", [sigilPlugin, { mode, dev }]],
18
- filename: "t.tsx",
19
- })!.code!;
20
- const body = code
21
- .replace(/import\s*\{[^}]*\}\s*from\s*["']@sigil-dev\/runtime["'];?/g, "")
22
- .replace(/^export\s+/gm, "");
23
- // grimoire's Head renders nothing on the server; the analysis only needs the import
24
- const grimoire = body.replace(
25
- /import\s*\{[^}]*\}\s*from\s*["']@sigil-dev\/grimoire["'];?/g,
26
- "",
27
- );
28
- return new Function(
29
- "document",
30
- "Head",
31
- ...Object.keys(runtime),
32
- `${grimoire};\nreturn App;`,
33
- )(globalThis.document, () => "", ...Object.values(runtime));
34
- }
35
-
36
- const g = globalThis as { __footerCalls?: number };
37
- const calls = () => g.__footerCalls ?? 0;
38
- afterEach(() => {
39
- g.__footerCalls = 0;
40
- });
41
-
42
- const noMarkers = (html: string) => html.replace(/<!--[^>]*-->/g, "");
43
-
44
- const FOOTER = `
45
- const Footer = () => {
46
- globalThis.__footerCalls = (globalThis.__footerCalls ?? 0) + 1;
47
- return <footer><p>© now</p></footer>;
48
- };
49
- const Counter = () => {
50
- let n = $state(0);
51
- return <button onClick={() => n++}>{n}</button>;
52
- };
53
- `;
54
-
55
- function renderAll(source: string, dev = false) {
56
- g.__footerCalls = 0;
57
- const ssr = String(compile(source, "ssr", dev)());
58
- const afterSsr = calls();
59
-
60
- const host = document.createElement("div");
61
- host.innerHTML = ssr;
62
- const before = Array.from(host.querySelectorAll("footer"));
63
- runtime.pushHydrationNodes(Array.from(host.childNodes));
64
- compile(source, "hydrate", dev)();
65
- runtime.popHydrationNodes();
66
- const afterHydrate = calls();
67
-
68
- const spa = document.createElement("div");
69
- runtime.pushHydrationNodes([]);
70
- runtime.insert(spa, compile(source, "hydrate", dev)());
71
- runtime.popHydrationNodes();
72
-
73
- return { ssr, host, spa, before, afterSsr, afterHydrate, afterSpa: calls() };
74
- }
75
-
76
- describe("a static use is fenced by the server and skipped by hydration", () => {
77
- const source = `${FOOTER}
78
- export const App = () => (
79
- <main>
80
- <Footer />
81
- <Counter />
82
- <p>after</p>
83
- </main>
84
- );`;
85
-
86
- test("the server fences only the static use", () => {
87
- const html = String(compile(source, "ssr")());
88
- expect(html).toContain("<!--s--><footer>");
89
- expect(html).toContain("</footer><!--/s-->");
90
- expect(html.match(/<!--s-->/g)).toHaveLength(1);
91
- // the dynamic sibling is not fenced
92
- expect(html).not.toMatch(/<!--s--><button/);
93
- });
94
-
95
- test("hydration never calls the component; a client render still does", () => {
96
- const r = renderAll(source);
97
- expect(r.afterSsr).toBe(1);
98
- expect(r.afterHydrate).toBe(1); // hydration added nothing
99
- expect(r.afterSpa).toBe(2); // the empty-pool render ran it
100
- });
101
-
102
- test("the server's own nodes are left exactly as they were", () => {
103
- const r = renderAll(source);
104
- const after = r.host.querySelector("footer");
105
- expect(after).toBe(r.before[0]!);
106
- });
107
-
108
- test("the dynamic sibling after it still hydrates, and the page equals the server HTML", () => {
109
- const r = renderAll(source);
110
- expect(noMarkers(r.host.innerHTML)).toBe(noMarkers(r.ssr));
111
- expect(noMarkers(r.spa.innerHTML)).toBe(noMarkers(r.ssr));
112
- const button = r.host.querySelector("button")!;
113
- button.click();
114
- return Promise.resolve(runtime.tick()).then(() => {
115
- expect(button.textContent).toBe("1");
116
- });
117
- });
118
- });
119
-
120
- describe("several static uses among dynamic ones and text", () => {
121
- const source = `${FOOTER}
122
- export const App = () => (
123
- <main>
124
- hello <Footer /> middle <Counter /><Footer /> end
125
- </main>
126
- );`;
127
-
128
- test("each is skipped, in order, and the text around them is claimed correctly", () => {
129
- const r = renderAll(source);
130
- expect(r.afterHydrate).toBe(r.afterSsr);
131
- expect(noMarkers(r.host.innerHTML)).toBe(noMarkers(r.ssr));
132
- expect(r.host.querySelectorAll("footer")).toHaveLength(2);
133
- for (let i = 0; i < 2; i++) {
134
- expect(r.host.querySelectorAll("footer")[i]).toBe(r.before[i]!);
135
- }
136
- });
137
- });
138
-
139
- describe("uses that are not skipped", () => {
140
- test("a use inside a condition, a component's children or a fragment is left alone", () => {
141
- const source = `${FOOTER}
142
- const Box = (props) => <div>{props.children}</div>;
143
- export const App = (props) => (
144
- <main>
145
- {props.show ? <Footer /> : null}
146
- <Box><Footer /></Box>
147
- </main>
148
- );`;
149
- const html = String(
150
- (compile(source, "ssr") as (p: unknown) => unknown)({ show: true }),
151
- );
152
- expect(html).toContain("<footer>");
153
- // <Box> with only static children is one static run; the Footer inside it
154
- // and the one in the condition are not fenced on their own
155
- expect(html.match(/<!--s-->/g)).toHaveLength(1);
156
- expect(html).not.toContain("<!--s--><footer>");
157
- const hydrate = transformSync(source, {
158
- plugins: ["@babel/plugin-syntax-jsx", [sigilPlugin, { mode: "hydrate" }]],
159
- filename: "t.tsx",
160
- })!.code!;
161
- expect(hydrate.match(/skipStatic\(/g)).toHaveLength(1);
162
- });
163
-
164
- test("a use given reactive props, a spread or a handler child is not static", () => {
165
- const source = `${FOOTER}
166
- const Tag = (props) => <i>{props.label}</i>;
167
- export const App = () => {
168
- let n = $state(0);
169
- return (
170
- <main>
171
- <Tag label={n} />
172
- <Tag {...{ label: "x" }} />
173
- </main>
174
- );
175
- };`;
176
- expect(String(compile(source, "ssr")())).not.toContain("<!--s-->");
177
- });
178
-
179
- test("static={false} forces a use to hydrate", () => {
180
- const source = `${FOOTER}
181
- export const App = () => <main><Footer static={false} /></main>;`;
182
- expect(String(compile(source, "ssr")())).not.toContain("<!--s-->");
183
- });
184
-
185
- test("the dom build has no markers and no skipping", () => {
186
- const source = `${FOOTER}
187
- export const App = () => <main><Footer /></main>;`;
188
- const out = transformSync(source, {
189
- plugins: ["@babel/plugin-syntax-jsx", [sigilPlugin, { mode: "dom" }]],
190
- filename: "t.tsx",
191
- })!.code!;
192
- expect(out).not.toContain("skipStatic(");
193
- });
194
- });
195
-
196
- describe("grimoire's Head does not make a component dynamic", () => {
197
- test("a component that only sets the head and renders text is static", () => {
198
- const source = `
199
- import { Head } from "@sigil-dev/grimoire";
200
- const Title = () => <section><Head><title>x</title></Head><p>hi</p></section>;
201
- export const App = () => <main><Title /></main>;`;
202
- expect(String(compile(source, "ssr")())).toContain("<!--s-->");
203
- });
204
- });
205
-
206
- describe("probeStatic", () => {
207
- test("passes a component that renders only HTML", () => {
208
- expect(() =>
209
- runtime.probeStatic("Plain", () => document.createElement("p")),
210
- ).not.toThrow();
211
- });
212
-
213
- test("throws for an effect that tracks a signal", () => {
214
- const count = runtime.createSignal(0);
215
- expect(() =>
216
- runtime.probeStatic("Live", () => {
217
- runtime.createEffect(() => {
218
- count();
219
- });
220
- }),
221
- ).toThrow(/<Live> was compiled as static/);
222
- });
223
-
224
- test("does not throw for an effect that reads nothing reactive", () => {
225
- expect(() =>
226
- runtime.probeStatic("Prop", () => {
227
- runtime.createEffect(() => {
228
- void "constant";
229
- });
230
- }),
231
- ).not.toThrow();
232
- });
233
-
234
- test("throws for an event listener", () => {
235
- expect(() =>
236
- runtime.probeStatic("Clicky", () => {
237
- document.createElement("button").addEventListener("click", () => {});
238
- }),
239
- ).toThrow(/1 event listener/);
240
- });
241
-
242
- test("restores what it patched, even after throwing", () => {
243
- const add = EventTarget.prototype.addEventListener;
244
- try {
245
- runtime.probeStatic("Clicky", () => {
246
- document.createElement("button").addEventListener("click", () => {});
247
- });
248
- } catch {}
249
- expect(EventTarget.prototype.addEventListener).toBe(add);
250
- // effects created afterwards are no longer counted
251
- const count = runtime.createSignal(0);
252
- expect(() =>
253
- runtime.probeStatic("After", () => document.createElement("i")),
254
- ).not.toThrow();
255
- runtime.createEffect(() => {
256
- count();
257
- });
258
- });
259
- });
260
-
261
- describe("skipStatic", () => {
262
- function pool(html: string) {
263
- const host = document.createElement("div");
264
- host.innerHTML = html;
265
- return Array.from(host.childNodes);
266
- }
267
-
268
- test("cuts the fenced run out of the pool and returns its first node", () => {
269
- const nodes = pool("<p>a</p><!--s--><i>x</i>text<!--/s--><p>b</p>");
270
- const first = runtime.skipStatic(nodes);
271
- expect((first as Element).tagName).toBe("I");
272
- expect(nodes.map((n) => (n as Element).tagName ?? "#")).toEqual(["P", "P"]);
273
- });
274
-
275
- test("matches nested runs by depth", () => {
276
- const nodes = pool(
277
- "<!--s--><b><!--s--><i></i><!--/s--></b><!--/s--><u></u>",
278
- );
279
- runtime.skipStatic(nodes);
280
- expect(nodes.map((n) => (n as Element).tagName)).toEqual(["U"]);
281
- });
282
-
283
- test("returns null, and leaves the pool alone, when there is nothing well formed to skip", () => {
284
- const none = pool("<p>a</p>");
285
- expect(runtime.skipStatic(none)).toBeNull();
286
- expect(none).toHaveLength(1);
287
- const open = pool("<!--s--><p>a</p>");
288
- expect(runtime.skipStatic(open)).toBeNull();
289
- expect(open).toHaveLength(2);
290
- expect(runtime.skipStatic([])).toBeNull();
291
- });
292
- });
1
+ /**
2
+ * Skipping hydration of components the analysis proved static.
3
+ *
4
+ * Same three renders as render-parity: server HTML, that HTML after hydration,
5
+ * and a fresh client render with an empty pool (what navigation runs). The
6
+ * difference is what each one calls.
7
+ */
8
+ import { afterEach, describe, expect, test } from "bun:test";
9
+ import { transformSync } from "@babel/core";
10
+ import * as runtime from "../../runtime/index.ts";
11
+ import sigilPlugin from "../src/babel/index.ts";
12
+
13
+ type Mode = "ssr" | "hydrate" | "dom";
14
+
15
+ function compile(source: string, mode: Mode, dev = false): () => unknown {
16
+ const code = transformSync(source, {
17
+ plugins: ["@babel/plugin-syntax-jsx", [sigilPlugin, { mode, dev }]],
18
+ filename: "t.tsx",
19
+ })!.code!;
20
+ const body = code
21
+ .replace(/import\s*\{[^}]*\}\s*from\s*["']@sigil-dev\/runtime["'];?/g, "")
22
+ .replace(/^export\s+/gm, "");
23
+ // grimoire's Head renders nothing on the server; the analysis only needs the import
24
+ const grimoire = body.replace(
25
+ /import\s*\{[^}]*\}\s*from\s*["']@sigil-dev\/grimoire["'];?/g,
26
+ "",
27
+ );
28
+ return new Function(
29
+ "document",
30
+ "Head",
31
+ ...Object.keys(runtime),
32
+ `${grimoire};\nreturn App;`,
33
+ )(globalThis.document, () => "", ...Object.values(runtime));
34
+ }
35
+
36
+ const g = globalThis as { __footerCalls?: number };
37
+ const calls = () => g.__footerCalls ?? 0;
38
+ afterEach(() => {
39
+ g.__footerCalls = 0;
40
+ });
41
+
42
+ const noMarkers = (html: string) => html.replace(/<!--[^>]*-->/g, "");
43
+
44
+ const FOOTER = `
45
+ const Footer = () => {
46
+ globalThis.__footerCalls = (globalThis.__footerCalls ?? 0) + 1;
47
+ return <footer><p>© now</p></footer>;
48
+ };
49
+ const Counter = () => {
50
+ let n = $state(0);
51
+ return <button onClick={() => n++}>{n}</button>;
52
+ };
53
+ `;
54
+
55
+ function renderAll(source: string, dev = false) {
56
+ g.__footerCalls = 0;
57
+ const ssr = String(compile(source, "ssr", dev)());
58
+ const afterSsr = calls();
59
+
60
+ const host = document.createElement("div");
61
+ host.innerHTML = ssr;
62
+ const before = Array.from(host.querySelectorAll("footer"));
63
+ runtime.pushHydrationNodes(Array.from(host.childNodes));
64
+ compile(source, "hydrate", dev)();
65
+ runtime.popHydrationNodes();
66
+ const afterHydrate = calls();
67
+
68
+ const spa = document.createElement("div");
69
+ runtime.pushHydrationNodes([]);
70
+ runtime.insert(spa, compile(source, "hydrate", dev)());
71
+ runtime.popHydrationNodes();
72
+
73
+ return { ssr, host, spa, before, afterSsr, afterHydrate, afterSpa: calls() };
74
+ }
75
+
76
+ describe("a static use is fenced by the server and skipped by hydration", () => {
77
+ const source = `${FOOTER}
78
+ export const App = () => (
79
+ <main>
80
+ <Footer />
81
+ <Counter />
82
+ <p>after</p>
83
+ </main>
84
+ );`;
85
+
86
+ test("the server fences only the static use", () => {
87
+ const html = String(compile(source, "ssr")());
88
+ expect(html).toContain("<!--s--><footer>");
89
+ expect(html).toContain("</footer><!--/s-->");
90
+ expect(html.match(/<!--s-->/g)).toHaveLength(1);
91
+ // the dynamic sibling is not fenced
92
+ expect(html).not.toMatch(/<!--s--><button/);
93
+ });
94
+
95
+ test("hydration never calls the component; a client render still does", () => {
96
+ const r = renderAll(source);
97
+ expect(r.afterSsr).toBe(1);
98
+ expect(r.afterHydrate).toBe(1); // hydration added nothing
99
+ expect(r.afterSpa).toBe(2); // the empty-pool render ran it
100
+ });
101
+
102
+ test("the server's own nodes are left exactly as they were", () => {
103
+ const r = renderAll(source);
104
+ const after = r.host.querySelector("footer");
105
+ expect(after).toBe(r.before[0]!);
106
+ });
107
+
108
+ test("the dynamic sibling after it still hydrates, and the page equals the server HTML", () => {
109
+ const r = renderAll(source);
110
+ expect(noMarkers(r.host.innerHTML)).toBe(noMarkers(r.ssr));
111
+ expect(noMarkers(r.spa.innerHTML)).toBe(noMarkers(r.ssr));
112
+ const button = r.host.querySelector("button")!;
113
+ button.click();
114
+ return Promise.resolve(runtime.tick()).then(() => {
115
+ expect(button.textContent).toBe("1");
116
+ });
117
+ });
118
+ });
119
+
120
+ describe("several static uses among dynamic ones and text", () => {
121
+ const source = `${FOOTER}
122
+ export const App = () => (
123
+ <main>
124
+ hello <Footer /> middle <Counter /><Footer /> end
125
+ </main>
126
+ );`;
127
+
128
+ test("each is skipped, in order, and the text around them is claimed correctly", () => {
129
+ const r = renderAll(source);
130
+ expect(r.afterHydrate).toBe(r.afterSsr);
131
+ expect(noMarkers(r.host.innerHTML)).toBe(noMarkers(r.ssr));
132
+ expect(r.host.querySelectorAll("footer")).toHaveLength(2);
133
+ for (let i = 0; i < 2; i++) {
134
+ expect(r.host.querySelectorAll("footer")[i]).toBe(r.before[i]!);
135
+ }
136
+ });
137
+ });
138
+
139
+ describe("uses that are not skipped", () => {
140
+ test("a use inside a condition, a component's children or a fragment is left alone", () => {
141
+ const source = `${FOOTER}
142
+ const Box = (props) => <div>{props.children}</div>;
143
+ export const App = (props) => (
144
+ <main>
145
+ {props.show ? <Footer /> : null}
146
+ <Box><Footer /></Box>
147
+ </main>
148
+ );`;
149
+ const html = String(
150
+ (compile(source, "ssr") as (p: unknown) => unknown)({ show: true }),
151
+ );
152
+ expect(html).toContain("<footer>");
153
+ // <Box> with only static children is one static run; the Footer inside it
154
+ // and the one in the condition are not fenced on their own
155
+ expect(html.match(/<!--s-->/g)).toHaveLength(1);
156
+ expect(html).not.toContain("<!--s--><footer>");
157
+ const hydrate = transformSync(source, {
158
+ plugins: ["@babel/plugin-syntax-jsx", [sigilPlugin, { mode: "hydrate" }]],
159
+ filename: "t.tsx",
160
+ })!.code!;
161
+ expect(hydrate.match(/skipStatic\(/g)).toHaveLength(1);
162
+ });
163
+
164
+ test("a use given reactive props, a spread or a handler child is not static", () => {
165
+ const source = `${FOOTER}
166
+ const Tag = (props) => <i>{props.label}</i>;
167
+ export const App = () => {
168
+ let n = $state(0);
169
+ return (
170
+ <main>
171
+ <Tag label={n} />
172
+ <Tag {...{ label: "x" }} />
173
+ </main>
174
+ );
175
+ };`;
176
+ expect(String(compile(source, "ssr")())).not.toContain("<!--s-->");
177
+ });
178
+
179
+ test("static={false} forces a use to hydrate", () => {
180
+ const source = `${FOOTER}
181
+ export const App = () => <main><Footer static={false} /></main>;`;
182
+ expect(String(compile(source, "ssr")())).not.toContain("<!--s-->");
183
+ });
184
+
185
+ test("the dom build has no markers and no skipping", () => {
186
+ const source = `${FOOTER}
187
+ export const App = () => <main><Footer /></main>;`;
188
+ const out = transformSync(source, {
189
+ plugins: ["@babel/plugin-syntax-jsx", [sigilPlugin, { mode: "dom" }]],
190
+ filename: "t.tsx",
191
+ })!.code!;
192
+ expect(out).not.toContain("skipStatic(");
193
+ });
194
+ });
195
+
196
+ describe("grimoire's Head does not make a component dynamic", () => {
197
+ test("a component that only sets the head and renders text is static", () => {
198
+ const source = `
199
+ import { Head } from "@sigil-dev/grimoire";
200
+ const Title = () => <section><Head><title>x</title></Head><p>hi</p></section>;
201
+ export const App = () => <main><Title /></main>;`;
202
+ expect(String(compile(source, "ssr")())).toContain("<!--s-->");
203
+ });
204
+ });
205
+
206
+ describe("probeStatic", () => {
207
+ test("passes a component that renders only HTML", () => {
208
+ expect(() =>
209
+ runtime.probeStatic("Plain", () => document.createElement("p")),
210
+ ).not.toThrow();
211
+ });
212
+
213
+ test("throws for an effect that tracks a signal", () => {
214
+ const count = runtime.createSignal(0);
215
+ expect(() =>
216
+ runtime.probeStatic("Live", () => {
217
+ runtime.createEffect(() => {
218
+ count();
219
+ });
220
+ }),
221
+ ).toThrow(/<Live> was compiled as static/);
222
+ });
223
+
224
+ test("does not throw for an effect that reads nothing reactive", () => {
225
+ expect(() =>
226
+ runtime.probeStatic("Prop", () => {
227
+ runtime.createEffect(() => {
228
+ void "constant";
229
+ });
230
+ }),
231
+ ).not.toThrow();
232
+ });
233
+
234
+ test("throws for an event listener", () => {
235
+ expect(() =>
236
+ runtime.probeStatic("Clicky", () => {
237
+ document.createElement("button").addEventListener("click", () => {});
238
+ }),
239
+ ).toThrow(/1 event listener/);
240
+ });
241
+
242
+ test("restores what it patched, even after throwing", () => {
243
+ const add = EventTarget.prototype.addEventListener;
244
+ try {
245
+ runtime.probeStatic("Clicky", () => {
246
+ document.createElement("button").addEventListener("click", () => {});
247
+ });
248
+ } catch {}
249
+ expect(EventTarget.prototype.addEventListener).toBe(add);
250
+ // effects created afterwards are no longer counted
251
+ const count = runtime.createSignal(0);
252
+ expect(() =>
253
+ runtime.probeStatic("After", () => document.createElement("i")),
254
+ ).not.toThrow();
255
+ runtime.createEffect(() => {
256
+ count();
257
+ });
258
+ });
259
+ });
260
+
261
+ describe("skipStatic", () => {
262
+ function pool(html: string) {
263
+ const host = document.createElement("div");
264
+ host.innerHTML = html;
265
+ return Array.from(host.childNodes);
266
+ }
267
+
268
+ test("cuts the fenced run out of the pool and returns its first node", () => {
269
+ const nodes = pool("<p>a</p><!--s--><i>x</i>text<!--/s--><p>b</p>");
270
+ const first = runtime.skipStatic(nodes);
271
+ expect((first as Element).tagName).toBe("I");
272
+ expect(nodes.map((n) => (n as Element).tagName ?? "#")).toEqual(["P", "P"]);
273
+ });
274
+
275
+ test("matches nested runs by depth", () => {
276
+ const nodes = pool(
277
+ "<!--s--><b><!--s--><i></i><!--/s--></b><!--/s--><u></u>",
278
+ );
279
+ runtime.skipStatic(nodes);
280
+ expect(nodes.map((n) => (n as Element).tagName)).toEqual(["U"]);
281
+ });
282
+
283
+ test("returns null, and leaves the pool alone, when there is nothing well formed to skip", () => {
284
+ const none = pool("<p>a</p>");
285
+ expect(runtime.skipStatic(none)).toBeNull();
286
+ expect(none).toHaveLength(1);
287
+ const open = pool("<!--s--><p>a</p>");
288
+ expect(runtime.skipStatic(open)).toBeNull();
289
+ expect(open).toHaveLength(2);
290
+ expect(runtime.skipStatic([])).toBeNull();
291
+ });
292
+ });
@@ -1,36 +1,36 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { transformSSR } from "./helpers/transform.ts";
3
-
4
- function render(src: string): string {
5
- const body = transformSSR(src).replace(/^export\s+/gm, "");
6
- return String(new Function(`${body};\nreturn Card();`)());
7
- }
8
-
9
- describe("SSR style objects", () => {
10
- test("style={{...}} serializes to CSS text, numbers in px", () => {
11
- expect(
12
- render(
13
- `export function Card() { return <div style={{ display: "grid", marginTop: 12 }}>x</div>; }`,
14
- ),
15
- ).toBe(`<div style="display:grid;margin-top:12px">x</div>`);
16
- });
17
-
18
- test("unitless properties, custom properties and empty values", () => {
19
- expect(
20
- render(
21
- `const s = { opacity: 0.5, zIndex: 3, "--gap": 4, color: null, lineHeight: 1.2, padding: 0 };
22
- export function Card() { return <div style={s} />; }`,
23
- ),
24
- ).toBe(
25
- `<div style="opacity:0.5;z-index:3;--gap:4;line-height:1.2;padding:0"></div>`,
26
- );
27
- });
28
-
29
- test("values are attribute-escaped", () => {
30
- expect(
31
- render(
32
- `const bg = 'url("a.png")'; export function Card() { return <div style={{ background: bg }} />; }`,
33
- ),
34
- ).toBe(`<div style="background:url(&quot;a.png&quot;)"></div>`);
35
- });
36
- });
1
+ import { describe, expect, test } from "bun:test";
2
+ import { transformSSR } from "./helpers/transform.ts";
3
+
4
+ function render(src: string): string {
5
+ const body = transformSSR(src).replace(/^export\s+/gm, "");
6
+ return String(new Function(`${body};\nreturn Card();`)());
7
+ }
8
+
9
+ describe("SSR style objects", () => {
10
+ test("style={{...}} serializes to CSS text, numbers in px", () => {
11
+ expect(
12
+ render(
13
+ `export function Card() { return <div style={{ display: "grid", marginTop: 12 }}>x</div>; }`,
14
+ ),
15
+ ).toBe(`<div style="display:grid;margin-top:12px">x</div>`);
16
+ });
17
+
18
+ test("unitless properties, custom properties and empty values", () => {
19
+ expect(
20
+ render(
21
+ `const s = { opacity: 0.5, zIndex: 3, "--gap": 4, color: null, lineHeight: 1.2, padding: 0 };
22
+ export function Card() { return <div style={s} />; }`,
23
+ ),
24
+ ).toBe(
25
+ `<div style="opacity:0.5;z-index:3;--gap:4;line-height:1.2;padding:0"></div>`,
26
+ );
27
+ });
28
+
29
+ test("values are attribute-escaped", () => {
30
+ expect(
31
+ render(
32
+ `const bg = 'url("a.png")'; export function Card() { return <div style={{ background: bg }} />; }`,
33
+ ),
34
+ ).toBe(`<div style="background:url(&quot;a.png&quot;)"></div>`);
35
+ });
36
+ });