clarusjs 0.14.0 → 0.16.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/dist/check/generate.d.ts +2 -5
- package/dist/check/generate.js +119 -5
- package/dist/cli/check.d.ts +10 -2
- package/dist/cli/check.js +38 -12
- package/dist/cli/index.js +4 -1
- package/dist/cli/mapper.js +24 -1
- package/dist/cli/project.d.ts +6 -0
- package/dist/cli/project.js +1 -1
- package/dist/compiler/class-style.js +2 -2
- package/dist/compiler/components.d.ts +61 -0
- package/dist/compiler/components.js +81 -0
- package/dist/compiler/element-attrs.d.ts +7 -0
- package/dist/compiler/element-attrs.js +32 -0
- package/dist/compiler/emit-client.js +23 -3
- package/dist/compiler/emit-server.js +11 -8
- package/dist/compiler/html-expr.d.ts +2 -1
- package/dist/compiler/html-expr.js +15 -9
- package/dist/compiler/index.d.ts +14 -2
- package/dist/compiler/index.js +143 -3
- package/dist/compiler/ir.d.ts +24 -0
- package/dist/compiler/ir.js +81 -11
- package/dist/compiler/lint.d.ts +26 -2
- package/dist/compiler/lint.js +64 -4
- package/dist/compiler/parse.d.ts +37 -0
- package/dist/compiler/parse.js +42 -10
- package/dist/compiler/script.d.ts +11 -0
- package/dist/compiler/script.js +41 -0
- package/dist/compiler/shared.d.ts +6 -0
- package/dist/compiler/shared.js +14 -0
- package/dist/forms/index.js +15 -3
- package/dist/runtime/attrs.d.ts +6 -0
- package/dist/runtime/attrs.js +22 -3
- package/dist/runtime/dom.d.ts +39 -1
- package/dist/runtime/dom.js +159 -23
- package/dist/runtime/html.d.ts +6 -0
- package/dist/runtime/html.js +12 -0
- package/dist/runtime/index.d.ts +1 -1
- package/dist/runtime/index.js +1 -1
- package/dist/runtime/loader.js +53 -7
- package/dist/runtime/router.d.ts +6 -1
- package/dist/runtime/router.js +1 -1
- package/dist/runtime/server.d.ts +8 -2
- package/dist/runtime/server.js +14 -7
- package/dist/server/handler.d.ts +28 -1
- package/dist/server/handler.js +44 -1
- package/dist/server/modes.d.ts +2 -0
- package/dist/server/modes.js +5 -1
- package/dist/vite/build.d.ts +9 -2
- package/dist/vite/build.js +20 -5
- package/dist/vite/libraries.d.ts +32 -0
- package/dist/vite/libraries.js +77 -0
- package/dist/vite/plugin.d.ts +23 -0
- package/dist/vite/plugin.js +52 -24
- package/package.json +1 -1
package/dist/forms/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { effect, onDispose, state, untrack } from '../runtime/index.js';
|
|
2
|
-
import { marked } from '../runtime/dom.js';
|
|
2
|
+
import { marked, openSlots, owner } from '../runtime/dom.js';
|
|
3
3
|
import { browser } from '../runtime/env.js';
|
|
4
4
|
const CONTAINERS = 'label, [data-cl-field], fieldset, cl-island';
|
|
5
5
|
const FIELDS = 'input[data-cl-path],select[data-cl-path],textarea[data-cl-path]';
|
|
@@ -9,16 +9,28 @@ const read = (value, path) => path.split('.').reduce((v, k) => (v == null ? v :
|
|
|
9
9
|
const dotted = (path = []) => path.map((p) => String(typeof p === 'object' ? p.key : p)).join('.');
|
|
10
10
|
function pathOf(field, root) {
|
|
11
11
|
const parts = [];
|
|
12
|
+
let through = 0;
|
|
12
13
|
for (let el = field; el && el !== root.parentElement; el = el.parentElement) {
|
|
13
14
|
const here = el;
|
|
15
|
+
if (through && here.localName === 'cl-island') {
|
|
16
|
+
through--;
|
|
17
|
+
through += openSlots(here);
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
if (through) {
|
|
21
|
+
through += openSlots(here);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
through += openSlots(here);
|
|
14
25
|
let part = here.getAttribute('data-cl-path');
|
|
15
26
|
if (part?.startsWith('@')) {
|
|
16
27
|
const [model, ...rest] = part.slice(1).split('.');
|
|
17
|
-
const island = here
|
|
18
|
-
if (!island)
|
|
28
|
+
const island = owner(here);
|
|
29
|
+
if (!island?.hasAttribute(`data-cl-path-${model}`))
|
|
19
30
|
return '';
|
|
20
31
|
part = [island.getAttribute(`data-cl-path-${model}`), ...rest].filter(Boolean).join('.');
|
|
21
32
|
el = island;
|
|
33
|
+
through = openSlots(island);
|
|
22
34
|
}
|
|
23
35
|
if (part)
|
|
24
36
|
parts.unshift(part);
|
package/dist/runtime/attrs.d.ts
CHANGED
|
@@ -30,3 +30,9 @@ export declare function spreadAttrs(obj: unknown, skip: readonly string[], tag?:
|
|
|
30
30
|
set: [string, string | true | null][];
|
|
31
31
|
dropped: [string, string][];
|
|
32
32
|
};
|
|
33
|
+
/**
|
|
34
|
+
* The attributes a parent passed to a component (#463): its props with an attribute-like name that it doesn't declare
|
|
35
|
+
* as an input. They land on its root element (a spread, so the root's own attributes win; style merges).
|
|
36
|
+
* @internal Compiled components call this.
|
|
37
|
+
*/
|
|
38
|
+
export declare function passed(props: Record<string, unknown>, inputs: readonly string[], element?: readonly string[]): Record<string, unknown>;
|
package/dist/runtime/attrs.js
CHANGED
|
@@ -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:
|
|
@@ -128,3 +130,20 @@ export function spreadAttrs(obj, skip, tag) {
|
|
|
128
130
|
}
|
|
129
131
|
return { set, dropped };
|
|
130
132
|
}
|
|
133
|
+
// What a parent's attributes may set on a component's root (#463): attribute-like names only, so a page's load() data or
|
|
134
|
+
// a layout's path never become attributes.
|
|
135
|
+
const PASSED = /^(id|role|tabindex|hidden|lang|dir|style|(data|aria)-[\w:.-]+)$/;
|
|
136
|
+
/**
|
|
137
|
+
* The attributes a parent passed to a component (#463): its props with an attribute-like name that it doesn't declare
|
|
138
|
+
* as an input. They land on its root element (a spread, so the root's own attributes win; style merges).
|
|
139
|
+
* @internal Compiled components call this.
|
|
140
|
+
*/
|
|
141
|
+
export function passed(props, inputs, element = []) {
|
|
142
|
+
const out = {};
|
|
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.
|
|
145
|
+
for (const k in props)
|
|
146
|
+
if ((PASSED.test(k) || element.includes(k.toLowerCase())) && !/^data-cl-/i.test(k) && !inputs.includes(k))
|
|
147
|
+
out[k] = props[k];
|
|
148
|
+
return out;
|
|
149
|
+
}
|
package/dist/runtime/dom.d.ts
CHANGED
|
@@ -28,6 +28,17 @@ export type Ctx = Element | {
|
|
|
28
28
|
start: Comment;
|
|
29
29
|
end: Comment;
|
|
30
30
|
};
|
|
31
|
+
/**
|
|
32
|
+
* The island that owns `node` (#465): its nearest <cl-island>, except that each slot range (`<!--[s-->…<!--]s-->`) it
|
|
33
|
+
* sits inside moves ownership one island outward, so content an island passes to another stays its own. Null when
|
|
34
|
+
* no island owns it. An [s the parser left without its ]s runs to the end of its parent.
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
export declare function owner(node: Node): Element | null;
|
|
38
|
+
/** @internal An island's provide()s, for content another island re-renders inside it. */
|
|
39
|
+
export declare function provideOn(root: Element, cx: object): void;
|
|
40
|
+
/** Slot ranges still open at `n`, among the siblings before it (#465). @internal */
|
|
41
|
+
export declare function openSlots(n: Node): number;
|
|
31
42
|
/** Hole markers in `ctx`, by index. @internal */
|
|
32
43
|
export declare function holes(ctx: Ctx): Hole[];
|
|
33
44
|
/** @if block markers in `ctx`, by index. @internal */
|
|
@@ -51,7 +62,7 @@ export declare function awaited(fn: () => unknown): {
|
|
|
51
62
|
v(): unknown;
|
|
52
63
|
};
|
|
53
64
|
/** @internal Compiled components call this; apps don't. */
|
|
54
|
-
export declare function region(ctx: Ctx, index: number, html: () => string, wire: () => void): void;
|
|
65
|
+
export declare function region(ctx: Ctx, index: number, html: (cx?: object) => string, wire: () => void, cx?: object): void;
|
|
55
66
|
/**
|
|
56
67
|
* A @for list. Rows the server rendered are adopted in place. When the list changes, rows are matched by key:
|
|
57
68
|
* kept rows keep their DOM nodes (moved only when out of order) and get the new item and index through their
|
|
@@ -76,6 +87,12 @@ export declare function text(hole: Hole, fn: () => unknown): void;
|
|
|
76
87
|
* @internal Compiled components call this; apps don't.
|
|
77
88
|
*/
|
|
78
89
|
export declare function on(ctx: Ctx, index: number, type: string, fn: (e: Event) => void): void;
|
|
90
|
+
/**
|
|
91
|
+
* [use]="behaviour" (#464): call each behaviour (one, or a list) with the element marked data-cl-u="index", once; a
|
|
92
|
+
* function it returns runs when the branch, row or island goes.
|
|
93
|
+
* @internal Compiled components call this.
|
|
94
|
+
*/
|
|
95
|
+
export declare function use(ctx: Ctx, index: number, get: () => unknown): void;
|
|
79
96
|
/** The element matching `selector` in `ctx`: this island's (not a nested island's), and in a row, that row's. */
|
|
80
97
|
export declare function marked(ctx: Ctx, selector: string): Element | undefined;
|
|
81
98
|
/**
|
|
@@ -95,6 +112,27 @@ export declare function attr(ctx: Ctx, index: number, name: string, fn: () => un
|
|
|
95
112
|
export declare function modelInput<T>(root: Element, name: string, initial: T): Signal<T> & {
|
|
96
113
|
own(value: T): void;
|
|
97
114
|
};
|
|
115
|
+
/**
|
|
116
|
+
* emit() in a component that declares @event (#442): a cancelable cl:event from the island to its parent's listener.
|
|
117
|
+
* False when the parent cancelled it ($info.cancel()).
|
|
118
|
+
* @internal Compiled components call this; apps don't.
|
|
119
|
+
*/
|
|
120
|
+
export declare function emit(root: Element, name: string, value: unknown, info?: {
|
|
121
|
+
reason?: string;
|
|
122
|
+
event?: Event;
|
|
123
|
+
}): boolean;
|
|
124
|
+
/** A component's attach() records its @event names. @internal */
|
|
125
|
+
export declare function declare(root: Element, names: string[]): void;
|
|
126
|
+
/**
|
|
127
|
+
* (name)="…" on a component in region `index`. A child island's emit('name', …) arrives here and goes no further
|
|
128
|
+
* (#442); any other name is the DOM event, bubbling up from what the child renders (Vue's fallthrough), unless the
|
|
129
|
+
* child island declares it. @internal
|
|
130
|
+
*/
|
|
131
|
+
export declare function listen(ctx: Ctx, index: number, name: string, handler: (value: unknown, info: {
|
|
132
|
+
reason?: string;
|
|
133
|
+
event?: Event;
|
|
134
|
+
cancel(): void;
|
|
135
|
+
}) => void, bound?: string): void;
|
|
98
136
|
/** [(name)]="…" on a child island in region `index`: the child's writes to its @model name arrive here. @internal */
|
|
99
137
|
export declare function model(ctx: Ctx, index: number, name: string, set: (value: unknown) => void): void;
|
|
100
138
|
export {};
|
package/dist/runtime/dom.js
CHANGED
|
@@ -10,6 +10,57 @@ function inside(ctx, node) {
|
|
|
10
10
|
return !!(ctx.start.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_FOLLOWING) &&
|
|
11
11
|
!!(ctx.end.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_PRECEDING);
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* The island that owns `node` (#465): its nearest <cl-island>, except that each slot range (`<!--[s-->…<!--]s-->`) it
|
|
15
|
+
* sits inside moves ownership one island outward, so content an island passes to another stays its own. Null when
|
|
16
|
+
* no island owns it. An [s the parser left without its ]s runs to the end of its parent.
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export function owner(node) {
|
|
20
|
+
let out = 0;
|
|
21
|
+
for (let n = node; n; n = n.parentNode) {
|
|
22
|
+
if (n !== node && n.localName === 'cl-island') {
|
|
23
|
+
if (!out)
|
|
24
|
+
return n;
|
|
25
|
+
out--;
|
|
26
|
+
}
|
|
27
|
+
out += openSlots(n);
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
// What each island provides (#465), for the components it holds that another island re-renders.
|
|
32
|
+
const provided = new WeakMap();
|
|
33
|
+
/** @internal An island's provide()s, for content another island re-renders inside it. */
|
|
34
|
+
export function provideOn(root, cx) { provided.set(root, cx); }
|
|
35
|
+
/**
|
|
36
|
+
* The context a region renders with: its island's own, plus what each island holding the region (it sits in their
|
|
37
|
+
* slot ranges, between them and `root`) provides, inner ones first. Spreading (Object.assign drops a __proto__ key)
|
|
38
|
+
* reads the getters now, so the region follows the values it read.
|
|
39
|
+
*/
|
|
40
|
+
function contextAt(node, root, own) {
|
|
41
|
+
let cx;
|
|
42
|
+
for (let n = node.parentNode; n && n !== root; n = n.parentNode)
|
|
43
|
+
if (n.localName === 'cl-island')
|
|
44
|
+
cx = { ...provided.get(n), ...cx };
|
|
45
|
+
return cx ? { ...own, ...cx } : own;
|
|
46
|
+
}
|
|
47
|
+
/** Slot ranges still open at `n`, among the siblings before it (#465). @internal */
|
|
48
|
+
export function openSlots(n) {
|
|
49
|
+
let open = 0;
|
|
50
|
+
let closed = 0;
|
|
51
|
+
for (let s = n.previousSibling; s; s = s.previousSibling) {
|
|
52
|
+
if (s.nodeType !== 8)
|
|
53
|
+
continue;
|
|
54
|
+
const d = s.data;
|
|
55
|
+
if (d === ']s')
|
|
56
|
+
closed++;
|
|
57
|
+
else if (d === '[s')
|
|
58
|
+
closed ? closed-- : open++;
|
|
59
|
+
}
|
|
60
|
+
return open;
|
|
61
|
+
}
|
|
62
|
+
/** The island a context's root stands for: itself when it is one, else the island it is in (null: none). */
|
|
63
|
+
const islandOf = (root) => (root.localName === 'cl-island' ? root : owner(root));
|
|
13
64
|
/**
|
|
14
65
|
* Find every marker in `ctx`, skipping nested islands:
|
|
15
66
|
* holes `<!--[N-->`, @if blocks `<!--[bN:K-->`, @for lists `<!--[fN-->` with rows `<!--[r-->` and `<!--[e-->`,
|
|
@@ -22,15 +73,7 @@ function scan(ctx) {
|
|
|
22
73
|
const fors = [];
|
|
23
74
|
const regions = [];
|
|
24
75
|
const open = [];
|
|
25
|
-
const
|
|
26
|
-
acceptNode: (n) => n.nodeType === 8 ? NodeFilter.FILTER_ACCEPT
|
|
27
|
-
: n !== root && n.localName === 'cl-island' ? NodeFilter.FILTER_REJECT
|
|
28
|
-
: NodeFilter.FILTER_SKIP,
|
|
29
|
-
});
|
|
30
|
-
for (let n = walker.nextNode(); n; n = walker.nextNode()) {
|
|
31
|
-
if (!inside(ctx, n))
|
|
32
|
-
continue;
|
|
33
|
-
const c = n;
|
|
76
|
+
const take = (c) => {
|
|
34
77
|
const data = c.data;
|
|
35
78
|
let m;
|
|
36
79
|
const push = (kind, index = 0, k = 0) => open.push({ kind, index, k, start: c, rows: [], empty: null });
|
|
@@ -64,7 +107,31 @@ function scan(ctx) {
|
|
|
64
107
|
parent.empty = { start: o.start, end: c };
|
|
65
108
|
}
|
|
66
109
|
}
|
|
67
|
-
}
|
|
110
|
+
};
|
|
111
|
+
// One pass (#465): `level` counts the islands entered below this one, minus the slot ranges entered (each moves its
|
|
112
|
+
// 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) => {
|
|
114
|
+
let ranges = 0;
|
|
115
|
+
for (let n = parent.firstChild; n; n = n.nextSibling) {
|
|
116
|
+
if (n.nodeType === 1)
|
|
117
|
+
level < 0 || visit(n, level + (n.localName === 'cl-island' ? 1 : 0));
|
|
118
|
+
else if (n.nodeType !== 8)
|
|
119
|
+
continue;
|
|
120
|
+
else if (n.data === '[s') {
|
|
121
|
+
ranges++;
|
|
122
|
+
level--;
|
|
123
|
+
}
|
|
124
|
+
else if (n.data === ']s') {
|
|
125
|
+
if (ranges) {
|
|
126
|
+
ranges--;
|
|
127
|
+
level++;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else if (!level && inside(ctx, n))
|
|
131
|
+
take(n);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
visit(root, 0);
|
|
68
135
|
return { holes, blocks, fors, regions };
|
|
69
136
|
}
|
|
70
137
|
/** Hole markers in `ctx`, by index. @internal */
|
|
@@ -164,7 +231,7 @@ export function awaited(fn) {
|
|
|
164
231
|
return { k: () => k.get(), v: () => v };
|
|
165
232
|
}
|
|
166
233
|
/** @internal Compiled components call this; apps don't. */
|
|
167
|
-
export function region(ctx, index, html, wire) {
|
|
234
|
+
export function region(ctx, index, html, wire, cx) {
|
|
168
235
|
const r = scan(ctx).regions[index];
|
|
169
236
|
if (!r)
|
|
170
237
|
return;
|
|
@@ -172,7 +239,7 @@ export function region(ctx, index, html, wire) {
|
|
|
172
239
|
let first = true;
|
|
173
240
|
onDispose(() => dispose?.());
|
|
174
241
|
effect(() => {
|
|
175
|
-
const next = html();
|
|
242
|
+
const next = html(contextAt(r.start, rootOf(ctx), cx));
|
|
176
243
|
untrack(() => {
|
|
177
244
|
if (!first) {
|
|
178
245
|
// A nested island with new props is updated in place (DOM and state kept), not replaced.
|
|
@@ -328,7 +395,7 @@ export function on(ctx, index, type, fn) {
|
|
|
328
395
|
// An event that doesn't bubble (mouseenter, focus) is its target's alone, not its ancestors'.
|
|
329
396
|
for (let el = e.target; el && el !== root.parentElement; el = e.bubbles ? el.parentElement : null) {
|
|
330
397
|
const i = el.getAttribute?.('data-cl-e');
|
|
331
|
-
if (i === null || i === undefined || el
|
|
398
|
+
if (i === null || i === undefined || owner(el) !== root)
|
|
332
399
|
continue;
|
|
333
400
|
for (const h of [...(own.get(Number(i)) ?? [])])
|
|
334
401
|
if (inside(h.ctx, el))
|
|
@@ -346,11 +413,28 @@ export function on(ctx, index, type, fn) {
|
|
|
346
413
|
onDispose(() => { const at = list.indexOf(handler); if (at !== -1)
|
|
347
414
|
list.splice(at, 1); }); // a removed branch or row stops listening
|
|
348
415
|
}
|
|
416
|
+
/**
|
|
417
|
+
* [use]="behaviour" (#464): call each behaviour (one, or a list) with the element marked data-cl-u="index", once; a
|
|
418
|
+
* function it returns runs when the branch, row or island goes.
|
|
419
|
+
* @internal Compiled components call this.
|
|
420
|
+
*/
|
|
421
|
+
export function use(ctx, index, get) {
|
|
422
|
+
const el = marked(ctx, `[data-cl-u="${index}"]`);
|
|
423
|
+
if (!el)
|
|
424
|
+
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
|
+
}
|
|
432
|
+
}
|
|
349
433
|
/** The element matching `selector` in `ctx`: this island's (not a nested island's), and in a row, that row's. */
|
|
350
434
|
export function marked(ctx, selector) {
|
|
351
435
|
const root = rootOf(ctx);
|
|
352
|
-
const island = root
|
|
353
|
-
return [...root.querySelectorAll(selector)].find((e) => e
|
|
436
|
+
const island = islandOf(root);
|
|
437
|
+
return [...root.querySelectorAll(selector)].find((e) => owner(e) === island && inside(ctx, e));
|
|
354
438
|
}
|
|
355
439
|
/**
|
|
356
440
|
* A reactive spread (#405): sets the attributes the object has, removes the ones it no longer has (only those it set:
|
|
@@ -426,8 +510,54 @@ export function modelInput(root, name, initial) {
|
|
|
426
510
|
},
|
|
427
511
|
};
|
|
428
512
|
}
|
|
429
|
-
/**
|
|
430
|
-
|
|
513
|
+
/**
|
|
514
|
+
* emit() in a component that declares @event (#442): a cancelable cl:event from the island to its parent's listener.
|
|
515
|
+
* False when the parent cancelled it ($info.cancel()).
|
|
516
|
+
* @internal Compiled components call this; apps don't.
|
|
517
|
+
*/
|
|
518
|
+
export function emit(root, name, value, info = {}) {
|
|
519
|
+
return root.dispatchEvent(new CustomEvent('cl:event', { detail: { name, value, ...info }, bubbles: true, cancelable: true }));
|
|
520
|
+
}
|
|
521
|
+
// The @event names each child island declares (recorded when it attaches): those don't fall through as DOM events.
|
|
522
|
+
const declared = new WeakMap();
|
|
523
|
+
/** A component's attach() records its @event names. @internal */
|
|
524
|
+
export function declare(root, names) {
|
|
525
|
+
declared.set(root, names);
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* (name)="…" on a component in region `index`. A child island's emit('name', …) arrives here and goes no further
|
|
529
|
+
* (#442); any other name is the DOM event, bubbling up from what the child renders (Vue's fallthrough), unless the
|
|
530
|
+
* child island declares it. @internal
|
|
531
|
+
*/
|
|
532
|
+
export function listen(ctx, index, name, handler, bound) {
|
|
533
|
+
// The bound component's island (by its c name): only its emit() is this listener's, and only its contract decides
|
|
534
|
+
// whether a DOM event is one it sends itself. A wrapper that declares nothing gets the DOM event.
|
|
535
|
+
const isBound = (el) => !bound || el.getAttribute('c') === bound;
|
|
536
|
+
fromChild(ctx, index, 'cl:event', name, (event, detail) => {
|
|
537
|
+
const { value, reason, event: cause } = detail;
|
|
538
|
+
handler(value, { reason, event: cause, cancel: () => event.preventDefault() });
|
|
539
|
+
}, (from) => isBound(from));
|
|
540
|
+
const r = scan(ctx).regions[index];
|
|
541
|
+
if (!r)
|
|
542
|
+
return;
|
|
543
|
+
const host = r.start.parentNode;
|
|
544
|
+
const dom = (event) => {
|
|
545
|
+
const from = event.target;
|
|
546
|
+
if (!(r.start.compareDocumentPosition(from) & 4) || !(r.end.compareDocumentPosition(from) & 2))
|
|
547
|
+
return;
|
|
548
|
+
// The bound island between the target and here: not attached yet, it replays the event once it is (its emit()
|
|
549
|
+
// decides then); declaring this name, it sends it as its own event.
|
|
550
|
+
for (let n = from; n && n !== host; n = n.parentNode) {
|
|
551
|
+
if (n.localName === 'cl-island' && isBound(n) && (!n.hasAttribute('data-cl-ready') || declared.get(n)?.includes(name)))
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
handler(event, { event, cancel: () => event.preventDefault() });
|
|
555
|
+
};
|
|
556
|
+
host.addEventListener(name, dom);
|
|
557
|
+
onDispose(() => host.removeEventListener(name, dom));
|
|
558
|
+
}
|
|
559
|
+
/** Listen for `type` from the child island bound in region `index` only (not one nested in it), named `name`. */
|
|
560
|
+
function fromChild(ctx, index, type, name, take, accept = () => true) {
|
|
431
561
|
const r = scan(ctx).regions[index];
|
|
432
562
|
if (!r)
|
|
433
563
|
return;
|
|
@@ -438,13 +568,19 @@ export function model(ctx, index, name, set) {
|
|
|
438
568
|
// Only from inside this region: 4 = the target follows the start marker, 2 = it precedes the end marker.
|
|
439
569
|
if (detail.name !== name || !(r.start.compareDocumentPosition(from) & 4) || !(r.end.compareDocumentPosition(from) & 2))
|
|
440
570
|
return;
|
|
441
|
-
// Only the child island this region binds: an island nested in it, which nobody bound, keeps its own
|
|
442
|
-
if (from
|
|
571
|
+
// Only the child island this region binds: an island nested in it, which nobody bound, keeps its own.
|
|
572
|
+
if (owner(from) !== rootOf(ctx) || !accept(from))
|
|
443
573
|
return;
|
|
444
|
-
event.preventDefault();
|
|
445
574
|
event.stopPropagation();
|
|
446
|
-
|
|
575
|
+
take(event, detail);
|
|
447
576
|
};
|
|
448
|
-
host.addEventListener(
|
|
449
|
-
onDispose(() => host.removeEventListener(
|
|
577
|
+
host.addEventListener(type, listen);
|
|
578
|
+
onDispose(() => host.removeEventListener(type, listen));
|
|
579
|
+
}
|
|
580
|
+
/** [(name)]="…" on a child island in region `index`: the child's writes to its @model name arrive here. @internal */
|
|
581
|
+
export function model(ctx, index, name, set) {
|
|
582
|
+
fromChild(ctx, index, 'cl:model', name, (event, detail) => {
|
|
583
|
+
event.preventDefault(); // taken: the child's value follows the parent's state
|
|
584
|
+
set(detail.value);
|
|
585
|
+
});
|
|
450
586
|
}
|
package/dist/runtime/html.d.ts
CHANGED
|
@@ -10,6 +10,12 @@ export declare function json(value: unknown): string;
|
|
|
10
10
|
* @internal
|
|
11
11
|
*/
|
|
12
12
|
export declare function spreadHtml(obj: unknown, skip: readonly string[], tag?: string, where?: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* The attributes a parent passed to a component (#463), on its root: each through attrHtml, except the names the root
|
|
15
|
+
* writes itself (`skip`) and style (merged on its own). No spread checks needed: passed() keeps only attribute-like names.
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
export declare function passedHtml(attrs: Record<string, unknown>, skip: readonly string[]): string;
|
|
13
19
|
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. @internal */
|
|
14
20
|
export declare function attrHtml(name: string, value: unknown, tag?: string): string;
|
|
15
21
|
/** An @if block inside an island: markers record which branch (k, or -1 for none) was rendered. @internal */
|
package/dist/runtime/html.js
CHANGED
|
@@ -34,6 +34,18 @@ export function spreadHtml(obj, skip, tag, where) {
|
|
|
34
34
|
}
|
|
35
35
|
return set.map(([name, v]) => (v === null ? '' : v === true ? ` ${name}` : ` ${name}="${escape(v)}"`)).join('');
|
|
36
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* The attributes a parent passed to a component (#463), on its root: each through attrHtml, except the names the root
|
|
39
|
+
* writes itself (`skip`) and style (merged on its own). No spread checks needed: passed() keeps only attribute-like names.
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
export function passedHtml(attrs, skip) {
|
|
43
|
+
let out = '';
|
|
44
|
+
for (const k in attrs)
|
|
45
|
+
if (k !== 'style' && !skip.includes(k.toLowerCase()))
|
|
46
|
+
out += attrHtml(k, attrs[k]);
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
37
49
|
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. @internal */
|
|
38
50
|
export function attrHtml(name, value, tag) {
|
|
39
51
|
const v = attrValue(name, value, tag);
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -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, 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, 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';
|
package/dist/runtime/index.js
CHANGED
|
@@ -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 } from './dom.js';
|
|
3
|
+
export { holes, blocks, text, on, attr, spread, block, awaited, forBlock, region, model, modelInput, emit, listen, declare, use, 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';
|
package/dist/runtime/loader.js
CHANGED
|
@@ -17,13 +17,28 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
17
17
|
}
|
|
18
18
|
return path;
|
|
19
19
|
};
|
|
20
|
+
import { owner } from './dom.js';
|
|
20
21
|
import { parse } from './serialize.js';
|
|
21
22
|
import { scope } from './signals.js';
|
|
22
23
|
// Islands scheduled or loading. `data-cl-ready` is set only after attach() succeeds.
|
|
23
24
|
const loading = new WeakSet();
|
|
25
|
+
// Islands attaching now (attach="load"), so an island holding them waits for them (#465): content it re-renders
|
|
26
|
+
// inside them reads what they provide.
|
|
27
|
+
const pending = new WeakMap();
|
|
28
|
+
/** Does `inner` hold content of `el` in one of its slot ranges? */
|
|
29
|
+
function holds(inner, el) {
|
|
30
|
+
const walker = document.createTreeWalker(inner, 128); // comments
|
|
31
|
+
for (let n; (n = walker.nextNode());)
|
|
32
|
+
if (n.data === '[s' && owner(n.nextSibling) === el)
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
24
35
|
const updates = new WeakMap();
|
|
25
36
|
// Each attached island's disposer: everything its attach() wired (effects, listeners) is released by it.
|
|
26
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]));
|
|
27
42
|
let lastLoad = (src) => import(__rewriteRelativeImportExtension(/* @vite-ignore */ src));
|
|
28
43
|
// Events recorded until an island attaches, then replayed. Clicks and submits are held back (no navigation, no
|
|
29
44
|
// form post) so the island can handle them once it is ready.
|
|
@@ -36,7 +51,7 @@ function record(el) {
|
|
|
36
51
|
const events = [];
|
|
37
52
|
const listener = (e) => {
|
|
38
53
|
const target = e.target;
|
|
39
|
-
if (!target || target
|
|
54
|
+
if (!target || owner(target) !== el)
|
|
40
55
|
return;
|
|
41
56
|
if (HOLD.has(e.type))
|
|
42
57
|
e.preventDefault();
|
|
@@ -84,7 +99,10 @@ function schedule(el, go) {
|
|
|
84
99
|
go();
|
|
85
100
|
}
|
|
86
101
|
});
|
|
87
|
-
|
|
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);
|
|
88
106
|
}
|
|
89
107
|
else if (mode === 'interaction') {
|
|
90
108
|
const start = () => { for (const type of INTERACTION)
|
|
@@ -105,14 +123,25 @@ async function attachOne(el, load, recorded) {
|
|
|
105
123
|
props = parse(el.getAttribute('p') ?? '{}');
|
|
106
124
|
const s = el.getAttribute('s');
|
|
107
125
|
state = s === null ? undefined : parse(s);
|
|
126
|
+
// Islands in it holding its content attach first (#465): content it re-renders there reads what they provide. One
|
|
127
|
+
// failing to load fails this one too (both retry). ponytail: later ones (idle, visible, interaction) aren't waited for.
|
|
128
|
+
for (const inner of el.querySelectorAll('cl-island')) {
|
|
129
|
+
if (!pending.has(inner) || !holds(inner, el))
|
|
130
|
+
continue;
|
|
131
|
+
await pending.get(inner);
|
|
132
|
+
if (!pending.has(inner))
|
|
133
|
+
throw 0;
|
|
134
|
+
}
|
|
108
135
|
}
|
|
109
136
|
catch (err) {
|
|
110
137
|
recorded.stop();
|
|
111
138
|
loading.delete(el); // network or parse failure: the next attachAll() retries
|
|
139
|
+
pending.delete(el);
|
|
112
140
|
console.error(`[clarusjs] island "${el.getAttribute('c')}" failed to load`, err);
|
|
113
141
|
return;
|
|
114
142
|
}
|
|
115
143
|
try {
|
|
144
|
+
rendered.set(el, rootAttrs(el));
|
|
116
145
|
attached.set(el, scope(() => {
|
|
117
146
|
// An island with inputs returns how to update them: kept for a parent island that passes new props.
|
|
118
147
|
const update = mod.attach(el, props, state);
|
|
@@ -151,8 +180,11 @@ export async function attachAll(root = document, load = lastLoad) {
|
|
|
151
180
|
for (const el of islands) {
|
|
152
181
|
loading.add(el);
|
|
153
182
|
const recorded = record(el);
|
|
154
|
-
if ((el.getAttribute('a') ?? 'load') === 'load')
|
|
155
|
-
|
|
183
|
+
if ((el.getAttribute('a') ?? 'load') === 'load') {
|
|
184
|
+
const p = attachOne(el, load, recorded);
|
|
185
|
+
pending.set(el, p);
|
|
186
|
+
now.push(p);
|
|
187
|
+
}
|
|
156
188
|
else
|
|
157
189
|
schedule(el, () => void attachOne(el, load, recorded));
|
|
158
190
|
}
|
|
@@ -195,8 +227,10 @@ function arrive() {
|
|
|
195
227
|
;
|
|
196
228
|
globalThis.__clUpdate = (el, next) => {
|
|
197
229
|
const update = updates.get(el);
|
|
198
|
-
// The same component: the same name and the same source (two panel.tscl files share a name).
|
|
199
|
-
|
|
230
|
+
// The same component: the same name and the same source (two panel.tscl files share a name). An attached island
|
|
231
|
+
// without inputs has nothing to update (#465: re-rendered because the content it holds changed, which its parent
|
|
232
|
+
// wires in place).
|
|
233
|
+
if (!attached.has(el) || el.getAttribute('c') !== next.getAttribute('c') || el.getAttribute('src') !== next.getAttribute('src'))
|
|
200
234
|
return false;
|
|
201
235
|
const p = next.getAttribute('p') ?? '{}';
|
|
202
236
|
el.setAttribute('p', p);
|
|
@@ -207,7 +241,19 @@ globalThis.__clUpdate = (el, next) => {
|
|
|
207
241
|
for (const { name, value } of [...next.attributes])
|
|
208
242
|
if (name.startsWith('data-cl-path-'))
|
|
209
243
|
el.setAttribute(name, value);
|
|
210
|
-
|
|
244
|
+
const root = el.firstElementChild;
|
|
245
|
+
const before = rendered.get(el);
|
|
246
|
+
const after = rootAttrs(next);
|
|
247
|
+
if (root && before) {
|
|
248
|
+
for (const [name, value] of after)
|
|
249
|
+
if (before.get(name) !== value)
|
|
250
|
+
root.setAttribute(name, value);
|
|
251
|
+
for (const name of before.keys())
|
|
252
|
+
if (!after.has(name))
|
|
253
|
+
root.removeAttribute(name);
|
|
254
|
+
}
|
|
255
|
+
rendered.set(el, after);
|
|
256
|
+
update?.(parse(p));
|
|
211
257
|
return true;
|
|
212
258
|
};
|
|
213
259
|
globalThis.__clAttach = (root, navigated) => {
|
package/dist/runtime/router.d.ts
CHANGED
|
@@ -3,7 +3,12 @@ type Entry = {
|
|
|
3
3
|
r: string;
|
|
4
4
|
};
|
|
5
5
|
type Page = {
|
|
6
|
-
render(props: object
|
|
6
|
+
render(props: object, slots?: undefined, opts?: {
|
|
7
|
+
route: {
|
|
8
|
+
path: string;
|
|
9
|
+
params: Record<string, string>;
|
|
10
|
+
};
|
|
11
|
+
}): string;
|
|
7
12
|
};
|
|
8
13
|
/** The route a path belongs to, with its decoded params. Same grammar as the server: whole [param] segments. */
|
|
9
14
|
export declare function matchRoute(table: Entry[], pathname: string): {
|
package/dist/runtime/router.js
CHANGED
|
@@ -64,7 +64,7 @@ isCurrent = () => true, load = (src) => import(__rewriteRelativeImportExtension(
|
|
|
64
64
|
return false;
|
|
65
65
|
// The head stays (its stylesheets are every client page's, linked by the shell); the title gets the site's
|
|
66
66
|
// titleTemplate, from the shell (always set: '%s' without one).
|
|
67
|
-
await swapIn(page.render(props), (title) => document.getElementById('cl-routes').dataset.title.replace('%s', () => title));
|
|
67
|
+
await swapIn(page.render(props, undefined, { route: { path: url.pathname, params: found.params } }), (title) => document.getElementById('cl-routes').dataset.title.replace('%s', () => title));
|
|
68
68
|
return true;
|
|
69
69
|
}
|
|
70
70
|
/**
|
package/dist/runtime/server.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { attrHtml } from './html.ts';
|
|
2
|
-
export { escape, json, blockHtml, forHtml, regionHtml, spreadHtml } from './html.ts';
|
|
3
|
-
export { classValue, styleValue } from './attrs.ts';
|
|
2
|
+
export { escape, json, blockHtml, forHtml, regionHtml, spreadHtml, passedHtml } from './html.ts';
|
|
3
|
+
export { classValue, styleValue, passed } from './attrs.ts';
|
|
4
4
|
/**
|
|
5
5
|
* Wrap a stateful component's HTML in its island boundary: name, client chunk URL, props (`p`) and, when the
|
|
6
6
|
* component has state, the state values it rendered with (`s`), so the browser starts from the same values.
|
|
7
7
|
* @internal Compiled components call this; apps don't.
|
|
8
8
|
*/
|
|
9
9
|
export declare function island(name: string, src: string, props: Record<string, unknown>, html: string, state?: Record<string, unknown>, attach?: string, paths?: Record<string, unknown>): string;
|
|
10
|
+
export declare function island(name: string, src: string, props: Record<string, unknown>, html: string | Promise<string>, state?: Record<string, unknown>, attach?: string, paths?: Record<string, unknown>): string | Promise<string>;
|
|
10
11
|
/**
|
|
11
12
|
* The parts of a template level that calls components or places slots (#430): joined into a string, or into a
|
|
12
13
|
* promise of one when a part is a promise (a component that awaits in its <script server>). @internal
|
|
@@ -17,6 +18,11 @@ export declare function join(parts: unknown[]): string | Promise<string>;
|
|
|
17
18
|
* with the error; without @catch, a rejection fails the render. @internal
|
|
18
19
|
*/
|
|
19
20
|
export declare function awaitHtml(promise: unknown, then: ((v: unknown) => string | Promise<string>) | null, caught: ((e: unknown) => string | Promise<string>) | null): Promise<string>;
|
|
21
|
+
/**
|
|
22
|
+
* A slot's HTML (#455): its fill, a function called with what the slot passes and the context at the slot (#465), or
|
|
23
|
+
* the fallback when nothing fills it. A string fill (hand-built slots) is used as is. @internal
|
|
24
|
+
*/
|
|
25
|
+
export declare function slot(fill: unknown, data: object | undefined, fallback: () => string | Promise<string>, cx?: object): string | Promise<string>;
|
|
20
26
|
/** A child component's HTML inside an island: a promise (it awaits) is an error naming both. @internal */
|
|
21
27
|
export declare function sync(html: string | Promise<string>, child: string, island: string): string;
|
|
22
28
|
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. @internal */
|