@serene-pub/sdk 0.6.0-pr-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +17 -0
- package/README.md +368 -0
- package/dist/actions.d.ts +572 -0
- package/dist/actions.js +829 -0
- package/dist/annexFields.d.ts +234 -0
- package/dist/annexFields.js +413 -0
- package/dist/announce.d.ts +464 -0
- package/dist/announce.js +426 -0
- package/dist/attributes.d.ts +670 -0
- package/dist/attributes.js +904 -0
- package/dist/bands.d.ts +125 -0
- package/dist/bands.js +256 -0
- package/dist/builder.d.ts +683 -0
- package/dist/builder.js +677 -0
- package/dist/candidates.d.ts +109 -0
- package/dist/candidates.js +118 -0
- package/dist/capabilities.d.ts +744 -0
- package/dist/capabilities.js +679 -0
- package/dist/channels.d.ts +106 -0
- package/dist/channels.js +129 -0
- package/dist/clauses.d.ts +40 -0
- package/dist/clauses.js +43 -0
- package/dist/component.d.ts +33 -0
- package/dist/component.js +27 -0
- package/dist/componentBuild.d.ts +48 -0
- package/dist/componentBuild.js +84 -0
- package/dist/componentClient.d.ts +100 -0
- package/dist/componentClient.js +13 -0
- package/dist/componentImports.d.ts +51 -0
- package/dist/componentImports.js +106 -0
- package/dist/componentModule.d.ts +8 -0
- package/dist/componentModule.js +270 -0
- package/dist/componentWire.d.ts +130 -0
- package/dist/componentWire.js +280 -0
- package/dist/config.d.ts +295 -0
- package/dist/config.js +231 -0
- package/dist/connections.d.ts +89 -0
- package/dist/connections.js +131 -0
- package/dist/declarations.d.ts +290 -0
- package/dist/declarations.js +794 -0
- package/dist/descriptors.d.ts +1774 -0
- package/dist/descriptors.js +1329 -0
- package/dist/dev.d.ts +136 -0
- package/dist/dev.js +161 -0
- package/dist/document.d.ts +202 -0
- package/dist/document.js +312 -0
- package/dist/engines.d.ts +117 -0
- package/dist/engines.js +203 -0
- package/dist/eventMapLaws.d.ts +63 -0
- package/dist/eventMapLaws.js +128 -0
- package/dist/events.d.ts +554 -0
- package/dist/events.js +633 -0
- package/dist/executor.d.ts +771 -0
- package/dist/executor.js +1829 -0
- package/dist/extension.d.ts +448 -0
- package/dist/extension.js +315 -0
- package/dist/facets.d.ts +53 -0
- package/dist/facets.js +60 -0
- package/dist/foldedSections.d.ts +74 -0
- package/dist/foldedSections.js +161 -0
- package/dist/genres.d.ts +507 -0
- package/dist/genres.js +633 -0
- package/dist/guardedConnection.d.ts +138 -0
- package/dist/guardedConnection.js +341 -0
- package/dist/handlebarsReach.d.ts +122 -0
- package/dist/handlebarsReach.js +222 -0
- package/dist/hash.d.ts +146 -0
- package/dist/hash.js +166 -0
- package/dist/hookGrants.d.ts +47 -0
- package/dist/hookGrants.js +68 -0
- package/dist/hooks.d.ts +290 -0
- package/dist/hooks.js +113 -0
- package/dist/hostElements.d.ts +447 -0
- package/dist/hostElements.js +702 -0
- package/dist/i18n.d.ts +94 -0
- package/dist/i18n.js +123 -0
- package/dist/identity.d.ts +93 -0
- package/dist/identity.js +135 -0
- package/dist/index.d.ts +85 -0
- package/dist/index.js +176 -0
- package/dist/layout.d.ts +194 -0
- package/dist/layout.js +40 -0
- package/dist/media.d.ts +202 -0
- package/dist/media.js +70 -0
- package/dist/messageBlocks.d.ts +487 -0
- package/dist/messageBlocks.js +683 -0
- package/dist/migration.d.ts +128 -0
- package/dist/migration.js +261 -0
- package/dist/names.d.ts +78 -0
- package/dist/names.js +136 -0
- package/dist/nodeInput.d.ts +399 -0
- package/dist/nodeInput.js +178 -0
- package/dist/participants.d.ts +291 -0
- package/dist/participants.js +252 -0
- package/dist/personGate.d.ts +62 -0
- package/dist/personGate.js +77 -0
- package/dist/pick.d.ts +84 -0
- package/dist/pick.js +103 -0
- package/dist/pluginHarness.d.ts +235 -0
- package/dist/pluginHarness.js +558 -0
- package/dist/pluginPermissions.d.ts +25 -0
- package/dist/pluginPermissions.js +97 -0
- package/dist/pluginRuleRef.d.ts +13 -0
- package/dist/pluginRuleRef.js +10 -0
- package/dist/predicates.d.ts +182 -0
- package/dist/predicates.js +249 -0
- package/dist/preview.d.ts +120 -0
- package/dist/preview.js +82 -0
- package/dist/promptBlocks.d.ts +130 -0
- package/dist/promptBlocks.js +197 -0
- package/dist/rankingDecisions.d.ts +60 -0
- package/dist/rankingDecisions.js +48 -0
- package/dist/receipt.d.ts +313 -0
- package/dist/receipt.js +60 -0
- package/dist/receiverRules.d.ts +24 -0
- package/dist/receiverRules.js +94 -0
- package/dist/refs.d.ts +117 -0
- package/dist/refs.js +137 -0
- package/dist/registry.d.ts +230 -0
- package/dist/registry.js +273 -0
- package/dist/review.d.ts +108 -0
- package/dist/review.js +97 -0
- package/dist/sampling.d.ts +152 -0
- package/dist/sampling.js +712 -0
- package/dist/scope.d.ts +65 -0
- package/dist/scope.js +128 -0
- package/dist/scripts.d.ts +243 -0
- package/dist/scripts.js +326 -0
- package/dist/sessionLayout.d.ts +339 -0
- package/dist/sessionLayout.js +503 -0
- package/dist/sessionSettings.d.ts +70 -0
- package/dist/sessionSettings.js +2 -0
- package/dist/settings.d.ts +501 -0
- package/dist/settings.js +604 -0
- package/dist/settingsSlot.d.ts +108 -0
- package/dist/settingsSlot.js +166 -0
- package/dist/shapes.d.ts +373 -0
- package/dist/shapes.js +408 -0
- package/dist/sheets.d.ts +165 -0
- package/dist/sheets.js +251 -0
- package/dist/statShapes.d.ts +96 -0
- package/dist/statShapes.js +112 -0
- package/dist/status.d.ts +103 -0
- package/dist/status.js +125 -0
- package/dist/storage.d.ts +145 -0
- package/dist/storage.js +33 -0
- package/dist/storyTime.d.ts +308 -0
- package/dist/storyTime.js +560 -0
- package/dist/surfaces.d.ts +501 -0
- package/dist/surfaces.js +292 -0
- package/dist/template.d.ts +312 -0
- package/dist/template.js +554 -0
- package/dist/templateCheck.d.ts +60 -0
- package/dist/templateCheck.js +516 -0
- package/dist/templateFit.d.ts +157 -0
- package/dist/templateFit.js +349 -0
- package/dist/templateIds.d.ts +138 -0
- package/dist/templateIds.js +144 -0
- package/dist/templateScopeAt.d.ts +131 -0
- package/dist/templateScopeAt.js +354 -0
- package/dist/testing.d.ts +177 -0
- package/dist/testing.js +383 -0
- package/dist/tokenizers.d.ts +113 -0
- package/dist/tokenizers.js +184 -0
- package/dist/turnLock.d.ts +96 -0
- package/dist/turnLock.js +79 -0
- package/dist/turnOrder.d.ts +143 -0
- package/dist/turnOrder.js +128 -0
- package/dist/validate.d.ts +74 -0
- package/dist/validate.js +1399 -0
- package/dist/values.d.ts +212 -0
- package/dist/values.js +390 -0
- package/dist/variables.d.ts +493 -0
- package/dist/variables.js +662 -0
- package/dist/verdicts.d.ts +275 -0
- package/dist/verdicts.js +348 -0
- package/dist/widgetDecls.d.ts +88 -0
- package/dist/widgetDecls.js +119 -0
- package/dist/widgets.d.ts +1527 -0
- package/dist/widgets.js +208 -0
- package/dist/wire.d.ts +126 -0
- package/dist/wire.js +140 -0
- package/dist/worldinfo.d.ts +150 -0
- package/dist/worldinfo.js +212 -0
- package/host-elements.d.ts +914 -0
- package/package.json +67 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 🚧 The guarded connection (C7): the ONE judge of the records a remote
|
|
3
|
+
* component's worker sends, before a Remote DOM receiver applies them. The
|
|
4
|
+
* page's receiver (the app's `receiverPolicy.ts`, inside `ComponentMount`)
|
|
5
|
+
* and the component harness (`@serene-pub/cli/testing`) both run it, so what
|
|
6
|
+
* an author's test shows is what the page shows, record for record.
|
|
7
|
+
*
|
|
8
|
+
* - an element outside the vocabulary (the rules' `element`, by default
|
|
9
|
+
* {@link receiverElementFinding}) never lands: its whole subtree is replaced
|
|
10
|
+
* by an empty comment under its id, so the remote's child INDICES stay the
|
|
11
|
+
* host's — Remote DOM addresses children by position, and a dropped node
|
|
12
|
+
* would shift every later insert and removal onto the wrong child. What the
|
|
13
|
+
* remote later writes INTO that subtree (a text change, an insert, a
|
|
14
|
+
* removal, an attribute) is dropped with it, never handed to a receiver
|
|
15
|
+
* that has no such node;
|
|
16
|
+
* - an attribute the element does not take, or a value the rules refuse (the
|
|
17
|
+
* rules' `attribute`, by default {@link receiverAttribute}), is dropped;
|
|
18
|
+
* - an update is judged by the node the receiver would write it to — its tag
|
|
19
|
+
* read off the attached node (`nodeOf`), never off an insert the receiver
|
|
20
|
+
* may have refused — and an insert reusing an attached id as another node
|
|
21
|
+
* is refused before anything of it is applied;
|
|
22
|
+
* - records are applied ONE AT A TIME, so each is judged against what the
|
|
23
|
+
* receiver accepted, never against what an earlier record only claimed;
|
|
24
|
+
* when a record is refused the connection stops — the rest of that batch
|
|
25
|
+
* is lost, and a box that no longer matches the remote would lie from then
|
|
26
|
+
* on, so no later batch is applied either;
|
|
27
|
+
* - PROPERTIES and METHOD calls are refused outright — a remote speaks in
|
|
28
|
+
* attributes and events only;
|
|
29
|
+
* - ids are prefixed when an `idPrefix` is given (`id`, `for`, `aria-*`
|
|
30
|
+
* references, a `#fragment` link, a control's `name`) — the page's, per
|
|
31
|
+
* box, so a remote can never name, label, jump to or join a group with an
|
|
32
|
+
* element of the page; the harness gives none and keeps the remote's own;
|
|
33
|
+
* - a link gets `rel="noopener noreferrer"` from the host, always;
|
|
34
|
+
* - an event listener is kept only for an event the element raises, as the
|
|
35
|
+
* host's own function (`fnFor`).
|
|
36
|
+
*
|
|
37
|
+
* The rest is the host's: the receiver itself, what a live control's value
|
|
38
|
+
* is, and what crosses back when a listener fires.
|
|
39
|
+
*
|
|
40
|
+
* Remote DOM's types only: the SDK carries no runtime import of the renderer
|
|
41
|
+
* (a component never bundles it, R23), so its protocol numbers are held here
|
|
42
|
+
* — the ones `@remote-dom/core` exports, which the SDK's tests pin.
|
|
43
|
+
*
|
|
44
|
+
* @experimental 🚧 provisional with the vocabulary it enforces (C7, R21).
|
|
45
|
+
*/
|
|
46
|
+
import { type FnHandle } from './componentWire.js';
|
|
47
|
+
import { type ReceiverAttribute } from './receiverRules.js';
|
|
48
|
+
/**
|
|
49
|
+
* One thing the guard dropped: the sentence saying what and why, and how
|
|
50
|
+
* much went with it. The page appends its own words to the sentence; the
|
|
51
|
+
* harness lists the sentence in `refused`.
|
|
52
|
+
* @experimental
|
|
53
|
+
*/
|
|
54
|
+
export interface ReceiverRefusal {
|
|
55
|
+
readonly finding: string;
|
|
56
|
+
/**
|
|
57
|
+
* `subtree`: an element and everything under it; `attribute`: one
|
|
58
|
+
* attribute's write; `property`: a property write (a remote speaks in
|
|
59
|
+
* attributes).
|
|
60
|
+
*/
|
|
61
|
+
readonly dropped: 'subtree' | 'attribute' | 'property';
|
|
62
|
+
}
|
|
63
|
+
/** The element and attribute rules a guarded connection applies — the SDK's unless a host names its own. @experimental */
|
|
64
|
+
export interface GuardedConnectionRules {
|
|
65
|
+
/** Why an element does not land (its whole subtree is dropped), or `undefined` when it does. */
|
|
66
|
+
element(tag: string, owner: string): string | undefined;
|
|
67
|
+
/** One attribute, judged: what to write, or why it is dropped. */
|
|
68
|
+
attribute(tag: string, attribute: string, value: unknown, owner: string): ReceiverAttribute;
|
|
69
|
+
}
|
|
70
|
+
/** @experimental */
|
|
71
|
+
export interface GuardedConnectionOptions {
|
|
72
|
+
/** Whose box this is — `core`, or a plugin's id (the rules' owner, {@link receiverAttribute}). */
|
|
73
|
+
owner: string;
|
|
74
|
+
/**
|
|
75
|
+
* Prefix for every id the remote writes — unique per box on the page.
|
|
76
|
+
* Omitted, ids are the remote's own (the harness).
|
|
77
|
+
*/
|
|
78
|
+
idPrefix?: string;
|
|
79
|
+
/** Turns a function handle into the host function the receiver attaches. */
|
|
80
|
+
fnFor: (handle: FnHandle, event: string) => (detail: unknown) => void;
|
|
81
|
+
/** Each thing dropped, as it is dropped. */
|
|
82
|
+
refuse: (refusal: ReceiverRefusal) => void;
|
|
83
|
+
/** Replaces the SDK's element or attribute rule. */
|
|
84
|
+
rules?: Partial<GuardedConnectionRules>;
|
|
85
|
+
}
|
|
86
|
+
/** A Remote DOM connection that judges every record before the receiver's applies it. */
|
|
87
|
+
/**
|
|
88
|
+
* A Remote DOM connection, by shape — held here so the SDK's published types
|
|
89
|
+
* need no Remote DOM installed; a real one is assignable to it.
|
|
90
|
+
*/
|
|
91
|
+
type RecordConnection = {
|
|
92
|
+
mutate(records: readonly unknown[]): void;
|
|
93
|
+
call(id: string, method: string, ...args: unknown[]): unknown;
|
|
94
|
+
};
|
|
95
|
+
/** @experimental */
|
|
96
|
+
export interface GuardedConnection extends RecordConnection {
|
|
97
|
+
/**
|
|
98
|
+
* How many remote ids the guard holds for refused subtrees — what the
|
|
99
|
+
* remote still has drawn under elements that never landed. It shrinks as
|
|
100
|
+
* the remote removes them, so it stays bounded by what is drawn.
|
|
101
|
+
*/
|
|
102
|
+
readonly refusedIdsHeld: number;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* The vocabulary as Remote DOM's element policy — which ELEMENTS, which
|
|
106
|
+
* events, no methods — for the receiver behind a guarded connection.
|
|
107
|
+
* Attributes are the guard's, not listed: a widget's `data-*` cannot be
|
|
108
|
+
* enumerated, and Remote DOM's policy takes names only — so the receiver
|
|
109
|
+
* keeps its own floor (no `on*`, no unsafe URL, no native method) and the
|
|
110
|
+
* guard decides the rest, properties included.
|
|
111
|
+
* @experimental
|
|
112
|
+
*/
|
|
113
|
+
export declare function receiverElementPolicy(): Record<string, {
|
|
114
|
+
events: Record<string, Record<string, never>>;
|
|
115
|
+
methods: string[];
|
|
116
|
+
}>;
|
|
117
|
+
/**
|
|
118
|
+
* The node a Remote DOM receiver holds a remote id as — its root for the
|
|
119
|
+
* root id — read off the receiver's own id → node map, the one it applies
|
|
120
|
+
* records by (and prunes as nodes leave). A guard judging by it can never
|
|
121
|
+
* believe something about a node the receiver would not write to. The map
|
|
122
|
+
* is private in Remote DOM's types, so it is held to its shape here, and a
|
|
123
|
+
* receiver without one is refused rather than guarded blind.
|
|
124
|
+
* @experimental
|
|
125
|
+
*/
|
|
126
|
+
export declare function receiverNodeOf(receiver: {
|
|
127
|
+
readonly root: DocumentFragment | Element;
|
|
128
|
+
}): (id: string) => Node | undefined;
|
|
129
|
+
/**
|
|
130
|
+
* A connection for a remote's records that enforces the vocabulary (see the
|
|
131
|
+
* module note) before `inner` — the receiver's own connection — applies
|
|
132
|
+
* them. `nodeOf` is the receiver's state: the host node a remote id is
|
|
133
|
+
* attached as, the box for the root ({@link receiverNodeOf}).
|
|
134
|
+
* @experimental
|
|
135
|
+
*/
|
|
136
|
+
export declare function guardedConnection(inner: RecordConnection, nodeOf: (id: string) => Node | undefined, options: GuardedConnectionOptions): GuardedConnection;
|
|
137
|
+
export {};
|
|
138
|
+
//# sourceMappingURL=guardedConnection.d.ts.map
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 🚧 The guarded connection (C7): the ONE judge of the records a remote
|
|
3
|
+
* component's worker sends, before a Remote DOM receiver applies them. The
|
|
4
|
+
* page's receiver (the app's `receiverPolicy.ts`, inside `ComponentMount`)
|
|
5
|
+
* and the component harness (`@serene-pub/cli/testing`) both run it, so what
|
|
6
|
+
* an author's test shows is what the page shows, record for record.
|
|
7
|
+
*
|
|
8
|
+
* - an element outside the vocabulary (the rules' `element`, by default
|
|
9
|
+
* {@link receiverElementFinding}) never lands: its whole subtree is replaced
|
|
10
|
+
* by an empty comment under its id, so the remote's child INDICES stay the
|
|
11
|
+
* host's — Remote DOM addresses children by position, and a dropped node
|
|
12
|
+
* would shift every later insert and removal onto the wrong child. What the
|
|
13
|
+
* remote later writes INTO that subtree (a text change, an insert, a
|
|
14
|
+
* removal, an attribute) is dropped with it, never handed to a receiver
|
|
15
|
+
* that has no such node;
|
|
16
|
+
* - an attribute the element does not take, or a value the rules refuse (the
|
|
17
|
+
* rules' `attribute`, by default {@link receiverAttribute}), is dropped;
|
|
18
|
+
* - an update is judged by the node the receiver would write it to — its tag
|
|
19
|
+
* read off the attached node (`nodeOf`), never off an insert the receiver
|
|
20
|
+
* may have refused — and an insert reusing an attached id as another node
|
|
21
|
+
* is refused before anything of it is applied;
|
|
22
|
+
* - records are applied ONE AT A TIME, so each is judged against what the
|
|
23
|
+
* receiver accepted, never against what an earlier record only claimed;
|
|
24
|
+
* when a record is refused the connection stops — the rest of that batch
|
|
25
|
+
* is lost, and a box that no longer matches the remote would lie from then
|
|
26
|
+
* on, so no later batch is applied either;
|
|
27
|
+
* - PROPERTIES and METHOD calls are refused outright — a remote speaks in
|
|
28
|
+
* attributes and events only;
|
|
29
|
+
* - ids are prefixed when an `idPrefix` is given (`id`, `for`, `aria-*`
|
|
30
|
+
* references, a `#fragment` link, a control's `name`) — the page's, per
|
|
31
|
+
* box, so a remote can never name, label, jump to or join a group with an
|
|
32
|
+
* element of the page; the harness gives none and keeps the remote's own;
|
|
33
|
+
* - a link gets `rel="noopener noreferrer"` from the host, always;
|
|
34
|
+
* - an event listener is kept only for an event the element raises, as the
|
|
35
|
+
* host's own function (`fnFor`).
|
|
36
|
+
*
|
|
37
|
+
* The rest is the host's: the receiver itself, what a live control's value
|
|
38
|
+
* is, and what crosses back when a listener fires.
|
|
39
|
+
*
|
|
40
|
+
* Remote DOM's types only: the SDK carries no runtime import of the renderer
|
|
41
|
+
* (a component never bundles it, R23), so its protocol numbers are held here
|
|
42
|
+
* — the ones `@remote-dom/core` exports, which the SDK's tests pin.
|
|
43
|
+
*
|
|
44
|
+
* @experimental 🚧 provisional with the vocabulary it enforces (C7, R21).
|
|
45
|
+
*/
|
|
46
|
+
import { isFnHandle } from './componentWire.js';
|
|
47
|
+
import { SP_HOST_ELEMENTS, hostEventAllowed } from './hostElements.js';
|
|
48
|
+
import { receiverAttribute, receiverElementFinding } from './receiverRules.js';
|
|
49
|
+
// Remote DOM's protocol (`@remote-dom/core`'s constants).
|
|
50
|
+
const ROOT_ID = '~';
|
|
51
|
+
const INSERT_CHILD = 0;
|
|
52
|
+
const REMOVE_CHILD = 1;
|
|
53
|
+
const UPDATE_TEXT = 2;
|
|
54
|
+
const UPDATE_PROPERTY = 3;
|
|
55
|
+
const KINDS = new Set([INSERT_CHILD, REMOVE_CHILD, UPDATE_TEXT, UPDATE_PROPERTY]);
|
|
56
|
+
const PROPERTY_ATTRIBUTE = 2;
|
|
57
|
+
const PROPERTY_EVENT_LISTENER = 3;
|
|
58
|
+
const ELEMENT = 1;
|
|
59
|
+
const COMMENT = 8;
|
|
60
|
+
/** Attributes whose value names an element by id. */
|
|
61
|
+
const ID_REFS = new Set([
|
|
62
|
+
'id',
|
|
63
|
+
'for',
|
|
64
|
+
'aria-labelledby',
|
|
65
|
+
'aria-describedby',
|
|
66
|
+
'aria-controls',
|
|
67
|
+
'aria-activedescendant',
|
|
68
|
+
'aria-owns',
|
|
69
|
+
'aria-errormessage',
|
|
70
|
+
'aria-details',
|
|
71
|
+
'aria-flowto',
|
|
72
|
+
]);
|
|
73
|
+
/** A value for an id-referencing attribute, every token prefixed. */
|
|
74
|
+
const prefixIds = (value, prefix) => value
|
|
75
|
+
.split(/\s+/)
|
|
76
|
+
.filter(Boolean)
|
|
77
|
+
.map((t) => `${prefix}${t}`)
|
|
78
|
+
.join(' ');
|
|
79
|
+
/** Every node of a serialized subtree, the root first. */
|
|
80
|
+
function* subtree(node) {
|
|
81
|
+
yield node;
|
|
82
|
+
for (const c of node.children ?? [])
|
|
83
|
+
yield* subtree(c);
|
|
84
|
+
}
|
|
85
|
+
/** Is this host node what the (clean) serialization describes — the same kind, the same element? */
|
|
86
|
+
const sameNode = (host, n) => host.nodeType === n.type && (n.type !== ELEMENT || host.localName === n.element);
|
|
87
|
+
/** A host node as a refusal names it. */
|
|
88
|
+
const described = (host) => host.nodeType === ELEMENT ? `<${host.localName}>` : host.nodeType === 3 ? 'text' : 'a comment';
|
|
89
|
+
/**
|
|
90
|
+
* The vocabulary as Remote DOM's element policy — which ELEMENTS, which
|
|
91
|
+
* events, no methods — for the receiver behind a guarded connection.
|
|
92
|
+
* Attributes are the guard's, not listed: a widget's `data-*` cannot be
|
|
93
|
+
* enumerated, and Remote DOM's policy takes names only — so the receiver
|
|
94
|
+
* keeps its own floor (no `on*`, no unsafe URL, no native method) and the
|
|
95
|
+
* guard decides the rest, properties included.
|
|
96
|
+
* @experimental
|
|
97
|
+
*/
|
|
98
|
+
export function receiverElementPolicy() {
|
|
99
|
+
const out = {};
|
|
100
|
+
for (const [tag, spec] of Object.entries(SP_HOST_ELEMENTS))
|
|
101
|
+
out[tag] = {
|
|
102
|
+
events: Object.fromEntries(spec.events.map((e) => [e, {}])),
|
|
103
|
+
methods: [],
|
|
104
|
+
};
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The node a Remote DOM receiver holds a remote id as — its root for the
|
|
109
|
+
* root id — read off the receiver's own id → node map, the one it applies
|
|
110
|
+
* records by (and prunes as nodes leave). A guard judging by it can never
|
|
111
|
+
* believe something about a node the receiver would not write to. The map
|
|
112
|
+
* is private in Remote DOM's types, so it is held to its shape here, and a
|
|
113
|
+
* receiver without one is refused rather than guarded blind.
|
|
114
|
+
* @experimental
|
|
115
|
+
*/
|
|
116
|
+
export function receiverNodeOf(receiver) {
|
|
117
|
+
const attached = receiver.attached;
|
|
118
|
+
if (!(attached instanceof Map))
|
|
119
|
+
throw new Error("Remote DOM's receiver no longer keeps its attached nodes — the guard cannot judge a remote's records");
|
|
120
|
+
const nodes = attached;
|
|
121
|
+
return (id) => (id === ROOT_ID ? receiver.root : nodes.get(id));
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* A connection for a remote's records that enforces the vocabulary (see the
|
|
125
|
+
* module note) before `inner` — the receiver's own connection — applies
|
|
126
|
+
* them. `nodeOf` is the receiver's state: the host node a remote id is
|
|
127
|
+
* attached as, the box for the root ({@link receiverNodeOf}).
|
|
128
|
+
* @experimental
|
|
129
|
+
*/
|
|
130
|
+
export function guardedConnection(inner, nodeOf, options) {
|
|
131
|
+
const { owner, idPrefix, fnFor, refuse } = options;
|
|
132
|
+
const elementRule = options.rules?.element ?? receiverElementFinding;
|
|
133
|
+
const attributeRule = options.rules?.attribute ?? receiverAttribute;
|
|
134
|
+
/** One attribute, judged; `undefined` means drop it, `null` means remove it. */
|
|
135
|
+
const attributeValue = (tag, name, value) => {
|
|
136
|
+
const judged = attributeRule(tag, name, value, owner);
|
|
137
|
+
if ('refused' in judged) {
|
|
138
|
+
refuse({ finding: judged.refused, dropped: 'attribute' });
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
const text = judged.value;
|
|
142
|
+
if (text === null || idPrefix === undefined)
|
|
143
|
+
return text;
|
|
144
|
+
// A control's `name` groups radios page-wide; an sp-icon's names an icon.
|
|
145
|
+
if (ID_REFS.has(name) || (name === 'name' && (tag === 'input' || tag === 'textarea')))
|
|
146
|
+
return prefixIds(text, idPrefix);
|
|
147
|
+
if (name === 'href' && text.startsWith('#'))
|
|
148
|
+
return `#${idPrefix}${text.slice(1)}`;
|
|
149
|
+
return text;
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* A serialized subtree made safe; a refused element becomes an inert
|
|
153
|
+
* stand-in — an empty comment under its id — and each refused element, as
|
|
154
|
+
* the remote sent it, is added to `refused`.
|
|
155
|
+
*/
|
|
156
|
+
const clean = (node, refused) => {
|
|
157
|
+
if (node.type !== ELEMENT)
|
|
158
|
+
return node; // text and comments carry data only
|
|
159
|
+
const tag = String(node.element ?? '').toLowerCase();
|
|
160
|
+
const why = elementRule(tag, owner);
|
|
161
|
+
if (why) {
|
|
162
|
+
refuse({ finding: why, dropped: 'subtree' });
|
|
163
|
+
refused.push(node);
|
|
164
|
+
return { id: node.id, type: COMMENT, data: '' };
|
|
165
|
+
}
|
|
166
|
+
const attributes = {};
|
|
167
|
+
for (const [name, value] of Object.entries(node.attributes ?? {})) {
|
|
168
|
+
const v = attributeValue(tag, name, value);
|
|
169
|
+
if (typeof v === 'string')
|
|
170
|
+
attributes[name] = v;
|
|
171
|
+
}
|
|
172
|
+
if (node.properties && Object.keys(node.properties).length)
|
|
173
|
+
refuse({ finding: `<${tag}> set properties (${Object.keys(node.properties).join(', ')})`, dropped: 'property' });
|
|
174
|
+
const eventListeners = {};
|
|
175
|
+
for (const [event, handle] of Object.entries(node.eventListeners ?? {}))
|
|
176
|
+
if (hostEventAllowed(tag, event) && isFnHandle(handle))
|
|
177
|
+
eventListeners[event] = fnFor(handle, event);
|
|
178
|
+
if (tag === 'a')
|
|
179
|
+
attributes.rel = 'noopener noreferrer';
|
|
180
|
+
const children = (node.children ?? []).map((child) => clean(child, refused));
|
|
181
|
+
return { id: node.id, type: ELEMENT, element: tag, attributes, eventListeners, children };
|
|
182
|
+
};
|
|
183
|
+
// A refused element landed as a stand-in and its subtree never did; the
|
|
184
|
+
// remote still draws there. Each stand-in's id → its subtree as the remote
|
|
185
|
+
// holds it: every parent's children, in the remote's order — so a removal
|
|
186
|
+
// inside it takes the removed node's ids with it. Every id in one → its
|
|
187
|
+
// stand-in's id; the stand-in's host comment → its id, so a removal that
|
|
188
|
+
// takes it is seen.
|
|
189
|
+
const refusedTrees = new Map();
|
|
190
|
+
const standInOf = new Map();
|
|
191
|
+
const standIns = new WeakMap();
|
|
192
|
+
let stopped = false;
|
|
193
|
+
/** Is this id a refused node — a stand-in, or anything under one — that is not attached? */
|
|
194
|
+
const refusedId = (id, host) => (host ? standIns.has(host) : standInOf.has(id));
|
|
195
|
+
/** The stand-in a refused id belongs to. */
|
|
196
|
+
const standInFor = (id, host) => (host ? standIns.get(host) : standInOf.get(id));
|
|
197
|
+
/** Hold a serialized subtree under its stand-in, its root placed among `at.parent`'s children. */
|
|
198
|
+
const hold = (standIn, node, at) => {
|
|
199
|
+
let tree = refusedTrees.get(standIn);
|
|
200
|
+
if (!tree)
|
|
201
|
+
refusedTrees.set(standIn, (tree = new Map()));
|
|
202
|
+
if (at) {
|
|
203
|
+
let siblings = tree.get(at.parent);
|
|
204
|
+
if (!siblings)
|
|
205
|
+
tree.set(at.parent, (siblings = []));
|
|
206
|
+
siblings.splice(at.index, 0, node.id);
|
|
207
|
+
}
|
|
208
|
+
for (const n of subtree(node)) {
|
|
209
|
+
standInOf.set(n.id, standIn);
|
|
210
|
+
if (n.type === ELEMENT)
|
|
211
|
+
tree.set(n.id, (n.children ?? []).map((c) => c.id));
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
/** Let go of one held id and everything under it. */
|
|
215
|
+
const release = (tree, id) => {
|
|
216
|
+
const children = tree.get(id);
|
|
217
|
+
tree.delete(id);
|
|
218
|
+
standInOf.delete(id);
|
|
219
|
+
for (const child of children ?? [])
|
|
220
|
+
release(tree, child);
|
|
221
|
+
};
|
|
222
|
+
/** A stand-in left the box: its whole subtree goes with it. */
|
|
223
|
+
const forget = (standIn) => {
|
|
224
|
+
const tree = refusedTrees.get(standIn);
|
|
225
|
+
if (tree)
|
|
226
|
+
release(tree, standIn);
|
|
227
|
+
refusedTrees.delete(standIn);
|
|
228
|
+
};
|
|
229
|
+
/** The stand-ins a removal takes with it: the node itself, or any under it. */
|
|
230
|
+
const standInsIn = (host, found = []) => {
|
|
231
|
+
if (!refusedTrees.size)
|
|
232
|
+
return found;
|
|
233
|
+
const id = standIns.get(host);
|
|
234
|
+
if (id !== undefined)
|
|
235
|
+
found.push(id);
|
|
236
|
+
for (const child of host.childNodes)
|
|
237
|
+
standInsIn(child, found);
|
|
238
|
+
return found;
|
|
239
|
+
};
|
|
240
|
+
const apply = (r) => inner.mutate([r]);
|
|
241
|
+
function step(r) {
|
|
242
|
+
// Closed world: the receiver picks its handler by plain key lookup, so a
|
|
243
|
+
// kind spelled `'3'` would reach `updateProperty` past every rule below.
|
|
244
|
+
// Only the four numeric kinds are records at all.
|
|
245
|
+
if (!Array.isArray(r) || !KINDS.has(r[0]))
|
|
246
|
+
throw new Error(`record kind ${JSON.stringify(Array.isArray(r) ? r[0] : r)} — not a record the box takes; refused`);
|
|
247
|
+
if (r[0] === INSERT_CHILD) {
|
|
248
|
+
const [, parentId, node, index] = r;
|
|
249
|
+
const parent = nodeOf(parentId);
|
|
250
|
+
// Into a refused subtree: nothing lands, and what the remote
|
|
251
|
+
// inserted there is held with it.
|
|
252
|
+
if (refusedId(parentId, parent)) {
|
|
253
|
+
hold(standInFor(parentId, parent), node, { parent: parentId, index });
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
const refusedHere = [];
|
|
257
|
+
const safe = clean(node, refusedHere);
|
|
258
|
+
// An id the box already holds as something else is a remote
|
|
259
|
+
// re-addressing a node: refused, before anything is applied.
|
|
260
|
+
for (const n of subtree(safe)) {
|
|
261
|
+
const held = n.id === ROOT_ID ? undefined : nodeOf(n.id);
|
|
262
|
+
if (held && !sameNode(held, n))
|
|
263
|
+
throw new Error(`'${n.id}' is already in the box as ${described(held)} — refused`);
|
|
264
|
+
}
|
|
265
|
+
apply([r[0], parentId, safe, index]);
|
|
266
|
+
for (const refused of refusedHere) {
|
|
267
|
+
const host = nodeOf(refused.id);
|
|
268
|
+
if (host)
|
|
269
|
+
standIns.set(host, refused.id);
|
|
270
|
+
hold(refused.id, refused);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
else if (r[0] === REMOVE_CHILD) {
|
|
274
|
+
const [, parentId, index] = r;
|
|
275
|
+
const parent = nodeOf(parentId);
|
|
276
|
+
// Inside a refused subtree: nothing to remove from the box, but
|
|
277
|
+
// what the remote removed there is let go.
|
|
278
|
+
if (refusedId(parentId, parent)) {
|
|
279
|
+
const tree = refusedTrees.get(standInFor(parentId, parent));
|
|
280
|
+
const [gone] = tree?.get(parentId)?.splice(index, 1) ?? [];
|
|
281
|
+
if (tree && gone !== undefined)
|
|
282
|
+
release(tree, gone);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const child = parent?.childNodes[index];
|
|
286
|
+
const gone = child ? standInsIn(child) : [];
|
|
287
|
+
apply(r);
|
|
288
|
+
for (const id of gone)
|
|
289
|
+
forget(id);
|
|
290
|
+
}
|
|
291
|
+
else if (r[0] === UPDATE_TEXT) {
|
|
292
|
+
const id = r[1];
|
|
293
|
+
if (refusedId(id, nodeOf(id)))
|
|
294
|
+
return;
|
|
295
|
+
apply(r);
|
|
296
|
+
}
|
|
297
|
+
else if (r[0] === UPDATE_PROPERTY) {
|
|
298
|
+
const [, id, name, value, type] = r;
|
|
299
|
+
// The tag the receiver would write to. None: a node of a subtree
|
|
300
|
+
// the vocabulary dropped, or one the receiver never took.
|
|
301
|
+
const host = id === ROOT_ID ? undefined : nodeOf(id);
|
|
302
|
+
if (host?.nodeType !== ELEMENT)
|
|
303
|
+
return;
|
|
304
|
+
const tag = host.localName;
|
|
305
|
+
if (type === PROPERTY_ATTRIBUTE) {
|
|
306
|
+
const v = attributeValue(tag, name, value);
|
|
307
|
+
if (v !== undefined)
|
|
308
|
+
apply([r[0], id, name, v, type]);
|
|
309
|
+
}
|
|
310
|
+
else if (type === PROPERTY_EVENT_LISTENER) {
|
|
311
|
+
if (!hostEventAllowed(tag, name))
|
|
312
|
+
return;
|
|
313
|
+
apply([r[0], id, name, isFnHandle(value) ? fnFor(value, name) : null, type]);
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
refuse({ finding: `<${tag}> set property '${name}'`, dropped: 'property' });
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return {
|
|
321
|
+
call() {
|
|
322
|
+
throw new Error('a remote calls no host methods');
|
|
323
|
+
},
|
|
324
|
+
mutate(records) {
|
|
325
|
+
if (stopped)
|
|
326
|
+
return;
|
|
327
|
+
try {
|
|
328
|
+
for (const r of records)
|
|
329
|
+
step(r);
|
|
330
|
+
}
|
|
331
|
+
catch (err) {
|
|
332
|
+
stopped = true;
|
|
333
|
+
throw err;
|
|
334
|
+
}
|
|
335
|
+
},
|
|
336
|
+
get refusedIdsHeld() {
|
|
337
|
+
return standInOf.size;
|
|
338
|
+
},
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
//# sourceMappingURL=guardedConnection.js.map
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a Handlebars reference can reach, at one point in a template.
|
|
3
|
+
*
|
|
4
|
+
* Typed templates P4. The template checker (`@serene-pub/sdk/template-check`)
|
|
5
|
+
* and a host's editor assist both need the same three answers — what `../`
|
|
6
|
+
* climbs to, what a block param is bound to, and what one `{{#each}}` element
|
|
7
|
+
* is — and when they were two copies they drifted: the assist treated an
|
|
8
|
+
* `{{#if}}` as a new context, so a root name inside one was "unknowable". This
|
|
9
|
+
* module is the one reading; it has no dependencies, so an editor that only
|
|
10
|
+
* scans tags (it sees half-typed source, which no parser accepts) can use it
|
|
11
|
+
* without the parser the checker carries.
|
|
12
|
+
*
|
|
13
|
+
* **Conservative by construction**: whatever the schema cannot speak to — an
|
|
14
|
+
* `'any'` root, a record's author-chosen key, a subexpression's result —
|
|
15
|
+
* resolves as *unchecked*, never as wrong.
|
|
16
|
+
*/
|
|
17
|
+
import type { PathResolution, TemplateScope, VarDecl, VarField } from './template.js';
|
|
18
|
+
/**
|
|
19
|
+
* One Handlebars context and the names visible in it.
|
|
20
|
+
*
|
|
21
|
+
* A reach with no `context` and no `parent` is the root, where a name resolves
|
|
22
|
+
* against the declared scope. Not a plugin frame (the sandboxed iframe a
|
|
23
|
+
* widget renders in) — the word is `reach` precisely so the two never meet.
|
|
24
|
+
* @experimental
|
|
25
|
+
*/
|
|
26
|
+
export interface HandlebarsReach {
|
|
27
|
+
/** What `this` is here, when the schema says. */
|
|
28
|
+
context?: VarField;
|
|
29
|
+
/** The context is real but unknowable — check nothing against it. */
|
|
30
|
+
unchecked: boolean;
|
|
31
|
+
/** `as |a b|` bindings visible here: the value's type, or undefined (an index, a key). */
|
|
32
|
+
params: ReadonlyMap<string, VarField | undefined>;
|
|
33
|
+
/** The context `../` climbs to. Only `{{#each}}`/`{{#with}}` add one. */
|
|
34
|
+
parent?: HandlebarsReach;
|
|
35
|
+
}
|
|
36
|
+
/** The root: names resolve against the scope. @experimental */
|
|
37
|
+
export declare const rootHandlebarsReach: () => HandlebarsReach;
|
|
38
|
+
/**
|
|
39
|
+
* A path as Handlebars reads it: `../this.[extra lore].note` climbs one
|
|
40
|
+
* context, is scoped (no helper, no block param), and walks
|
|
41
|
+
* `['extra lore', 'note']`.
|
|
42
|
+
* @experimental
|
|
43
|
+
*/
|
|
44
|
+
export interface HandlebarsPath {
|
|
45
|
+
/** How many `../` it climbs. */
|
|
46
|
+
depth: number;
|
|
47
|
+
/** Written `this.`/`./` — never a block param. */
|
|
48
|
+
scoped: boolean;
|
|
49
|
+
/** `@index`, `@root.x` — Handlebars' own frame data, never the scope's. */
|
|
50
|
+
data: boolean;
|
|
51
|
+
/** The segments after the climb, brackets unwrapped. Empty = `this`. */
|
|
52
|
+
parts: string[];
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Read a written path the way Handlebars' parser does, for a caller that only
|
|
56
|
+
* has the text (an editor scanning tags). The checker reads the parser's own
|
|
57
|
+
* AST instead and builds the same shape from it.
|
|
58
|
+
* @experimental
|
|
59
|
+
*/
|
|
60
|
+
export declare function handlebarsPath(expr: string): HandlebarsPath;
|
|
61
|
+
/**
|
|
62
|
+
* What a path resolved to.
|
|
63
|
+
*
|
|
64
|
+
* - `unchecked` — nothing can be said (off the top of the climb, frame data,
|
|
65
|
+
* an unknowable context). Never a finding.
|
|
66
|
+
* - `unknown-root` — a root name the scope does not have. `available` is what
|
|
67
|
+
* it does have.
|
|
68
|
+
* - `resolved` — `resolution` is the walk of `rest` from `base`; `ok: false`
|
|
69
|
+
* is a path the schema positively contradicts. `label` is the name the
|
|
70
|
+
* walk's messages start from (`c`, `characters`, `this`).
|
|
71
|
+
* @experimental
|
|
72
|
+
*/
|
|
73
|
+
export type HandlebarsPathResolution = {
|
|
74
|
+
kind: 'unchecked';
|
|
75
|
+
} | {
|
|
76
|
+
kind: 'unknown-root';
|
|
77
|
+
root: string;
|
|
78
|
+
available: string[];
|
|
79
|
+
} | {
|
|
80
|
+
kind: 'resolved';
|
|
81
|
+
base: VarDecl | undefined;
|
|
82
|
+
label: string;
|
|
83
|
+
rest: string[];
|
|
84
|
+
resolution: PathResolution;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Resolve a path within a reach — Handlebars' own order: `../` climbs first; a
|
|
88
|
+
* block param only answers an unscoped, unclimbed name; the root answers from
|
|
89
|
+
* the scope; any other context answers from its type.
|
|
90
|
+
* @experimental
|
|
91
|
+
*/
|
|
92
|
+
export declare function resolveHandlebarsPath(path: HandlebarsPath, reach: HandlebarsReach, scope: TemplateScope): HandlebarsPathResolution;
|
|
93
|
+
/** What a path's value is, as a declaration — undefined when unknowable. @experimental */
|
|
94
|
+
export declare function typeOfHandlebarsPath(path: HandlebarsPath, reach: HandlebarsReach, scope: TemplateScope): VarDecl | undefined;
|
|
95
|
+
/** A declaration that is a walkable field — not `'any'`, not the legacy name list. @experimental */
|
|
96
|
+
export declare function asVarField(decl: VarDecl | undefined): VarField | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* The reach a block's body sees.
|
|
99
|
+
*
|
|
100
|
+
* Only `each` and `with` move the context, and only they give `../` a step to
|
|
101
|
+
* climb; `if`, `unless` and every other block helper keep the reach they were
|
|
102
|
+
* opened in (plus any `as |x|` names, unchecked). `each` binds its first param
|
|
103
|
+
* to one element and the second to the index or key; `with` binds it to the
|
|
104
|
+
* value itself.
|
|
105
|
+
*
|
|
106
|
+
* An `{{else}}` branch of `each`/`with` runs in the reach the block was opened
|
|
107
|
+
* in — the caller keeps that one, it is not this.
|
|
108
|
+
* @experimental
|
|
109
|
+
*/
|
|
110
|
+
export declare function enterHandlebarsBlock(reach: HandlebarsReach, helper: string, target: VarDecl | undefined, blockParams?: readonly string[]): HandlebarsReach;
|
|
111
|
+
/**
|
|
112
|
+
* The nearest name, when there is an obviously-nearest one — the "did you
|
|
113
|
+
* mean" half of a finding.
|
|
114
|
+
*
|
|
115
|
+
* Deliberately tight: within one edit for a name of four characters or fewer,
|
|
116
|
+
* two otherwise. A wrong guess sends someone to change a line that was
|
|
117
|
+
* correct, which costs more than saying nothing. Ties resolve alphabetically,
|
|
118
|
+
* so the answer is stable.
|
|
119
|
+
* @experimental
|
|
120
|
+
*/
|
|
121
|
+
export declare function nearestName(typed: string, available: readonly string[]): string | undefined;
|
|
122
|
+
//# sourceMappingURL=handlebarsReach.d.ts.map
|