@openelement/protocol 0.41.0-beta.2 → 1.0.0-alpha.11

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 (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +42 -0
  3. package/package.json +90 -51
  4. package/src/app-model.d.ts +19 -0
  5. package/src/app-model.js +1 -0
  6. package/src/client-assets.d.ts +44 -0
  7. package/src/client-assets.js +24 -0
  8. package/src/data.d.ts +81 -11
  9. package/src/data.js +11 -7
  10. package/src/errors.d.ts +253 -14
  11. package/src/errors.js +275 -15
  12. package/src/forbidden-sinks.d.ts +60 -0
  13. package/src/forbidden-sinks.js +81 -0
  14. package/src/framework.d.ts +183 -99
  15. package/src/framework.js +13 -5
  16. package/src/index.d.ts +21 -19
  17. package/src/index.js +21 -20
  18. package/src/island-admission.d.ts +5 -0
  19. package/src/island-admission.js +9 -0
  20. package/src/island.d.ts +32 -19
  21. package/src/island.js +1 -4
  22. package/src/manifest.d.ts +15 -27
  23. package/src/manifest.js +1 -4
  24. package/src/module-descriptors.d.ts +40 -0
  25. package/src/module-descriptors.js +1 -0
  26. package/src/module-vocabulary.d.ts +5 -0
  27. package/src/module-vocabulary.js +21 -0
  28. package/src/part-program.d.ts +407 -0
  29. package/src/part-program.js +758 -0
  30. package/src/policy.d.ts +64 -0
  31. package/src/policy.js +65 -0
  32. package/src/registry-markers.d.ts +39 -0
  33. package/src/registry-markers.js +40 -0
  34. package/src/render.d.ts +29 -123
  35. package/src/render.js +1 -7
  36. package/src/runtime.d.ts +13 -4
  37. package/src/runtime.js +1 -8
  38. package/src/signal.d.ts +31 -7
  39. package/src/signal.js +5 -6
  40. package/src/ssg.d.ts +317 -151
  41. package/src/ssg.js +1 -7
  42. package/src/stream-frame-policy.d.ts +49 -0
  43. package/src/stream-frame-policy.js +108 -0
  44. package/src/style-sheet.d.ts +5 -1
  45. package/src/style-sheet.js +1 -8
  46. package/src/void-tags.d.ts +18 -0
  47. package/src/void-tags.js +34 -0
  48. package/src/context.d.ts +0 -25
  49. package/src/context.js +0 -7
  50. package/src/hydration-markers.d.ts +0 -25
  51. package/src/hydration-markers.js +0 -26
  52. package/src/isr.d.ts +0 -30
  53. package/src/isr.js +0 -4
  54. package/src/prop.d.ts +0 -46
  55. package/src/prop.js +0 -6
  56. package/src/vnode.d.ts +0 -19
  57. package/src/vnode.js +0 -9
@@ -0,0 +1,758 @@
1
+ import { ProgramErrorCode, raiseFrameworkError } from "./errors.js";
2
+ import { forbiddenSinkReason, isSafeAttributeName } from "./forbidden-sinks.js";
3
+ import { VOID_TAGS } from "./void-tags.js";
4
+ //#region src/part-program.ts
5
+ /**
6
+ * @openelement/element — serializable Part Program v1 protocol.
7
+ *
8
+ * The canonical exchange artifact shared by the compiler semantic core, the
9
+ * server serializer, fresh DOM creation, and existing-DOM claim. Its only
10
+ * imports are the import-free, host-free canonical owners — VOID_TAGS,
11
+ * forbidden sinks, and the error contract (#1386 item 3); all three are
12
+ * import-free, so the artifact keeps its freedom from runtime/host edges. The
13
+ * generated JSON is the seam, and both the runtime and the compiler import
14
+ * this one module instead of keeping mirrored copies. Every dynamic location
15
+ * receives a compiler-owned identity. Runtime code does not discover bindings
16
+ * by walking a VNode or a generic DOM tree.
17
+ */
18
+ /** The Part Program IR version this package defines (currently v1). */
19
+ const PART_PROGRAM_VERSION = 1;
20
+ /**
21
+ * Version of the compiled-module ABI: the statics a generated module carries
22
+ * (`__partProgram`, `__compiledProperties`, `__elementMetadata`,
23
+ * `__observedAttributes`, `__computedFields`) and the runtime facade binds.
24
+ * Bump when the generated-module contract changes shape; generated artifacts
25
+ * stamp both this and PART_PROGRAM_VERSION so consumers can tell format drift
26
+ * from release drift.
27
+ */
28
+ const COMPILED_MODULE_ABI_VERSION = 1;
29
+ /**
30
+ * Operator/literal compatibility for when-Region tests (issue #1372). Both
31
+ * wire validators (this module and the server serializer's shared validator)
32
+ * close the space through this one predicate:
33
+ * - ordering operators require a finite numeric literal;
34
+ * - `equals`/`not-equals` admit a finite number, string, or boolean;
35
+ * - `truthy` admits exactly `true` or `false` (the recorded expectation).
36
+ */
37
+ function conditionLiteralAllowed(op, value) {
38
+ switch (op) {
39
+ case "greater-than":
40
+ case "greater-or-equal":
41
+ case "less-than":
42
+ case "less-or-equal": return typeof value === "number" && Number.isFinite(value);
43
+ case "equals":
44
+ case "not-equals":
45
+ if (typeof value === "number") return Number.isFinite(value);
46
+ return typeof value === "string" || typeof value === "boolean";
47
+ case "truthy": return value === true || value === false;
48
+ default: return false;
49
+ }
50
+ }
51
+ /** Anchor marker payloads shared by seed runtime consumers. */
52
+ function partAnchorMarker(index) {
53
+ return `oe:p${index}`;
54
+ }
55
+ /** The comment marker closing a part anchor in serialized program markup. */
56
+ function partAnchorEndMarker(index) {
57
+ return `oe:/p${index}`;
58
+ }
59
+ /**
60
+ * The one `<style>` element the server serializer emits as the first DSD
61
+ * template child when the class carries static styles (renderDsd
62
+ * parity — pages never upgrade, so their styles must ship in the payload).
63
+ * The claim path skips exactly this marked node; a marked node on a
64
+ * style-less class is claim drift and fails closed.
65
+ */
66
+ const STATIC_STYLES_MARKER = "data-oe-static-styles";
67
+ /**
68
+ * Internal SSR provenance marker on light-mode host tags (#1148).
69
+ * Present only when the host's light subtree was server-rendered under the
70
+ * in-place activation contract: the client binds the existing DOM instead of
71
+ * clearing it, and a parent's activation walk prunes the nested host's
72
+ * subtree. Client rendering never writes it and it is never removed. The
73
+ * writer (internal/compiled/server) and readers (internal/compiled/runtime)
74
+ * import this constant; nothing writes the literal.
75
+ */
76
+ const DATA_OE_LIGHT = "data-oe-light";
77
+ /**
78
+ * Reject one malformed Part Program record (#1386 item 3). Every validation
79
+ * site in this module funnels through here, so the whole wire boundary raises
80
+ * one catchable error type with one stable code.
81
+ */
82
+ function fail(reason) {
83
+ raiseFrameworkError("validation", ProgramErrorCode.INVALID_PROGRAM, `[compiled-program] invalid Part Program v1: ${reason}`);
84
+ }
85
+ function isRecord(value) {
86
+ return typeof value === "object" && value !== null && !Array.isArray(value);
87
+ }
88
+ function validateKeys(value, allowed, where) {
89
+ const keys = new Set(allowed);
90
+ for (const key of Object.keys(value)) if (!keys.has(key)) fail(`${where}.${key} is not part of Part Program v1`);
91
+ }
92
+ function isIntegerArray(value) {
93
+ return Array.isArray(value) && value.every((entry) => Number.isInteger(entry) && entry >= 0);
94
+ }
95
+ function isIdentifier(value) {
96
+ return typeof value === "string" && /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(value);
97
+ }
98
+ /**
99
+ * The wire-validator face of the one canonical attribute-name predicate
100
+ * (delegates to `./forbidden-sinks.ts`); exported for parity tests.
101
+ */
102
+ function isAttributeName(value) {
103
+ return typeof value === "string" && isSafeAttributeName(value);
104
+ }
105
+ function samePath(left, right) {
106
+ return isIntegerArray(left) && JSON.stringify(left) === JSON.stringify(right);
107
+ }
108
+ function isSerializable(value, seen = /* @__PURE__ */ new Set()) {
109
+ if (value === null || typeof value === "string" || typeof value === "boolean") return true;
110
+ if (typeof value === "number") return Number.isFinite(value);
111
+ if (typeof value !== "object") return false;
112
+ if (seen.has(value)) return false;
113
+ seen.add(value);
114
+ if (Array.isArray(value)) return value.every((entry) => isSerializable(entry, seen));
115
+ const prototype = Object.getPrototypeOf(value);
116
+ if (prototype !== Object.prototype && prototype !== null) return false;
117
+ return Object.values(value).every((entry) => isSerializable(entry, seen));
118
+ }
119
+ function validatePosition(value, where) {
120
+ if (!isRecord(value)) fail(`${where} must be a position object`);
121
+ validateKeys(value, [
122
+ "offset",
123
+ "line",
124
+ "column"
125
+ ], where);
126
+ const offset = value.offset;
127
+ const line = value.line;
128
+ const column = value.column;
129
+ if (typeof offset !== "number" || !Number.isInteger(offset) || offset < 0) fail(`${where}.offset must be non-negative`);
130
+ if (typeof line !== "number" || !Number.isInteger(line) || line < 1) fail(`${where}.line must be positive`);
131
+ if (typeof column !== "number" || !Number.isInteger(column) || column < 1) fail(`${where}.column must be positive`);
132
+ }
133
+ function validateSourceRange(value, where) {
134
+ if (!isRecord(value) || typeof value.file !== "string" || value.file.length === 0) fail(`${where} must contain a source file`);
135
+ validateKeys(value, [
136
+ "file",
137
+ "start",
138
+ "end"
139
+ ], where);
140
+ validatePosition(value.start, `${where}.start`);
141
+ validatePosition(value.end, `${where}.end`);
142
+ const start = value.start;
143
+ if (value.end.offset < start.offset) fail(`${where} end precedes start`);
144
+ }
145
+ function validateTreeNodes(nodes, where, allowAnchors, allowItemValues, elementIds, anchorIds, elementLocations, pathPrefix = [], rootPath = void 0) {
146
+ if (!Array.isArray(nodes)) fail(`${where} must be an array`);
147
+ for (const [position, rawNode] of nodes.entries()) {
148
+ if (!isRecord(rawNode)) fail(`${where}[${position}] must be an object`);
149
+ switch (rawNode.k) {
150
+ case "el": {
151
+ validateKeys(rawNode, [
152
+ "k",
153
+ "id",
154
+ "tag",
155
+ "attrs",
156
+ "iattrs",
157
+ "children"
158
+ ], `${where}[${position}]`);
159
+ if (typeof rawNode.id !== "string" || !/^e\d+$/.test(rawNode.id)) fail(`${where}[${position}] element needs a stable eN id`);
160
+ if (elementIds.has(rawNode.id)) fail(`${where}[${position}] duplicates element ${rawNode.id}`);
161
+ elementIds.add(rawNode.id);
162
+ if (typeof rawNode.tag !== "string" || !/^[a-z][a-z0-9]*(-[a-z0-9]+)*$/.test(rawNode.tag)) fail(`${where}[${position}].tag must be a lowercase element tag`);
163
+ const tagReason = forbiddenSinkReason("tag", rawNode.tag);
164
+ if (tagReason !== null) fail(`${where}[${position}].tag ${tagReason}`);
165
+ elementLocations.set(rawNode.id, {
166
+ tag: rawNode.tag,
167
+ path: position === 0 && rootPath !== void 0 ? [...rootPath] : [...pathPrefix, position]
168
+ });
169
+ if (!Array.isArray(rawNode.attrs)) fail(`${where}[${position}].attrs must be an array`);
170
+ const attributeNames = /* @__PURE__ */ new Set();
171
+ for (const [attrPosition, attr] of rawNode.attrs.entries()) {
172
+ if (!Array.isArray(attr) || attr.length !== 2 || typeof attr[0] !== "string" || typeof attr[1] !== "string") fail(`${where}[${position}].attrs[${attrPosition}] must be a [name, value] pair`);
173
+ if (!isAttributeName(attr[0])) fail(`${where}[${position}].attrs[${attrPosition}] has an unsafe name`);
174
+ if (attributeNames.has(attr[0].toLowerCase())) fail(`${where}[${position}].attrs[${attrPosition}] duplicates attribute ${attr[0]}`);
175
+ attributeNames.add(attr[0].toLowerCase());
176
+ }
177
+ if (rawNode.iattrs !== void 0) {
178
+ if (!allowItemValues) fail(`${where}[${position}].iattrs item attribute slots need an each item template`);
179
+ if (!Array.isArray(rawNode.iattrs)) fail(`${where}[${position}].iattrs must be an array`);
180
+ for (const [slotPosition, slot] of rawNode.iattrs.entries()) {
181
+ if (!Array.isArray(slot) || slot.length !== 2 || typeof slot[0] !== "string" || typeof slot[1] !== "string") fail(`${where}[${position}].iattrs[${slotPosition}] must be a [name, field] pair`);
182
+ if (!isAttributeName(slot[0])) fail(`${where}[${position}].iattrs[${slotPosition}] has an unsafe name`);
183
+ if (slot[0].toLowerCase() === "key") fail(`${where}[${position}].iattrs[${slotPosition}] may not bind the item key`);
184
+ if (!isIdentifier(slot[1])) fail(`${where}[${position}].iattrs[${slotPosition}] field must be an identifier`);
185
+ if (attributeNames.has(slot[0].toLowerCase())) fail(`${where}[${position}].iattrs[${slotPosition}] duplicates static attribute ${slot[0]}`);
186
+ attributeNames.add(slot[0].toLowerCase());
187
+ }
188
+ }
189
+ if (!Array.isArray(rawNode.children)) fail(`${where}[${position}].children must be an array`);
190
+ if (VOID_TAGS.has(rawNode.tag) && rawNode.children.length > 0) fail(`${where}[${position}] void elements may not have children`);
191
+ validateTreeNodes(rawNode.children, `${where}[${position}].children`, allowAnchors, allowItemValues, elementIds, anchorIds, elementLocations, position === 0 && rootPath !== void 0 ? [...rootPath] : [...pathPrefix, position]);
192
+ break;
193
+ }
194
+ case "text":
195
+ validateKeys(rawNode, ["k", "value"], `${where}[${position}]`);
196
+ if (typeof rawNode.value !== "string" || rawNode.value.length === 0) fail(`${where}[${position}].value must be a non-empty string`);
197
+ break;
198
+ case "part": {
199
+ validateKeys(rawNode, [
200
+ "k",
201
+ "id",
202
+ "index"
203
+ ], `${where}[${position}]`);
204
+ if (!allowAnchors) fail(`${where}[${position}] may not contain a part anchor`);
205
+ const index = rawNode.index;
206
+ if (typeof index !== "number" || !Number.isInteger(index) || index < 0) fail(`${where}[${position}].index must be a non-negative integer`);
207
+ if (rawNode.id !== `p${index}`) fail(`${where}[${position}] id must match its part index`);
208
+ if (anchorIds.has(index)) fail(`${where} duplicates part ${index}`);
209
+ anchorIds.set(index, rawNode.id);
210
+ break;
211
+ }
212
+ case "ival":
213
+ validateKeys(rawNode, ["k", "field"], `${where}[${position}]`);
214
+ if (!allowItemValues) fail(`${where}[${position}] may not contain an item value slot`);
215
+ if (!isIdentifier(rawNode.field)) fail(`${where}[${position}].field must be an identifier`);
216
+ break;
217
+ default: fail(`${where}[${position}] has unknown node kind ${String(rawNode.k)}`);
218
+ }
219
+ }
220
+ }
221
+ function validateLocation(value, where) {
222
+ if (!isRecord(value)) fail(`${where} must be a location object`);
223
+ validateKeys(value, value.kind === "sink" ? [
224
+ "id",
225
+ "kind",
226
+ "path",
227
+ "node"
228
+ ] : [
229
+ "id",
230
+ "kind",
231
+ "path"
232
+ ], where);
233
+ if (typeof value.id !== "string" || !/^p\d+$/.test(value.id)) fail(`${where}.id is invalid`);
234
+ if (value.kind !== "anchor" && value.kind !== "sink") fail(`${where}.kind is invalid`);
235
+ if (!isIntegerArray(value.path)) fail(`${where}.path must be non-negative child indices`);
236
+ if (value.kind === "sink" && (typeof value.node !== "string" || !/^e\d+$/.test(value.node))) fail(`${where}.node must name a stable element id`);
237
+ }
238
+ function validateCondition(value, where) {
239
+ if (!isRecord(value) || !isIdentifier(value.signal) || !conditionLiteralAllowed(value.op, value.value)) fail(`${where} supports this.<property> compared with a numeric literal (>, >=, <, <=), a number/string/boolean equality (===, !==), or a bare this.<property> truthiness test`);
240
+ validateKeys(value, [
241
+ "signal",
242
+ "op",
243
+ "value"
244
+ ], where);
245
+ }
246
+ function validateItemValueFields(nodes, field, where) {
247
+ nodes.forEach((node, position) => {
248
+ if (node.k === "ival") {
249
+ if (field !== void 0 && node.field !== field) fail(`${where}[${position}] must use item field ${field}`);
250
+ return;
251
+ }
252
+ if (node.k === "el") validateItemValueFields(node.children, field, `${where}[${position}].children`);
253
+ });
254
+ }
255
+ function validatePartPath(template, path, where, elementIds) {
256
+ if (!isIntegerArray(path) || path.length === 0) fail(`${where}.path must target an element`);
257
+ let nodes = template;
258
+ let node;
259
+ for (const index of path) {
260
+ node = nodes[index];
261
+ if (!node) fail(`${where}.path [${path.join(",")}] is unresolved`);
262
+ nodes = node.k === "el" ? node.children : [];
263
+ }
264
+ if (!node || node.k !== "el") fail(`${where}.path must target an element`);
265
+ if (!elementIds.has(node.id)) fail(`${where}.path targets an unknown element`);
266
+ return node.id;
267
+ }
268
+ function validateAnchorPath(template, path, partIndex, where) {
269
+ if (!isIntegerArray(path) || path.length === 0) fail(`${where}.path must target a template anchor`);
270
+ let nodes = template;
271
+ let node;
272
+ for (const [position, index] of path.entries()) {
273
+ node = nodes[index];
274
+ if (!node) fail(`${where}.path [${path.join(",")}] is unresolved`);
275
+ if (position < path.length - 1) {
276
+ if (node.k !== "el") fail(`${where}.path must target a template anchor`);
277
+ nodes = node.children;
278
+ }
279
+ }
280
+ if (!node || node.k !== "part" || node.index !== partIndex || node.id !== `p${partIndex}`) fail(`${where}.path must target anchor p${partIndex}`);
281
+ }
282
+ function validateFixedPartPath(template, path, where) {
283
+ let nodes = template;
284
+ for (const target of path) {
285
+ for (let sibling = 0; sibling < target; sibling++) if (nodes[sibling]?.k === "part") fail(`${where}.path is preceded by a dynamic anchor`);
286
+ const next = nodes[target];
287
+ nodes = next?.k === "el" ? next.children : [];
288
+ }
289
+ }
290
+ function hasSignal(part) {
291
+ return part.k === "text" || part.k === "prop" || part.k === "attr" || part.k === "bool" || part.k === "class" || part.k === "style" || part.k === "html" || part.k === "when" || part.k === "each";
292
+ }
293
+ function isRegion(part) {
294
+ return part.k === "when" || part.k === "each";
295
+ }
296
+ function validateEventAction(value, where) {
297
+ if (!isRecord(value) || typeof value.kind !== "string") fail(`${where} must be an event action`);
298
+ if (value.kind === "method" || value.kind === "call") {
299
+ validateKeys(value, ["kind", "name"], where);
300
+ if (!isIdentifier(value.name)) fail(`${where}.name must be an identifier`);
301
+ return;
302
+ }
303
+ if (value.kind === "increment" || value.kind === "decrement") {
304
+ validateKeys(value, ["kind", "signal"], where);
305
+ if (!isIdentifier(value.signal)) fail(`${where}.signal must be an identifier`);
306
+ return;
307
+ }
308
+ if (value.kind === "assign") {
309
+ validateKeys(value, [
310
+ "kind",
311
+ "signal",
312
+ "value"
313
+ ], where);
314
+ if (!isIdentifier(value.signal) || !isSerializable(value.value)) fail(`${where} assign action needs a signal and serializable value`);
315
+ return;
316
+ }
317
+ if (value.kind === "add" || value.kind === "subtract") {
318
+ validateKeys(value, [
319
+ "kind",
320
+ "signal",
321
+ "value"
322
+ ], where);
323
+ if (!isIdentifier(value.signal) || typeof value.value !== "number" || !Number.isFinite(value.value)) fail(`${where} arithmetic action is invalid`);
324
+ return;
325
+ }
326
+ fail(`${where} has unknown action kind ${value.kind}`);
327
+ }
328
+ /**
329
+ * Validate an unknown value as a Part Program v1. The validator is intentionally
330
+ * strict: unknown instruction kinds, missing ownership records and unsafe paths
331
+ * fail before a runtime can guess at their meaning.
332
+ */
333
+ function validatePartProgram(raw) {
334
+ if (!isRecord(raw)) fail("program must be an object");
335
+ validateKeys(raw, [
336
+ "version",
337
+ "tag",
338
+ "root",
339
+ "template",
340
+ "parts",
341
+ "regions",
342
+ "dependencies",
343
+ "locations",
344
+ "sourceMap",
345
+ "metadata"
346
+ ], "program");
347
+ if (raw.version !== 1) fail("version must be 1");
348
+ if (typeof raw.tag !== "string" || !/^[a-z][a-z0-9]*(-[a-z0-9]+)+$/.test(raw.tag)) fail("tag must be a custom-element tag name");
349
+ if (!isRecord(raw.root) || raw.root.id !== "root") fail("root must be a root record");
350
+ validateKeys(raw.root, [
351
+ "id",
352
+ "kind",
353
+ "nodes"
354
+ ], "root");
355
+ if (raw.root.kind !== "light" && raw.root.kind !== "shadow-open" && raw.root.kind !== "shadow-closed") fail("root.kind is unsupported");
356
+ if (!Array.isArray(raw.root.nodes) || raw.root.nodes.some((id) => typeof id !== "string")) fail("root.nodes must list element ids");
357
+ const elementIds = /* @__PURE__ */ new Set();
358
+ const anchorIds = /* @__PURE__ */ new Map();
359
+ const elementLocations = /* @__PURE__ */ new Map();
360
+ validateTreeNodes(raw.template, "template", true, false, elementIds, anchorIds, elementLocations);
361
+ const topLevelElements = raw.template.filter((node) => node.k === "el").map((node) => node.id);
362
+ if (JSON.stringify(raw.root.nodes) !== JSON.stringify(topLevelElements)) fail("root.nodes must match top-level template elements in order");
363
+ if (!Array.isArray(raw.parts)) fail("parts must be an array");
364
+ const parts = raw.parts;
365
+ const anchorPartCounts = /* @__PURE__ */ new Map();
366
+ parts.forEach((part, position) => {
367
+ if (!isRecord(part)) fail(`parts[${position}] must be an object`);
368
+ if (part.index !== position) fail(`parts[${position}].index must equal its position`);
369
+ validateLocation(part.location, `parts[${position}].location`);
370
+ if (part.location.id !== `p${position}`) fail(`parts[${position}].location id is out of order`);
371
+ switch (part.k) {
372
+ case "text":
373
+ validateKeys(part, [
374
+ "k",
375
+ "index",
376
+ "signal",
377
+ "location"
378
+ ], `parts[${position}]`);
379
+ if (!isIdentifier(part.signal)) fail(`parts[${position}].signal must be an identifier`);
380
+ if (part.location.kind !== "anchor") fail(`parts[${position}] text must own an anchor`);
381
+ validateAnchorPath(raw.template, part.location.path, position, `parts[${position}]`);
382
+ anchorPartCounts.set(position, (anchorPartCounts.get(position) ?? 0) + 1);
383
+ break;
384
+ case "prop":
385
+ case "attr":
386
+ case "bool":
387
+ validateKeys(part, [
388
+ "k",
389
+ "index",
390
+ "signal",
391
+ "name",
392
+ "path",
393
+ "location"
394
+ ], `parts[${position}]`);
395
+ if (!isIdentifier(part.signal) || typeof part.name !== "string" || part.name.length === 0) fail(`parts[${position}] ${part.k} needs signal and name`);
396
+ if (part.k === "prop" && (!isIdentifier(part.name) || /^on/i.test(part.name))) fail(`parts[${position}] prop name must be an identifier`);
397
+ if (part.k === "prop") {
398
+ const propNameReason = forbiddenSinkReason("prop", part.name);
399
+ if (propNameReason !== null) fail(`parts[${position}] ${propNameReason}`);
400
+ }
401
+ if ((part.k === "attr" || part.k === "bool") && !isAttributeName(part.name)) fail(`parts[${position}] ${part.k} name is unsafe`);
402
+ if (part.location.kind !== "sink") fail(`parts[${position}] ${part.k} must own a sink`);
403
+ if (validatePartPath(raw.template, part.path, `parts[${position}]`, elementIds) !== part.location.node) fail(`parts[${position}] location node does not match path`);
404
+ if (!samePath(part.location.path, part.path)) fail(`parts[${position}] location path does not match path`);
405
+ break;
406
+ case "class":
407
+ case "style":
408
+ validateKeys(part, [
409
+ "k",
410
+ "index",
411
+ "signal",
412
+ "path",
413
+ "location"
414
+ ], `parts[${position}]`);
415
+ if (!isIdentifier(part.signal) || part.location.kind !== "sink") fail(`parts[${position}] ${part.k} is invalid`);
416
+ if (validatePartPath(raw.template, part.path, `parts[${position}]`, elementIds) !== part.location.node) fail(`parts[${position}] location node does not match path`);
417
+ if (!samePath(part.location.path, part.path)) fail(`parts[${position}] location path does not match path`);
418
+ break;
419
+ case "html": {
420
+ validateKeys(part, [
421
+ "k",
422
+ "index",
423
+ "signal",
424
+ "path",
425
+ "location"
426
+ ], `parts[${position}]`);
427
+ if (!isIdentifier(part.signal) || part.location.kind !== "sink") fail(`parts[${position}] html is invalid`);
428
+ if (validatePartPath(raw.template, part.path, `parts[${position}]`, elementIds) !== part.location.node) fail(`parts[${position}] location node does not match path`);
429
+ if (!samePath(part.location.path, part.path)) fail(`parts[${position}] location path does not match path`);
430
+ let htmlTarget;
431
+ let htmlNodes = raw.template;
432
+ for (const pathIndex of part.path) {
433
+ htmlTarget = htmlNodes[pathIndex];
434
+ htmlNodes = htmlTarget?.k === "el" ? htmlTarget.children : [];
435
+ }
436
+ if (htmlTarget?.k !== "el" || htmlTarget.children.length > 0) fail(`parts[${position}] html sink target must be a childless element`);
437
+ break;
438
+ }
439
+ case "ref":
440
+ validateKeys(part, [
441
+ "k",
442
+ "index",
443
+ "ref",
444
+ "path",
445
+ "location"
446
+ ], `parts[${position}]`);
447
+ if (!isIdentifier(part.ref) || part.location.kind !== "sink") fail(`parts[${position}] ref is invalid`);
448
+ if (validatePartPath(raw.template, part.path, `parts[${position}]`, elementIds) !== part.location.node) fail(`parts[${position}] location node does not match path`);
449
+ if (!samePath(part.location.path, part.path)) fail(`parts[${position}] location path does not match path`);
450
+ break;
451
+ case "event":
452
+ validateKeys(part, [
453
+ "k",
454
+ "index",
455
+ "event",
456
+ "handler",
457
+ "action",
458
+ "path",
459
+ "location"
460
+ ], `parts[${position}]`);
461
+ if (!isIdentifier(part.handler) || typeof part.event !== "string" || part.event.length === 0) fail(`parts[${position}] event needs event and handler`);
462
+ if (!/^[a-z][a-z0-9:-]*$/.test(part.event)) fail(`parts[${position}] event name is unsafe`);
463
+ validateEventAction(part.action, `parts[${position}].action`);
464
+ if (part.location.kind !== "sink") fail(`parts[${position}] event must own a sink`);
465
+ if (validatePartPath(raw.template, part.path, `parts[${position}]`, elementIds) !== part.location.node) fail(`parts[${position}] event location node does not match path`);
466
+ if (!samePath(part.location.path, part.path)) fail(`parts[${position}] location path does not match path`);
467
+ break;
468
+ case "when":
469
+ validateKeys(part, [
470
+ "k",
471
+ "index",
472
+ "signal",
473
+ "test",
474
+ "on",
475
+ "off",
476
+ "location"
477
+ ], `parts[${position}]`);
478
+ if (!isIdentifier(part.signal)) fail(`parts[${position}] when needs a signal`);
479
+ if (part.location.kind !== "anchor") fail(`parts[${position}] when must own an anchor`);
480
+ validateCondition(part.test, `parts[${position}].test`);
481
+ if (part.test.signal !== part.signal) fail(`parts[${position}] test signal mismatch`);
482
+ validateTreeNodes(part.on, `parts[${position}].on`, false, false, elementIds, /* @__PURE__ */ new Map(), elementLocations, [], []);
483
+ validateTreeNodes(part.off, `parts[${position}].off`, false, false, elementIds, /* @__PURE__ */ new Map(), elementLocations, [], []);
484
+ validateAnchorPath(raw.template, part.location.path, position, `parts[${position}]`);
485
+ anchorPartCounts.set(position, (anchorPartCounts.get(position) ?? 0) + 1);
486
+ break;
487
+ case "each":
488
+ validateKeys(part, [
489
+ "k",
490
+ "index",
491
+ "signal",
492
+ "key",
493
+ "field",
494
+ "item",
495
+ "itemEvents",
496
+ "location"
497
+ ], `parts[${position}]`);
498
+ if (!isIdentifier(part.signal) || !isIdentifier(part.key) || part.field !== void 0 && !isIdentifier(part.field)) fail(`parts[${position}] each needs signal and key identifiers`);
499
+ if (part.location.kind !== "anchor") fail(`parts[${position}] each must own an anchor`);
500
+ validateTreeNodes(part.item, `parts[${position}].item`, false, true, elementIds, /* @__PURE__ */ new Map(), elementLocations, [], []);
501
+ validateItemValueFields(part.item, part.field, `parts[${position}].item`);
502
+ if (part.itemEvents !== void 0) {
503
+ if (!Array.isArray(part.itemEvents)) fail(`parts[${position}].itemEvents must be an array`);
504
+ part.itemEvents.forEach((binding, bindingIndex) => {
505
+ const where = `parts[${position}].itemEvents[${bindingIndex}]`;
506
+ validateKeys(binding, [
507
+ "event",
508
+ "handler",
509
+ "action",
510
+ "selector"
511
+ ], where);
512
+ if (!isIdentifier(binding.handler) || typeof binding.event !== "string" || binding.event.length === 0) fail(`${where} needs event and handler`);
513
+ if (!/^[a-z][a-z0-9:-]*$/.test(binding.event)) fail(`${where} event name is unsafe`);
514
+ if (binding.selector !== void 0 && typeof binding.selector !== "string") fail(`${where} selector must be a string`);
515
+ validateEventAction(binding.action, `${where}.action`);
516
+ });
517
+ }
518
+ validateAnchorPath(raw.template, part.location.path, position, `parts[${position}]`);
519
+ anchorPartCounts.set(position, (anchorPartCounts.get(position) ?? 0) + 1);
520
+ break;
521
+ default: fail(`parts[${position}] has unknown part kind ${String(part.k)}`);
522
+ }
523
+ });
524
+ for (const part of parts) {
525
+ if (part.k === "text" || part.k === "when" || part.k === "each") continue;
526
+ validateFixedPartPath(raw.template, part.path, `parts[${part.index}]`);
527
+ }
528
+ for (const [index, id] of anchorIds) {
529
+ const part = parts[index];
530
+ if (!part || !isRegion(part) && part.k !== "text") fail(`anchor ${id} must reference a text or Region part`);
531
+ if ((anchorPartCounts.get(index) ?? 0) !== 1) fail(`part ${index} must have exactly one anchor`);
532
+ }
533
+ for (const part of parts) if (isRegion(part) || part.k === "text") {
534
+ if (!anchorIds.has(part.index)) fail(`part ${part.index} is missing its template anchor`);
535
+ } else if (anchorIds.has(part.index)) fail(`fixed part ${part.index} may not have a template anchor`);
536
+ if (!Array.isArray(raw.regions)) fail("regions must be an array");
537
+ const regions = raw.regions;
538
+ const regionParts = parts.filter(isRegion);
539
+ if (regions.length !== regionParts.length) fail("regions must describe every Region part exactly once");
540
+ regions.forEach((region, position) => {
541
+ if (isRecord(region)) validateKeys(region, [
542
+ "id",
543
+ "index",
544
+ "kind",
545
+ "anchor",
546
+ "end",
547
+ "source"
548
+ ], `regions[${position}]`);
549
+ if (!isRecord(region) || region.id !== `r${region.index}` || region.index !== regionParts[position].index || region.kind !== "when" && region.kind !== "each" || region.anchor !== `p${region.index}` || region.end !== `p${region.index}:end` || region.source !== `p${region.index}`) fail(`regions[${position}] has an invalid Region record`);
550
+ if (region.kind !== regionParts[position].k) fail(`regions[${position}] kind mismatches its part`);
551
+ });
552
+ if (!Array.isArray(raw.dependencies)) fail("dependencies must be an array");
553
+ const dependencies = raw.dependencies;
554
+ const dependencyOwners = /* @__PURE__ */ new Set();
555
+ const expectedDependencyOwners = new Set(parts.filter(hasSignal).map((part) => `${isRegion(part) ? "region" : "part"}:${part.index}`));
556
+ for (const [position, dependency] of dependencies.entries()) {
557
+ if (!isRecord(dependency) || !isIdentifier(dependency.signal) || !isRecord(dependency.owner)) fail(`dependencies[${position}] is invalid`);
558
+ validateKeys(dependency, [
559
+ "signal",
560
+ "owner",
561
+ "location"
562
+ ], `dependencies[${position}]`);
563
+ validateKeys(dependency.owner, ["kind", "index"], `dependencies[${position}].owner`);
564
+ if (dependency.owner.kind !== "part" && dependency.owner.kind !== "region") fail(`dependencies[${position}].owner.kind is invalid`);
565
+ if (!Number.isInteger(dependency.owner.index) || dependency.owner.index < 0) fail(`dependencies[${position}].owner.index is invalid`);
566
+ const owner = parts[dependency.owner.index];
567
+ if (!owner || !hasSignal(owner) || (isRegion(owner) ? "region" : "part") !== dependency.owner.kind) fail(`dependencies[${position}] points at a non-signal owner`);
568
+ if (owner.signal !== dependency.signal || dependency.location !== owner.location.id) fail(`dependencies[${position}] does not match its owning instruction`);
569
+ const ownerKey = `${dependency.owner.kind}:${dependency.owner.index}`;
570
+ if (dependencyOwners.has(ownerKey)) fail(`dependencies duplicate ${ownerKey}`);
571
+ dependencyOwners.add(ownerKey);
572
+ }
573
+ if (dependencyOwners.size !== expectedDependencyOwners.size) fail("every signal-owning instruction needs one dependency record");
574
+ for (const owner of expectedDependencyOwners) if (!dependencyOwners.has(owner)) fail(`missing dependency ${owner}`);
575
+ if (!Array.isArray(raw.locations)) fail("locations must be an array");
576
+ const locations = raw.locations;
577
+ const locationIds = /* @__PURE__ */ new Set();
578
+ const locationById = /* @__PURE__ */ new Map();
579
+ for (const [position, location] of locations.entries()) {
580
+ if (!isRecord(location) || typeof location.id !== "string" || locationIds.has(location.id)) fail(`locations[${position}] must have a unique id`);
581
+ validateKeys(location, location.kind === "element" ? [
582
+ "id",
583
+ "kind",
584
+ "tag",
585
+ "path"
586
+ ] : location.kind === "anchor" ? [
587
+ "id",
588
+ "kind",
589
+ "part",
590
+ "path"
591
+ ] : location.kind === "sink" ? [
592
+ "id",
593
+ "kind",
594
+ "part",
595
+ "node",
596
+ "path"
597
+ ] : [
598
+ "id",
599
+ "kind",
600
+ "path"
601
+ ], `locations[${position}]`);
602
+ locationIds.add(location.id);
603
+ locationById.set(location.id, location);
604
+ if (!isIntegerArray(location.path)) fail(`locations[${position}].path is invalid`);
605
+ if (location.kind === "element") {
606
+ const element = elementLocations.get(location.id);
607
+ if (!element || location.tag !== element.tag || !samePath(location.path, element.path)) fail(`locations[${position}] element is invalid`);
608
+ } else if (location.kind === "anchor") {
609
+ const part = Number.isInteger(location.part) ? parts[location.part] : void 0;
610
+ if (!part || !isRegion(part) && part.k !== "text" || location.id !== `p${location.part}` || !samePath(location.path, part.location.path)) fail(`locations[${position}] anchor is invalid`);
611
+ } else if (location.kind === "sink") {
612
+ const part = Number.isInteger(location.part) ? parts[location.part] : void 0;
613
+ if (!part || isRegion(part) || part.k === "text" || location.id !== `p${location.part}` || !elementIds.has(location.node) || !samePath(location.path, part.location.path) || part.location.node !== location.node) fail(`locations[${position}] sink is invalid`);
614
+ } else fail(`locations[${position}] has an unknown kind`);
615
+ }
616
+ for (const elementId of elementIds) if (!locationIds.has(elementId)) fail(`missing location for ${elementId}`);
617
+ for (const part of parts) {
618
+ if (!locationIds.has(part.location.id)) fail(`missing location for ${part.location.id}`);
619
+ const location = locationById.get(part.location.id);
620
+ if (!location || location.kind !== part.location.kind) fail(`location ${part.location.id} does not match its instruction`);
621
+ }
622
+ if (!isRecord(raw.metadata)) fail("metadata must be an object");
623
+ const metadata = raw.metadata;
624
+ validateKeys(metadata, [
625
+ "tag",
626
+ "className",
627
+ "sourceFile",
628
+ "properties",
629
+ "observedAttributes",
630
+ "cem"
631
+ ], "metadata");
632
+ const sourceIds = /* @__PURE__ */ new Set();
633
+ if (raw.sourceMap !== void 0) {
634
+ if (!isRecord(raw.sourceMap) || raw.sourceMap.version !== 1 || raw.sourceMap.file !== metadata.sourceFile) fail("sourceMap must be version 1 and identify the metadata source file");
635
+ validateKeys(raw.sourceMap, [
636
+ "version",
637
+ "file",
638
+ "records"
639
+ ], "sourceMap");
640
+ if (!Array.isArray(raw.sourceMap.records)) fail("sourceMap.records must be an array");
641
+ for (const [position, record] of raw.sourceMap.records.entries()) {
642
+ if (!isRecord(record) || typeof record.id !== "string" || sourceIds.has(record.id)) fail(`sourceMap.records[${position}] must have a unique id`);
643
+ validateKeys(record, [
644
+ "id",
645
+ "kind",
646
+ "source"
647
+ ], `sourceMap.records[${position}]`);
648
+ sourceIds.add(record.id);
649
+ const kinds = /* @__PURE__ */ new Set([
650
+ "root",
651
+ "element",
652
+ "part",
653
+ "region",
654
+ "property",
655
+ "handler"
656
+ ]);
657
+ if (typeof record.kind !== "string" || !kinds.has(record.kind)) fail(`sourceMap.records[${position}].kind is invalid`);
658
+ validateSourceRange(record.source, `sourceMap.records[${position}].source`);
659
+ }
660
+ }
661
+ if (metadata.tag !== raw.tag || typeof metadata.className !== "string" || typeof metadata.sourceFile !== "string") fail("metadata identity does not match the program");
662
+ if (!Array.isArray(metadata.properties) || !Array.isArray(metadata.observedAttributes)) fail("metadata properties and observedAttributes must be arrays");
663
+ const propertyTypes = /* @__PURE__ */ new Set([
664
+ "string",
665
+ "number",
666
+ "boolean",
667
+ "array",
668
+ "object"
669
+ ]);
670
+ const metadataNames = /* @__PURE__ */ new Set();
671
+ const metadataProperties = [];
672
+ for (const [position, property] of metadata.properties.entries()) {
673
+ const name = isIdentifier(property && isRecord(property) ? property.name : void 0) ? property.name : "";
674
+ const attribute = property && isRecord(property) && property.attribute !== void 0 ? property.attribute : void 0;
675
+ const type = property && isRecord(property) ? property.type : void 0;
676
+ const converter = property && isRecord(property) ? property.converter : void 0;
677
+ const reflect = property && isRecord(property) ? property.reflect : void 0;
678
+ const defaultValue = property && isRecord(property) ? property.default : void 0;
679
+ const computed = property && isRecord(property) ? property.computed : void 0;
680
+ const deps = property && isRecord(property) ? property.deps : void 0;
681
+ if (!isRecord(property) || !isIdentifier(name) || metadataNames.has(name) || attribute !== null && !isAttributeName(attribute) || attribute === void 0 || typeof type !== "string" || !propertyTypes.has(type) || typeof converter !== "string" || !propertyTypes.has(converter) || typeof reflect !== "boolean" || !isSerializable(defaultValue)) fail(`metadata.properties[${position}] is invalid`);
682
+ validateKeys(property, [
683
+ "name",
684
+ "attribute",
685
+ "type",
686
+ "converter",
687
+ "reflect",
688
+ "default",
689
+ "computed",
690
+ "deps"
691
+ ], `metadata.properties[${position}]`);
692
+ if (computed !== void 0) {
693
+ if (computed !== true || attribute !== null || reflect !== false || !Array.isArray(deps) || deps.length === 0 || deps.some((dep) => !isIdentifier(dep))) fail(`metadata.properties[${position}].computed is invalid`);
694
+ } else if (deps !== void 0) fail(`metadata.properties[${position}].deps requires computed: true`);
695
+ metadataNames.add(name);
696
+ metadataProperties.push({
697
+ name,
698
+ attribute,
699
+ type,
700
+ reflect
701
+ });
702
+ }
703
+ const observed = /* @__PURE__ */ new Set();
704
+ for (const [position, attribute] of metadata.observedAttributes.entries()) {
705
+ if (!isAttributeName(attribute) || attribute.length === 0 || observed.has(attribute)) fail(`metadata.observedAttributes[${position}] is invalid`);
706
+ observed.add(attribute);
707
+ }
708
+ const expectedObserved = metadataProperties.flatMap((property) => property.attribute === null ? [] : [property.attribute]);
709
+ if (JSON.stringify(expectedObserved) !== JSON.stringify(metadata.observedAttributes)) fail("metadata.observedAttributes must match property attributes in declaration order");
710
+ const cem = metadata.cem;
711
+ if (!isRecord(cem) || cem.tagName !== raw.tag || cem.className !== metadata.className || !isRecord(cem.declaration) || cem.declaration.name !== metadata.className || typeof cem.declaration.module !== "string" || !Array.isArray(cem.attributes) || !Array.isArray(cem.members)) fail("metadata.cem identity or shape is invalid");
712
+ validateKeys(cem, [
713
+ "tagName",
714
+ "className",
715
+ "declaration",
716
+ "attributes",
717
+ "members"
718
+ ], "metadata.cem");
719
+ validateKeys(cem.declaration, ["name", "module"], "metadata.cem.declaration");
720
+ const cemAttributes = cem.attributes;
721
+ const expectedCemAttributes = metadataProperties.filter((property) => property.attribute !== null);
722
+ if (cemAttributes.length !== expectedCemAttributes.length) fail("metadata.cem attributes are incomplete");
723
+ for (const [position, attribute] of cemAttributes.entries()) {
724
+ if (!isRecord(attribute) || !isAttributeName(attribute.name) || !isIdentifier(attribute.fieldName) || typeof attribute.type !== "string" || !propertyTypes.has(attribute.type) || typeof attribute.reflect !== "boolean") fail(`metadata.cem.attributes[${position}] is invalid`);
725
+ validateKeys(attribute, [
726
+ "name",
727
+ "fieldName",
728
+ "type",
729
+ "reflect"
730
+ ], `metadata.cem.attributes[${position}]`);
731
+ const expected = expectedCemAttributes[position];
732
+ if (attribute.name !== expected.attribute || attribute.fieldName !== expected.name || attribute.type !== expected.type || attribute.reflect !== expected.reflect) fail(`metadata.cem.attributes[${position}] does not match property metadata`);
733
+ }
734
+ if (cem.members.length !== metadataProperties.length) fail("metadata.cem members are incomplete");
735
+ for (const [position, member] of cem.members.entries()) {
736
+ if (!isRecord(member) || !isIdentifier(member.name) || !isIdentifier(member.fieldName) || typeof member.type !== "string" || !propertyTypes.has(member.type) || member.attribute !== null && !isAttributeName(member.attribute) || typeof member.reflect !== "boolean") fail(`metadata.cem.members[${position}] is invalid`);
737
+ validateKeys(member, [
738
+ "name",
739
+ "fieldName",
740
+ "type",
741
+ "attribute",
742
+ "reflect"
743
+ ], `metadata.cem.members[${position}]`);
744
+ const expected = metadataProperties[position];
745
+ if (member.name !== expected.name || member.fieldName !== expected.name || member.type !== expected.type || member.attribute !== expected.attribute || member.reflect !== expected.reflect) fail(`metadata.cem.members[${position}] does not match property metadata`);
746
+ }
747
+ if (raw.sourceMap !== void 0) {
748
+ const expectedSourceIds = /* @__PURE__ */ new Set([
749
+ "root",
750
+ ...metadataProperties.map((property) => `property:${property.name}`),
751
+ ...locations.map((location) => location.id),
752
+ ...regions.map((region) => region.id)
753
+ ]);
754
+ for (const sourceId of expectedSourceIds) if (!sourceIds.has(sourceId)) fail(`sourceMap is missing record ${sourceId}`);
755
+ }
756
+ }
757
+ //#endregion
758
+ export { COMPILED_MODULE_ABI_VERSION, DATA_OE_LIGHT, PART_PROGRAM_VERSION, STATIC_STYLES_MARKER, VOID_TAGS, conditionLiteralAllowed, isAttributeName, partAnchorEndMarker, partAnchorMarker, validatePartProgram };