clarusjs 0.15.0 → 0.17.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.
Files changed (75) hide show
  1. package/dist/check/generate.js +115 -13
  2. package/dist/cli/check.d.ts +9 -2
  3. package/dist/cli/check.js +91 -15
  4. package/dist/cli/explain.js +3 -0
  5. package/dist/cli/index.js +24 -3
  6. package/dist/cli/mapper.js +6 -1
  7. package/dist/cli/project.d.ts +7 -0
  8. package/dist/cli/project.js +1 -1
  9. package/dist/compiler/components.d.ts +29 -0
  10. package/dist/compiler/components.js +18 -0
  11. package/dist/compiler/element-attrs.d.ts +7 -0
  12. package/dist/compiler/element-attrs.js +32 -0
  13. package/dist/compiler/emit-client.js +39 -9
  14. package/dist/compiler/emit-server.js +1 -1
  15. package/dist/compiler/events.d.ts +9 -0
  16. package/dist/compiler/events.js +46 -0
  17. package/dist/compiler/html-expr.js +13 -8
  18. package/dist/compiler/index.d.ts +2 -2
  19. package/dist/compiler/index.js +126 -12
  20. package/dist/compiler/ir.d.ts +18 -0
  21. package/dist/compiler/ir.js +79 -10
  22. package/dist/compiler/lint.d.ts +31 -3
  23. package/dist/compiler/lint.js +177 -8
  24. package/dist/compiler/loops.d.ts +13 -0
  25. package/dist/compiler/loops.js +49 -0
  26. package/dist/compiler/parse.d.ts +40 -0
  27. package/dist/compiler/parse.js +164 -1
  28. package/dist/compiler/script.d.ts +2 -0
  29. package/dist/compiler/script.js +78 -1
  30. package/dist/forms/index.js +35 -15
  31. package/dist/forms/types.d.ts +7 -0
  32. package/dist/node/serve.d.ts +26 -0
  33. package/dist/node/serve.js +150 -0
  34. package/dist/runtime/attrs.d.ts +1 -1
  35. package/dist/runtime/attrs.js +8 -5
  36. package/dist/runtime/dom.d.ts +40 -4
  37. package/dist/runtime/dom.js +200 -49
  38. package/dist/runtime/hot.js +3 -2
  39. package/dist/runtime/index.d.ts +1 -1
  40. package/dist/runtime/index.js +1 -1
  41. package/dist/runtime/loader.js +39 -7
  42. package/dist/runtime/motion.d.ts +59 -0
  43. package/dist/runtime/motion.js +339 -0
  44. package/dist/runtime/navigate.d.ts +2 -0
  45. package/dist/runtime/navigate.js +7 -1
  46. package/dist/runtime/navigation.d.ts +44 -0
  47. package/dist/runtime/navigation.js +69 -3
  48. package/dist/runtime/signals.d.ts +2 -0
  49. package/dist/runtime/signals.js +31 -0
  50. package/dist/seo.d.ts +140 -0
  51. package/dist/seo.js +64 -0
  52. package/dist/server/handler.d.ts +1 -0
  53. package/dist/server/handler.js +1 -0
  54. package/dist/server/head.js +5 -0
  55. package/dist/vite/build.d.ts +9 -2
  56. package/dist/vite/build.js +20 -5
  57. package/dist/vite/cloudflare.d.ts +3 -0
  58. package/dist/vite/cloudflare.js +55 -0
  59. package/dist/vite/env.d.ts +19 -0
  60. package/dist/vite/env.js +34 -0
  61. package/dist/vite/headers.d.ts +27 -0
  62. package/dist/vite/headers.js +70 -0
  63. package/dist/vite/libraries.d.ts +32 -0
  64. package/dist/vite/libraries.js +77 -0
  65. package/dist/vite/manifest.d.ts +11 -1
  66. package/dist/vite/manifest.js +10 -1
  67. package/dist/vite/netlify.d.ts +5 -2
  68. package/dist/vite/netlify.js +4 -2
  69. package/dist/vite/node.d.ts +4 -0
  70. package/dist/vite/node.js +9 -2
  71. package/dist/vite/plugin.d.ts +30 -3
  72. package/dist/vite/plugin.js +97 -28
  73. package/dist/vite/vercel.d.ts +3 -0
  74. package/dist/vite/vercel.js +53 -0
  75. package/package.json +14 -1
@@ -7,13 +7,15 @@ const URL_ATTRS = new Set([
7
7
  // SVG animation values can set an href, but only on animation elements; elsewhere these are ordinary data.
8
8
  const ANIMATION_ATTRS = new Set(['to', 'from', 'values', 'by']);
9
9
  const ANIMATION_ELEMENTS = new Set(['animate', 'set', 'animatemotion', 'animatetransform']);
10
- // Attributes that load an image, where a raster data: URL is fine.
10
+ // Attributes that load an image, where a raster data: URL or an object URL (blob:, a preview of a picked file) is fine:
11
+ // an image can't run script. Attributes that load a page or a script (iframe, embed, object, script) are static only,
12
+ // so a bound value never reaches them; href keeps refusing blob: (a blob of HTML would open as this site's page).
11
13
  const IMAGE_ATTRS = new Set(['src', 'srcset', 'poster']);
12
14
  const SAFE_SCHEMES = new Set(['http', 'https', 'mailto', 'tel']);
13
15
  const RASTER_DATA = /^data:image\/(png|gif|jpe?g|webp|avif|bmp);/i;
14
16
  export const UNSAFE_URL = 'about:invalid#unsafe';
15
17
  /**
16
- * Allowlist: a relative URL, a safe scheme, or (for image attributes) a raster data: image.
18
+ * Allowlist: a relative URL, a safe scheme, or (for image attributes) a raster data: image or an object URL.
17
19
  * Browsers ignore control characters and whitespace around and inside the scheme, so the check does too.
18
20
  */
19
21
  function safeUrl(url, image) {
@@ -23,7 +25,7 @@ function safeUrl(url, image) {
23
25
  return true; // relative: /x, ../x, #x, ?x
24
26
  if (SAFE_SCHEMES.has(scheme))
25
27
  return true;
26
- return image && RASTER_DATA.test(compact);
28
+ return image && (scheme === 'blob' || RASTER_DATA.test(compact));
27
29
  }
28
30
  // false is a value here, not a missing attribute: enumerated attributes (removing one means its default), and the
29
31
  // ARIA states whose absence means "doesn't apply" (no aria-expanded: nothing expands). Other aria-* (aria-current:
@@ -136,11 +138,12 @@ const PASSED = /^(id|role|tabindex|hidden|lang|dir|style|(data|aria)-[\w:.-]+)$/
136
138
  * as an input. They land on its root element (a spread, so the root's own attributes win; style merges).
137
139
  * @internal Compiled components call this.
138
140
  */
139
- export function passed(props, inputs) {
141
+ export function passed(props, inputs, element = []) {
140
142
  const out = {};
141
143
  // Never clarusjs's own markers (data-cl-e, data-cl-u, data-cl-path…): passed data must not spoof the runtime's wiring.
144
+ // element: an @extends component's element attributes (button's disabled, form…), passed through as well.
142
145
  for (const k in props)
143
- if (PASSED.test(k) && !/^data-cl-/i.test(k) && !inputs.includes(k))
146
+ if ((PASSED.test(k) || element.includes(k.toLowerCase())) && !/^data-cl-/i.test(k) && !inputs.includes(k))
144
147
  out[k] = props[k];
145
148
  return out;
146
149
  }
@@ -39,10 +39,23 @@ export declare function owner(node: Node): Element | null;
39
39
  export declare function provideOn(root: Element, cx: object): void;
40
40
  /** Slot ranges still open at `n`, among the siblings before it (#465). @internal */
41
41
  export declare function openSlots(n: Node): number;
42
+ /** The DOM's shape changed: counts kept so far may be wrong. */
43
+ /** @internal Nodes were added or removed: cached slot ranges are stale. */
44
+ export declare const reshaped: () => void;
45
+ /**
46
+ * Run `fn` with openSlots() counts kept between calls; nothing outside this file may add or remove nodes meanwhile.
47
+ * @internal The loader (attach) and the forms engine (refresh) call this.
48
+ */
49
+ export declare function keepSlots<T>(fn: () => T): T;
42
50
  /** Hole markers in `ctx`, by index. @internal */
43
51
  export declare function holes(ctx: Ctx): Hole[];
44
52
  /** @if block markers in `ctx`, by index. @internal */
45
53
  export declare function blocks(ctx: Ctx): Block[];
54
+ /** Turn HTML into nodes and put them before `end`; returns the first and last inserted node. @internal */
55
+ export declare function insertHtml(html: string, end: Node): {
56
+ first: Node | null;
57
+ last: Node | null;
58
+ };
46
59
  /**
47
60
  * An @if block. `which()` picks the branch (-1 for none). The server-rendered branch is wired in place; a change
48
61
  * removes the old branch (disposing its effects and handlers), inserts the new branch's HTML and wires it.
@@ -63,6 +76,17 @@ export declare function awaited(fn: () => unknown): {
63
76
  };
64
77
  /** @internal Compiled components call this; apps don't. */
65
78
  export declare function region(ctx: Ctx, index: number, html: (cx?: object) => string, wire: () => void, cx?: object): void;
79
+ /**
80
+ * @transition around a @for (clarusjs/motion): `in` gets each new row's nodes (`initial` for the server's rows on
81
+ * attach), `out` a removed row's (instead of removing them), and the update runs inside `frame` so moves can be
82
+ * measured before and after.
83
+ * @internal
84
+ */
85
+ export type ForTransition = {
86
+ in(nodes: Node[], initial?: boolean): void;
87
+ out(nodes: Node[]): void;
88
+ frame(start: Node, end: Node, run: () => void): void;
89
+ };
66
90
  /**
67
91
  * A @for list. Rows the server rendered are adopted in place. When the list changes, rows are matched by key:
68
92
  * kept rows keep their DOM nodes (moved only when out of order) and get the new item and index through their
@@ -76,7 +100,7 @@ export declare function forBlock(ctx: Ctx, index: number, list: () => unknown, k
76
100
  }, empty: {
77
101
  html: () => string;
78
102
  wire: (ctx: Ctx) => void;
79
- } | null): void;
103
+ } | null, t?: ForTransition): void;
80
104
  /** Keep exactly one text node between the hole's markers, set to `fn()`. @internal */
81
105
  export declare function text(hole: Hole, fn: () => unknown): void;
82
106
  /**
@@ -86,7 +110,13 @@ export declare function text(hole: Hole, fn: () => unknown): void;
86
110
  * for elements in that row.
87
111
  * @internal Compiled components call this; apps don't.
88
112
  */
89
- export declare function on(ctx: Ctx, index: number, type: string, fn: (e: Event) => void): void;
113
+ export declare function on(ctx: Ctx, index: number, type: string, fn: (e: Event, el: Element) => void): void;
114
+ /**
115
+ * $values (#444): el's form's fields (el's, or its form's) as an object; a repeated name gives a list, and a
116
+ * submit's button counts.
117
+ * @internal Compiled handlers call this.
118
+ */
119
+ export declare function values(el: Element, e?: Event): Record<string, unknown>;
90
120
  /**
91
121
  * [use]="behaviour" (#464): call each behaviour (one, or a list) with the element marked data-cl-u="index", once; a
92
122
  * function it returns runs when the branch, row or island goes.
@@ -121,12 +151,18 @@ export declare function emit(root: Element, name: string, value: unknown, info?:
121
151
  reason?: string;
122
152
  event?: Event;
123
153
  }): boolean;
124
- /** (name)="…" on a child island in region `index` (#442): its emit('name', …) arrives here, and goes no further. @internal */
154
+ /** A component's attach() records its @event names. @internal */
155
+ export declare function declare(root: Element, names: string[]): void;
156
+ /**
157
+ * (name)="…" on a component in region `index`. A child island's emit('name', …) arrives here and goes no further
158
+ * (#442); any other name is the DOM event, bubbling up from what the child renders (Vue's fallthrough), unless the
159
+ * child island declares it. @internal
160
+ */
125
161
  export declare function listen(ctx: Ctx, index: number, name: string, handler: (value: unknown, info: {
126
162
  reason?: string;
127
163
  event?: Event;
128
164
  cancel(): void;
129
- }) => void): void;
165
+ }) => void, bound?: string): void;
130
166
  /** [(name)]="…" on a child island in region `index`: the child's writes to its @model name arrive here. @internal */
131
167
  export declare function model(ctx: Ctx, index: number, name: string, set: (value: unknown) => void): void;
132
168
  export {};
@@ -1,7 +1,7 @@
1
1
  // Attach helpers: find markers in server HTML and keep them updated. Browser only. Never re-renders existing HTML;
2
2
  // only @if branches and @for rows that did not exist on the server are rendered here.
3
3
  import { attrValue, spreadAttrs } from './attrs.js';
4
- import { batch, effect, onDispose, scope, state, untrack } from './signals.js';
4
+ import { afterWiring, batch, effect, onDispose, scope, state, untrack } from './signals.js';
5
5
  const rootOf = (ctx) => ('root' in ctx ? ctx.root : ctx);
6
6
  /** Is `node` inside `ctx` (always true for a whole island)? */
7
7
  function inside(ctx, node) {
@@ -46,6 +46,23 @@ function contextAt(node, root, own) {
46
46
  }
47
47
  /** Slot ranges still open at `n`, among the siblings before it (#465). @internal */
48
48
  export function openSlots(n) {
49
+ if (slots) {
50
+ // While keepSlots() runs: from the nearest sibling on the left already counted, then rightwards, keeping each count. A list's
51
+ // rows then cost one pass, not one walk back per row. Left to right it's a depth: [s opens, ]s closes an open one.
52
+ const todo = [];
53
+ let s = n.previousSibling;
54
+ while (s && !slots.has(s)) {
55
+ todo.push(s);
56
+ s = s.previousSibling;
57
+ }
58
+ let depth = s ? step(slots.get(s), s) : 0;
59
+ for (let i = todo.length - 1; i >= 0; i--) {
60
+ slots.set(todo[i], depth);
61
+ depth = step(depth, todo[i]);
62
+ }
63
+ slots.set(n, depth);
64
+ return depth;
65
+ }
49
66
  let open = 0;
50
67
  let closed = 0;
51
68
  for (let s = n.previousSibling; s; s = s.previousSibling) {
@@ -59,6 +76,28 @@ export function openSlots(n) {
59
76
  }
60
77
  return open;
61
78
  }
79
+ // The slot ranges open before each node (#589 review), kept while keepSlots() runs: an island’s attach, a forms refresh.
80
+ let slots = null;
81
+ const step = (depth, n) => (n.nodeType !== 8 ? depth : n.data === '[s' ? depth + 1 : n.data === ']s' && depth ? depth - 1 : depth);
82
+ /** The DOM's shape changed: counts kept so far may be wrong. */
83
+ /** @internal Nodes were added or removed: cached slot ranges are stale. */
84
+ export const reshaped = () => { if (slots)
85
+ slots = new WeakMap(); };
86
+ /**
87
+ * Run `fn` with openSlots() counts kept between calls; nothing outside this file may add or remove nodes meanwhile.
88
+ * @internal The loader (attach) and the forms engine (refresh) call this.
89
+ */
90
+ export function keepSlots(fn) {
91
+ if (slots)
92
+ return fn();
93
+ slots = new WeakMap();
94
+ try {
95
+ return fn();
96
+ }
97
+ finally {
98
+ slots = null;
99
+ }
100
+ }
62
101
  /** The island a context's root stands for: itself when it is one, else the island it is in (null: none). */
63
102
  const islandOf = (root) => (root.localName === 'cl-island' ? root : owner(root));
64
103
  /**
@@ -110,11 +149,12 @@ function scan(ctx) {
110
149
  };
111
150
  // One pass (#465): `level` counts the islands entered below this one, minus the slot ranges entered (each moves its
112
151
  // content one island out), so the island's own comments are at 0 and nothing below 0 is (as owner() says, linearly).
113
- const visit = (parent, level) => {
152
+ // The siblings from `first` up to `stop` (not included), and what they hold.
153
+ const visit = (first, stop, level) => {
114
154
  let ranges = 0;
115
- for (let n = parent.firstChild; n; n = n.nextSibling) {
155
+ for (let n = first; n && n !== stop; n = n.nextSibling) {
116
156
  if (n.nodeType === 1)
117
- level < 0 || visit(n, level + (n.localName === 'cl-island' ? 1 : 0));
157
+ level < 0 || visit(n.firstChild, null, level + (n.localName === 'cl-island' ? 1 : 0));
118
158
  else if (n.nodeType !== 8)
119
159
  continue;
120
160
  else if (n.data === '[s') {
@@ -127,11 +167,17 @@ function scan(ctx) {
127
167
  level++;
128
168
  }
129
169
  }
130
- else if (!level && inside(ctx, n))
170
+ else if (!level && (near || inside(ctx, n)))
131
171
  take(n);
132
172
  }
133
173
  };
134
- visit(root, 0);
174
+ // A row whose markers are siblings: only between them, not the whole island once per row. Otherwise (a <tr> row
175
+ // straight in a <table>: the parser puts a <tbody> between them) the whole island, kept to the row's range.
176
+ const near = 'root' in ctx && ctx.start.parentNode === ctx.end.parentNode;
177
+ if (near)
178
+ visit(ctx.start.nextSibling, ctx.end, 0);
179
+ else
180
+ visit(root.firstChild, null, 0);
135
181
  return { holes, blocks, fors, regions };
136
182
  }
137
183
  /** Hole markers in `ctx`, by index. @internal */
@@ -142,13 +188,14 @@ export function holes(ctx) {
142
188
  export function blocks(ctx) {
143
189
  return scan(ctx).blocks;
144
190
  }
145
- /** Turn HTML into nodes and put them before `end`; returns the first and last inserted node. */
146
- function insertHtml(html, end) {
191
+ /** Turn HTML into nodes and put them before `end`; returns the first and last inserted node. @internal */
192
+ export function insertHtml(html, end) {
147
193
  const t = document.createElement('template');
148
194
  t.innerHTML = html;
149
195
  const first = t.content.firstChild;
150
196
  const last = t.content.lastChild;
151
197
  end.parentNode.insertBefore(t.content, end);
198
+ reshaped();
152
199
  // Child islands in the new HTML attach like the ones on the page (the loader registers this hook).
153
200
  const attachIslands = globalThis.__clAttach;
154
201
  if (attachIslands && end.parentNode)
@@ -187,6 +234,7 @@ export function block(ctx, index, which, branches) {
187
234
  dispose?.();
188
235
  while (b.start.nextSibling && b.start.nextSibling !== b.end)
189
236
  b.start.nextSibling.remove();
237
+ reshaped();
190
238
  if (k >= 0)
191
239
  insertHtml(branches[k].html(), b.end);
192
240
  current = k;
@@ -255,6 +303,7 @@ export function region(ctx, index, html, wire, cx) {
255
303
  dispose?.();
256
304
  while (r.start.nextSibling && r.start.nextSibling !== r.end)
257
305
  r.start.nextSibling.remove();
306
+ reshaped();
258
307
  insertHtml(next, r.end);
259
308
  }
260
309
  first = false;
@@ -269,7 +318,7 @@ export function region(ctx, index, html, wire, cx) {
269
318
  * the list is empty.
270
319
  * @internal Compiled components call this; apps don't.
271
320
  */
272
- export function forBlock(ctx, index, list, keyOf, row, empty) {
321
+ export function forBlock(ctx, index, list, keyOf, row, empty, t) {
273
322
  const f = scan(ctx).fors[index];
274
323
  if (!f)
275
324
  return;
@@ -310,40 +359,79 @@ export function forBlock(ctx, index, list, keyOf, row, empty) {
310
359
  });
311
360
  if (first) {
312
361
  first = false;
313
- rows = f.rows.map((range, i) => wireRow(range, keys[i], items[i], i));
362
+ if (f.rows.length === items.length) {
363
+ rows = f.rows.map((range, i) => wireRow(range, keys[i], items[i], i));
364
+ if (f.empty)
365
+ emptyRange = wireEmpty(f.empty);
366
+ if (t)
367
+ for (const r of rows)
368
+ t.in(rangeNodes(r.start, r.end), true);
369
+ return;
370
+ }
371
+ // The browser's list isn't the one the server rendered (it read something the server couldn't: the URL,
372
+ // storage): drop the server's rows and render the browser's below, as for any change.
373
+ for (const range of f.rows)
374
+ for (const node of rangeNodes(range.start, range.end))
375
+ node.parentNode?.removeChild(node);
314
376
  if (f.empty)
315
377
  emptyRange = wireEmpty(f.empty);
316
- return;
378
+ reshaped();
317
379
  }
318
- const old = new Map(rows.map((r) => [r.key, r]));
319
- const next = items.map((item, i) => {
320
- const kept = old.get(keys[i]);
321
- if (kept) {
322
- old.delete(keys[i]);
323
- kept.item.set(item);
324
- kept.index.set(i);
325
- return kept;
380
+ // With @transition, the update runs inside t.frame (clarusjs/motion measures rows before and after it).
381
+ const update = () => {
382
+ const added = [];
383
+ const old = new Map(rows.map((r) => [r.key, r]));
384
+ const next = items.map((item, i) => {
385
+ const kept = old.get(keys[i]);
386
+ if (kept) {
387
+ old.delete(keys[i]);
388
+ kept.item.set(item);
389
+ kept.index.set(i);
390
+ return kept;
391
+ }
392
+ const { first: start, last: end } = insertHtml(`<!--[r-->${row.html(item, i, items)}<!--]-->`, f.end);
393
+ const r = wireRow({ start: start, end: end }, keys[i], item, i);
394
+ added.push(r);
395
+ return r;
396
+ });
397
+ for (const r of old.values()) {
398
+ r.dispose();
399
+ // With @transition, a removed row leaves first (clarusjs/motion), else it goes now.
400
+ if (t)
401
+ t.out(rangeNodes(r.start, r.end));
402
+ else
403
+ for (const n of rangeNodes(r.start, r.end))
404
+ n.parentNode?.removeChild(n);
326
405
  }
327
- const { first: start, last: end } = insertHtml(`<!--[r-->${row.html(item, i, items)}<!--]-->`, f.end);
328
- return wireRow({ start: start, end: end }, keys[i], item, i);
329
- });
330
- for (const r of old.values()) {
331
- r.dispose();
332
- for (const n of rangeNodes(r.start, r.end))
333
- n.parentNode?.removeChild(n);
334
- }
335
- // Put rows in order, moving only those that are not already right after the previous one.
336
- let anchor = f.start;
337
- for (const r of next) {
338
- if (anchor.nextSibling !== r.start)
339
- anchor.after(...rangeNodes(r.start, r.end));
340
- anchor = r.end;
341
- }
342
- rows = next;
406
+ reshaped();
407
+ // Put rows in order, moving only those that are not already right after the previous one.
408
+ let anchor = f.start;
409
+ for (const r of next) {
410
+ // With @transition, leaving rows (no markers left) sit between live ones: stepped over, they keep their place.
411
+ let at = anchor.nextSibling;
412
+ if (t)
413
+ while (at && at.nodeType !== 8)
414
+ at = at.nextSibling;
415
+ if (at !== r.start) {
416
+ anchor.after(...rangeNodes(r.start, r.end));
417
+ reshaped();
418
+ }
419
+ anchor = r.end;
420
+ }
421
+ rows = next;
422
+ if (t)
423
+ for (const r of added)
424
+ t.in(rangeNodes(r.start, r.end));
425
+ };
426
+ if (t)
427
+ t.frame(f.start, f.end, update);
428
+ else
429
+ update();
343
430
  if (items.length && emptyRange) {
344
431
  emptyRange.dispose();
345
432
  for (const n of rangeNodes(emptyRange.start, emptyRange.end))
346
433
  n.parentNode?.removeChild(n);
434
+ reshaped();
347
435
  emptyRange = null;
348
436
  }
349
437
  else if (!items.length && !emptyRange && empty) {
@@ -399,7 +487,7 @@ export function on(ctx, index, type, fn) {
399
487
  continue;
400
488
  for (const h of [...(own.get(Number(i)) ?? [])])
401
489
  if (inside(h.ctx, el))
402
- h.fn(e);
490
+ h.fn(e, el);
403
491
  if (e.cancelBubble)
404
492
  break;
405
493
  }
@@ -413,6 +501,22 @@ export function on(ctx, index, type, fn) {
413
501
  onDispose(() => { const at = list.indexOf(handler); if (at !== -1)
414
502
  list.splice(at, 1); }); // a removed branch or row stops listening
415
503
  }
504
+ /**
505
+ * $values (#444): el's form's fields (el's, or its form's) as an object; a repeated name gives a list, and a
506
+ * submit's button counts.
507
+ * @internal Compiled handlers call this.
508
+ */
509
+ export function values(el, e) {
510
+ const form = el instanceof HTMLFormElement ? el : el.form ?? el.closest('form');
511
+ if (!form)
512
+ return {};
513
+ const data = new FormData(form, e?.submitter);
514
+ // own properties: a field named __proto__ stays a field
515
+ return Object.fromEntries([...new Set(data.keys())].map((k) => {
516
+ const all = data.getAll(k);
517
+ return [k, all.length > 1 ? all : all[0]];
518
+ }));
519
+ }
416
520
  /**
417
521
  * [use]="behaviour" (#464): call each behaviour (one, or a list) with the element marked data-cl-u="index", once; a
418
522
  * function it returns runs when the branch, row or island goes.
@@ -422,19 +526,35 @@ export function use(ctx, index, get) {
422
526
  const el = marked(ctx, `[data-cl-u="${index}"]`);
423
527
  if (!el)
424
528
  return;
425
- for (const fn of [untrack(get)].flat()) {
426
- if (typeof fn !== 'function')
427
- continue;
428
- const dispose = fn(el);
429
- if (typeof dispose === 'function')
430
- onDispose(dispose);
431
- }
529
+ // After the wiring around it (#550), if its element is still there.
530
+ afterWiring(() => {
531
+ if (!el.isConnected)
532
+ return;
533
+ for (const fn of [untrack(get)].flat()) {
534
+ if (typeof fn !== 'function')
535
+ continue;
536
+ const dispose = fn(el);
537
+ if (typeof dispose === 'function')
538
+ onDispose(dispose);
539
+ }
540
+ });
432
541
  }
433
542
  /** The element matching `selector` in `ctx`: this island's (not a nested island's), and in a row, that row's. */
434
543
  export function marked(ctx, selector) {
435
544
  const root = rootOf(ctx);
436
545
  const island = islandOf(root);
437
- return [...root.querySelectorAll(selector)].find((e) => owner(e) === island && inside(ctx, e));
546
+ const mine = (e) => owner(e) === island;
547
+ if (!('root' in ctx) || ctx.start.parentNode !== ctx.end.parentNode)
548
+ return [...root.querySelectorAll(selector)].find((e) => mine(e) && inside(ctx, e));
549
+ // A row with sibling markers: the elements between them, not the whole island's once per row.
550
+ for (let n = ctx.start.nextSibling; n && n !== ctx.end; n = n.nextSibling) {
551
+ if (n.nodeType !== 1)
552
+ continue;
553
+ const found = (n.matches(selector) ? [n] : []).concat([...n.querySelectorAll(selector)]).find(mine);
554
+ if (found)
555
+ return found;
556
+ }
557
+ return undefined;
438
558
  }
439
559
  /**
440
560
  * A reactive spread (#405): sets the attributes the object has, removes the ones it no longer has (only those it set:
@@ -518,15 +638,46 @@ export function modelInput(root, name, initial) {
518
638
  export function emit(root, name, value, info = {}) {
519
639
  return root.dispatchEvent(new CustomEvent('cl:event', { detail: { name, value, ...info }, bubbles: true, cancelable: true }));
520
640
  }
521
- /** (name)="…" on a child island in region `index` (#442): its emit('name', …) arrives here, and goes no further. @internal */
522
- export function listen(ctx, index, name, handler) {
641
+ // The @event names each child island declares (recorded when it attaches): those don't fall through as DOM events.
642
+ const declared = new WeakMap();
643
+ /** A component's attach() records its @event names. @internal */
644
+ export function declare(root, names) {
645
+ declared.set(root, names);
646
+ }
647
+ /**
648
+ * (name)="…" on a component in region `index`. A child island's emit('name', …) arrives here and goes no further
649
+ * (#442); any other name is the DOM event, bubbling up from what the child renders (Vue's fallthrough), unless the
650
+ * child island declares it. @internal
651
+ */
652
+ export function listen(ctx, index, name, handler, bound) {
653
+ // The bound component's island (by its c name): only its emit() is this listener's, and only its contract decides
654
+ // whether a DOM event is one it sends itself. A wrapper that declares nothing gets the DOM event.
655
+ const isBound = (el) => !bound || el.getAttribute('c') === bound;
523
656
  fromChild(ctx, index, 'cl:event', name, (event, detail) => {
524
657
  const { value, reason, event: cause } = detail;
525
658
  handler(value, { reason, event: cause, cancel: () => event.preventDefault() });
526
- });
659
+ }, (from) => isBound(from));
660
+ const r = scan(ctx).regions[index];
661
+ if (!r)
662
+ return;
663
+ const host = r.start.parentNode;
664
+ const dom = (event) => {
665
+ const from = event.target;
666
+ if (!(r.start.compareDocumentPosition(from) & 4) || !(r.end.compareDocumentPosition(from) & 2))
667
+ return;
668
+ // The bound island between the target and here: not attached yet, it replays the event once it is (its emit()
669
+ // decides then); declaring this name, it sends it as its own event.
670
+ for (let n = from; n && n !== host; n = n.parentNode) {
671
+ if (n.localName === 'cl-island' && isBound(n) && (!n.hasAttribute('data-cl-ready') || declared.get(n)?.includes(name)))
672
+ return;
673
+ }
674
+ handler(event, { event, cancel: () => event.preventDefault() });
675
+ };
676
+ host.addEventListener(name, dom);
677
+ onDispose(() => host.removeEventListener(name, dom));
527
678
  }
528
679
  /** Listen for `type` from the child island bound in region `index` only (not one nested in it), named `name`. */
529
- function fromChild(ctx, index, type, name, take) {
680
+ function fromChild(ctx, index, type, name, take, accept = () => true) {
530
681
  const r = scan(ctx).regions[index];
531
682
  if (!r)
532
683
  return;
@@ -538,7 +689,7 @@ function fromChild(ctx, index, type, name, take) {
538
689
  if (detail.name !== name || !(r.start.compareDocumentPosition(from) & 4) || !(r.end.compareDocumentPosition(from) & 2))
539
690
  return;
540
691
  // Only the child island this region binds: an island nested in it, which nobody bound, keeps its own.
541
- if (owner(from) !== rootOf(ctx))
692
+ if (owner(from) !== rootOf(ctx) || !accept(from))
542
693
  return;
543
694
  event.stopPropagation();
544
695
  take(event, detail);
@@ -8,7 +8,8 @@ export async function swap(url) {
8
8
  const next = new URL(url, location.href);
9
9
  const pathOf = (el) => new URL(el.getAttribute('src') ?? '', location.href).pathname;
10
10
  // The outermost islands of this component: one nested in another is swapped with it.
11
- const mine = (root) => [...root.querySelectorAll('cl-island')].filter((el) => pathOf(el) === next.pathname && !el.parentElement?.closest(`cl-island[src="${CSS.escape(el.getAttribute('src'))}"]`));
11
+ // A copy still leaving (@transition: inert) isn't the page's any more: not counted.
12
+ const mine = (root) => [...root.querySelectorAll('cl-island')].filter((el) => pathOf(el) === next.pathname && !el.closest('[inert]') && !el.parentElement?.closest(`cl-island[src="${CSS.escape(el.getAttribute('src'))}"]`));
12
13
  const old = mine(document);
13
14
  if (!old.length)
14
15
  return;
@@ -28,7 +29,7 @@ export async function swap(url) {
28
29
  swapped.replaceChildren(...[...fresh[i].childNodes].map((n) => document.importNode(n, true)));
29
30
  swapped.setAttribute('s', fresh[i].getAttribute('s') ?? '{}');
30
31
  // State: what the server rendered with, then what the running version had, island by island in page order.
31
- const before = [el, ...el.querySelectorAll('cl-island')];
32
+ const before = [el, ...[...el.querySelectorAll('cl-island')].filter((x) => !x.closest('[inert]'))];
32
33
  const after = [swapped, ...swapped.querySelectorAll('cl-island')];
33
34
  if (before.length === after.length) {
34
35
  before.forEach((b, j) => {
@@ -1,5 +1,5 @@
1
1
  export { state, derived, shared, effect, scope, onDispose, untrack, batch, type Signal, type ReadSignal } from './signals.ts';
2
- export { holes, blocks, text, on, attr, spread, block, awaited, forBlock, region, model, modelInput, emit, listen, use, provideOn, type Hole, type Block, type Ctx } from './dom.ts';
2
+ export { holes, blocks, text, on, attr, spread, block, awaited, forBlock, region, model, modelInput, emit, listen, declare, use, values, provideOn, type Hole, type Block, type Ctx } from './dom.ts';
3
3
  export { escape, attrHtml, spreadHtml, blockHtml, forHtml, regionHtml } from './html.ts';
4
4
  export { parse as parseIslandData } from './serialize.ts';
5
5
  export { classValue, styleValue } from './attrs.ts';
@@ -1,6 +1,6 @@
1
1
  // Browser runtime used by compiled attach() functions.
2
2
  export { state, derived, shared, effect, scope, onDispose, untrack, batch } from './signals.js';
3
- export { holes, blocks, text, on, attr, spread, block, awaited, forBlock, region, model, modelInput, emit, listen, use, provideOn } from './dom.js';
3
+ export { holes, blocks, text, on, attr, spread, block, awaited, forBlock, region, model, modelInput, emit, listen, declare, use, values, provideOn } from './dom.js';
4
4
  export { escape, attrHtml, spreadHtml, blockHtml, forHtml, regionHtml } from './html.js';
5
5
  export { parse as parseIslandData } from './serialize.js';
6
6
  export { classValue, styleValue } from './attrs.js';
@@ -17,7 +17,7 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
17
17
  }
18
18
  return path;
19
19
  };
20
- import { owner } from './dom.js';
20
+ import { keepSlots, owner } from './dom.js';
21
21
  import { parse } from './serialize.js';
22
22
  import { scope } from './signals.js';
23
23
  // Islands scheduled or loading. `data-cl-ready` is set only after attach() succeeds.
@@ -35,6 +35,10 @@ function holds(inner, el) {
35
35
  const updates = new WeakMap();
36
36
  // Each attached island's disposer: everything its attach() wired (effects, listeners) is released by it.
37
37
  const attached = new Map();
38
+ // Each island root's attributes as the server last rendered them: a re-render's changes (the parent's passed
39
+ // attributes) are applied to the kept root; ones its own state drives render the same and are left to it.
40
+ const rendered = new WeakMap();
41
+ const rootAttrs = (island) => new Map([...(island.firstElementChild?.attributes ?? [])].filter((a) => !a.name.startsWith('data-cl-')).map((a) => [a.name, a.value]));
38
42
  let lastLoad = (src) => import(__rewriteRelativeImportExtension(/* @vite-ignore */ src));
39
43
  // Events recorded until an island attaches, then replayed. Clicks and submits are held back (no navigation, no
40
44
  // form post) so the island can handle them once it is ready.
@@ -95,7 +99,10 @@ function schedule(el, go) {
95
99
  go();
96
100
  }
97
101
  });
98
- io.observe(el);
102
+ // <cl-island> is display: contents (no box: it never intersects), so watch what it holds.
103
+ const targets = el.children.length ? [...el.children] : [el.parentElement ?? el];
104
+ for (const target of targets)
105
+ io.observe(target);
99
106
  }
100
107
  else if (mode === 'interaction') {
101
108
  const start = () => { for (const type of INTERACTION)
@@ -113,9 +120,9 @@ async function attachOne(el, load, recorded) {
113
120
  let state;
114
121
  try {
115
122
  mod = await load(el.getAttribute('src'));
116
- props = parse(el.getAttribute('p') ?? '{}');
117
- const s = el.getAttribute('s');
118
- state = s === null ? undefined : parse(s);
123
+ // Waiting (idle, visible, interaction) or loading while it started leaving (@transition: inert): it's going.
124
+ if (el.closest('[inert]'))
125
+ return;
119
126
  // Islands in it holding its content attach first (#465): content it re-renders there reads what they provide. One
120
127
  // failing to load fails this one too (both retry). ponytail: later ones (idle, visible, interaction) aren't waited for.
121
128
  for (const inner of el.querySelectorAll('cl-island')) {
@@ -125,6 +132,10 @@ async function attachOne(el, load, recorded) {
125
132
  if (!pending.has(inner))
126
133
  throw 0;
127
134
  }
135
+ // Read last: a parent may have passed new props meanwhile (#524).
136
+ props = parse(el.getAttribute('p') ?? '{}');
137
+ const s = el.getAttribute('s');
138
+ state = s === null ? undefined : parse(s);
128
139
  }
129
140
  catch (err) {
130
141
  recorded.stop();
@@ -134,9 +145,10 @@ async function attachOne(el, load, recorded) {
134
145
  return;
135
146
  }
136
147
  try {
148
+ rendered.set(el, rootAttrs(el));
137
149
  attached.set(el, scope(() => {
138
150
  // An island with inputs returns how to update them: kept for a parent island that passes new props.
139
- const update = mod.attach(el, props, state);
151
+ const update = keepSlots(() => mod.attach(el, props, state)); // openSlots counts kept while it wires (dom.ts)
140
152
  if (typeof update === 'function')
141
153
  updates.set(el, update);
142
154
  }));
@@ -222,7 +234,10 @@ globalThis.__clUpdate = (el, next) => {
222
234
  // The same component: the same name and the same source (two panel.tscl files share a name). An attached island
223
235
  // without inputs has nothing to update (#465: re-rendered because the content it holds changed, which its parent
224
236
  // wires in place).
225
- if (!attached.has(el) || el.getAttribute('c') !== next.getAttribute('c') || el.getAttribute('src') !== next.getAttribute('src'))
237
+ if (el.getAttribute('c') !== next.getAttribute('c') || el.getAttribute('src') !== next.getAttribute('src'))
238
+ return false;
239
+ // Not attached yet but on its way: it takes the new props and attaches with them (#524).
240
+ if (!attached.has(el) && !loading.has(el))
226
241
  return false;
227
242
  const p = next.getAttribute('p') ?? '{}';
228
243
  el.setAttribute('p', p);
@@ -233,6 +248,23 @@ globalThis.__clUpdate = (el, next) => {
233
248
  for (const { name, value } of [...next.attributes])
234
249
  if (name.startsWith('data-cl-path-'))
235
250
  el.setAttribute(name, value);
251
+ if (!attached.has(el)) {
252
+ // And the markup rendered for them, which it adopts.
253
+ el.replaceChildren(...next.childNodes);
254
+ return true;
255
+ }
256
+ const root = el.firstElementChild;
257
+ const before = rendered.get(el);
258
+ const after = rootAttrs(next);
259
+ if (root && before) {
260
+ for (const [name, value] of after)
261
+ if (before.get(name) !== value)
262
+ root.setAttribute(name, value);
263
+ for (const name of before.keys())
264
+ if (!after.has(name))
265
+ root.removeAttribute(name);
266
+ }
267
+ rendered.set(el, after);
236
268
  update?.(parse(p));
237
269
  return true;
238
270
  };