@usethrottle/builder-schema 0.0.0 → 0.1.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/README.md +39 -0
- package/dist/index.cjs +3654 -338
- package/dist/index.d.cts +494 -2
- package/dist/index.d.ts +494 -2
- package/dist/index.js +3652 -336
- package/package.json +7 -2
package/dist/index.cjs
CHANGED
|
@@ -3,9 +3,9 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (
|
|
6
|
+
var __export = (target2, all) => {
|
|
7
7
|
for (var name in all)
|
|
8
|
-
__defProp(
|
|
8
|
+
__defProp(target2, name, { get: all[name], enumerable: true });
|
|
9
9
|
};
|
|
10
10
|
var __copyProps = (to, from, except, desc) => {
|
|
11
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
@@ -20,20 +20,33 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
AGENT_MAX_OPS: () => AGENT_MAX_OPS,
|
|
24
|
+
ALLOWED_ATTRS: () => ALLOWED_ATTRS,
|
|
25
|
+
ALLOWED_ELEMENTS: () => ALLOWED_ELEMENTS,
|
|
26
|
+
ALLOWED_ROLES: () => ALLOWED_ROLES,
|
|
23
27
|
ANCHOR_RE: () => ANCHOR_RE,
|
|
24
28
|
BASIC_WIDGET_DEFINITIONS: () => BASIC_WIDGET_DEFINITIONS,
|
|
29
|
+
BIDI_CONTROL_RE: () => BIDI_CONTROL_RE,
|
|
25
30
|
BREAKPOINTS: () => BREAKPOINTS,
|
|
26
31
|
CATALOG_STRING_KEYS: () => CATALOG_STRING_KEYS,
|
|
32
|
+
CLASS_TOKEN_RE: () => CLASS_TOKEN_RE,
|
|
27
33
|
COLOR_VALUE_RE: () => COLOR_VALUE_RE,
|
|
34
|
+
COMMON_SETTING_KEYS: () => COMMON_SETTING_KEYS,
|
|
28
35
|
COMMON_WIDGET_SETTINGS: () => COMMON_WIDGET_SETTINGS,
|
|
36
|
+
CSS_CONTROLS: () => CSS_CONTROLS,
|
|
29
37
|
CURRENT_SCHEMA_VERSION: () => CURRENT_SCHEMA_VERSION,
|
|
38
|
+
DATA_ATTR_RE: () => DATA_ATTR_RE,
|
|
30
39
|
DATA_RESULT_TYPES: () => DATA_RESULT_TYPES,
|
|
40
|
+
EACH_ITERATION_CAP: () => EACH_ITERATION_CAP,
|
|
31
41
|
ISSUE_CODES: () => ISSUE_CODES,
|
|
32
42
|
LIMITS: () => LIMITS,
|
|
33
43
|
LOCALE_TAG_RE: () => LOCALE_TAG_RE,
|
|
34
44
|
MAX_ISSUES: () => MAX_ISSUES,
|
|
45
|
+
MAX_TEMPLATE_WEIGHT: () => MAX_TEMPLATE_WEIGHT,
|
|
35
46
|
NODE_ID_RE: () => NODE_ID_RE,
|
|
36
47
|
PREVIEW_WIDTHS: () => PREVIEW_WIDTHS,
|
|
48
|
+
RESERVED_CLASS_PREFIXES: () => RESERVED_CLASS_PREFIXES,
|
|
49
|
+
RESERVED_DATA_PREFIX: () => RESERVED_DATA_PREFIX,
|
|
37
50
|
SAFE_HREF: () => SAFE_HREF,
|
|
38
51
|
SETTING_CONTROLS: () => SETTING_CONTROLS,
|
|
39
52
|
SETTING_TABS: () => SETTING_TABS,
|
|
@@ -45,12 +58,20 @@ __export(index_exports, {
|
|
|
45
58
|
SITE_STRING_KEYS: () => SITE_STRING_KEYS,
|
|
46
59
|
SITE_STRING_NAMESPACES: () => SITE_STRING_NAMESPACES,
|
|
47
60
|
SPACING_TOKENS: () => SPACING_TOKENS,
|
|
61
|
+
TEMPLATE_FILTERS: () => TEMPLATE_FILTERS,
|
|
48
62
|
THROTTLE_PK_RE: () => THROTTLE_PK_RE,
|
|
63
|
+
VOID_ELEMENTS: () => VOID_ELEMENTS,
|
|
49
64
|
WIDGET_GROUPS: () => WIDGET_GROUPS,
|
|
50
65
|
WIDGET_TYPE_RE: () => WIDGET_TYPE_RE,
|
|
66
|
+
agentDocOpSchema: () => agentDocOpSchema,
|
|
67
|
+
agentDocOpsSchema: () => agentDocOpsSchema,
|
|
68
|
+
applyOps: () => applyOps,
|
|
51
69
|
assetIdSchema: () => assetIdSchema,
|
|
70
|
+
canPlace: () => canPlace,
|
|
52
71
|
capIssues: () => capIssues,
|
|
53
72
|
checkLimits: () => checkLimits,
|
|
73
|
+
checkTopLevel: () => checkTopLevel,
|
|
74
|
+
checkWidgetPart: () => checkWidgetPart,
|
|
54
75
|
colorValueSchema: () => colorValueSchema,
|
|
55
76
|
columnPropsSchema: () => columnPropsSchema,
|
|
56
77
|
createRegistry: () => createRegistry,
|
|
@@ -58,10 +79,14 @@ __export(index_exports, {
|
|
|
58
79
|
dataBindingSchema: () => dataBindingSchema,
|
|
59
80
|
dataSourceDescriptorSchema: () => dataSourceDescriptorSchema,
|
|
60
81
|
defaultSiteSettings: () => defaultSiteSettings,
|
|
82
|
+
docOpSchema: () => docOpSchema,
|
|
61
83
|
emptyDocument: () => emptyDocument,
|
|
62
84
|
fieldSpecSchema: () => fieldSpecSchema,
|
|
63
85
|
formatPath: () => formatPath,
|
|
64
86
|
frozenAssetSchema: () => frozenAssetSchema,
|
|
87
|
+
innerSectionNodeSchema: () => innerSectionNodeSchema,
|
|
88
|
+
invertOps: () => invertOps,
|
|
89
|
+
isReservedClass: () => isReservedClass,
|
|
65
90
|
isSettingValue: () => isSettingValue,
|
|
66
91
|
localeTagSchema: () => localeTagSchema,
|
|
67
92
|
localizedTextSchema: () => localizedTextSchema,
|
|
@@ -71,26 +96,39 @@ __export(index_exports, {
|
|
|
71
96
|
newNodeId: () => newNodeId,
|
|
72
97
|
nodeIdSchema: () => nodeIdSchema,
|
|
73
98
|
pageDocumentSchema: () => pageDocumentSchema,
|
|
99
|
+
parseTemplate: () => parseTemplate,
|
|
74
100
|
resolveColumnProps: () => resolveColumnProps,
|
|
75
101
|
resolveColumnWidths: () => resolveColumnWidths,
|
|
76
102
|
resolveResponsive: () => resolveResponsive,
|
|
103
|
+
safeUrl: () => safeUrl,
|
|
104
|
+
sanitizeWidgetCss: () => sanitizeWidgetCss,
|
|
105
|
+
sectionNodeSchema: () => sectionNodeSchema,
|
|
77
106
|
sectionPropsSchema: () => sectionPropsSchema,
|
|
78
107
|
settingFieldSchema: () => settingFieldSchema,
|
|
79
108
|
settingValueSchema: () => settingValueSchema,
|
|
80
109
|
siteSettingsSchema: () => siteSettingsSchema,
|
|
81
110
|
spacingValueSchema: () => spacingValueSchema,
|
|
111
|
+
templateWeight: () => templateWeight,
|
|
82
112
|
upgradeDocument: () => upgradeDocument,
|
|
113
|
+
upgradeNodes: () => upgradeNodes,
|
|
83
114
|
utf8Length: () => utf8Length,
|
|
115
|
+
validateCustomWidget: () => validateCustomWidget,
|
|
84
116
|
validateDocument: () => validateDocument,
|
|
85
117
|
validateSettingValue: () => validateSettingValue,
|
|
118
|
+
validateTemplate: () => validateTemplate,
|
|
86
119
|
validateWidgetProps: () => validateWidgetProps,
|
|
87
120
|
walkNodes: () => walkNodes,
|
|
88
121
|
widgetDefinitionSchema: () => widgetDefinitionSchema,
|
|
122
|
+
widgetNodeSchema: () => widgetNodeSchema,
|
|
89
123
|
wrapColumnRows: () => wrapColumnRows,
|
|
90
124
|
zodIssues: () => zodIssues
|
|
91
125
|
});
|
|
92
126
|
module.exports = __toCommonJS(index_exports);
|
|
93
127
|
|
|
128
|
+
// src/jitless.ts
|
|
129
|
+
var import_zod = require("zod");
|
|
130
|
+
import_zod.z.config({ jitless: true });
|
|
131
|
+
|
|
94
132
|
// src/ids.ts
|
|
95
133
|
var NODE_ID_RE = /^[a-z0-9]{8,24}$/;
|
|
96
134
|
var ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
@@ -118,10 +156,37 @@ var ISSUE_CODES = [
|
|
|
118
156
|
"UNSUPPORTED_SCHEMA_VERSION",
|
|
119
157
|
"INVALID_WIDGET_TYPE",
|
|
120
158
|
"INVALID_COLUMN_WIDTHS",
|
|
121
|
-
"INNER_IN_INNER"
|
|
159
|
+
"INNER_IN_INNER",
|
|
122
160
|
// <task-8>
|
|
161
|
+
"SECTION_NOT_NESTABLE",
|
|
162
|
+
"INNER_NOT_TOP_LEVEL",
|
|
163
|
+
"COLUMN_LIMIT",
|
|
164
|
+
"NOT_ALLOWED_IN_PART",
|
|
165
|
+
"MOVE_INTO_SELF",
|
|
166
|
+
"COLUMN_REMOVE_NEEDS_SETCOLUMNS",
|
|
167
|
+
"COLUMN_NOT_EMPTY",
|
|
168
|
+
"UNKNOWN_PROP",
|
|
169
|
+
"SOURCE_NOT_ENABLED",
|
|
170
|
+
"RESULT_TYPE_MISMATCH",
|
|
171
|
+
"ID_NOT_FRESH",
|
|
172
|
+
"NODE_NOT_FOUND",
|
|
123
173
|
// </task-8>
|
|
124
174
|
// <task-9>
|
|
175
|
+
"TEMPLATE_SYNTAX",
|
|
176
|
+
"TEMPLATE_FORBIDDEN",
|
|
177
|
+
"ELEMENT_NOT_ALLOWED",
|
|
178
|
+
"ATTRIBUTE_NOT_ALLOWED",
|
|
179
|
+
"FIELD_NOT_ALLOWED",
|
|
180
|
+
"UNKNOWN_REFERENCE",
|
|
181
|
+
"CSS_SYNTAX",
|
|
182
|
+
"CSS_FORBIDDEN",
|
|
183
|
+
"CSS_PROPERTY_NOT_ALLOWED",
|
|
184
|
+
"NO_RECURSION",
|
|
185
|
+
"BINDING_TARGET_MISSING",
|
|
186
|
+
"BINDING_TYPE_MISMATCH",
|
|
187
|
+
"WIDGET_TOO_LARGE",
|
|
188
|
+
"DUPLICATE_KEY",
|
|
189
|
+
"VERSION_RULE"
|
|
125
190
|
// </task-9>
|
|
126
191
|
];
|
|
127
192
|
var MAX_ISSUES = 50;
|
|
@@ -144,43 +209,43 @@ function capIssues(issues) {
|
|
|
144
209
|
return issues.length > MAX_ISSUES ? issues.slice(0, MAX_ISSUES) : issues;
|
|
145
210
|
}
|
|
146
211
|
var KNOWN_CODES = new Set(ISSUE_CODES);
|
|
147
|
-
function codeFor(
|
|
148
|
-
if (
|
|
149
|
-
const params =
|
|
212
|
+
function codeFor(issue2) {
|
|
213
|
+
if (issue2.code === "custom") {
|
|
214
|
+
const params = issue2.params;
|
|
150
215
|
if (typeof params?.code === "string" && KNOWN_CODES.has(params.code)) {
|
|
151
216
|
return params.code;
|
|
152
217
|
}
|
|
153
218
|
return "INVALID_VALUE";
|
|
154
219
|
}
|
|
155
|
-
if (
|
|
156
|
-
if (
|
|
157
|
-
if (
|
|
158
|
-
return /received undefined/.test(
|
|
220
|
+
if (issue2.code === "unrecognized_keys") return "UNKNOWN_KEY";
|
|
221
|
+
if (issue2.code === "too_big") return "LIMIT_EXCEEDED";
|
|
222
|
+
if (issue2.code === "invalid_type") {
|
|
223
|
+
return /received undefined/.test(issue2.message) ? "REQUIRED" : "INVALID_SHAPE";
|
|
159
224
|
}
|
|
160
225
|
return "INVALID_VALUE";
|
|
161
226
|
}
|
|
162
|
-
function flatten(
|
|
163
|
-
if (
|
|
227
|
+
function flatten(issue2, out, prefix = []) {
|
|
228
|
+
if (issue2.code === "invalid_union") {
|
|
164
229
|
let best;
|
|
165
|
-
for (const branch of
|
|
230
|
+
for (const branch of issue2.errors) {
|
|
166
231
|
if (best === void 0 || branch.length < best.length) {
|
|
167
232
|
best = branch;
|
|
168
233
|
}
|
|
169
234
|
}
|
|
170
235
|
if (best !== void 0) {
|
|
171
|
-
for (const inner of best) flatten(inner, out, [...prefix, ...
|
|
236
|
+
for (const inner of best) flatten(inner, out, [...prefix, ...issue2.path]);
|
|
172
237
|
return;
|
|
173
238
|
}
|
|
174
239
|
}
|
|
175
|
-
const path = [...prefix, ...
|
|
240
|
+
const path = [...prefix, ...issue2.path].map(
|
|
176
241
|
(p) => typeof p === "symbol" ? String(p) : p
|
|
177
242
|
);
|
|
178
|
-
out.push(makeIssue(path, codeFor(
|
|
243
|
+
out.push(makeIssue(path, codeFor(issue2), issue2.message));
|
|
179
244
|
}
|
|
180
245
|
function zodIssues(error) {
|
|
181
246
|
const out = [];
|
|
182
|
-
for (const
|
|
183
|
-
flatten(
|
|
247
|
+
for (const issue2 of error.issues) {
|
|
248
|
+
flatten(issue2, out);
|
|
184
249
|
if (out.length >= MAX_ISSUES) break;
|
|
185
250
|
}
|
|
186
251
|
return capIssues(dropShadowed(out));
|
|
@@ -194,7 +259,7 @@ function dropShadowed(issues) {
|
|
|
194
259
|
}
|
|
195
260
|
|
|
196
261
|
// src/document.ts
|
|
197
|
-
var
|
|
262
|
+
var import_zod4 = require("zod");
|
|
198
263
|
|
|
199
264
|
// src/walk.ts
|
|
200
265
|
function walkNodes(doc, visit) {
|
|
@@ -369,7 +434,7 @@ function checkLimits(doc) {
|
|
|
369
434
|
}
|
|
370
435
|
|
|
371
436
|
// src/site-settings.ts
|
|
372
|
-
var
|
|
437
|
+
var import_zod3 = require("zod");
|
|
373
438
|
|
|
374
439
|
// src/fonts.ts
|
|
375
440
|
var WEIGHTS_100_900 = [100, 200, 300, 400, 500, 600, 700, 800, 900];
|
|
@@ -426,7 +491,7 @@ var SITE_FONTS = FONT_DEFINITIONS;
|
|
|
426
491
|
var SITE_FONT_FAMILIES = FONT_DEFINITIONS.map((f) => f.family);
|
|
427
492
|
|
|
428
493
|
// src/safe.ts
|
|
429
|
-
var
|
|
494
|
+
var import_zod2 = require("zod");
|
|
430
495
|
var customIssue = (ctx, code, message, path = []) => {
|
|
431
496
|
ctx.addIssue({ code: "custom", message, path, params: { code } });
|
|
432
497
|
};
|
|
@@ -435,8 +500,8 @@ var FORBIDDEN_KEYS = /* @__PURE__ */ new Set([
|
|
|
435
500
|
"constructor",
|
|
436
501
|
"prototype"
|
|
437
502
|
]);
|
|
438
|
-
function safeRecord(value, keySchema =
|
|
439
|
-
return
|
|
503
|
+
function safeRecord(value, keySchema = import_zod2.z.string().max(64)) {
|
|
504
|
+
return import_zod2.z.preprocess((input, ctx) => {
|
|
440
505
|
if (typeof input === "object" && input !== null && !Array.isArray(input)) {
|
|
441
506
|
for (const key of Object.getOwnPropertyNames(input)) {
|
|
442
507
|
if (FORBIDDEN_KEYS.has(key)) {
|
|
@@ -450,7 +515,7 @@ function safeRecord(value, keySchema = import_zod.z.string().max(64)) {
|
|
|
450
515
|
}
|
|
451
516
|
}
|
|
452
517
|
return input;
|
|
453
|
-
},
|
|
518
|
+
}, import_zod2.z.record(keySchema, value));
|
|
454
519
|
}
|
|
455
520
|
|
|
456
521
|
// src/strings.ts
|
|
@@ -524,31 +589,31 @@ var CATALOG_STRING_KEYS = [
|
|
|
524
589
|
|
|
525
590
|
// src/site-settings.ts
|
|
526
591
|
var LOCALE_TAG_RE = /^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8}){0,3}$/;
|
|
527
|
-
var localeTagSchema =
|
|
528
|
-
var localizedTextSchema = safeRecord(
|
|
529
|
-
var assetIdSchema =
|
|
592
|
+
var localeTagSchema = import_zod3.z.string().regex(LOCALE_TAG_RE);
|
|
593
|
+
var localizedTextSchema = safeRecord(import_zod3.z.string().max(2e3), localeTagSchema);
|
|
594
|
+
var assetIdSchema = import_zod3.z.string().min(1).max(64).regex(/^[A-Za-z0-9_-]+$/);
|
|
530
595
|
var THROTTLE_PK_RE = /^pk_[a-z0-9]+_[A-Za-z0-9]{8,}$/;
|
|
531
596
|
var SAFE_HREF = (
|
|
532
597
|
// eslint-disable-next-line no-control-regex
|
|
533
598
|
/^(?:https?:\/\/[^\s\\\u0000-\u001F\u007F]+|\/(?![/\\])[^\s\\\u0000-\u001F\u007F]*)$/iu
|
|
534
599
|
);
|
|
535
600
|
var ULID_RE = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
536
|
-
var menuTargetSchema =
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
kind:
|
|
543
|
-
q:
|
|
601
|
+
var menuTargetSchema = import_zod3.z.discriminatedUnion("kind", [
|
|
602
|
+
import_zod3.z.object({ kind: import_zod3.z.literal("page"), id: import_zod3.z.string().regex(ULID_RE) }).strict(),
|
|
603
|
+
import_zod3.z.object({ kind: import_zod3.z.literal("category"), id: import_zod3.z.string().min(1).max(64) }).strict(),
|
|
604
|
+
import_zod3.z.object({ kind: import_zod3.z.literal("collection"), id: import_zod3.z.string().min(1).max(64) }).strict(),
|
|
605
|
+
import_zod3.z.object({ kind: import_zod3.z.literal("product"), id: import_zod3.z.string().min(1).max(64) }).strict(),
|
|
606
|
+
import_zod3.z.object({
|
|
607
|
+
kind: import_zod3.z.literal("search"),
|
|
608
|
+
q: import_zod3.z.string().min(1).max(200).regex(/^[^\u0000-\u001F\u007F]+$/u)
|
|
544
609
|
}).strict(),
|
|
545
|
-
|
|
546
|
-
kind:
|
|
547
|
-
href:
|
|
610
|
+
import_zod3.z.object({
|
|
611
|
+
kind: import_zod3.z.literal("url"),
|
|
612
|
+
href: import_zod3.z.string().max(2e3).regex(SAFE_HREF)
|
|
548
613
|
}).strict()
|
|
549
614
|
]);
|
|
550
615
|
var itemBase = { label: localizedTextSchema, target: menuTargetSchema };
|
|
551
|
-
var menuItemLevel3 =
|
|
616
|
+
var menuItemLevel3 = import_zod3.z.object({ ...itemBase, children: import_zod3.z.array(import_zod3.z.unknown()).optional() }).strict().superRefine((item, ctx) => {
|
|
552
617
|
if (item.children !== void 0 && item.children.length > 0) {
|
|
553
618
|
customIssue(
|
|
554
619
|
ctx,
|
|
@@ -558,8 +623,8 @@ var menuItemLevel3 = import_zod2.z.object({ ...itemBase, children: import_zod2.z
|
|
|
558
623
|
);
|
|
559
624
|
}
|
|
560
625
|
});
|
|
561
|
-
var menuItemLevel2 =
|
|
562
|
-
var menuItemLevel1 =
|
|
626
|
+
var menuItemLevel2 = import_zod3.z.object({ ...itemBase, children: import_zod3.z.array(menuItemLevel3).optional() }).strict();
|
|
627
|
+
var menuItemLevel1 = import_zod3.z.object({ ...itemBase, children: import_zod3.z.array(menuItemLevel2).optional() }).strict();
|
|
563
628
|
var menuItemSchema = menuItemLevel1;
|
|
564
629
|
function countItems(items) {
|
|
565
630
|
let count = 0;
|
|
@@ -569,7 +634,7 @@ function countItems(items) {
|
|
|
569
634
|
}
|
|
570
635
|
return count;
|
|
571
636
|
}
|
|
572
|
-
var menuSchema =
|
|
637
|
+
var menuSchema = import_zod3.z.array(menuItemSchema).superRefine((items, ctx) => {
|
|
573
638
|
if (countItems(items) > LIMITS.menuItems) {
|
|
574
639
|
customIssue(
|
|
575
640
|
ctx,
|
|
@@ -579,7 +644,7 @@ var menuSchema = import_zod2.z.array(menuItemSchema).superRefine((items, ctx) =>
|
|
|
579
644
|
}
|
|
580
645
|
});
|
|
581
646
|
var MENU_NAME_RE = /^[a-z][a-z0-9-]{0,39}$/;
|
|
582
|
-
var menusSchema =
|
|
647
|
+
var menusSchema = import_zod3.z.object({
|
|
583
648
|
header: menuSchema.default(() => []),
|
|
584
649
|
footer: menuSchema.default(() => [])
|
|
585
650
|
}).catchall(menuSchema).superRefine((menus, ctx) => {
|
|
@@ -599,8 +664,8 @@ var menusSchema = import_zod2.z.object({
|
|
|
599
664
|
);
|
|
600
665
|
}
|
|
601
666
|
});
|
|
602
|
-
var colour =
|
|
603
|
-
var paletteSchema =
|
|
667
|
+
var colour = import_zod3.z.string().regex(/^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/u);
|
|
668
|
+
var paletteSchema = import_zod3.z.object({
|
|
604
669
|
colorPrimary: colour,
|
|
605
670
|
colorPrimaryForeground: colour,
|
|
606
671
|
colorBackground: colour,
|
|
@@ -648,58 +713,58 @@ var defaultDark = () => ({
|
|
|
648
713
|
colorWarning: "#fbbf24",
|
|
649
714
|
colorDanger: "#f87171"
|
|
650
715
|
});
|
|
651
|
-
var fontFamily =
|
|
652
|
-
var themeSchema =
|
|
653
|
-
preset:
|
|
716
|
+
var fontFamily = import_zod3.z.enum(SITE_FONT_FAMILIES);
|
|
717
|
+
var themeSchema = import_zod3.z.object({
|
|
718
|
+
preset: import_zod3.z.enum(["clean", "editorial", "bold"]).default("clean"),
|
|
654
719
|
// When false the preset's tokens apply, when true the merchant's values do.
|
|
655
|
-
customized:
|
|
720
|
+
customized: import_zod3.z.boolean().default(false),
|
|
656
721
|
light: paletteSchema.default(defaultLight),
|
|
657
722
|
dark: paletteSchema.default(defaultDark),
|
|
658
|
-
fonts:
|
|
723
|
+
fonts: import_zod3.z.object({
|
|
659
724
|
heading: fontFamily.default("system-sans"),
|
|
660
725
|
body: fontFamily.default("system-sans"),
|
|
661
726
|
mono: fontFamily.default("system-mono")
|
|
662
727
|
}).strict().prefault({}),
|
|
663
|
-
radius:
|
|
664
|
-
spacingScale:
|
|
665
|
-
colorScheme:
|
|
728
|
+
radius: import_zod3.z.number().int().min(0).max(32).default(8),
|
|
729
|
+
spacingScale: import_zod3.z.enum(["compact", "comfortable", "roomy"]).default("comfortable"),
|
|
730
|
+
colorScheme: import_zod3.z.enum(["light", "dark", "auto"]).default("auto")
|
|
666
731
|
}).strict();
|
|
667
|
-
var brandSchema =
|
|
668
|
-
storeName:
|
|
669
|
-
logo:
|
|
732
|
+
var brandSchema = import_zod3.z.object({
|
|
733
|
+
storeName: import_zod3.z.string().max(100).regex(/^[^\u0000-\u001F\u007F]*$/u).default(""),
|
|
734
|
+
logo: import_zod3.z.object({
|
|
670
735
|
light: assetIdSchema.nullable().default(null),
|
|
671
736
|
dark: assetIdSchema.nullable().default(null)
|
|
672
737
|
}).strict().prefault({}),
|
|
673
738
|
favicon: assetIdSchema.nullable().default(null)
|
|
674
739
|
}).strict();
|
|
675
|
-
var isoInstant =
|
|
676
|
-
var announcementSchema =
|
|
677
|
-
enabled:
|
|
740
|
+
var isoInstant = import_zod3.z.iso.datetime({ offset: true });
|
|
741
|
+
var announcementSchema = import_zod3.z.object({
|
|
742
|
+
enabled: import_zod3.z.boolean().default(false),
|
|
678
743
|
message: localizedTextSchema.default({}),
|
|
679
744
|
target: menuTargetSchema.nullable().default(null),
|
|
680
745
|
startsAt: isoInstant.nullable().default(null),
|
|
681
746
|
endsAt: isoInstant.nullable().default(null)
|
|
682
747
|
}).strict();
|
|
683
|
-
var seoSchema =
|
|
684
|
-
titleTemplate:
|
|
748
|
+
var seoSchema = import_zod3.z.object({
|
|
749
|
+
titleTemplate: import_zod3.z.string().min(1).max(200).default("{page} \u2013 {store}"),
|
|
685
750
|
defaultDescription: localizedTextSchema.default({}),
|
|
686
751
|
shareImage: assetIdSchema.nullable().default(null)
|
|
687
752
|
}).strict();
|
|
688
|
-
var stringsSchema =
|
|
753
|
+
var stringsSchema = import_zod3.z.object(
|
|
689
754
|
Object.fromEntries(
|
|
690
755
|
SITE_STRING_KEYS.map((key) => [key, localizedTextSchema.optional()])
|
|
691
756
|
)
|
|
692
757
|
).strict();
|
|
693
|
-
var routesSchema =
|
|
694
|
-
page:
|
|
695
|
-
home:
|
|
758
|
+
var routesSchema = import_zod3.z.object({
|
|
759
|
+
page: import_zod3.z.string().max(200).regex(/^\/(?!\/)[^\s\\]*\{handle\}[^\s\\]*$/).default("/pages/{handle}"),
|
|
760
|
+
home: import_zod3.z.literal("/").default("/")
|
|
696
761
|
}).strict();
|
|
697
|
-
var connectionsSchema =
|
|
698
|
-
throttlePublishableKey:
|
|
762
|
+
var connectionsSchema = import_zod3.z.object({
|
|
763
|
+
throttlePublishableKey: import_zod3.z.string().regex(THROTTLE_PK_RE).nullable().default(null)
|
|
699
764
|
}).strict();
|
|
700
|
-
var localesSchema =
|
|
765
|
+
var localesSchema = import_zod3.z.object({
|
|
701
766
|
default: localeTagSchema.default("en"),
|
|
702
|
-
list:
|
|
767
|
+
list: import_zod3.z.array(localeTagSchema).min(1).max(50).default(["en"])
|
|
703
768
|
}).strict().superRefine((locales, ctx) => {
|
|
704
769
|
if (!locales.list.includes(locales.default)) {
|
|
705
770
|
customIssue(ctx, "INVALID_VALUE", "The default locale must be in the list", ["list"]);
|
|
@@ -709,8 +774,8 @@ var localesSchema = import_zod2.z.object({
|
|
|
709
774
|
}
|
|
710
775
|
});
|
|
711
776
|
var SITE_SETTINGS_SCHEMA_VERSION = 1;
|
|
712
|
-
var siteSettingsSchema =
|
|
713
|
-
schemaVersion:
|
|
777
|
+
var siteSettingsSchema = import_zod3.z.object({
|
|
778
|
+
schemaVersion: import_zod3.z.literal(1).default(1),
|
|
714
779
|
brand: brandSchema.prefault({}),
|
|
715
780
|
theme: themeSchema.prefault({}),
|
|
716
781
|
menus: menusSchema.prefault({}),
|
|
@@ -721,7 +786,7 @@ var siteSettingsSchema = import_zod2.z.object({
|
|
|
721
786
|
connections: connectionsSchema.prefault({}),
|
|
722
787
|
locales: localesSchema.prefault({}),
|
|
723
788
|
/** The store origin, for editor links and the default notice endpoint. */
|
|
724
|
-
storeUrl:
|
|
789
|
+
storeUrl: import_zod3.z.string().max(2e3).regex(/^https?:\/\/[^\s/\\]+$/i).nullable().default(null)
|
|
725
790
|
}).strict();
|
|
726
791
|
function defaultSiteSettings() {
|
|
727
792
|
return siteSettingsSchema.parse({});
|
|
@@ -756,7 +821,7 @@ function isSettingValue(value, depth = 0) {
|
|
|
756
821
|
return false;
|
|
757
822
|
}
|
|
758
823
|
}
|
|
759
|
-
var settingValueSchema =
|
|
824
|
+
var settingValueSchema = import_zod4.z.preprocess((input, ctx) => {
|
|
760
825
|
if (!isSettingValue(input)) {
|
|
761
826
|
ctx.addIssue({
|
|
762
827
|
code: "custom",
|
|
@@ -766,23 +831,24 @@ var settingValueSchema = import_zod3.z.preprocess((input, ctx) => {
|
|
|
766
831
|
return null;
|
|
767
832
|
}
|
|
768
833
|
return input;
|
|
769
|
-
},
|
|
770
|
-
var nodeIdSchema =
|
|
834
|
+
}, import_zod4.z.json());
|
|
835
|
+
var nodeIdSchema = import_zod4.z.string().regex(NODE_ID_RE);
|
|
771
836
|
var COLOR_VALUE_RE = /^(?:token:[a-z][a-z0-9-]{0,30}|#[0-9a-fA-F]{6}(?:[0-9a-fA-F]{2})?)$/;
|
|
772
|
-
var colorValueSchema =
|
|
837
|
+
var colorValueSchema = import_zod4.z.string().regex(COLOR_VALUE_RE);
|
|
773
838
|
var SPACING_TOKENS = ["none", "xs", "s", "m", "l", "xl", "2xl"];
|
|
774
|
-
var spacingValueSchema =
|
|
775
|
-
|
|
776
|
-
|
|
839
|
+
var spacingValueSchema = import_zod4.z.union([
|
|
840
|
+
import_zod4.z.enum(SPACING_TOKENS),
|
|
841
|
+
import_zod4.z.number().min(0).max(400)
|
|
777
842
|
]);
|
|
843
|
+
var COMMON_SETTING_KEYS = ["hidden", "anchor", "spacingTop", "spacingBottom"];
|
|
778
844
|
var ANCHOR_RE = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
|
|
779
|
-
var anchorSchema =
|
|
845
|
+
var anchorSchema = import_zod4.z.string().regex(ANCHOR_RE);
|
|
780
846
|
var VERTICAL_ALIGN = ["top", "center", "bottom", "stretch"];
|
|
781
|
-
var backgroundSchema =
|
|
847
|
+
var backgroundSchema = import_zod4.z.object({
|
|
782
848
|
color: colorValueSchema.optional(),
|
|
783
849
|
image: assetIdSchema.optional(),
|
|
784
850
|
overlay: colorValueSchema.optional(),
|
|
785
|
-
position:
|
|
851
|
+
position: import_zod4.z.enum([
|
|
786
852
|
"center",
|
|
787
853
|
"top",
|
|
788
854
|
"bottom",
|
|
@@ -793,33 +859,33 @@ var backgroundSchema = import_zod3.z.object({
|
|
|
793
859
|
"bottom left",
|
|
794
860
|
"bottom right"
|
|
795
861
|
]).optional(),
|
|
796
|
-
size:
|
|
862
|
+
size: import_zod4.z.enum(["cover", "contain", "auto"]).optional()
|
|
797
863
|
}).strict();
|
|
798
|
-
var sectionPropsSchema =
|
|
799
|
-
width:
|
|
800
|
-
contentWidth:
|
|
864
|
+
var sectionPropsSchema = import_zod4.z.object({
|
|
865
|
+
width: import_zod4.z.enum(["boxed", "full"]),
|
|
866
|
+
contentWidth: import_zod4.z.number().int().min(600).max(1600),
|
|
801
867
|
gap: spacingValueSchema,
|
|
802
868
|
paddingY: spacingValueSchema,
|
|
803
869
|
paddingX: spacingValueSchema,
|
|
804
|
-
minHeight:
|
|
805
|
-
verticalAlign:
|
|
870
|
+
minHeight: import_zod4.z.number().min(0).max(2e3),
|
|
871
|
+
verticalAlign: import_zod4.z.enum(VERTICAL_ALIGN),
|
|
806
872
|
background: backgroundSchema,
|
|
807
|
-
stackOrder:
|
|
808
|
-
hidden:
|
|
873
|
+
stackOrder: import_zod4.z.enum(["normal", "reverse"]),
|
|
874
|
+
hidden: import_zod4.z.boolean(),
|
|
809
875
|
anchor: anchorSchema
|
|
810
876
|
}).partial().strict();
|
|
811
|
-
var columnWidth =
|
|
877
|
+
var columnWidth = import_zod4.z.number().min(10).max(100).refine((w) => Math.abs(w * 100 - Math.round(w * 100)) < 1e-6, {
|
|
812
878
|
message: "A column width has at most 2 decimals"
|
|
813
879
|
});
|
|
814
880
|
var columnPropsShape = {
|
|
815
881
|
width: columnWidth,
|
|
816
|
-
verticalAlign:
|
|
882
|
+
verticalAlign: import_zod4.z.enum(VERTICAL_ALIGN),
|
|
817
883
|
padding: spacingValueSchema,
|
|
818
884
|
background: backgroundSchema,
|
|
819
|
-
hidden:
|
|
885
|
+
hidden: import_zod4.z.boolean(),
|
|
820
886
|
anchor: anchorSchema
|
|
821
887
|
};
|
|
822
|
-
var columnPropsSchema =
|
|
888
|
+
var columnPropsSchema = import_zod4.z.object({ ...columnPropsShape, width: columnPropsShape.width }).partial({ verticalAlign: true, padding: true, background: true, hidden: true, anchor: true }).strict();
|
|
823
889
|
function overridesOf(shape) {
|
|
824
890
|
const entries = Object.entries(shape).map(
|
|
825
891
|
([key, schema]) => [key, schema.nullable().optional()]
|
|
@@ -827,13 +893,13 @@ function overridesOf(shape) {
|
|
|
827
893
|
return Object.fromEntries(entries);
|
|
828
894
|
}
|
|
829
895
|
function responsiveOf(shape) {
|
|
830
|
-
const device =
|
|
831
|
-
return
|
|
896
|
+
const device = import_zod4.z.object(overridesOf(shape)).strict();
|
|
897
|
+
return import_zod4.z.object({ tablet: device.optional(), phone: device.optional() }).strict();
|
|
832
898
|
}
|
|
833
899
|
var sectionResponsive = responsiveOf(sectionPropsSchema.shape);
|
|
834
900
|
var columnResponsive = responsiveOf(columnPropsShape);
|
|
835
901
|
var WIDGET_TYPE_RE = /^(?:basic|catalog|commerce|custom)\.[a-z][a-z0-9-]{0,63}$/;
|
|
836
|
-
var widgetTypeSchema =
|
|
902
|
+
var widgetTypeSchema = import_zod4.z.string().superRefine((value, ctx) => {
|
|
837
903
|
if (value === "inner-section") {
|
|
838
904
|
customIssue(
|
|
839
905
|
ctx,
|
|
@@ -848,32 +914,32 @@ var widgetTypeSchema = import_zod3.z.string().superRefine((value, ctx) => {
|
|
|
848
914
|
);
|
|
849
915
|
}
|
|
850
916
|
});
|
|
851
|
-
var dataNameSchema =
|
|
852
|
-
var dataBindingSchema =
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
source:
|
|
917
|
+
var dataNameSchema = import_zod4.z.string().regex(/^[a-z][A-Za-z0-9_]{0,39}$/);
|
|
918
|
+
var dataBindingSchema = import_zod4.z.union([
|
|
919
|
+
import_zod4.z.object({ source: import_zod4.z.string().regex(/^context\.[a-z][a-z0-9-]{0,39}$/) }).strict(),
|
|
920
|
+
import_zod4.z.object({
|
|
921
|
+
source: import_zod4.z.string().regex(/^(?!context\.)[a-z][a-z0-9]*\.[a-z][a-z0-9-]{0,39}$/),
|
|
856
922
|
params: safeRecord(settingValueSchema)
|
|
857
923
|
}).strict()
|
|
858
924
|
]);
|
|
859
925
|
var settingsRecord = safeRecord(settingValueSchema);
|
|
860
|
-
var widgetNodeSchema =
|
|
926
|
+
var widgetNodeSchema = import_zod4.z.object({
|
|
861
927
|
id: nodeIdSchema,
|
|
862
928
|
type: widgetTypeSchema,
|
|
863
|
-
v:
|
|
929
|
+
v: import_zod4.z.number().int().min(1).max(9999),
|
|
864
930
|
props: settingsRecord,
|
|
865
|
-
responsive:
|
|
931
|
+
responsive: import_zod4.z.object({
|
|
866
932
|
tablet: safeRecord(settingValueSchema).optional(),
|
|
867
933
|
phone: safeRecord(settingValueSchema).optional()
|
|
868
934
|
}).strict().optional(),
|
|
869
935
|
data: safeRecord(dataBindingSchema, dataNameSchema).optional()
|
|
870
936
|
}).strict();
|
|
871
|
-
var innerColumnNodeSchema =
|
|
937
|
+
var innerColumnNodeSchema = import_zod4.z.object({
|
|
872
938
|
id: nodeIdSchema,
|
|
873
|
-
type:
|
|
939
|
+
type: import_zod4.z.literal("column"),
|
|
874
940
|
props: columnPropsSchema,
|
|
875
941
|
responsive: columnResponsive.optional(),
|
|
876
|
-
children:
|
|
942
|
+
children: import_zod4.z.array(widgetNodeSchema).max(LIMITS.childrenPerColumn)
|
|
877
943
|
}).strict();
|
|
878
944
|
function checkColumnWidths(columns, ctx) {
|
|
879
945
|
const sum = columns.reduce((total, column) => total + column.props.width, 0);
|
|
@@ -901,57 +967,57 @@ function checkColumnWidths(columns, ctx) {
|
|
|
901
967
|
}
|
|
902
968
|
}
|
|
903
969
|
var columnsLength = { min: LIMITS.columnsMin, max: LIMITS.columnsMax };
|
|
904
|
-
var innerSectionNodeSchema =
|
|
970
|
+
var innerSectionNodeSchema = import_zod4.z.object({
|
|
905
971
|
id: nodeIdSchema,
|
|
906
|
-
type:
|
|
972
|
+
type: import_zod4.z.literal("inner-section"),
|
|
907
973
|
props: sectionPropsSchema,
|
|
908
974
|
responsive: sectionResponsive.optional(),
|
|
909
|
-
columns:
|
|
975
|
+
columns: import_zod4.z.array(innerColumnNodeSchema).min(columnsLength.min).max(columnsLength.max)
|
|
910
976
|
}).strict().superRefine((section, ctx) => checkColumnWidths(section.columns, ctx));
|
|
911
|
-
var columnNodeSchema =
|
|
977
|
+
var columnNodeSchema = import_zod4.z.object({
|
|
912
978
|
id: nodeIdSchema,
|
|
913
|
-
type:
|
|
979
|
+
type: import_zod4.z.literal("column"),
|
|
914
980
|
props: columnPropsSchema,
|
|
915
981
|
responsive: columnResponsive.optional(),
|
|
916
|
-
children:
|
|
982
|
+
children: import_zod4.z.array(import_zod4.z.union([innerSectionNodeSchema, widgetNodeSchema])).max(LIMITS.childrenPerColumn)
|
|
917
983
|
}).strict();
|
|
918
|
-
var sectionNodeSchema =
|
|
984
|
+
var sectionNodeSchema = import_zod4.z.object({
|
|
919
985
|
id: nodeIdSchema,
|
|
920
|
-
type:
|
|
986
|
+
type: import_zod4.z.literal("section"),
|
|
921
987
|
props: sectionPropsSchema,
|
|
922
988
|
responsive: sectionResponsive.optional(),
|
|
923
|
-
columns:
|
|
989
|
+
columns: import_zod4.z.array(columnNodeSchema).min(columnsLength.min).max(columnsLength.max)
|
|
924
990
|
}).strict().superRefine((section, ctx) => checkColumnWidths(section.columns, ctx));
|
|
925
|
-
var assetUrl =
|
|
926
|
-
var frozenAssetSchema =
|
|
991
|
+
var assetUrl = import_zod4.z.string().max(2e3).regex(SAFE_HREF);
|
|
992
|
+
var frozenAssetSchema = import_zod4.z.object({
|
|
927
993
|
url: assetUrl,
|
|
928
|
-
width:
|
|
929
|
-
height:
|
|
930
|
-
variants:
|
|
931
|
-
|
|
994
|
+
width: import_zod4.z.number().int().min(1).max(2e4),
|
|
995
|
+
height: import_zod4.z.number().int().min(1).max(2e4),
|
|
996
|
+
variants: import_zod4.z.array(
|
|
997
|
+
import_zod4.z.object({ width: import_zod4.z.number().int().min(1).max(2e4), url: assetUrl }).strict()
|
|
932
998
|
).max(12)
|
|
933
999
|
}).strict();
|
|
934
1000
|
var CUSTOM_SNAPSHOT_KEY_RE = /^custom\.[a-z][a-z0-9-]{0,63}@[1-9][0-9]{0,3}$/;
|
|
935
|
-
var customWidgetSnapshotSchema =
|
|
936
|
-
kind:
|
|
937
|
-
type:
|
|
938
|
-
v:
|
|
939
|
-
title:
|
|
940
|
-
description:
|
|
941
|
-
icon:
|
|
942
|
-
group:
|
|
943
|
-
settings:
|
|
944
|
-
renamed:
|
|
945
|
-
removed:
|
|
1001
|
+
var customWidgetSnapshotSchema = import_zod4.z.object({
|
|
1002
|
+
kind: import_zod4.z.enum(["composite", "template"]),
|
|
1003
|
+
type: import_zod4.z.string().regex(/^custom\.[a-z][a-z0-9-]{0,63}$/),
|
|
1004
|
+
v: import_zod4.z.number().int().min(1).max(9999),
|
|
1005
|
+
title: import_zod4.z.string().max(100),
|
|
1006
|
+
description: import_zod4.z.string().max(500),
|
|
1007
|
+
icon: import_zod4.z.string().max(40),
|
|
1008
|
+
group: import_zod4.z.literal("custom"),
|
|
1009
|
+
settings: import_zod4.z.array(import_zod4.z.record(import_zod4.z.string(), import_zod4.z.unknown())).max(LIMITS.widgetSettings + COMMON_SETTING_KEYS.length),
|
|
1010
|
+
renamed: import_zod4.z.record(import_zod4.z.string(), import_zod4.z.string()).optional(),
|
|
1011
|
+
removed: import_zod4.z.array(import_zod4.z.string()).optional()
|
|
946
1012
|
}).loose();
|
|
947
1013
|
var CURRENT_SCHEMA_VERSION = 1;
|
|
948
|
-
var pageDocumentSchema =
|
|
949
|
-
schemaVersion:
|
|
950
|
-
sections:
|
|
1014
|
+
var pageDocumentSchema = import_zod4.z.object({
|
|
1015
|
+
schemaVersion: import_zod4.z.literal(1),
|
|
1016
|
+
sections: import_zod4.z.array(sectionNodeSchema).max(LIMITS.sections),
|
|
951
1017
|
// Published snapshots only.
|
|
952
1018
|
customWidgets: safeRecord(
|
|
953
1019
|
customWidgetSnapshotSchema,
|
|
954
|
-
|
|
1020
|
+
import_zod4.z.string().regex(CUSTOM_SNAPSHOT_KEY_RE)
|
|
955
1021
|
).optional(),
|
|
956
1022
|
assets: safeRecord(frozenAssetSchema, assetIdSchema).optional()
|
|
957
1023
|
}).strict().superRefine((doc, ctx) => {
|
|
@@ -1084,206 +1150,7 @@ function validateDocument(input) {
|
|
|
1084
1150
|
}
|
|
1085
1151
|
|
|
1086
1152
|
// src/widgets.ts
|
|
1087
|
-
var
|
|
1088
|
-
var WIDGET_GROUPS = ["layout", "basic", "catalog", "commerce", "custom"];
|
|
1089
|
-
var SETTING_TABS = ["content", "style", "advanced"];
|
|
1090
|
-
var SETTING_CONTROLS = [
|
|
1091
|
-
"text",
|
|
1092
|
-
"richtext",
|
|
1093
|
-
"number",
|
|
1094
|
-
"select",
|
|
1095
|
-
"toggle",
|
|
1096
|
-
"color",
|
|
1097
|
-
"spacing",
|
|
1098
|
-
"image",
|
|
1099
|
-
"link",
|
|
1100
|
-
"icon",
|
|
1101
|
-
"alignment",
|
|
1102
|
-
"font-size"
|
|
1103
|
-
];
|
|
1104
|
-
var DATA_RESULT_TYPES = [
|
|
1105
|
-
"product[]",
|
|
1106
|
-
"product",
|
|
1107
|
-
"collection",
|
|
1108
|
-
"category[]",
|
|
1109
|
-
"review[]",
|
|
1110
|
-
"customer",
|
|
1111
|
-
"cart"
|
|
1112
|
-
];
|
|
1113
|
-
var settingValue = settingValueSchema;
|
|
1114
|
-
var settingFieldSchema = import_zod4.z.object({
|
|
1115
|
-
key: import_zod4.z.string().regex(/^[a-z][A-Za-z0-9]{0,39}$/),
|
|
1116
|
-
label: import_zod4.z.string().min(1).max(100),
|
|
1117
|
-
tab: import_zod4.z.enum(SETTING_TABS),
|
|
1118
|
-
output: import_zod4.z.enum(["css", "markup"]),
|
|
1119
|
-
control: import_zod4.z.enum(SETTING_CONTROLS),
|
|
1120
|
-
translatable: import_zod4.z.boolean().optional(),
|
|
1121
|
-
default: settingValue,
|
|
1122
|
-
min: import_zod4.z.number().optional(),
|
|
1123
|
-
max: import_zod4.z.number().optional(),
|
|
1124
|
-
options: import_zod4.z.array(import_zod4.z.object({ value: import_zod4.z.string().max(60), label: import_zod4.z.string().max(100) }).strict()).max(50).optional(),
|
|
1125
|
-
maxLength: import_zod4.z.number().int().min(1).max(2e4).optional()
|
|
1126
|
-
}).strict();
|
|
1127
|
-
var fieldSpecSchema = import_zod4.z.object({
|
|
1128
|
-
/** A dotted path a template may read, such as `price.display`. */
|
|
1129
|
-
name: import_zod4.z.string().regex(/^[a-z][A-Za-z0-9]*(?:\.[a-z][A-Za-z0-9]*)*$/),
|
|
1130
|
-
type: import_zod4.z.enum(["string", "number", "boolean", "money", "image", "url", "date", "list"]),
|
|
1131
|
-
example: settingValue.optional()
|
|
1132
|
-
}).strict();
|
|
1133
|
-
var dataSourceDescriptorSchema = import_zod4.z.object({
|
|
1134
|
-
id: import_zod4.z.string().regex(/^[a-z][a-z0-9]*\.[a-z][a-z0-9-]*$/),
|
|
1135
|
-
provider: import_zod4.z.enum(["catalog", "commerce"]),
|
|
1136
|
-
title: import_zod4.z.string().min(1).max(100),
|
|
1137
|
-
description: import_zod4.z.string().max(500),
|
|
1138
|
-
params: import_zod4.z.array(settingFieldSchema).max(LIMITS.widgetSettings),
|
|
1139
|
-
result: import_zod4.z.enum(DATA_RESULT_TYPES),
|
|
1140
|
-
fields: import_zod4.z.array(fieldSpecSchema).max(200),
|
|
1141
|
-
perShopper: import_zod4.z.boolean(),
|
|
1142
|
-
cacheKind: import_zod4.z.enum(["listings", "product", "reviews", "none"])
|
|
1143
|
-
}).strict();
|
|
1144
|
-
var widgetDefinitionSchema = import_zod4.z.object({
|
|
1145
|
-
type: import_zod4.z.string().regex(WIDGET_TYPE_RE),
|
|
1146
|
-
v: import_zod4.z.number().int().min(1).max(9999),
|
|
1147
|
-
title: import_zod4.z.string().min(1).max(100),
|
|
1148
|
-
group: import_zod4.z.enum(WIDGET_GROUPS),
|
|
1149
|
-
icon: import_zod4.z.string().min(1).max(40),
|
|
1150
|
-
description: import_zod4.z.string().min(1).max(500),
|
|
1151
|
-
settings: import_zod4.z.array(settingFieldSchema).max(LIMITS.widgetSettings),
|
|
1152
|
-
data: import_zod4.z.record(
|
|
1153
|
-
import_zod4.z.string(),
|
|
1154
|
-
import_zod4.z.object({
|
|
1155
|
-
accepts: import_zod4.z.array(import_zod4.z.enum(DATA_RESULT_TYPES)).min(1),
|
|
1156
|
-
required: import_zod4.z.boolean(),
|
|
1157
|
-
default: dataBindingSchema.optional(),
|
|
1158
|
-
label: import_zod4.z.string().min(1).max(100)
|
|
1159
|
-
}).strict()
|
|
1160
|
-
).optional(),
|
|
1161
|
-
placement: import_zod4.z.object({
|
|
1162
|
-
slotsOnly: import_zod4.z.boolean().optional(),
|
|
1163
|
-
notIn: import_zod4.z.array(import_zod4.z.enum(["header", "footer", "slot"])).optional()
|
|
1164
|
-
}).strict().optional()
|
|
1165
|
-
}).strict();
|
|
1166
|
-
var SIZE_TOKENS = ["xs", "s", "m", "l", "xl", "2xl", "3xl"];
|
|
1167
|
-
var ALIGNMENTS = ["left", "center", "right", "justify"];
|
|
1168
|
-
var LOCALE_KEY = /^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8}){0,3}$/;
|
|
1169
|
-
function fitsText(value, field2) {
|
|
1170
|
-
const max = field2.maxLength ?? 2e3;
|
|
1171
|
-
if (field2.translatable === true) {
|
|
1172
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
1173
|
-
return "Expected a map of locale to text";
|
|
1174
|
-
}
|
|
1175
|
-
for (const [locale, text] of Object.entries(value)) {
|
|
1176
|
-
if (!LOCALE_KEY.test(locale)) return `"${locale}" is not a locale tag`;
|
|
1177
|
-
if (typeof text !== "string") return `The text for "${locale}" must be a string`;
|
|
1178
|
-
if (text.length > max) return `The text for "${locale}" is over ${String(max)} characters`;
|
|
1179
|
-
}
|
|
1180
|
-
return null;
|
|
1181
|
-
}
|
|
1182
|
-
if (typeof value !== "string") return "Expected text";
|
|
1183
|
-
if (value.length > max) return `Text is over ${String(max)} characters`;
|
|
1184
|
-
return null;
|
|
1185
|
-
}
|
|
1186
|
-
function validateSettingValue(field2, value) {
|
|
1187
|
-
if (value === null) {
|
|
1188
|
-
return field2.default === null ? null : "This setting cannot be empty";
|
|
1189
|
-
}
|
|
1190
|
-
switch (field2.control) {
|
|
1191
|
-
case "text":
|
|
1192
|
-
case "richtext":
|
|
1193
|
-
if (field2.key === "anchor") {
|
|
1194
|
-
return typeof value === "string" && ANCHOR_RE.test(value) ? null : "An anchor starts with a letter and uses letters, digits, - and _ (64 at most)";
|
|
1195
|
-
}
|
|
1196
|
-
return fitsText(value, field2);
|
|
1197
|
-
case "icon":
|
|
1198
|
-
return typeof value === "string" && /^[a-z][a-z0-9-]{0,39}$/.test(value) ? null : "Expected an icon name";
|
|
1199
|
-
case "number": {
|
|
1200
|
-
if (typeof value !== "number" || !Number.isFinite(value)) return "Expected a number";
|
|
1201
|
-
if (field2.min !== void 0 && value < field2.min) return `Must be at least ${String(field2.min)}`;
|
|
1202
|
-
if (field2.max !== void 0 && value > field2.max) return `Must be at most ${String(field2.max)}`;
|
|
1203
|
-
return null;
|
|
1204
|
-
}
|
|
1205
|
-
case "select":
|
|
1206
|
-
return typeof value === "string" && (field2.options ?? []).some((o) => o.value === value) ? null : "Expected one of the listed options";
|
|
1207
|
-
case "toggle":
|
|
1208
|
-
return typeof value === "boolean" ? null : "Expected true or false";
|
|
1209
|
-
case "color":
|
|
1210
|
-
return typeof value === "string" && COLOR_VALUE_RE.test(value) ? null : "Expected token:name or #rrggbb[aa]";
|
|
1211
|
-
case "spacing":
|
|
1212
|
-
return typeof value === "string" && SPACING_TOKENS.includes(value) || typeof value === "number" && value >= 0 && value <= 400 ? null : "Expected a spacing token or 0 to 400 px";
|
|
1213
|
-
case "font-size":
|
|
1214
|
-
return typeof value === "string" && SIZE_TOKENS.includes(value) || typeof value === "number" && value >= 8 && value <= 200 ? null : "Expected a size token or 8 to 200 px";
|
|
1215
|
-
case "alignment":
|
|
1216
|
-
return typeof value === "string" && ALIGNMENTS.includes(value) ? null : "Expected an alignment";
|
|
1217
|
-
case "image":
|
|
1218
|
-
return typeof value === "string" && /^[A-Za-z0-9_-]{1,64}$/.test(value) ? null : "Expected a media asset id";
|
|
1219
|
-
case "link":
|
|
1220
|
-
return menuTargetSchema.safeParse(value).success ? null : "Expected a link target";
|
|
1221
|
-
default:
|
|
1222
|
-
return "Unknown control";
|
|
1223
|
-
}
|
|
1224
|
-
}
|
|
1225
|
-
function validateWidgetProps(def, props) {
|
|
1226
|
-
const byKey = new Map(def.settings.map((s) => [s.key, s]));
|
|
1227
|
-
const problems = [];
|
|
1228
|
-
for (const [key, value] of Object.entries(props)) {
|
|
1229
|
-
const field2 = byKey.get(key);
|
|
1230
|
-
if (field2 === void 0) {
|
|
1231
|
-
problems.push({ path: key, message: `Unknown setting "${key}"` });
|
|
1232
|
-
continue;
|
|
1233
|
-
}
|
|
1234
|
-
const message = validateSettingValue(field2, value);
|
|
1235
|
-
if (message !== null) problems.push({ path: key, message });
|
|
1236
|
-
}
|
|
1237
|
-
return problems;
|
|
1238
|
-
}
|
|
1239
|
-
function definitionOfSnapshot(snapshot) {
|
|
1240
|
-
const { type, v, title, description, icon, settings } = snapshot;
|
|
1241
|
-
const data = snapshot.data;
|
|
1242
|
-
return {
|
|
1243
|
-
type,
|
|
1244
|
-
v,
|
|
1245
|
-
title,
|
|
1246
|
-
group: "custom",
|
|
1247
|
-
icon,
|
|
1248
|
-
description,
|
|
1249
|
-
settings,
|
|
1250
|
-
...data === void 0 ? {} : { data }
|
|
1251
|
-
};
|
|
1252
|
-
}
|
|
1253
|
-
function createRegistry(defs, sources, custom = []) {
|
|
1254
|
-
const byType = /* @__PURE__ */ new Map();
|
|
1255
|
-
const add = (def) => {
|
|
1256
|
-
let versions = byType.get(def.type);
|
|
1257
|
-
if (versions === void 0) {
|
|
1258
|
-
versions = /* @__PURE__ */ new Map();
|
|
1259
|
-
byType.set(def.type, versions);
|
|
1260
|
-
}
|
|
1261
|
-
if (versions.has(def.v)) {
|
|
1262
|
-
throw new Error(`Duplicate widget definition ${def.type}@${String(def.v)}`);
|
|
1263
|
-
}
|
|
1264
|
-
versions.set(def.v, def);
|
|
1265
|
-
};
|
|
1266
|
-
for (const def of defs) add(def);
|
|
1267
|
-
for (const snapshot of custom) add(definitionOfSnapshot(snapshot));
|
|
1268
|
-
const sourcesById = /* @__PURE__ */ new Map();
|
|
1269
|
-
for (const source of sources) {
|
|
1270
|
-
if (sourcesById.has(source.id)) throw new Error(`Duplicate data source ${source.id}`);
|
|
1271
|
-
sourcesById.set(source.id, source);
|
|
1272
|
-
}
|
|
1273
|
-
const latest = (type) => {
|
|
1274
|
-
const versions = byType.get(type);
|
|
1275
|
-
if (versions === void 0) return void 0;
|
|
1276
|
-
let best;
|
|
1277
|
-
for (const def of versions.values()) if (best === void 0 || def.v > best.v) best = def;
|
|
1278
|
-
return best;
|
|
1279
|
-
};
|
|
1280
|
-
return {
|
|
1281
|
-
widget: (type, v) => byType.get(type)?.get(v),
|
|
1282
|
-
latest,
|
|
1283
|
-
source: (id) => sourcesById.get(id),
|
|
1284
|
-
all: () => [...byType.keys()].sort().map((type) => latest(type)).filter((def) => def !== void 0)
|
|
1285
|
-
};
|
|
1286
|
-
}
|
|
1153
|
+
var import_zod5 = require("zod");
|
|
1287
1154
|
|
|
1288
1155
|
// src/basics.ts
|
|
1289
1156
|
var opts = (...values) => values.map((value) => ({ value, label: value.charAt(0).toUpperCase() + value.slice(1) }));
|
|
@@ -1620,3 +1487,3452 @@ var BASIC_WIDGET_DEFINITIONS = [
|
|
|
1620
1487
|
size("s")
|
|
1621
1488
|
])
|
|
1622
1489
|
];
|
|
1490
|
+
|
|
1491
|
+
// src/widgets.ts
|
|
1492
|
+
var WIDGET_GROUPS = ["layout", "basic", "catalog", "commerce", "custom"];
|
|
1493
|
+
var SETTING_TABS = ["content", "style", "advanced"];
|
|
1494
|
+
var SETTING_CONTROLS = [
|
|
1495
|
+
"text",
|
|
1496
|
+
"richtext",
|
|
1497
|
+
"number",
|
|
1498
|
+
"select",
|
|
1499
|
+
"toggle",
|
|
1500
|
+
"color",
|
|
1501
|
+
"spacing",
|
|
1502
|
+
"image",
|
|
1503
|
+
"link",
|
|
1504
|
+
"icon",
|
|
1505
|
+
"alignment",
|
|
1506
|
+
"font-size"
|
|
1507
|
+
];
|
|
1508
|
+
var DATA_RESULT_TYPES = [
|
|
1509
|
+
"product[]",
|
|
1510
|
+
"product",
|
|
1511
|
+
"collection",
|
|
1512
|
+
"category[]",
|
|
1513
|
+
"review[]",
|
|
1514
|
+
"customer",
|
|
1515
|
+
"cart"
|
|
1516
|
+
];
|
|
1517
|
+
var settingValue = settingValueSchema;
|
|
1518
|
+
var CSS_CONTROLS = ["color", "spacing", "font-size", "alignment", "number", "toggle", "select"];
|
|
1519
|
+
var CSS_OPTION_RE = /^[a-z0-9][a-z0-9:-]{0,39}$/;
|
|
1520
|
+
var settingFieldSchema = import_zod5.z.object({
|
|
1521
|
+
key: import_zod5.z.string().regex(/^[a-z][A-Za-z0-9]{0,39}$/),
|
|
1522
|
+
label: import_zod5.z.string().min(1).max(100),
|
|
1523
|
+
tab: import_zod5.z.enum(SETTING_TABS),
|
|
1524
|
+
output: import_zod5.z.enum(["css", "markup"]),
|
|
1525
|
+
control: import_zod5.z.enum(SETTING_CONTROLS),
|
|
1526
|
+
translatable: import_zod5.z.boolean().optional(),
|
|
1527
|
+
default: settingValue,
|
|
1528
|
+
min: import_zod5.z.number().optional(),
|
|
1529
|
+
max: import_zod5.z.number().optional(),
|
|
1530
|
+
options: import_zod5.z.array(import_zod5.z.object({ value: import_zod5.z.string().max(60), label: import_zod5.z.string().max(100) }).strict()).max(50).optional(),
|
|
1531
|
+
maxLength: import_zod5.z.number().int().min(1).max(2e4).optional()
|
|
1532
|
+
}).strict().superRefine((field2, ctx) => {
|
|
1533
|
+
if (field2.output !== "css") return;
|
|
1534
|
+
if (!CSS_CONTROLS.includes(field2.control) || field2.translatable === true) {
|
|
1535
|
+
customIssue(
|
|
1536
|
+
ctx,
|
|
1537
|
+
"INVALID_VALUE",
|
|
1538
|
+
`A css setting uses the ${CSS_CONTROLS.join(", ")} control, not ${field2.control}`,
|
|
1539
|
+
["control"]
|
|
1540
|
+
);
|
|
1541
|
+
return;
|
|
1542
|
+
}
|
|
1543
|
+
(field2.options ?? []).forEach((option, i) => {
|
|
1544
|
+
if (!CSS_OPTION_RE.test(option.value)) {
|
|
1545
|
+
customIssue(ctx, "INVALID_VALUE", "An option of a css setting is lower case letters, digits, - and :", ["options", i, "value"]);
|
|
1546
|
+
}
|
|
1547
|
+
});
|
|
1548
|
+
const problem = validateSettingValue(field2, field2.default);
|
|
1549
|
+
if (problem !== null) customIssue(ctx, "INVALID_VALUE", `The default is invalid: ${problem}`, ["default"]);
|
|
1550
|
+
});
|
|
1551
|
+
var fieldSpecSchema = import_zod5.z.object({
|
|
1552
|
+
/** A dotted path a template may read, such as `price.display`. */
|
|
1553
|
+
name: import_zod5.z.string().regex(/^[a-z][A-Za-z0-9]*(?:\.[a-z][A-Za-z0-9]*)*$/),
|
|
1554
|
+
type: import_zod5.z.enum(["string", "number", "boolean", "money", "image", "url", "date", "list"]),
|
|
1555
|
+
example: settingValue.optional()
|
|
1556
|
+
}).strict();
|
|
1557
|
+
var dataSourceDescriptorSchema = import_zod5.z.object({
|
|
1558
|
+
id: import_zod5.z.string().regex(/^[a-z][a-z0-9]*\.[a-z][a-z0-9-]*$/),
|
|
1559
|
+
provider: import_zod5.z.enum(["catalog", "commerce"]),
|
|
1560
|
+
title: import_zod5.z.string().min(1).max(100),
|
|
1561
|
+
description: import_zod5.z.string().max(500),
|
|
1562
|
+
params: import_zod5.z.array(settingFieldSchema).max(LIMITS.widgetSettings),
|
|
1563
|
+
result: import_zod5.z.enum(DATA_RESULT_TYPES),
|
|
1564
|
+
fields: import_zod5.z.array(fieldSpecSchema).max(200),
|
|
1565
|
+
perShopper: import_zod5.z.boolean(),
|
|
1566
|
+
cacheKind: import_zod5.z.enum(["listings", "product", "reviews", "none"])
|
|
1567
|
+
}).strict();
|
|
1568
|
+
var widgetDefinitionSchema = import_zod5.z.object({
|
|
1569
|
+
type: import_zod5.z.string().regex(WIDGET_TYPE_RE),
|
|
1570
|
+
v: import_zod5.z.number().int().min(1).max(9999),
|
|
1571
|
+
title: import_zod5.z.string().min(1).max(100),
|
|
1572
|
+
group: import_zod5.z.enum(WIDGET_GROUPS),
|
|
1573
|
+
icon: import_zod5.z.string().min(1).max(40),
|
|
1574
|
+
description: import_zod5.z.string().min(1).max(500),
|
|
1575
|
+
// The common props (hidden, anchor, spacing) do not count against the cap.
|
|
1576
|
+
settings: import_zod5.z.array(settingFieldSchema).max(LIMITS.widgetSettings + COMMON_SETTING_KEYS.length),
|
|
1577
|
+
data: import_zod5.z.record(
|
|
1578
|
+
import_zod5.z.string(),
|
|
1579
|
+
import_zod5.z.object({
|
|
1580
|
+
accepts: import_zod5.z.array(import_zod5.z.enum(DATA_RESULT_TYPES)).min(1),
|
|
1581
|
+
required: import_zod5.z.boolean(),
|
|
1582
|
+
default: dataBindingSchema.optional(),
|
|
1583
|
+
label: import_zod5.z.string().min(1).max(100)
|
|
1584
|
+
}).strict()
|
|
1585
|
+
).optional(),
|
|
1586
|
+
placement: import_zod5.z.object({
|
|
1587
|
+
slotsOnly: import_zod5.z.boolean().optional(),
|
|
1588
|
+
notIn: import_zod5.z.array(import_zod5.z.enum(["header", "footer", "slot"])).optional(),
|
|
1589
|
+
/** Not allowed inside an inner section. */
|
|
1590
|
+
topLevelOnly: import_zod5.z.boolean().optional()
|
|
1591
|
+
}).strict().optional()
|
|
1592
|
+
}).strict().superRefine((def, ctx) => {
|
|
1593
|
+
const own = def.settings.filter(
|
|
1594
|
+
(f) => !COMMON_SETTING_KEYS.includes(f.key)
|
|
1595
|
+
).length;
|
|
1596
|
+
if (own > LIMITS.widgetSettings) {
|
|
1597
|
+
customIssue(ctx, "LIMIT_EXCEEDED", `A widget has at most ${String(LIMITS.widgetSettings)} settings`, ["settings"]);
|
|
1598
|
+
}
|
|
1599
|
+
});
|
|
1600
|
+
var SIZE_TOKENS = ["xs", "s", "m", "l", "xl", "2xl", "3xl"];
|
|
1601
|
+
var ALIGNMENTS = ["left", "center", "right", "justify"];
|
|
1602
|
+
var LOCALE_KEY = /^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8}){0,3}$/;
|
|
1603
|
+
function fitsText(value, field2) {
|
|
1604
|
+
const max = field2.maxLength ?? 2e3;
|
|
1605
|
+
if (field2.translatable === true) {
|
|
1606
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
1607
|
+
return "Expected a map of locale to text";
|
|
1608
|
+
}
|
|
1609
|
+
for (const [locale, text] of Object.entries(value)) {
|
|
1610
|
+
if (!LOCALE_KEY.test(locale)) return `"${locale}" is not a locale tag`;
|
|
1611
|
+
if (typeof text !== "string") return `The text for "${locale}" must be a string`;
|
|
1612
|
+
if (text.length > max) return `The text for "${locale}" is over ${String(max)} characters`;
|
|
1613
|
+
}
|
|
1614
|
+
return null;
|
|
1615
|
+
}
|
|
1616
|
+
if (typeof value !== "string") return "Expected text";
|
|
1617
|
+
if (value.length > max) return `Text is over ${String(max)} characters`;
|
|
1618
|
+
return null;
|
|
1619
|
+
}
|
|
1620
|
+
function validateSettingValue(field2, value) {
|
|
1621
|
+
if (value === null) {
|
|
1622
|
+
return field2.default === null ? null : "This setting cannot be empty";
|
|
1623
|
+
}
|
|
1624
|
+
switch (field2.control) {
|
|
1625
|
+
case "text":
|
|
1626
|
+
case "richtext":
|
|
1627
|
+
if (field2.key === "anchor") {
|
|
1628
|
+
return typeof value === "string" && ANCHOR_RE.test(value) ? null : "An anchor starts with a letter and uses letters, digits, - and _ (64 at most)";
|
|
1629
|
+
}
|
|
1630
|
+
return fitsText(value, field2);
|
|
1631
|
+
case "icon":
|
|
1632
|
+
return typeof value === "string" && /^[a-z][a-z0-9-]{0,39}$/.test(value) ? null : "Expected an icon name";
|
|
1633
|
+
case "number": {
|
|
1634
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return "Expected a number";
|
|
1635
|
+
if (field2.min !== void 0 && value < field2.min) return `Must be at least ${String(field2.min)}`;
|
|
1636
|
+
if (field2.max !== void 0 && value > field2.max) return `Must be at most ${String(field2.max)}`;
|
|
1637
|
+
return null;
|
|
1638
|
+
}
|
|
1639
|
+
case "select":
|
|
1640
|
+
return typeof value === "string" && (field2.options ?? []).some((o) => o.value === value) ? null : "Expected one of the listed options";
|
|
1641
|
+
case "toggle":
|
|
1642
|
+
return typeof value === "boolean" ? null : "Expected true or false";
|
|
1643
|
+
case "color":
|
|
1644
|
+
return typeof value === "string" && COLOR_VALUE_RE.test(value) ? null : "Expected token:name or #rrggbb[aa]";
|
|
1645
|
+
case "spacing":
|
|
1646
|
+
return typeof value === "string" && SPACING_TOKENS.includes(value) || typeof value === "number" && value >= 0 && value <= 400 ? null : "Expected a spacing token or 0 to 400 px";
|
|
1647
|
+
case "font-size":
|
|
1648
|
+
return typeof value === "string" && SIZE_TOKENS.includes(value) || typeof value === "number" && value >= 8 && value <= 200 ? null : "Expected a size token or 8 to 200 px";
|
|
1649
|
+
case "alignment":
|
|
1650
|
+
return typeof value === "string" && ALIGNMENTS.includes(value) ? null : "Expected an alignment";
|
|
1651
|
+
case "image":
|
|
1652
|
+
return typeof value === "string" && /^[A-Za-z0-9_-]{1,64}$/.test(value) ? null : "Expected a media asset id";
|
|
1653
|
+
case "link":
|
|
1654
|
+
return menuTargetSchema.safeParse(value).success ? null : "Expected a link target";
|
|
1655
|
+
default:
|
|
1656
|
+
return "Unknown control";
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
function validateWidgetProps(def, props) {
|
|
1660
|
+
const byKey = new Map([...COMMON_WIDGET_SETTINGS, ...def.settings].map((s) => [s.key, s]));
|
|
1661
|
+
const problems = [];
|
|
1662
|
+
for (const [key, value] of Object.entries(props)) {
|
|
1663
|
+
const field2 = byKey.get(key);
|
|
1664
|
+
if (field2 === void 0) {
|
|
1665
|
+
problems.push({ path: key, message: `Unknown setting "${key}"` });
|
|
1666
|
+
continue;
|
|
1667
|
+
}
|
|
1668
|
+
const message = validateSettingValue(field2, value);
|
|
1669
|
+
if (message !== null) problems.push({ path: key, message });
|
|
1670
|
+
}
|
|
1671
|
+
return problems;
|
|
1672
|
+
}
|
|
1673
|
+
var snapshotPlacementSchema = import_zod5.z.object({ topLevelOnly: import_zod5.z.boolean().optional() }).strict();
|
|
1674
|
+
function snapshotPlacement(snapshot) {
|
|
1675
|
+
const raw2 = snapshot.placement;
|
|
1676
|
+
if (raw2 === void 0) return void 0;
|
|
1677
|
+
const parsed = snapshotPlacementSchema.safeParse(raw2);
|
|
1678
|
+
if (!parsed.success) throw new Error(`Invalid placement on ${snapshot.type}@${String(snapshot.v)}`);
|
|
1679
|
+
return parsed.data;
|
|
1680
|
+
}
|
|
1681
|
+
function definitionOfSnapshot(snapshot) {
|
|
1682
|
+
const { type, v, title, description, icon, settings, kind } = snapshot;
|
|
1683
|
+
const data = snapshot.data;
|
|
1684
|
+
const placement = snapshotPlacement(snapshot);
|
|
1685
|
+
return {
|
|
1686
|
+
type,
|
|
1687
|
+
kind,
|
|
1688
|
+
v,
|
|
1689
|
+
title,
|
|
1690
|
+
group: "custom",
|
|
1691
|
+
icon,
|
|
1692
|
+
description,
|
|
1693
|
+
settings,
|
|
1694
|
+
...placement === void 0 ? {} : { placement },
|
|
1695
|
+
...data === void 0 ? {} : { data },
|
|
1696
|
+
...snapshot.renamed === void 0 ? {} : { renamed: snapshot.renamed },
|
|
1697
|
+
...snapshot.removed === void 0 ? {} : { removed: snapshot.removed }
|
|
1698
|
+
};
|
|
1699
|
+
}
|
|
1700
|
+
function createRegistry(defs, sources, custom = []) {
|
|
1701
|
+
const byType = /* @__PURE__ */ new Map();
|
|
1702
|
+
const add = (def) => {
|
|
1703
|
+
let versions = byType.get(def.type);
|
|
1704
|
+
if (versions === void 0) {
|
|
1705
|
+
versions = /* @__PURE__ */ new Map();
|
|
1706
|
+
byType.set(def.type, versions);
|
|
1707
|
+
}
|
|
1708
|
+
if (versions.has(def.v)) {
|
|
1709
|
+
throw new Error(`Duplicate widget definition ${def.type}@${String(def.v)}`);
|
|
1710
|
+
}
|
|
1711
|
+
versions.set(def.v, def);
|
|
1712
|
+
};
|
|
1713
|
+
for (const def of defs) add(def);
|
|
1714
|
+
for (const snapshot of custom) add(definitionOfSnapshot(snapshot));
|
|
1715
|
+
const sourcesById = /* @__PURE__ */ new Map();
|
|
1716
|
+
for (const source of sources) {
|
|
1717
|
+
if (sourcesById.has(source.id)) throw new Error(`Duplicate data source ${source.id}`);
|
|
1718
|
+
sourcesById.set(source.id, source);
|
|
1719
|
+
}
|
|
1720
|
+
const latest = (type) => {
|
|
1721
|
+
const versions = byType.get(type);
|
|
1722
|
+
if (versions === void 0) return void 0;
|
|
1723
|
+
let best;
|
|
1724
|
+
for (const def of versions.values()) if (best === void 0 || def.v > best.v) best = def;
|
|
1725
|
+
return best;
|
|
1726
|
+
};
|
|
1727
|
+
return {
|
|
1728
|
+
widget: (type, v) => byType.get(type)?.get(v),
|
|
1729
|
+
latest,
|
|
1730
|
+
source: (id) => sourcesById.get(id),
|
|
1731
|
+
sources: () => [...sourcesById.values()],
|
|
1732
|
+
all: () => [...byType.keys()].sort().map((type) => latest(type)).filter((def) => def !== void 0)
|
|
1733
|
+
};
|
|
1734
|
+
}
|
|
1735
|
+
|
|
1736
|
+
// src/tree.ts
|
|
1737
|
+
var isRec = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1738
|
+
var has = (rec, key) => Object.prototype.hasOwnProperty.call(rec, key);
|
|
1739
|
+
function findPath(doc, id) {
|
|
1740
|
+
let found;
|
|
1741
|
+
walkNodes(doc, (node, path) => {
|
|
1742
|
+
if (found === void 0 && node.id === id) found = path;
|
|
1743
|
+
});
|
|
1744
|
+
return found;
|
|
1745
|
+
}
|
|
1746
|
+
function getAt(root, path) {
|
|
1747
|
+
let current = root;
|
|
1748
|
+
for (const key of path) {
|
|
1749
|
+
if (typeof current !== "object" || current === null) return void 0;
|
|
1750
|
+
current = current[key];
|
|
1751
|
+
}
|
|
1752
|
+
return current;
|
|
1753
|
+
}
|
|
1754
|
+
function setAt(root, path, value) {
|
|
1755
|
+
const [key, ...rest] = path;
|
|
1756
|
+
if (key === void 0) return value;
|
|
1757
|
+
const child = root[key];
|
|
1758
|
+
const next = rest.length === 0 ? value : setAt(child, rest, value);
|
|
1759
|
+
if (Array.isArray(root)) {
|
|
1760
|
+
const copy = root.slice();
|
|
1761
|
+
copy[key] = next;
|
|
1762
|
+
return copy;
|
|
1763
|
+
}
|
|
1764
|
+
return { ...root, [key]: next };
|
|
1765
|
+
}
|
|
1766
|
+
function insertAtIndex(items, index, item) {
|
|
1767
|
+
return [...items.slice(0, index), item, ...items.slice(index)];
|
|
1768
|
+
}
|
|
1769
|
+
function parentOf(doc, path) {
|
|
1770
|
+
const arrayPath = path.slice(0, -1);
|
|
1771
|
+
const index = path[path.length - 1];
|
|
1772
|
+
if (path.length === 2) return { parentId: "root", arrayPath, index };
|
|
1773
|
+
const parent = getAt(doc, path.slice(0, -2));
|
|
1774
|
+
return { parentId: parent.id, arrayPath, index };
|
|
1775
|
+
}
|
|
1776
|
+
function widgetsIn(node, path = [], inner = false) {
|
|
1777
|
+
if (!isRec(node)) return [];
|
|
1778
|
+
if (node.type === "section" || node.type === "inner-section") {
|
|
1779
|
+
if (!Array.isArray(node.columns)) return [];
|
|
1780
|
+
const within = inner || node.type === "inner-section";
|
|
1781
|
+
return node.columns.flatMap((column, j) => widgetsIn(column, [...path, "columns", j], within));
|
|
1782
|
+
}
|
|
1783
|
+
if (node.type === "column") {
|
|
1784
|
+
if (!Array.isArray(node.children)) return [];
|
|
1785
|
+
return node.children.flatMap((child, k) => widgetsIn(child, [...path, "children", k], inner));
|
|
1786
|
+
}
|
|
1787
|
+
return [{ node, path, inner }];
|
|
1788
|
+
}
|
|
1789
|
+
function idsIn(node) {
|
|
1790
|
+
if (!isRec(node)) return [];
|
|
1791
|
+
const own = typeof node.id === "string" ? [node.id] : [];
|
|
1792
|
+
const nested = Array.isArray(node.columns) ? node.columns : Array.isArray(node.children) ? node.children : [];
|
|
1793
|
+
return [...own, ...nested.flatMap(idsIn)];
|
|
1794
|
+
}
|
|
1795
|
+
function collectIds(doc) {
|
|
1796
|
+
const ids = /* @__PURE__ */ new Set();
|
|
1797
|
+
walkNodes(doc, (node) => ids.add(node.id));
|
|
1798
|
+
return ids;
|
|
1799
|
+
}
|
|
1800
|
+
function freshId(base, suffix, used) {
|
|
1801
|
+
const root = base.slice(0, 24 - suffix.length - 3);
|
|
1802
|
+
for (let n = 0; ; n += 1) {
|
|
1803
|
+
const candidate = `${root}${suffix}${n === 0 ? "" : String(n)}`;
|
|
1804
|
+
if (!used.has(candidate)) {
|
|
1805
|
+
used.add(candidate);
|
|
1806
|
+
return candidate;
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
function cloneJson(value) {
|
|
1811
|
+
if (Array.isArray(value)) return value.map(cloneJson);
|
|
1812
|
+
if (typeof value === "object" && value !== null) {
|
|
1813
|
+
const out = {};
|
|
1814
|
+
for (const [key, item] of Object.entries(value)) out[key] = cloneJson(item);
|
|
1815
|
+
return out;
|
|
1816
|
+
}
|
|
1817
|
+
return value;
|
|
1818
|
+
}
|
|
1819
|
+
function deepEqual(a, b) {
|
|
1820
|
+
if (a === b) return true;
|
|
1821
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
1822
|
+
return Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((x, i) => deepEqual(x, b[i]));
|
|
1823
|
+
}
|
|
1824
|
+
if (isRec(a) && isRec(b)) {
|
|
1825
|
+
const keys = Object.keys(a);
|
|
1826
|
+
return keys.length === Object.keys(b).length && keys.every((k) => has(b, k) && deepEqual(a[k], b[k]));
|
|
1827
|
+
}
|
|
1828
|
+
return false;
|
|
1829
|
+
}
|
|
1830
|
+
|
|
1831
|
+
// src/placement.ts
|
|
1832
|
+
var refuse = (code, message) => ({ ok: false, code, message });
|
|
1833
|
+
var PART_OF_KIND = {
|
|
1834
|
+
"part:header": "header",
|
|
1835
|
+
"part:footer": "footer",
|
|
1836
|
+
slot: "slot"
|
|
1837
|
+
};
|
|
1838
|
+
function placementOf(type, ctx, v) {
|
|
1839
|
+
const def = (v === void 0 ? void 0 : ctx.registry.widget(type, v)) ?? ctx.registry.latest(type);
|
|
1840
|
+
return def === void 0 ? void 0 : { title: def.title, placement: def.placement };
|
|
1841
|
+
}
|
|
1842
|
+
function checkWidgetPart(type, ctx, v) {
|
|
1843
|
+
const found = placementOf(type, ctx, v);
|
|
1844
|
+
const placement = found?.placement;
|
|
1845
|
+
if (found === void 0 || placement === void 0) return { ok: true };
|
|
1846
|
+
if (placement.slotsOnly === true && ctx.documentKind !== "slot") {
|
|
1847
|
+
return refuse("NOT_ALLOWED_IN_PART", `${found.title} can only be used in a slot.`);
|
|
1848
|
+
}
|
|
1849
|
+
if (ctx.documentKind !== "page") {
|
|
1850
|
+
const part = PART_OF_KIND[ctx.documentKind];
|
|
1851
|
+
if (placement.notIn?.includes(part) === true) {
|
|
1852
|
+
return refuse(
|
|
1853
|
+
"NOT_ALLOWED_IN_PART",
|
|
1854
|
+
`${found.title} can't be used in the ${part === "slot" ? "slot" : `site ${part}`}.`
|
|
1855
|
+
);
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
return { ok: true };
|
|
1859
|
+
}
|
|
1860
|
+
function checkTopLevel(type, ctx, v) {
|
|
1861
|
+
const found = placementOf(type, ctx, v);
|
|
1862
|
+
if (found?.placement?.topLevelOnly === true) {
|
|
1863
|
+
return refuse(
|
|
1864
|
+
"INNER_NOT_TOP_LEVEL",
|
|
1865
|
+
`${found.title} can't go inside an inner section. Put it in a column of a section.`
|
|
1866
|
+
);
|
|
1867
|
+
}
|
|
1868
|
+
return { ok: true };
|
|
1869
|
+
}
|
|
1870
|
+
var startsWith = (path, prefix) => prefix.length <= path.length && prefix.every((key, i) => path[i] === key);
|
|
1871
|
+
function canPlace(doc, node, parentId, ctx) {
|
|
1872
|
+
const { type } = node;
|
|
1873
|
+
if (type === "column") {
|
|
1874
|
+
return refuse(
|
|
1875
|
+
"COLUMN_REMOVE_NEEDS_SETCOLUMNS",
|
|
1876
|
+
"Columns are added, removed and resized with the column controls."
|
|
1877
|
+
);
|
|
1878
|
+
}
|
|
1879
|
+
const own = node.id === void 0 ? void 0 : findPath(doc, node.id);
|
|
1880
|
+
const parentPath = parentId === "root" ? [] : findPath(doc, parentId);
|
|
1881
|
+
if (own !== void 0 && parentPath !== void 0 && (parentId === node.id || startsWith(parentPath, own))) {
|
|
1882
|
+
return refuse("MOVE_INTO_SELF", "A block can't be moved into itself.");
|
|
1883
|
+
}
|
|
1884
|
+
const isWidget = type !== "section" && type !== "inner-section";
|
|
1885
|
+
if (parentId === "root") {
|
|
1886
|
+
if (type === "inner-section") {
|
|
1887
|
+
return refuse(
|
|
1888
|
+
"INNER_NOT_TOP_LEVEL",
|
|
1889
|
+
"An inner section goes inside a column of a section, not on the page itself."
|
|
1890
|
+
);
|
|
1891
|
+
}
|
|
1892
|
+
return isWidget ? checkWidgetPart(type, ctx, node.v) : { ok: true };
|
|
1893
|
+
}
|
|
1894
|
+
const parent = parentPath === void 0 ? void 0 : getAt(doc, parentPath);
|
|
1895
|
+
if (!isRec(parent) || parent.type !== "column") {
|
|
1896
|
+
if (type === "inner-section") {
|
|
1897
|
+
return refuse(
|
|
1898
|
+
"INNER_NOT_TOP_LEVEL",
|
|
1899
|
+
"An inner section goes inside a column of a section."
|
|
1900
|
+
);
|
|
1901
|
+
}
|
|
1902
|
+
return refuse("SECTION_NOT_NESTABLE", "Blocks can only be placed inside a column.");
|
|
1903
|
+
}
|
|
1904
|
+
const inner = parentPath.length > 4;
|
|
1905
|
+
if (type === "section") {
|
|
1906
|
+
return refuse(
|
|
1907
|
+
"SECTION_NOT_NESTABLE",
|
|
1908
|
+
"A section can't go inside another section. Use an inner section."
|
|
1909
|
+
);
|
|
1910
|
+
}
|
|
1911
|
+
if (type === "inner-section" && inner) {
|
|
1912
|
+
return refuse("INNER_IN_INNER", "An inner section can't go inside another inner section.");
|
|
1913
|
+
}
|
|
1914
|
+
if (isWidget && inner) {
|
|
1915
|
+
const top = checkTopLevel(type, ctx, node.v);
|
|
1916
|
+
if (!top.ok) return top;
|
|
1917
|
+
}
|
|
1918
|
+
const children = parent.children;
|
|
1919
|
+
if (Array.isArray(children)) {
|
|
1920
|
+
const alreadyThere = own !== void 0 && children.some((child) => isRec(child) && child.id === node.id);
|
|
1921
|
+
const count = alreadyThere ? children.length - 1 : children.length;
|
|
1922
|
+
if (count >= LIMITS.childrenPerColumn) {
|
|
1923
|
+
return refuse("COLUMN_LIMIT", `This column is full (${String(LIMITS.childrenPerColumn)} blocks at most).`);
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
return isWidget ? checkWidgetPart(type, ctx, node.v) : { ok: true };
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1929
|
+
// src/ops.ts
|
|
1930
|
+
var at = (i, ...rest) => ["ops", i, ...rest];
|
|
1931
|
+
var fail = (path, code, message) => ({ issues: [makeIssue(path, code, message)] });
|
|
1932
|
+
var issue = (path, code, message) => makeIssue(path, code, message);
|
|
1933
|
+
var asNode = (value) => value;
|
|
1934
|
+
var DEVICES = ["tablet", "phone"];
|
|
1935
|
+
var SECTION_KEYS = new Set(Object.keys(sectionPropsSchema.shape));
|
|
1936
|
+
var COLUMN_KEYS = new Set(Object.keys(columnPropsSchema.shape));
|
|
1937
|
+
function propIssues(def, props, path, skipNull, common2 = false) {
|
|
1938
|
+
const fields = common2 ? [...def.settings, ...COMMON_WIDGET_SETTINGS] : def.settings;
|
|
1939
|
+
const byKey = new Map(fields.map((s) => [s.key, s]));
|
|
1940
|
+
const out = [];
|
|
1941
|
+
for (const [key, value] of Object.entries(props)) {
|
|
1942
|
+
if (FORBIDDEN_KEYS.has(key)) {
|
|
1943
|
+
out.push(issue([...path, key], "FORBIDDEN_KEY", `The key "${key}" is not allowed`));
|
|
1944
|
+
continue;
|
|
1945
|
+
}
|
|
1946
|
+
const field2 = byKey.get(key);
|
|
1947
|
+
if (field2 === void 0) {
|
|
1948
|
+
out.push(issue([...path, key], "UNKNOWN_PROP", `"${key}" is not a setting of ${def.title}`));
|
|
1949
|
+
continue;
|
|
1950
|
+
}
|
|
1951
|
+
if (value === null && skipNull) continue;
|
|
1952
|
+
if (!isSettingValue(value)) {
|
|
1953
|
+
out.push(issue([...path, key], "INVALID_VALUE", "Expected a JSON value"));
|
|
1954
|
+
continue;
|
|
1955
|
+
}
|
|
1956
|
+
const message = validateSettingValue(field2, value);
|
|
1957
|
+
if (message !== null) out.push(issue([...path, key], "INVALID_VALUE", message));
|
|
1958
|
+
}
|
|
1959
|
+
return out;
|
|
1960
|
+
}
|
|
1961
|
+
function bindingIssues(def, name, binding, ctx, path) {
|
|
1962
|
+
const input = def.data?.[name];
|
|
1963
|
+
if (input === void 0) {
|
|
1964
|
+
return [issue(path, "UNKNOWN_PROP", `${def.title} has no data input "${name}"`)];
|
|
1965
|
+
}
|
|
1966
|
+
if (!isRec(binding) || typeof binding.source !== "string") {
|
|
1967
|
+
return [issue(path, "INVALID_SHAPE", "A binding is { source, params }")];
|
|
1968
|
+
}
|
|
1969
|
+
const { source: id } = binding;
|
|
1970
|
+
if (id.startsWith("context.")) return [];
|
|
1971
|
+
const source = ctx.registry.source(id);
|
|
1972
|
+
if (source === void 0 || !ctx.enabledSources.has(id)) {
|
|
1973
|
+
return [issue([...path, "source"], "SOURCE_NOT_ENABLED", `The data source "${id}" is not enabled`)];
|
|
1974
|
+
}
|
|
1975
|
+
const out = [];
|
|
1976
|
+
if (isRec(binding.params)) {
|
|
1977
|
+
out.push(...propIssues({ settings: source.params, title: source.title }, binding.params, [...path, "params"], false));
|
|
1978
|
+
}
|
|
1979
|
+
if (!input.accepts.includes(source.result)) {
|
|
1980
|
+
out.push(
|
|
1981
|
+
issue(
|
|
1982
|
+
path,
|
|
1983
|
+
"RESULT_TYPE_MISMATCH",
|
|
1984
|
+
`${input.label} takes ${input.accepts.join(" or ")}, but ${source.title} gives ${source.result}`
|
|
1985
|
+
)
|
|
1986
|
+
);
|
|
1987
|
+
}
|
|
1988
|
+
return out;
|
|
1989
|
+
}
|
|
1990
|
+
function widgetIssues(widget, ctx, path, inner = false) {
|
|
1991
|
+
const { type, v } = widget;
|
|
1992
|
+
if (typeof type !== "string" || typeof v !== "number") {
|
|
1993
|
+
return [issue(path, "INVALID_SHAPE", "A widget has a type and a version")];
|
|
1994
|
+
}
|
|
1995
|
+
const def = ctx.registry.widget(type, v);
|
|
1996
|
+
if (def === void 0) {
|
|
1997
|
+
return [
|
|
1998
|
+
issue(path, "INVALID_WIDGET_TYPE", `The widget "${type}" version ${String(v)} is not available`)
|
|
1999
|
+
];
|
|
2000
|
+
}
|
|
2001
|
+
const out = [];
|
|
2002
|
+
const part = checkWidgetPart(type, ctx, v);
|
|
2003
|
+
if (!part.ok) out.push(issue(path, part.code, part.message));
|
|
2004
|
+
if (inner) {
|
|
2005
|
+
const top = checkTopLevel(type, ctx, v);
|
|
2006
|
+
if (!top.ok) out.push(issue(path, top.code, top.message));
|
|
2007
|
+
}
|
|
2008
|
+
if (isRec(widget.props)) out.push(...propIssues(def, widget.props, [...path, "props"], false, true));
|
|
2009
|
+
if (isRec(widget.responsive)) {
|
|
2010
|
+
for (const device of DEVICES) {
|
|
2011
|
+
const overrides = widget.responsive[device];
|
|
2012
|
+
if (isRec(overrides)) {
|
|
2013
|
+
out.push(...propIssues(def, overrides, [...path, "responsive", device], true, true));
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
}
|
|
2017
|
+
if (isRec(widget.data)) {
|
|
2018
|
+
for (const [name, binding] of Object.entries(widget.data)) {
|
|
2019
|
+
out.push(...bindingIssues(def, name, binding, ctx, [...path, "data", name]));
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2022
|
+
return out;
|
|
2023
|
+
}
|
|
2024
|
+
function patchProps(node, changes) {
|
|
2025
|
+
const props = { ...isRec(node.props) ? node.props : {} };
|
|
2026
|
+
for (const [key, value] of Object.entries(changes)) {
|
|
2027
|
+
if (value === null) delete props[key];
|
|
2028
|
+
else props[key] = value;
|
|
2029
|
+
}
|
|
2030
|
+
return { ...node, props };
|
|
2031
|
+
}
|
|
2032
|
+
function patchResponsive(node, device, changes) {
|
|
2033
|
+
const responsive = { ...isRec(node.responsive) ? node.responsive : {} };
|
|
2034
|
+
const overrides = { ...isRec(responsive[device]) ? responsive[device] : {} };
|
|
2035
|
+
for (const [key, value] of Object.entries(changes)) {
|
|
2036
|
+
if (value === null) delete overrides[key];
|
|
2037
|
+
else overrides[key] = value;
|
|
2038
|
+
}
|
|
2039
|
+
if (Object.keys(overrides).length === 0) delete responsive[device];
|
|
2040
|
+
else responsive[device] = overrides;
|
|
2041
|
+
const next = { ...node };
|
|
2042
|
+
if (Object.keys(responsive).length === 0) delete next.responsive;
|
|
2043
|
+
else next.responsive = responsive;
|
|
2044
|
+
return next;
|
|
2045
|
+
}
|
|
2046
|
+
var without = (rec, key) => {
|
|
2047
|
+
const copy = { ...rec };
|
|
2048
|
+
delete copy[key];
|
|
2049
|
+
return copy;
|
|
2050
|
+
};
|
|
2051
|
+
function target(doc, id, i, field2 = "id") {
|
|
2052
|
+
const path = typeof id === "string" ? findPath(doc, id) : void 0;
|
|
2053
|
+
if (path === void 0) {
|
|
2054
|
+
return fail(at(i, field2), "NODE_NOT_FOUND", `There is no node with id "${String(id)}"`);
|
|
2055
|
+
}
|
|
2056
|
+
return { path, node: asNode(getAt(doc, path)) };
|
|
2057
|
+
}
|
|
2058
|
+
var isFailure = (value) => "issues" in value;
|
|
2059
|
+
function definitionOf(node, registry, i, field2) {
|
|
2060
|
+
const def = typeof node.type === "string" && typeof node.v === "number" ? registry.widget(node.type, node.v) : void 0;
|
|
2061
|
+
return def ?? fail(at(i, field2), "INVALID_WIDGET_TYPE", "This widget's definition is not available");
|
|
2062
|
+
}
|
|
2063
|
+
function wrapperOf(widget, used) {
|
|
2064
|
+
const sectionId = freshId(widget.id, "s", used);
|
|
2065
|
+
const columnId = freshId(widget.id, "c", used);
|
|
2066
|
+
return {
|
|
2067
|
+
id: sectionId,
|
|
2068
|
+
type: "section",
|
|
2069
|
+
props: {},
|
|
2070
|
+
columns: [{ id: columnId, type: "column", props: { width: 100 }, children: [widget] }]
|
|
2071
|
+
};
|
|
2072
|
+
}
|
|
2073
|
+
function childArray(doc, parentId) {
|
|
2074
|
+
if (parentId === "root") return { arrayPath: ["sections"], length: doc.sections.length };
|
|
2075
|
+
const path = findPath(doc, parentId);
|
|
2076
|
+
const parent = path === void 0 ? void 0 : getAt(doc, path);
|
|
2077
|
+
if (!isRec(parent) || !Array.isArray(parent.children)) return void 0;
|
|
2078
|
+
return { arrayPath: [...path, "children"], length: parent.children.length };
|
|
2079
|
+
}
|
|
2080
|
+
function stepInsert(doc, op, ctx, i) {
|
|
2081
|
+
const node = op.node;
|
|
2082
|
+
if (!isRec(node) || typeof node.type !== "string" || typeof node.id !== "string") {
|
|
2083
|
+
return fail(at(i, "node"), "INVALID_SHAPE", "A node has an id and a type");
|
|
2084
|
+
}
|
|
2085
|
+
if (op.parentId !== "root" && findPath(doc, op.parentId) === void 0) {
|
|
2086
|
+
return fail(at(i, "parentId"), "NODE_NOT_FOUND", `There is no node with id "${String(op.parentId)}"`);
|
|
2087
|
+
}
|
|
2088
|
+
const placement = canPlace(doc, { type: node.type, v: node.v }, op.parentId, ctx);
|
|
2089
|
+
if (!placement.ok) return fail(at(i, "parentId"), placement.code, placement.message);
|
|
2090
|
+
const used = collectIds(doc);
|
|
2091
|
+
const issues = [];
|
|
2092
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2093
|
+
for (const id of idsIn(node)) {
|
|
2094
|
+
if (used.has(id) || seen.has(id)) {
|
|
2095
|
+
issues.push(issue(at(i, "node"), "ID_NOT_FRESH", `The id "${id}" is already used in this document`));
|
|
2096
|
+
}
|
|
2097
|
+
seen.add(id);
|
|
2098
|
+
}
|
|
2099
|
+
for (const w of widgetsIn(node)) {
|
|
2100
|
+
issues.push(...widgetIssues(w.node, ctx, at(i, "node", ...w.path), w.inner));
|
|
2101
|
+
}
|
|
2102
|
+
if (issues.length > 0) return { issues };
|
|
2103
|
+
const container = childArray(doc, op.parentId);
|
|
2104
|
+
if (container === void 0) {
|
|
2105
|
+
return fail(at(i, "parentId"), "SECTION_NOT_NESTABLE", "Blocks can only be placed inside a column.");
|
|
2106
|
+
}
|
|
2107
|
+
if (!Number.isInteger(op.index) || op.index < 0 || op.index > container.length) {
|
|
2108
|
+
return fail(at(i, "index"), "INVALID_VALUE", `index must be 0 to ${String(container.length)}`);
|
|
2109
|
+
}
|
|
2110
|
+
const wrap = op.parentId === "root" && node.type !== "section";
|
|
2111
|
+
const item = wrap ? wrapperOf(node, /* @__PURE__ */ new Set([...used, ...seen])) : node;
|
|
2112
|
+
const list = getAt(doc, container.arrayPath);
|
|
2113
|
+
return {
|
|
2114
|
+
doc: setAt(doc, container.arrayPath, insertAtIndex(list, op.index, item)),
|
|
2115
|
+
inverse: [{ op: "remove", id: item.id }]
|
|
2116
|
+
};
|
|
2117
|
+
}
|
|
2118
|
+
function stepRemove(doc, op, i) {
|
|
2119
|
+
const found = target(doc, op.id, i);
|
|
2120
|
+
if (isFailure(found)) return found;
|
|
2121
|
+
if (found.node.type === "column") {
|
|
2122
|
+
return fail(
|
|
2123
|
+
at(i, "id"),
|
|
2124
|
+
"COLUMN_REMOVE_NEEDS_SETCOLUMNS",
|
|
2125
|
+
"A column is removed with the column controls (setColumns)."
|
|
2126
|
+
);
|
|
2127
|
+
}
|
|
2128
|
+
const { parentId, arrayPath, index } = parentOf(doc, found.path);
|
|
2129
|
+
const list = getAt(doc, arrayPath);
|
|
2130
|
+
return {
|
|
2131
|
+
doc: setAt(doc, arrayPath, list.filter((_, k) => k !== index)),
|
|
2132
|
+
inverse: [{ op: "insert", parentId, index, node: found.node }]
|
|
2133
|
+
};
|
|
2134
|
+
}
|
|
2135
|
+
function stepMove(doc, op, ctx, i) {
|
|
2136
|
+
const found = target(doc, op.id, i);
|
|
2137
|
+
if (isFailure(found)) return found;
|
|
2138
|
+
const node = found.node;
|
|
2139
|
+
if (op.parentId !== "root" && findPath(doc, op.parentId) === void 0) {
|
|
2140
|
+
return fail(at(i, "parentId"), "NODE_NOT_FOUND", `There is no node with id "${String(op.parentId)}"`);
|
|
2141
|
+
}
|
|
2142
|
+
const placement = canPlace(doc, { type: node.type, id: op.id, v: node.v }, op.parentId, ctx);
|
|
2143
|
+
if (!placement.ok) return fail(at(i, "parentId"), placement.code, placement.message);
|
|
2144
|
+
const from = parentOf(doc, found.path);
|
|
2145
|
+
const removed = setAt(
|
|
2146
|
+
doc,
|
|
2147
|
+
from.arrayPath,
|
|
2148
|
+
getAt(doc, from.arrayPath).filter((_, k) => k !== from.index)
|
|
2149
|
+
);
|
|
2150
|
+
const container = childArray(removed, op.parentId);
|
|
2151
|
+
if (container === void 0) {
|
|
2152
|
+
return fail(at(i, "parentId"), "SECTION_NOT_NESTABLE", "Blocks can only be placed inside a column.");
|
|
2153
|
+
}
|
|
2154
|
+
if (!Number.isInteger(op.index) || op.index < 0 || op.index > container.length) {
|
|
2155
|
+
return fail(at(i, "index"), "INVALID_VALUE", `index must be 0 to ${String(container.length)}`);
|
|
2156
|
+
}
|
|
2157
|
+
const wrap = op.parentId === "root" && node.type !== "section";
|
|
2158
|
+
const item = wrap ? wrapperOf(node, collectIds(doc)) : node;
|
|
2159
|
+
const list = getAt(removed, container.arrayPath);
|
|
2160
|
+
const inverse = [{ op: "move", id: op.id, parentId: from.parentId, index: from.index }];
|
|
2161
|
+
if (wrap) inverse.push({ op: "remove", id: item.id });
|
|
2162
|
+
return { doc: setAt(removed, container.arrayPath, insertAtIndex(list, op.index, item)), inverse };
|
|
2163
|
+
}
|
|
2164
|
+
function allowedKeys(node, registry, i) {
|
|
2165
|
+
if (node.type === "section" || node.type === "inner-section") return { keys: SECTION_KEYS };
|
|
2166
|
+
if (node.type === "column") return { keys: COLUMN_KEYS };
|
|
2167
|
+
const def = definitionOf(node, registry, i, "id");
|
|
2168
|
+
if (isFailure(def)) return def;
|
|
2169
|
+
return {
|
|
2170
|
+
keys: new Set([...def.settings, ...COMMON_WIDGET_SETTINGS].map((s) => s.key)),
|
|
2171
|
+
def
|
|
2172
|
+
};
|
|
2173
|
+
}
|
|
2174
|
+
function stepProps(doc, op, ctx, i) {
|
|
2175
|
+
const found = target(doc, op.id, i);
|
|
2176
|
+
if (isFailure(found)) return found;
|
|
2177
|
+
if (!isRec(op.props)) return fail(at(i, "props"), "INVALID_SHAPE", "props is an object");
|
|
2178
|
+
if (op.op === "setResponsive" && !DEVICES.includes(op.device)) {
|
|
2179
|
+
return fail(at(i, "device"), "INVALID_VALUE", 'device is "tablet" or "phone"');
|
|
2180
|
+
}
|
|
2181
|
+
const allowed = allowedKeys(found.node, ctx.registry, i);
|
|
2182
|
+
if (isFailure(allowed)) return allowed;
|
|
2183
|
+
const issues = [];
|
|
2184
|
+
for (const key of Object.keys(op.props)) {
|
|
2185
|
+
if (FORBIDDEN_KEYS.has(key)) {
|
|
2186
|
+
issues.push(issue(at(i, "props", key), "FORBIDDEN_KEY", `The key "${key}" is not allowed`));
|
|
2187
|
+
} else if (!allowed.keys.has(key)) {
|
|
2188
|
+
issues.push(issue(at(i, "props", key), "UNKNOWN_PROP", `"${key}" is not a setting of this ${String(found.node.type)}`));
|
|
2189
|
+
}
|
|
2190
|
+
}
|
|
2191
|
+
if (issues.length === 0 && allowed.def !== void 0) {
|
|
2192
|
+
issues.push(...propIssues(allowed.def, op.props, at(i, "props"), true, true));
|
|
2193
|
+
} else if (issues.length === 0) {
|
|
2194
|
+
for (const [key, value] of Object.entries(op.props)) {
|
|
2195
|
+
if (value !== null && !isSettingValue(value)) {
|
|
2196
|
+
issues.push(issue(at(i, "props", key), "INVALID_VALUE", "Expected a JSON value"));
|
|
2197
|
+
}
|
|
2198
|
+
}
|
|
2199
|
+
}
|
|
2200
|
+
if (issues.length > 0) return { issues };
|
|
2201
|
+
const keys = Object.keys(op.props);
|
|
2202
|
+
const previous = op.op === "setProps" ? found.node.props : isRec(found.node.responsive) && isRec(found.node.responsive[op.device]) ? found.node.responsive[op.device] : {};
|
|
2203
|
+
const restore = {};
|
|
2204
|
+
for (const key of keys) restore[key] = has(previous, key) ? previous[key] : null;
|
|
2205
|
+
const next = op.op === "setProps" ? patchProps(found.node, op.props) : patchResponsive(found.node, op.device, op.props);
|
|
2206
|
+
const inverse = op.op === "setProps" ? { op: "setProps", id: op.id, props: restore } : { op: "setResponsive", id: op.id, device: op.device, props: restore };
|
|
2207
|
+
return { doc: setAt(doc, found.path, next), inverse: [inverse] };
|
|
2208
|
+
}
|
|
2209
|
+
var widthOverride = (column, device) => {
|
|
2210
|
+
const overrides = isRec(column.responsive) ? column.responsive[device] : void 0;
|
|
2211
|
+
const width = isRec(overrides) ? overrides.width : void 0;
|
|
2212
|
+
return typeof width === "number" ? width : void 0;
|
|
2213
|
+
};
|
|
2214
|
+
var clearWidthOverrides = (column) => patchResponsive(patchResponsive(column, "tablet", { width: null }), "phone", { width: null });
|
|
2215
|
+
function restoreWidthOverrides(columns) {
|
|
2216
|
+
const ops = [];
|
|
2217
|
+
for (const device of ["tablet", "phone"]) {
|
|
2218
|
+
for (const column of columns) {
|
|
2219
|
+
const width = widthOverride(column, device);
|
|
2220
|
+
if (width !== void 0) {
|
|
2221
|
+
ops.push({ op: "setResponsive", id: column.id, device, props: { width } });
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
}
|
|
2225
|
+
return ops;
|
|
2226
|
+
}
|
|
2227
|
+
function restoreColumnLooks(column) {
|
|
2228
|
+
const ops = [];
|
|
2229
|
+
const id = column.id;
|
|
2230
|
+
const props = without(column.props, "width");
|
|
2231
|
+
if (Object.keys(props).length > 0) {
|
|
2232
|
+
ops.push({ op: "setProps", id, props });
|
|
2233
|
+
}
|
|
2234
|
+
for (const device of ["tablet", "phone"]) {
|
|
2235
|
+
const overrides = isRec(column.responsive) ? column.responsive[device] : void 0;
|
|
2236
|
+
const rest = isRec(overrides) ? without(overrides, "width") : {};
|
|
2237
|
+
if (Object.keys(rest).length > 0) {
|
|
2238
|
+
ops.push({ op: "setResponsive", id, device, props: rest });
|
|
2239
|
+
}
|
|
2240
|
+
}
|
|
2241
|
+
return ops;
|
|
2242
|
+
}
|
|
2243
|
+
function stepSetColumns(doc, op, i) {
|
|
2244
|
+
const found = target(doc, op.sectionId, i, "sectionId");
|
|
2245
|
+
if (isFailure(found)) return found;
|
|
2246
|
+
const section = found.node;
|
|
2247
|
+
if (section.type !== "section" && section.type !== "inner-section") {
|
|
2248
|
+
return fail(at(i, "sectionId"), "INVALID_VALUE", "setColumns needs a section or an inner section");
|
|
2249
|
+
}
|
|
2250
|
+
const columns = section.columns;
|
|
2251
|
+
const widths = op.widths;
|
|
2252
|
+
if (!Array.isArray(widths) || widths.some((w) => typeof w !== "number" || !Number.isFinite(w))) {
|
|
2253
|
+
return fail(at(i, "widths"), "INVALID_SHAPE", "widths is a list of numbers");
|
|
2254
|
+
}
|
|
2255
|
+
if (widths.length < LIMITS.columnsMin || widths.length > LIMITS.columnsMax) {
|
|
2256
|
+
return fail(
|
|
2257
|
+
at(i, "widths"),
|
|
2258
|
+
"COLUMN_LIMIT",
|
|
2259
|
+
`A section has ${String(LIMITS.columnsMin)} to ${String(LIMITS.columnsMax)} columns`
|
|
2260
|
+
);
|
|
2261
|
+
}
|
|
2262
|
+
const nums = widths;
|
|
2263
|
+
if (op.device !== void 0) {
|
|
2264
|
+
if (!DEVICES.includes(op.device)) {
|
|
2265
|
+
return fail(at(i, "device"), "INVALID_VALUE", 'device is "tablet" or "phone"');
|
|
2266
|
+
}
|
|
2267
|
+
if (nums.length !== columns.length) {
|
|
2268
|
+
return fail(
|
|
2269
|
+
at(i, "widths"),
|
|
2270
|
+
"INVALID_COLUMN_WIDTHS",
|
|
2271
|
+
`A ${op.device} width override must set every column of the section (${String(columns.length)})`
|
|
2272
|
+
);
|
|
2273
|
+
}
|
|
2274
|
+
const next = columns.map(
|
|
2275
|
+
(column, j) => patchResponsive(column, op.device, { width: nums[j] })
|
|
2276
|
+
);
|
|
2277
|
+
return {
|
|
2278
|
+
doc: setAt(doc, [...found.path, "columns"], next),
|
|
2279
|
+
inverse: columns.map((column) => ({
|
|
2280
|
+
op: "setResponsive",
|
|
2281
|
+
id: column.id,
|
|
2282
|
+
device: op.device,
|
|
2283
|
+
props: { width: widthOverride(column, op.device) ?? null }
|
|
2284
|
+
}))
|
|
2285
|
+
};
|
|
2286
|
+
}
|
|
2287
|
+
if (op.moveChildrenTo !== void 0 && op.moveChildrenTo !== "previous") {
|
|
2288
|
+
return fail(at(i, "moveChildrenTo"), "INVALID_VALUE", 'moveChildrenTo is "previous"');
|
|
2289
|
+
}
|
|
2290
|
+
const before = columns.map((column) => column.props.width);
|
|
2291
|
+
const n = nums.length;
|
|
2292
|
+
const m = columns.length;
|
|
2293
|
+
if (n === m) {
|
|
2294
|
+
return {
|
|
2295
|
+
doc: setAt(
|
|
2296
|
+
doc,
|
|
2297
|
+
[...found.path, "columns"],
|
|
2298
|
+
columns.map((column, j) => patchProps(column, { width: nums[j] }))
|
|
2299
|
+
),
|
|
2300
|
+
inverse: [{ op: "setColumns", sectionId: op.sectionId, widths: before }]
|
|
2301
|
+
};
|
|
2302
|
+
}
|
|
2303
|
+
const kept = columns.slice(0, Math.min(n, m)).map(
|
|
2304
|
+
(column, j) => patchProps(clearWidthOverrides(column), { width: nums[j] })
|
|
2305
|
+
);
|
|
2306
|
+
if (n > m) {
|
|
2307
|
+
const used = collectIds(doc);
|
|
2308
|
+
const given = /* @__PURE__ */ new Set();
|
|
2309
|
+
const added = [];
|
|
2310
|
+
for (let j = m; j < n; j += 1) {
|
|
2311
|
+
const wanted = Array.isArray(op.newIds) ? op.newIds[j - m] : void 0;
|
|
2312
|
+
if (wanted !== void 0) {
|
|
2313
|
+
if (typeof wanted !== "string" || used.has(wanted) || given.has(wanted)) {
|
|
2314
|
+
return fail(at(i, "newIds"), "ID_NOT_FRESH", `The id "${String(wanted)}" is already used in this document`);
|
|
2315
|
+
}
|
|
2316
|
+
given.add(wanted);
|
|
2317
|
+
used.add(wanted);
|
|
2318
|
+
}
|
|
2319
|
+
const id = wanted ?? freshId(op.sectionId, "c", used);
|
|
2320
|
+
added.push({ id, type: "column", props: { width: nums[j] }, children: [] });
|
|
2321
|
+
}
|
|
2322
|
+
return {
|
|
2323
|
+
doc: setAt(doc, [...found.path, "columns"], [...kept, ...added]),
|
|
2324
|
+
inverse: [
|
|
2325
|
+
{ op: "setColumns", sectionId: op.sectionId, widths: before },
|
|
2326
|
+
...restoreWidthOverrides(columns)
|
|
2327
|
+
]
|
|
2328
|
+
};
|
|
2329
|
+
}
|
|
2330
|
+
const removed = columns.slice(n);
|
|
2331
|
+
const orphans = removed.flatMap((column) => column.children);
|
|
2332
|
+
if (orphans.length > 0 && op.moveChildrenTo !== "previous") {
|
|
2333
|
+
return fail(
|
|
2334
|
+
at(i, "moveChildrenTo"),
|
|
2335
|
+
"COLUMN_NOT_EMPTY",
|
|
2336
|
+
"A column that still has blocks can't be removed. Move its blocks to the previous column first."
|
|
2337
|
+
);
|
|
2338
|
+
}
|
|
2339
|
+
const survivor = kept[n - 1];
|
|
2340
|
+
kept[n - 1] = { ...survivor, children: [...survivor.children, ...orphans] };
|
|
2341
|
+
const inverse = [
|
|
2342
|
+
{ op: "setColumns", sectionId: op.sectionId, widths: before, newIds: removed.map((c) => c.id) },
|
|
2343
|
+
...removed.flatMap(restoreColumnLooks),
|
|
2344
|
+
...removed.flatMap(
|
|
2345
|
+
(column) => column.children.map(
|
|
2346
|
+
(child, k) => ({ op: "move", id: child.id, parentId: column.id, index: k })
|
|
2347
|
+
)
|
|
2348
|
+
),
|
|
2349
|
+
...restoreWidthOverrides(columns)
|
|
2350
|
+
];
|
|
2351
|
+
return { doc: setAt(doc, [...found.path, "columns"], kept), inverse };
|
|
2352
|
+
}
|
|
2353
|
+
function stepSetData(doc, op, ctx, i) {
|
|
2354
|
+
const found = target(doc, op.id, i);
|
|
2355
|
+
if (isFailure(found)) return found;
|
|
2356
|
+
const node = found.node;
|
|
2357
|
+
if (node.type === "section" || node.type === "inner-section" || node.type === "column") {
|
|
2358
|
+
return fail(at(i, "id"), "INVALID_VALUE", "Only a widget has data inputs");
|
|
2359
|
+
}
|
|
2360
|
+
if (typeof op.name !== "string" || FORBIDDEN_KEYS.has(op.name)) {
|
|
2361
|
+
return fail(at(i, "name"), "FORBIDDEN_KEY", `The key "${String(op.name)}" is not allowed`);
|
|
2362
|
+
}
|
|
2363
|
+
const def = definitionOf(node, ctx.registry, i, "id");
|
|
2364
|
+
if (isFailure(def)) return def;
|
|
2365
|
+
const data = isRec(node.data) ? node.data : {};
|
|
2366
|
+
const previous = has(data, op.name) ? data[op.name] : null;
|
|
2367
|
+
if (op.binding !== null) {
|
|
2368
|
+
const issues = bindingIssues(def, op.name, op.binding, ctx, at(i, "binding"));
|
|
2369
|
+
if (issues.length > 0) return { issues };
|
|
2370
|
+
} else if (def.data?.[op.name] === void 0) {
|
|
2371
|
+
return fail(at(i, "name"), "UNKNOWN_PROP", `${def.title} has no data input "${op.name}"`);
|
|
2372
|
+
}
|
|
2373
|
+
const nextData = { ...data };
|
|
2374
|
+
if (op.binding === null) delete nextData[op.name];
|
|
2375
|
+
else nextData[op.name] = op.binding;
|
|
2376
|
+
const next = { ...node, data: nextData };
|
|
2377
|
+
if (Object.keys(nextData).length === 0) delete next.data;
|
|
2378
|
+
return {
|
|
2379
|
+
doc: setAt(doc, found.path, next),
|
|
2380
|
+
inverse: [{ op: "setData", id: op.id, name: op.name, binding: previous }]
|
|
2381
|
+
};
|
|
2382
|
+
}
|
|
2383
|
+
var isCustom = (type) => typeof type === "string" && type.startsWith("custom.");
|
|
2384
|
+
function renameAndRemove(record, renamed, removed) {
|
|
2385
|
+
const out = { ...record };
|
|
2386
|
+
for (const [from, to] of Object.entries(renamed ?? {})) {
|
|
2387
|
+
if (has(out, from) && !FORBIDDEN_KEYS.has(to)) {
|
|
2388
|
+
out[to] = out[from];
|
|
2389
|
+
delete out[from];
|
|
2390
|
+
}
|
|
2391
|
+
}
|
|
2392
|
+
for (const key of removed ?? []) delete out[key];
|
|
2393
|
+
return out;
|
|
2394
|
+
}
|
|
2395
|
+
function liftWidget(widget, toV, registry) {
|
|
2396
|
+
const type = widget.type;
|
|
2397
|
+
let props = widget.props;
|
|
2398
|
+
let responsive = isRec(widget.responsive) ? widget.responsive : void 0;
|
|
2399
|
+
for (let from = widget.v; from < toV; from += 1) {
|
|
2400
|
+
const next = registry.widget(type, from + 1);
|
|
2401
|
+
if (isCustom(type)) {
|
|
2402
|
+
if (next === void 0) return { message: `${type} version ${String(from + 1)} is not available` };
|
|
2403
|
+
props = renameAndRemove(props, next.renamed, next.removed);
|
|
2404
|
+
if (responsive !== void 0) {
|
|
2405
|
+
const lifted = {};
|
|
2406
|
+
for (const device of DEVICES) {
|
|
2407
|
+
const overrides = responsive[device];
|
|
2408
|
+
if (isRec(overrides)) lifted[device] = renameAndRemove(overrides, next.renamed, next.removed);
|
|
2409
|
+
}
|
|
2410
|
+
responsive = lifted;
|
|
2411
|
+
}
|
|
2412
|
+
} else {
|
|
2413
|
+
const migrate = next?.migrations?.[from] ?? registry.widget(type, toV)?.migrations?.[from];
|
|
2414
|
+
if (migrate !== void 0) {
|
|
2415
|
+
const run = (record) => {
|
|
2416
|
+
try {
|
|
2417
|
+
const out = migrate(cloneJson(record));
|
|
2418
|
+
return isRec(out) ? out : void 0;
|
|
2419
|
+
} catch {
|
|
2420
|
+
return void 0;
|
|
2421
|
+
}
|
|
2422
|
+
};
|
|
2423
|
+
const failed = { message: `The migration of ${type} from version ${String(from)} failed` };
|
|
2424
|
+
const migrated = run(props);
|
|
2425
|
+
if (migrated === void 0) return failed;
|
|
2426
|
+
props = migrated;
|
|
2427
|
+
if (responsive !== void 0) {
|
|
2428
|
+
const probe = run({});
|
|
2429
|
+
if (probe === void 0 || Object.keys(probe).length > 0) {
|
|
2430
|
+
return {
|
|
2431
|
+
message: `The migration of ${type} from version ${String(from)} cannot be applied to responsive overrides`
|
|
2432
|
+
};
|
|
2433
|
+
}
|
|
2434
|
+
const lifted = {};
|
|
2435
|
+
for (const device of DEVICES) {
|
|
2436
|
+
const overrides = responsive[device];
|
|
2437
|
+
if (!isRec(overrides)) continue;
|
|
2438
|
+
const out = run(overrides);
|
|
2439
|
+
if (out === void 0) return failed;
|
|
2440
|
+
lifted[device] = out;
|
|
2441
|
+
}
|
|
2442
|
+
responsive = lifted;
|
|
2443
|
+
}
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
const node = { ...widget, v: toV, props };
|
|
2448
|
+
if (responsive !== void 0) node.responsive = responsive;
|
|
2449
|
+
return { node };
|
|
2450
|
+
}
|
|
2451
|
+
function stepUpgradeNode(doc, op, ctx, i) {
|
|
2452
|
+
const found = target(doc, op.id, i);
|
|
2453
|
+
if (isFailure(found)) return found;
|
|
2454
|
+
const node = found.node;
|
|
2455
|
+
if (node.type === "section" || node.type === "inner-section" || node.type === "column") {
|
|
2456
|
+
return fail(at(i, "id"), "INVALID_VALUE", "Only a widget has a version");
|
|
2457
|
+
}
|
|
2458
|
+
if (typeof node.type !== "string" || typeof node.v !== "number") {
|
|
2459
|
+
return fail(at(i, "id"), "INVALID_SHAPE", "A widget has a type and a version");
|
|
2460
|
+
}
|
|
2461
|
+
const targetDef = ctx.registry.widget(node.type, op.toV);
|
|
2462
|
+
if (targetDef === void 0) {
|
|
2463
|
+
return fail(at(i, "toV"), "INVALID_WIDGET_TYPE", `${node.type} version ${String(op.toV)} is not available`);
|
|
2464
|
+
}
|
|
2465
|
+
let next;
|
|
2466
|
+
if (op.restore !== void 0) {
|
|
2467
|
+
const restore = op.restore;
|
|
2468
|
+
if (!isRec(restore) || !isRec(restore.props)) {
|
|
2469
|
+
return fail(at(i, "restore"), "INVALID_SHAPE", "restore is { props, responsive? }");
|
|
2470
|
+
}
|
|
2471
|
+
if (!Number.isInteger(op.toV) || op.toV >= node.v) {
|
|
2472
|
+
return fail(at(i, "toV"), "INVALID_VALUE", `A restore goes back to a version below the current one (${String(node.v)})`);
|
|
2473
|
+
}
|
|
2474
|
+
next = { ...node, v: op.toV, props: restore.props };
|
|
2475
|
+
delete next.responsive;
|
|
2476
|
+
if (restore.responsive !== void 0) {
|
|
2477
|
+
if (!isRec(restore.responsive)) {
|
|
2478
|
+
return fail(at(i, "restore", "responsive"), "INVALID_SHAPE", "responsive is { tablet?, phone? }");
|
|
2479
|
+
}
|
|
2480
|
+
for (const [device, overrides] of Object.entries(restore.responsive)) {
|
|
2481
|
+
if (!DEVICES.includes(device) || !isRec(overrides)) {
|
|
2482
|
+
return fail(at(i, "restore", "responsive", device), "INVALID_SHAPE", "responsive holds tablet and phone objects only");
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
next.responsive = restore.responsive;
|
|
2486
|
+
}
|
|
2487
|
+
const invalid = widgetIssues(next, ctx, at(i), found.path.length > 6);
|
|
2488
|
+
if (invalid.length > 0) return { issues: invalid };
|
|
2489
|
+
const replay = liftWidget(next, node.v, ctx.registry);
|
|
2490
|
+
if ("message" in replay || !deepEqual(replay.node.props, node.props) || !deepEqual(replay.node.responsive ?? {}, node.responsive ?? {})) {
|
|
2491
|
+
return fail(
|
|
2492
|
+
at(i, "restore"),
|
|
2493
|
+
"INVALID_VALUE",
|
|
2494
|
+
"A restore must be the exact inverse of an upgrade: migrating it forward must give the current node"
|
|
2495
|
+
);
|
|
2496
|
+
}
|
|
2497
|
+
} else {
|
|
2498
|
+
if (!Number.isInteger(op.toV) || op.toV <= node.v) {
|
|
2499
|
+
return fail(at(i, "toV"), "INVALID_VALUE", `toV must be higher than the current version (${String(node.v)})`);
|
|
2500
|
+
}
|
|
2501
|
+
const lifted = liftWidget(node, op.toV, ctx.registry);
|
|
2502
|
+
if ("message" in lifted) return fail(at(i, "toV"), "INVALID_VALUE", lifted.message);
|
|
2503
|
+
next = lifted.node;
|
|
2504
|
+
const invalid = widgetIssues(next, ctx, at(i), found.path.length > 6);
|
|
2505
|
+
if (invalid.length > 0) return { issues: invalid };
|
|
2506
|
+
}
|
|
2507
|
+
return {
|
|
2508
|
+
doc: setAt(doc, found.path, next),
|
|
2509
|
+
inverse: [
|
|
2510
|
+
{
|
|
2511
|
+
op: "upgradeNode",
|
|
2512
|
+
id: op.id,
|
|
2513
|
+
toV: node.v,
|
|
2514
|
+
restore: {
|
|
2515
|
+
props: node.props,
|
|
2516
|
+
...isRec(node.responsive) ? { responsive: node.responsive } : {}
|
|
2517
|
+
}
|
|
2518
|
+
}
|
|
2519
|
+
]
|
|
2520
|
+
};
|
|
2521
|
+
}
|
|
2522
|
+
function mapShared(items, f) {
|
|
2523
|
+
let changed = false;
|
|
2524
|
+
const out = items.map((item) => {
|
|
2525
|
+
const next = f(item);
|
|
2526
|
+
if (next !== item) changed = true;
|
|
2527
|
+
return next;
|
|
2528
|
+
});
|
|
2529
|
+
return changed ? out : items;
|
|
2530
|
+
}
|
|
2531
|
+
function upgradeNodes(doc, registry) {
|
|
2532
|
+
const upgraded = [];
|
|
2533
|
+
const lift = (widget) => {
|
|
2534
|
+
const type = widget.type;
|
|
2535
|
+
if (typeof type !== "string" || typeof widget.v !== "number" || isCustom(type)) return widget;
|
|
2536
|
+
const latest = registry.latest(type);
|
|
2537
|
+
if (latest === void 0 || latest.v <= widget.v) return widget;
|
|
2538
|
+
const result = liftWidget(widget, latest.v, registry);
|
|
2539
|
+
if ("message" in result) return widget;
|
|
2540
|
+
upgraded.push(widget.id);
|
|
2541
|
+
return result.node;
|
|
2542
|
+
};
|
|
2543
|
+
const section = (node) => {
|
|
2544
|
+
const columns = mapShared(node.columns, (column) => {
|
|
2545
|
+
const children = mapShared(
|
|
2546
|
+
column.children,
|
|
2547
|
+
(child) => child.type === "inner-section" ? section(child) : lift(child)
|
|
2548
|
+
);
|
|
2549
|
+
return children === column.children ? column : { ...column, children };
|
|
2550
|
+
});
|
|
2551
|
+
return columns === node.columns ? node : { ...node, columns };
|
|
2552
|
+
};
|
|
2553
|
+
const sections = mapShared(doc.sections, section);
|
|
2554
|
+
return {
|
|
2555
|
+
doc: sections === doc.sections ? doc : { ...doc, sections },
|
|
2556
|
+
upgraded
|
|
2557
|
+
};
|
|
2558
|
+
}
|
|
2559
|
+
function stepReplace(op, doc, ctx, i) {
|
|
2560
|
+
const result = validateDocument(op.document);
|
|
2561
|
+
if (!result.ok) {
|
|
2562
|
+
return {
|
|
2563
|
+
issues: result.issues.map((found) => ({
|
|
2564
|
+
...found,
|
|
2565
|
+
path: found.path === "" ? "ops[" + String(i) + "].document" : `ops[${String(i)}].document.${found.path}`
|
|
2566
|
+
}))
|
|
2567
|
+
};
|
|
2568
|
+
}
|
|
2569
|
+
const issues = [];
|
|
2570
|
+
walkNodes(result.doc, (node, path) => {
|
|
2571
|
+
if (node.type !== "section" && node.type !== "inner-section" && node.type !== "column") {
|
|
2572
|
+
issues.push(
|
|
2573
|
+
...widgetIssues(node, ctx, at(i, "document", ...path), path.length > 6)
|
|
2574
|
+
);
|
|
2575
|
+
}
|
|
2576
|
+
});
|
|
2577
|
+
if (issues.length > 0) return { issues };
|
|
2578
|
+
return { doc: result.doc, inverse: [{ op: "replaceDocument", document: doc }] };
|
|
2579
|
+
}
|
|
2580
|
+
function applyOne(doc, op, ctx, i) {
|
|
2581
|
+
if (!isRec(op)) return fail(at(i), "INVALID_SHAPE", "An op is an object");
|
|
2582
|
+
switch (op.op) {
|
|
2583
|
+
case "insert":
|
|
2584
|
+
return stepInsert(doc, op, ctx, i);
|
|
2585
|
+
case "remove":
|
|
2586
|
+
return stepRemove(doc, op, i);
|
|
2587
|
+
case "move":
|
|
2588
|
+
return stepMove(doc, op, ctx, i);
|
|
2589
|
+
case "setProps":
|
|
2590
|
+
case "setResponsive":
|
|
2591
|
+
return stepProps(doc, op, ctx, i);
|
|
2592
|
+
case "setColumns":
|
|
2593
|
+
return stepSetColumns(doc, op, i);
|
|
2594
|
+
case "setData":
|
|
2595
|
+
return stepSetData(doc, op, ctx, i);
|
|
2596
|
+
case "replaceDocument":
|
|
2597
|
+
return stepReplace(op, doc, ctx, i);
|
|
2598
|
+
case "upgradeNode":
|
|
2599
|
+
return stepUpgradeNode(doc, op, ctx, i);
|
|
2600
|
+
default:
|
|
2601
|
+
return fail(at(i, "op"), "INVALID_SHAPE", `Unknown op "${String(op.op)}"`);
|
|
2602
|
+
}
|
|
2603
|
+
}
|
|
2604
|
+
function applyOps(doc, ops, ctx) {
|
|
2605
|
+
if (!Array.isArray(ops)) {
|
|
2606
|
+
return { ok: false, issues: [issue(["ops"], "INVALID_SHAPE", "ops is a list of operations")] };
|
|
2607
|
+
}
|
|
2608
|
+
let working = doc;
|
|
2609
|
+
for (const [i, op] of ops.entries()) {
|
|
2610
|
+
const step = applyOne(working, op, ctx, i);
|
|
2611
|
+
if (isFailure(step)) return { ok: false, issues: capIssues(step.issues) };
|
|
2612
|
+
working = step.doc;
|
|
2613
|
+
}
|
|
2614
|
+
if (working === doc) return { ok: true, doc };
|
|
2615
|
+
const checked = validateDocument(working);
|
|
2616
|
+
if (!checked.ok) return { ok: false, issues: checked.issues };
|
|
2617
|
+
return { ok: true, doc: working };
|
|
2618
|
+
}
|
|
2619
|
+
function invertOps(before, ops, ctx) {
|
|
2620
|
+
let doc = before;
|
|
2621
|
+
const inverse = [];
|
|
2622
|
+
for (const [i, op] of ops.entries()) {
|
|
2623
|
+
const step = applyOne(doc, op, ctx, i);
|
|
2624
|
+
if (isFailure(step)) {
|
|
2625
|
+
throw new Error(`Cannot invert: ${step.issues[0]?.message ?? "an op does not apply"}`);
|
|
2626
|
+
}
|
|
2627
|
+
doc = step.doc;
|
|
2628
|
+
inverse.unshift(...step.inverse);
|
|
2629
|
+
}
|
|
2630
|
+
return inverse;
|
|
2631
|
+
}
|
|
2632
|
+
|
|
2633
|
+
// src/op-schema.ts
|
|
2634
|
+
var import_zod6 = require("zod");
|
|
2635
|
+
var parentIdSchema = import_zod6.z.union([import_zod6.z.literal("root"), nodeIdSchema]);
|
|
2636
|
+
var deviceSchema = import_zod6.z.enum(["tablet", "phone"]);
|
|
2637
|
+
var propsSchema = safeRecord(settingValueSchema);
|
|
2638
|
+
var dataNameSchema2 = import_zod6.z.string().regex(/^[a-z][A-Za-z0-9_]{0,39}$/);
|
|
2639
|
+
var INDEX_MAX = 1e4;
|
|
2640
|
+
var overridesSchema = import_zod6.z.object({ tablet: propsSchema.optional(), phone: propsSchema.optional() }).strict();
|
|
2641
|
+
var insertOp = import_zod6.z.object({
|
|
2642
|
+
op: import_zod6.z.literal("insert"),
|
|
2643
|
+
parentId: parentIdSchema,
|
|
2644
|
+
index: import_zod6.z.number().int().min(0).max(INDEX_MAX),
|
|
2645
|
+
node: import_zod6.z.union([sectionNodeSchema, innerSectionNodeSchema, widgetNodeSchema])
|
|
2646
|
+
}).strict();
|
|
2647
|
+
var removeOp = import_zod6.z.object({ op: import_zod6.z.literal("remove"), id: nodeIdSchema }).strict();
|
|
2648
|
+
var moveOp = import_zod6.z.object({
|
|
2649
|
+
op: import_zod6.z.literal("move"),
|
|
2650
|
+
id: nodeIdSchema,
|
|
2651
|
+
parentId: parentIdSchema,
|
|
2652
|
+
index: import_zod6.z.number().int().min(0).max(INDEX_MAX)
|
|
2653
|
+
}).strict();
|
|
2654
|
+
var setPropsOp = import_zod6.z.object({ op: import_zod6.z.literal("setProps"), id: nodeIdSchema, props: propsSchema }).strict();
|
|
2655
|
+
var setResponsiveOp = import_zod6.z.object({
|
|
2656
|
+
op: import_zod6.z.literal("setResponsive"),
|
|
2657
|
+
id: nodeIdSchema,
|
|
2658
|
+
device: deviceSchema,
|
|
2659
|
+
props: propsSchema
|
|
2660
|
+
}).strict();
|
|
2661
|
+
var setColumnsOp = import_zod6.z.object({
|
|
2662
|
+
op: import_zod6.z.literal("setColumns"),
|
|
2663
|
+
sectionId: nodeIdSchema,
|
|
2664
|
+
widths: import_zod6.z.array(import_zod6.z.number().finite()).max(50),
|
|
2665
|
+
device: deviceSchema.optional(),
|
|
2666
|
+
moveChildrenTo: import_zod6.z.literal("previous").optional(),
|
|
2667
|
+
newIds: import_zod6.z.array(nodeIdSchema).max(50).optional()
|
|
2668
|
+
}).strict();
|
|
2669
|
+
var setDataOp = import_zod6.z.object({
|
|
2670
|
+
op: import_zod6.z.literal("setData"),
|
|
2671
|
+
id: nodeIdSchema,
|
|
2672
|
+
name: dataNameSchema2,
|
|
2673
|
+
binding: dataBindingSchema.nullable()
|
|
2674
|
+
}).strict();
|
|
2675
|
+
var replaceDocumentOp = import_zod6.z.object({ op: import_zod6.z.literal("replaceDocument"), document: pageDocumentSchema }).strict();
|
|
2676
|
+
var upgradeNodeShape = { op: import_zod6.z.literal("upgradeNode"), id: nodeIdSchema, toV: import_zod6.z.number().int().min(1).max(9999) };
|
|
2677
|
+
var upgradeNodeOp = import_zod6.z.object({
|
|
2678
|
+
...upgradeNodeShape,
|
|
2679
|
+
restore: import_zod6.z.object({ props: propsSchema, responsive: overridesSchema.optional() }).strict().optional()
|
|
2680
|
+
}).strict();
|
|
2681
|
+
var agentUpgradeNodeOp = import_zod6.z.object(upgradeNodeShape).strict();
|
|
2682
|
+
var docOpSchema = import_zod6.z.discriminatedUnion("op", [
|
|
2683
|
+
insertOp,
|
|
2684
|
+
removeOp,
|
|
2685
|
+
moveOp,
|
|
2686
|
+
setPropsOp,
|
|
2687
|
+
setResponsiveOp,
|
|
2688
|
+
setColumnsOp,
|
|
2689
|
+
setDataOp,
|
|
2690
|
+
replaceDocumentOp,
|
|
2691
|
+
upgradeNodeOp
|
|
2692
|
+
]);
|
|
2693
|
+
var AGENT_MAX_OPS = 500;
|
|
2694
|
+
var agentDocOpSchema = import_zod6.z.discriminatedUnion("op", [
|
|
2695
|
+
insertOp,
|
|
2696
|
+
removeOp,
|
|
2697
|
+
moveOp,
|
|
2698
|
+
setPropsOp,
|
|
2699
|
+
setResponsiveOp,
|
|
2700
|
+
setColumnsOp,
|
|
2701
|
+
setDataOp,
|
|
2702
|
+
agentUpgradeNodeOp
|
|
2703
|
+
]);
|
|
2704
|
+
var agentDocOpsSchema = import_zod6.z.array(agentDocOpSchema).min(1).max(AGENT_MAX_OPS);
|
|
2705
|
+
|
|
2706
|
+
// src/urls.ts
|
|
2707
|
+
var MAX_URL_LENGTH = 2e3;
|
|
2708
|
+
var NOT_PLAIN_ASCII = /[^\x21-\x7E]/;
|
|
2709
|
+
var MARKUP_OR_BACKSLASH = /[\\<>"'`]/;
|
|
2710
|
+
var CHARACTER_REFERENCE = /&#|&[a-z][a-z0-9]*;/i;
|
|
2711
|
+
var ABSOLUTE = /^(https):\/\/([^/?#]*)(.*)$/is;
|
|
2712
|
+
var AUTHORITY = /^[A-Za-z0-9](?:[A-Za-z0-9.-]*[A-Za-z0-9])?(?::[0-9]{1,5})?$/;
|
|
2713
|
+
function plain(value) {
|
|
2714
|
+
return value.length > 0 && value.length <= MAX_URL_LENGTH && !NOT_PLAIN_ASCII.test(value) && !MARKUP_OR_BACKSLASH.test(value) && !CHARACTER_REFERENCE.test(value);
|
|
2715
|
+
}
|
|
2716
|
+
function originOf(value) {
|
|
2717
|
+
const match = ABSOLUTE.exec(value);
|
|
2718
|
+
if (match === null) return null;
|
|
2719
|
+
const [, scheme, authority] = match;
|
|
2720
|
+
if (authority === void 0 || !AUTHORITY.test(authority)) return null;
|
|
2721
|
+
return `${scheme.toLowerCase()}://${authority.toLowerCase()}`;
|
|
2722
|
+
}
|
|
2723
|
+
function safeUrl(value, kind, ctx) {
|
|
2724
|
+
if (typeof value !== "string" || !plain(value)) return null;
|
|
2725
|
+
if (kind === "href") {
|
|
2726
|
+
if (value.startsWith("/")) return value[1] === "/" ? null : value;
|
|
2727
|
+
return originOf(value) === null ? null : value;
|
|
2728
|
+
}
|
|
2729
|
+
const media = originOf(ctx.mediaOrigin.replace(/\/+$/, ""));
|
|
2730
|
+
return media !== null && originOf(value) === media ? value : null;
|
|
2731
|
+
}
|
|
2732
|
+
|
|
2733
|
+
// src/template/ast.ts
|
|
2734
|
+
var TEMPLATE_FILTERS = ["upper", "lower", "truncate", "default"];
|
|
2735
|
+
var ALLOWED_ELEMENTS = [
|
|
2736
|
+
"div",
|
|
2737
|
+
"span",
|
|
2738
|
+
"p",
|
|
2739
|
+
"h1",
|
|
2740
|
+
"h2",
|
|
2741
|
+
"h3",
|
|
2742
|
+
"h4",
|
|
2743
|
+
"h5",
|
|
2744
|
+
"h6",
|
|
2745
|
+
"a",
|
|
2746
|
+
"img",
|
|
2747
|
+
"ul",
|
|
2748
|
+
"ol",
|
|
2749
|
+
"li",
|
|
2750
|
+
"strong",
|
|
2751
|
+
"em",
|
|
2752
|
+
"small",
|
|
2753
|
+
"figure",
|
|
2754
|
+
"figcaption",
|
|
2755
|
+
"section",
|
|
2756
|
+
"article",
|
|
2757
|
+
"header",
|
|
2758
|
+
"footer",
|
|
2759
|
+
"br",
|
|
2760
|
+
"hr"
|
|
2761
|
+
];
|
|
2762
|
+
var VOID_ELEMENTS = ["img", "br", "hr"];
|
|
2763
|
+
var ALLOWED_ATTRS = {
|
|
2764
|
+
"*": ["class", "title", "aria-label", "aria-hidden", "role"],
|
|
2765
|
+
a: ["href"],
|
|
2766
|
+
img: ["src", "alt"]
|
|
2767
|
+
};
|
|
2768
|
+
var ALLOWED_ROLES = [
|
|
2769
|
+
"article",
|
|
2770
|
+
"banner",
|
|
2771
|
+
"complementary",
|
|
2772
|
+
"contentinfo",
|
|
2773
|
+
"figure",
|
|
2774
|
+
"group",
|
|
2775
|
+
"heading",
|
|
2776
|
+
"img",
|
|
2777
|
+
"list",
|
|
2778
|
+
"listitem",
|
|
2779
|
+
"none",
|
|
2780
|
+
"note",
|
|
2781
|
+
"presentation",
|
|
2782
|
+
"region",
|
|
2783
|
+
"separator"
|
|
2784
|
+
];
|
|
2785
|
+
var RESERVED_CLASS_PREFIXES = ["b-", "tb-", "tsf-", "throttle-"];
|
|
2786
|
+
var isReservedClass = (name) => RESERVED_CLASS_PREFIXES.some((prefix) => name.startsWith(prefix));
|
|
2787
|
+
var BIDI_CONTROL_RE = /[\u202A-\u202E\u2066-\u2069]/;
|
|
2788
|
+
var CLASS_TOKEN_RE = /^[a-z][a-z0-9-]{0,40}$/;
|
|
2789
|
+
var DATA_ATTR_RE = /^data-[a-z][a-z0-9-]{0,40}$/;
|
|
2790
|
+
var RESERVED_DATA_PREFIX = "data-b-";
|
|
2791
|
+
var MAX_TEMPLATE_WEIGHT = 2e4;
|
|
2792
|
+
var EACH_ITERATION_CAP = 100;
|
|
2793
|
+
function templateWeight(ast, multiplier = 1) {
|
|
2794
|
+
let total = 0;
|
|
2795
|
+
for (const node of ast) {
|
|
2796
|
+
total += multiplier;
|
|
2797
|
+
switch (node.type) {
|
|
2798
|
+
case "if":
|
|
2799
|
+
total += templateWeight(node.then, multiplier) + templateWeight(node.else, multiplier);
|
|
2800
|
+
break;
|
|
2801
|
+
case "unless":
|
|
2802
|
+
total += templateWeight(node.body, multiplier);
|
|
2803
|
+
break;
|
|
2804
|
+
case "each":
|
|
2805
|
+
total += templateWeight(node.body, multiplier * EACH_ITERATION_CAP);
|
|
2806
|
+
break;
|
|
2807
|
+
case "element":
|
|
2808
|
+
for (const attr of node.attrs) total += multiplier * attr.value.length;
|
|
2809
|
+
total += templateWeight(node.children, multiplier);
|
|
2810
|
+
break;
|
|
2811
|
+
default:
|
|
2812
|
+
break;
|
|
2813
|
+
}
|
|
2814
|
+
}
|
|
2815
|
+
return total;
|
|
2816
|
+
}
|
|
2817
|
+
|
|
2818
|
+
// src/template/lexer.ts
|
|
2819
|
+
var TemplateError = class extends Error {
|
|
2820
|
+
constructor(code, message, pos) {
|
|
2821
|
+
super(message);
|
|
2822
|
+
this.code = code;
|
|
2823
|
+
this.pos = pos;
|
|
2824
|
+
}
|
|
2825
|
+
};
|
|
2826
|
+
var isLetter = (c) => c !== void 0 && (c >= "a" && c <= "z" || c >= "A" && c <= "Z");
|
|
2827
|
+
var isDigit = (c) => c !== void 0 && c >= "0" && c <= "9";
|
|
2828
|
+
var isSpace = (c) => c === " " || c === " " || c === "\n" || c === "\r" || c === "\f";
|
|
2829
|
+
function readMustache(src, i) {
|
|
2830
|
+
if (src[i + 2] === "{") {
|
|
2831
|
+
throw new TemplateError(
|
|
2832
|
+
"TEMPLATE_FORBIDDEN",
|
|
2833
|
+
"Triple-stash {{{ }}} (raw output) is not allowed. Use {{ }}, which always escapes.",
|
|
2834
|
+
i
|
|
2835
|
+
);
|
|
2836
|
+
}
|
|
2837
|
+
const end = src.indexOf("}}", i + 2);
|
|
2838
|
+
if (end === -1) throw new TemplateError("TEMPLATE_SYNTAX", "This {{ is never closed with }}", i);
|
|
2839
|
+
const body = src.slice(i + 2, end);
|
|
2840
|
+
if (body.includes("{{")) {
|
|
2841
|
+
throw new TemplateError("TEMPLATE_SYNTAX", "A {{ }} tag cannot contain another {{", i);
|
|
2842
|
+
}
|
|
2843
|
+
return { body, next: end + 2 };
|
|
2844
|
+
}
|
|
2845
|
+
function readAttributeValue(src, start) {
|
|
2846
|
+
const quote = src[start];
|
|
2847
|
+
if (quote !== '"' && quote !== "'") {
|
|
2848
|
+
throw new TemplateError("TEMPLATE_SYNTAX", "Attribute values must be in quotes", start);
|
|
2849
|
+
}
|
|
2850
|
+
const parts = [];
|
|
2851
|
+
let text = "";
|
|
2852
|
+
let i = start + 1;
|
|
2853
|
+
while (i < src.length) {
|
|
2854
|
+
const c = src[i];
|
|
2855
|
+
if (c === quote) {
|
|
2856
|
+
if (text !== "") parts.push({ kind: "text", value: text });
|
|
2857
|
+
return { parts, next: i + 1 };
|
|
2858
|
+
}
|
|
2859
|
+
if (c === "{" && src[i + 1] === "{") {
|
|
2860
|
+
if (text !== "") parts.push({ kind: "text", value: text });
|
|
2861
|
+
text = "";
|
|
2862
|
+
const { body, next } = readMustache(src, i);
|
|
2863
|
+
parts.push({ kind: "mustache", body, pos: i });
|
|
2864
|
+
i = next;
|
|
2865
|
+
continue;
|
|
2866
|
+
}
|
|
2867
|
+
text += c;
|
|
2868
|
+
i += 1;
|
|
2869
|
+
}
|
|
2870
|
+
throw new TemplateError("TEMPLATE_SYNTAX", "This attribute value is never closed", start);
|
|
2871
|
+
}
|
|
2872
|
+
function readOpenTag(src, start) {
|
|
2873
|
+
let i = start + 1;
|
|
2874
|
+
let name = "";
|
|
2875
|
+
while (isLetter(src[i]) || isDigit(src[i]) || src[i] === "-") {
|
|
2876
|
+
name += src[i];
|
|
2877
|
+
i += 1;
|
|
2878
|
+
}
|
|
2879
|
+
const after = src[i];
|
|
2880
|
+
if (!isSpace(after) && after !== "/" && after !== ">") {
|
|
2881
|
+
throw new TemplateError("TEMPLATE_SYNTAX", `Unexpected character after <${name}`, i);
|
|
2882
|
+
}
|
|
2883
|
+
const attrs = [];
|
|
2884
|
+
for (; ; ) {
|
|
2885
|
+
while (isSpace(src[i])) i += 1;
|
|
2886
|
+
const c = src[i];
|
|
2887
|
+
if (c === void 0) throw new TemplateError("TEMPLATE_SYNTAX", `<${name}> is never closed`, start);
|
|
2888
|
+
if (c === ">") {
|
|
2889
|
+
return { token: { kind: "open", name, attrs, selfClosing: false, pos: start }, next: i + 1 };
|
|
2890
|
+
}
|
|
2891
|
+
if (c === "/") {
|
|
2892
|
+
if (src[i + 1] !== ">") throw new TemplateError("TEMPLATE_SYNTAX", "Unexpected /", i);
|
|
2893
|
+
return { token: { kind: "open", name, attrs, selfClosing: true, pos: start }, next: i + 2 };
|
|
2894
|
+
}
|
|
2895
|
+
if (!(isLetter(c) || c === "_" || c === ":")) {
|
|
2896
|
+
throw new TemplateError("TEMPLATE_SYNTAX", `Unexpected character "${c}" in <${name}>`, i);
|
|
2897
|
+
}
|
|
2898
|
+
const attrStart = i;
|
|
2899
|
+
let attrName = "";
|
|
2900
|
+
while (isLetter(src[i]) || isDigit(src[i]) || src[i] === "-" || src[i] === "_" || src[i] === ":" || src[i] === ".") {
|
|
2901
|
+
attrName += src[i];
|
|
2902
|
+
i += 1;
|
|
2903
|
+
}
|
|
2904
|
+
while (isSpace(src[i])) i += 1;
|
|
2905
|
+
if (src[i] !== "=") {
|
|
2906
|
+
attrs.push({ name: attrName, parts: null, pos: attrStart });
|
|
2907
|
+
continue;
|
|
2908
|
+
}
|
|
2909
|
+
i += 1;
|
|
2910
|
+
while (isSpace(src[i])) i += 1;
|
|
2911
|
+
const value = readAttributeValue(src, i);
|
|
2912
|
+
attrs.push({ name: attrName, parts: value.parts, pos: attrStart });
|
|
2913
|
+
i = value.next;
|
|
2914
|
+
}
|
|
2915
|
+
}
|
|
2916
|
+
function lex(src) {
|
|
2917
|
+
const tokens = [];
|
|
2918
|
+
let text = "";
|
|
2919
|
+
let textPos = 0;
|
|
2920
|
+
const flush = () => {
|
|
2921
|
+
if (text !== "") tokens.push({ kind: "text", value: text, pos: textPos });
|
|
2922
|
+
text = "";
|
|
2923
|
+
};
|
|
2924
|
+
let i = 0;
|
|
2925
|
+
while (i < src.length) {
|
|
2926
|
+
const c = src[i];
|
|
2927
|
+
if (c === "{" && src[i + 1] === "{") {
|
|
2928
|
+
flush();
|
|
2929
|
+
const { body, next } = readMustache(src, i);
|
|
2930
|
+
tokens.push({ kind: "mustache", body, pos: i });
|
|
2931
|
+
i = next;
|
|
2932
|
+
continue;
|
|
2933
|
+
}
|
|
2934
|
+
if (c === "<") {
|
|
2935
|
+
const n = src[i + 1];
|
|
2936
|
+
if (n === "!" || n === "?") {
|
|
2937
|
+
throw new TemplateError(
|
|
2938
|
+
"TEMPLATE_SYNTAX",
|
|
2939
|
+
"Comments, doctypes and processing instructions are not allowed",
|
|
2940
|
+
i
|
|
2941
|
+
);
|
|
2942
|
+
}
|
|
2943
|
+
if (n === "/") {
|
|
2944
|
+
flush();
|
|
2945
|
+
let j = i + 2;
|
|
2946
|
+
let name = "";
|
|
2947
|
+
while (isLetter(src[j]) || isDigit(src[j]) || src[j] === "-") {
|
|
2948
|
+
name += src[j];
|
|
2949
|
+
j += 1;
|
|
2950
|
+
}
|
|
2951
|
+
while (isSpace(src[j])) j += 1;
|
|
2952
|
+
if (name === "" || src[j] !== ">") {
|
|
2953
|
+
throw new TemplateError("TEMPLATE_SYNTAX", "Malformed closing tag", i);
|
|
2954
|
+
}
|
|
2955
|
+
tokens.push({ kind: "close", name, pos: i });
|
|
2956
|
+
i = j + 1;
|
|
2957
|
+
continue;
|
|
2958
|
+
}
|
|
2959
|
+
if (isLetter(n)) {
|
|
2960
|
+
flush();
|
|
2961
|
+
const { token, next } = readOpenTag(src, i);
|
|
2962
|
+
tokens.push(token);
|
|
2963
|
+
i = next;
|
|
2964
|
+
continue;
|
|
2965
|
+
}
|
|
2966
|
+
}
|
|
2967
|
+
if (text === "") textPos = i;
|
|
2968
|
+
text += c;
|
|
2969
|
+
i += 1;
|
|
2970
|
+
}
|
|
2971
|
+
flush();
|
|
2972
|
+
return tokens;
|
|
2973
|
+
}
|
|
2974
|
+
function lineCol(src, pos) {
|
|
2975
|
+
let line = 1;
|
|
2976
|
+
let col = 1;
|
|
2977
|
+
for (let i = 0; i < pos && i < src.length; i += 1) {
|
|
2978
|
+
if (src[i] === "\n") {
|
|
2979
|
+
line += 1;
|
|
2980
|
+
col = 1;
|
|
2981
|
+
} else col += 1;
|
|
2982
|
+
}
|
|
2983
|
+
return { line, col };
|
|
2984
|
+
}
|
|
2985
|
+
|
|
2986
|
+
// src/template/parse.ts
|
|
2987
|
+
var PATH_RE = /^[a-z][A-Za-z0-9]{0,39}(?:\.[a-z][A-Za-z0-9]{0,39}){0,5}$/;
|
|
2988
|
+
var VAR_RE = /^[a-z][A-Za-z0-9]{0,19}$/;
|
|
2989
|
+
var ROOTS = ["settings", "data", "locale"];
|
|
2990
|
+
var MAX_FILTERS = 4;
|
|
2991
|
+
var MAX_TRUNCATE = 500;
|
|
2992
|
+
var MAX_DEFAULT_TEXT = 200;
|
|
2993
|
+
var MAX_STATIC_ATTR = 300;
|
|
2994
|
+
var PROTO_NAMES = /* @__PURE__ */ new Set([
|
|
2995
|
+
...FORBIDDEN_KEYS,
|
|
2996
|
+
...Object.getOwnPropertyNames(Object.prototype)
|
|
2997
|
+
]);
|
|
2998
|
+
var ELEMENTS = new Set(ALLOWED_ELEMENTS);
|
|
2999
|
+
var VOIDS = new Set(VOID_ELEMENTS);
|
|
3000
|
+
var ROLES = new Set(ALLOWED_ROLES);
|
|
3001
|
+
var FILTERS = new Set(TEMPLATE_FILTERS);
|
|
3002
|
+
var HEADINGS = /* @__PURE__ */ new Set(["h1", "h2", "h3", "h4", "h5", "h6"]);
|
|
3003
|
+
var CLOSES_P = /* @__PURE__ */ new Set([
|
|
3004
|
+
"div",
|
|
3005
|
+
"p",
|
|
3006
|
+
"h1",
|
|
3007
|
+
"h2",
|
|
3008
|
+
"h3",
|
|
3009
|
+
"h4",
|
|
3010
|
+
"h5",
|
|
3011
|
+
"h6",
|
|
3012
|
+
"ul",
|
|
3013
|
+
"ol",
|
|
3014
|
+
"li",
|
|
3015
|
+
"figure",
|
|
3016
|
+
"section",
|
|
3017
|
+
"article",
|
|
3018
|
+
"header",
|
|
3019
|
+
"footer",
|
|
3020
|
+
"hr"
|
|
3021
|
+
]);
|
|
3022
|
+
var forbidden = (message, pos) => new TemplateError("TEMPLATE_FORBIDDEN", message, pos);
|
|
3023
|
+
var syntax = (message, pos) => new TemplateError("TEMPLATE_SYNTAX", message, pos);
|
|
3024
|
+
function parsePath(raw2, pos, scope) {
|
|
3025
|
+
const text = raw2.trim();
|
|
3026
|
+
if (text === "") throw syntax("Expected a path such as settings.title", pos);
|
|
3027
|
+
if (/\s/.test(text)) {
|
|
3028
|
+
throw forbidden(
|
|
3029
|
+
"Helpers, arguments and comparisons are not supported. A tag holds one path and optional filters.",
|
|
3030
|
+
pos
|
|
3031
|
+
);
|
|
3032
|
+
}
|
|
3033
|
+
if (!PATH_RE.test(text)) {
|
|
3034
|
+
throw forbidden(
|
|
3035
|
+
`"${text.slice(0, 40)}" is not a plain path. Lookups, parent paths, index access and expressions are not supported.`,
|
|
3036
|
+
pos
|
|
3037
|
+
);
|
|
3038
|
+
}
|
|
3039
|
+
const path = text.split(".");
|
|
3040
|
+
for (const segment of path) {
|
|
3041
|
+
if (PROTO_NAMES.has(segment)) throw forbidden(`"${segment}" is not allowed in a path`, pos);
|
|
3042
|
+
}
|
|
3043
|
+
const root = path[0];
|
|
3044
|
+
if (root === "settings") {
|
|
3045
|
+
if (path.length !== 2) throw syntax("A settings path is settings.<key>", pos);
|
|
3046
|
+
} else if (root === "locale") {
|
|
3047
|
+
if (path.length !== 1) throw syntax("locale has no fields", pos);
|
|
3048
|
+
} else if (root === "data") {
|
|
3049
|
+
if (path.length < 2) throw syntax("A data path is data.<input> or data.<input>.<field>", pos);
|
|
3050
|
+
} else if (!scope.includes(root)) {
|
|
3051
|
+
throw new TemplateError(
|
|
3052
|
+
"UNKNOWN_REFERENCE",
|
|
3053
|
+
`"${root}" is not settings, data, locale or a loop variable in scope`,
|
|
3054
|
+
pos
|
|
3055
|
+
);
|
|
3056
|
+
}
|
|
3057
|
+
return path;
|
|
3058
|
+
}
|
|
3059
|
+
function splitPipes(body) {
|
|
3060
|
+
const parts = [];
|
|
3061
|
+
let current = "";
|
|
3062
|
+
let quote = null;
|
|
3063
|
+
for (const c of body) {
|
|
3064
|
+
if (quote !== null) {
|
|
3065
|
+
if (c === quote) quote = null;
|
|
3066
|
+
current += c;
|
|
3067
|
+
} else if (c === '"' || c === "'") {
|
|
3068
|
+
quote = c;
|
|
3069
|
+
current += c;
|
|
3070
|
+
} else if (c === "|") {
|
|
3071
|
+
parts.push(current);
|
|
3072
|
+
current = "";
|
|
3073
|
+
} else current += c;
|
|
3074
|
+
}
|
|
3075
|
+
parts.push(current);
|
|
3076
|
+
return parts;
|
|
3077
|
+
}
|
|
3078
|
+
function parseFilter(raw2, pos) {
|
|
3079
|
+
const text = raw2.trim();
|
|
3080
|
+
const colon = text.indexOf(":");
|
|
3081
|
+
const name = (colon === -1 ? text : text.slice(0, colon)).trim();
|
|
3082
|
+
const arg = colon === -1 ? void 0 : text.slice(colon + 1).trim();
|
|
3083
|
+
if (name === "") throw syntax("Empty filter after |", pos);
|
|
3084
|
+
if (!FILTERS.has(name)) {
|
|
3085
|
+
throw forbidden(`The filter "${name.slice(0, 30)}" is not allowed. Use ${TEMPLATE_FILTERS.join(", ")}.`, pos);
|
|
3086
|
+
}
|
|
3087
|
+
if (name === "upper" || name === "lower") {
|
|
3088
|
+
if (arg !== void 0) throw syntax(`${name} takes no argument`, pos);
|
|
3089
|
+
return { name };
|
|
3090
|
+
}
|
|
3091
|
+
if (name === "truncate") {
|
|
3092
|
+
if (arg === void 0 || !/^[0-9]{1,4}$/.test(arg)) {
|
|
3093
|
+
throw syntax("truncate needs a number, such as truncate:40", pos);
|
|
3094
|
+
}
|
|
3095
|
+
const n = Number(arg);
|
|
3096
|
+
if (n < 1 || n > MAX_TRUNCATE) throw syntax(`truncate takes 1 to ${String(MAX_TRUNCATE)}`, pos);
|
|
3097
|
+
return { name: "truncate", n };
|
|
3098
|
+
}
|
|
3099
|
+
const quote = arg?.[0];
|
|
3100
|
+
if (arg === void 0 || quote !== '"' && quote !== "'" || arg.length < 2 || arg[arg.length - 1] !== quote) {
|
|
3101
|
+
throw syntax('default needs quoted text, such as default:"none"', pos);
|
|
3102
|
+
}
|
|
3103
|
+
const textValue = arg.slice(1, -1);
|
|
3104
|
+
if (/[\\\u0000-\u001F\u007F]/.test(textValue) || textValue.includes(quote)) {
|
|
3105
|
+
throw syntax("default text cannot contain a backslash, a control character or its own quote", pos);
|
|
3106
|
+
}
|
|
3107
|
+
if (textValue.length > MAX_DEFAULT_TEXT) {
|
|
3108
|
+
throw syntax(`default text is over ${String(MAX_DEFAULT_TEXT)} characters`, pos);
|
|
3109
|
+
}
|
|
3110
|
+
return { name: "default", text: textValue };
|
|
3111
|
+
}
|
|
3112
|
+
function parseOutput(body, pos, scope) {
|
|
3113
|
+
const segments = splitPipes(body);
|
|
3114
|
+
const path = parsePath(segments[0], pos, scope);
|
|
3115
|
+
const filters = segments.slice(1).map((s) => parseFilter(s, pos));
|
|
3116
|
+
if (filters.length > MAX_FILTERS) throw syntax(`At most ${String(MAX_FILTERS)} filters per output`, pos);
|
|
3117
|
+
return { type: "output", path, filters };
|
|
3118
|
+
}
|
|
3119
|
+
function classify(body, pos, scope) {
|
|
3120
|
+
const text = body.trim();
|
|
3121
|
+
if (text === "") throw syntax("Empty {{ }}", pos);
|
|
3122
|
+
const first = text[0];
|
|
3123
|
+
if (first === "#") {
|
|
3124
|
+
const words = text.slice(1).trim().split(/\s+/);
|
|
3125
|
+
const block = words[0];
|
|
3126
|
+
if (block === "if" || block === "unless") {
|
|
3127
|
+
if (words.length !== 2) {
|
|
3128
|
+
throw forbidden(`{{#${block}}} tests one path. Comparisons and expressions are not supported.`, pos);
|
|
3129
|
+
}
|
|
3130
|
+
return { kind: "open", block, path: parsePath(words[1], pos, scope) };
|
|
3131
|
+
}
|
|
3132
|
+
if (block === "each") {
|
|
3133
|
+
if (words.length !== 4 || words[2] !== "as") {
|
|
3134
|
+
throw syntax("Write {{#each path as item}}", pos);
|
|
3135
|
+
}
|
|
3136
|
+
const as = words[3];
|
|
3137
|
+
if (!VAR_RE.test(as) || ROOTS.includes(as) || PROTO_NAMES.has(as) || scope.includes(as)) {
|
|
3138
|
+
throw forbidden(`"${as.slice(0, 30)}" cannot be used as a loop variable name`, pos);
|
|
3139
|
+
}
|
|
3140
|
+
return { kind: "open", block: "each", path: parsePath(words[1], pos, scope), as };
|
|
3141
|
+
}
|
|
3142
|
+
throw forbidden(`{{#${String(block).slice(0, 30)}}} is not supported. Blocks are if, unless and each.`, pos);
|
|
3143
|
+
}
|
|
3144
|
+
if (first === "/") {
|
|
3145
|
+
return { kind: "close", block: text.slice(1).trim() };
|
|
3146
|
+
}
|
|
3147
|
+
if (text === "else") return { kind: "else" };
|
|
3148
|
+
if (!/^[a-z]/.test(first)) {
|
|
3149
|
+
throw forbidden(
|
|
3150
|
+
"Partials, comments, raw output and inverted sections are not supported",
|
|
3151
|
+
pos
|
|
3152
|
+
);
|
|
3153
|
+
}
|
|
3154
|
+
if (/^else\s/.test(text)) throw forbidden("{{else if}} is not supported", pos);
|
|
3155
|
+
return { kind: "output", node: parseOutput(text, pos, scope) };
|
|
3156
|
+
}
|
|
3157
|
+
function staticText(parts, name, pos) {
|
|
3158
|
+
let value = "";
|
|
3159
|
+
for (const part of parts) {
|
|
3160
|
+
if (part.kind === "mustache") {
|
|
3161
|
+
throw new TemplateError(
|
|
3162
|
+
"ATTRIBUTE_NOT_ALLOWED",
|
|
3163
|
+
`${name} must be static text, not a {{ }} value`,
|
|
3164
|
+
pos
|
|
3165
|
+
);
|
|
3166
|
+
}
|
|
3167
|
+
value += part.value;
|
|
3168
|
+
}
|
|
3169
|
+
return value;
|
|
3170
|
+
}
|
|
3171
|
+
function isBlockTag(body) {
|
|
3172
|
+
const t = body.trim();
|
|
3173
|
+
return t.startsWith("#") || t.startsWith("/") || t === "else" || /^else\s/.test(t);
|
|
3174
|
+
}
|
|
3175
|
+
function attrParts(parts, scope, count) {
|
|
3176
|
+
const out = [];
|
|
3177
|
+
for (const part of parts) {
|
|
3178
|
+
count();
|
|
3179
|
+
if (part.kind === "text") {
|
|
3180
|
+
out.push({ type: "text", value: part.value });
|
|
3181
|
+
continue;
|
|
3182
|
+
}
|
|
3183
|
+
if (isBlockTag(part.body)) {
|
|
3184
|
+
throw forbidden("Blocks are not allowed inside an attribute", part.pos);
|
|
3185
|
+
}
|
|
3186
|
+
out.push(classifyOutput(part.body, part.pos, scope));
|
|
3187
|
+
}
|
|
3188
|
+
return out;
|
|
3189
|
+
}
|
|
3190
|
+
function classifyOutput(body, pos, scope) {
|
|
3191
|
+
const tag = classify(body, pos, scope);
|
|
3192
|
+
if (tag.kind !== "output") throw forbidden("Blocks are not allowed inside an attribute", pos);
|
|
3193
|
+
return tag.node;
|
|
3194
|
+
}
|
|
3195
|
+
function buildAttrs(tag, raw2, scope, count) {
|
|
3196
|
+
const attrs = [];
|
|
3197
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3198
|
+
const bad = (message, pos) => new TemplateError("ATTRIBUTE_NOT_ALLOWED", message, pos);
|
|
3199
|
+
for (const attr of raw2) {
|
|
3200
|
+
const { name, pos } = attr;
|
|
3201
|
+
if (name !== name.toLowerCase()) throw bad(`Attribute "${name}" must be lower case`, pos);
|
|
3202
|
+
if (name.startsWith("on")) throw bad(`Event handler attribute "${name}" is not allowed`, pos);
|
|
3203
|
+
if (name === "style") throw bad("The style attribute is not allowed. Use the CSS block.", pos);
|
|
3204
|
+
if (name === "id") throw bad("The id attribute is not allowed", pos);
|
|
3205
|
+
const isData = name.startsWith("data-");
|
|
3206
|
+
const allowed = (ALLOWED_ATTRS["*"] ?? []).includes(name) || (ALLOWED_ATTRS[tag] ?? []).includes(name);
|
|
3207
|
+
if (isData) {
|
|
3208
|
+
if (!DATA_ATTR_RE.test(name) || name.startsWith(RESERVED_DATA_PREFIX)) {
|
|
3209
|
+
throw bad(`"${name}" is not an allowed data attribute (data-b- is reserved)`, pos);
|
|
3210
|
+
}
|
|
3211
|
+
} else if (!allowed) {
|
|
3212
|
+
throw bad(`<${tag}> cannot have the attribute "${name}"`, pos);
|
|
3213
|
+
}
|
|
3214
|
+
if (seen.has(name)) throw bad(`Duplicate attribute "${name}"`, pos);
|
|
3215
|
+
seen.add(name);
|
|
3216
|
+
if (attr.parts === null) throw bad(`Attribute "${name}" needs a value`, pos);
|
|
3217
|
+
const parts = attr.parts;
|
|
3218
|
+
if (isData || name === "class" || name === "aria-hidden" || name === "role") {
|
|
3219
|
+
const value = staticText(parts, name, pos);
|
|
3220
|
+
count();
|
|
3221
|
+
if (/[\u0000-\u001F\u007F]/.test(value) || value.length > MAX_STATIC_ATTR) {
|
|
3222
|
+
throw bad(`"${name}" has a control character or is too long`, pos);
|
|
3223
|
+
}
|
|
3224
|
+
if (name === "class") {
|
|
3225
|
+
const tokens = value.split(/\s+/).filter((t) => t !== "");
|
|
3226
|
+
if (tokens.length === 0 || tokens.length > 10 || !tokens.every((t) => CLASS_TOKEN_RE.test(t))) {
|
|
3227
|
+
throw bad("class takes 1 to 10 tokens of lower case letters, digits and -, starting with a letter", pos);
|
|
3228
|
+
}
|
|
3229
|
+
const reserved = tokens.find(isReservedClass);
|
|
3230
|
+
if (reserved !== void 0) {
|
|
3231
|
+
throw bad(`The class "${reserved}" starts with a prefix reserved for the platform (b-, tb-, tsf-, throttle-)`, pos);
|
|
3232
|
+
}
|
|
3233
|
+
attrs.push({ name, value: [{ type: "text", value: tokens.join(" ") }] });
|
|
3234
|
+
continue;
|
|
3235
|
+
}
|
|
3236
|
+
if (name === "aria-hidden" && value !== "true" && value !== "false") {
|
|
3237
|
+
throw bad('aria-hidden is "true" or "false"', pos);
|
|
3238
|
+
}
|
|
3239
|
+
if (name === "role" && !ROLES.has(value)) {
|
|
3240
|
+
throw bad(`role "${value.slice(0, 30)}" is not allowed`, pos);
|
|
3241
|
+
}
|
|
3242
|
+
attrs.push({ name, value: [{ type: "text", value }] });
|
|
3243
|
+
continue;
|
|
3244
|
+
}
|
|
3245
|
+
if (name === "href" || name === "src") {
|
|
3246
|
+
if (parts.length === 1 && parts[0]?.kind === "mustache") {
|
|
3247
|
+
const out = attrParts(parts, scope, count)[0];
|
|
3248
|
+
if (out.type === "output" && out.filters.length > 0) {
|
|
3249
|
+
throw bad(`${name} takes a value as it is, with no filters`, pos);
|
|
3250
|
+
}
|
|
3251
|
+
attrs.push({ name, value: [out] });
|
|
3252
|
+
continue;
|
|
3253
|
+
}
|
|
3254
|
+
if (name === "src") {
|
|
3255
|
+
throw bad("src must be one {{ }} value: an image setting or an image field", pos);
|
|
3256
|
+
}
|
|
3257
|
+
const value = staticText(parts, name, pos);
|
|
3258
|
+
count();
|
|
3259
|
+
if (safeUrl(value, "href", { mediaOrigin: "" }) === null) {
|
|
3260
|
+
throw bad("A fixed href must be an https:// URL or a path starting with /", pos);
|
|
3261
|
+
}
|
|
3262
|
+
attrs.push({ name, value: [{ type: "text", value }] });
|
|
3263
|
+
continue;
|
|
3264
|
+
}
|
|
3265
|
+
const built = attrParts(parts, scope, count);
|
|
3266
|
+
const staticLength = built.reduce((n, p) => n + (p.type === "text" ? p.value.length : 0), 0);
|
|
3267
|
+
if (staticLength > MAX_STATIC_ATTR) throw bad(`"${name}" is too long`, pos);
|
|
3268
|
+
attrs.push({ name, value: built });
|
|
3269
|
+
}
|
|
3270
|
+
return attrs;
|
|
3271
|
+
}
|
|
3272
|
+
var Parser = class {
|
|
3273
|
+
constructor(tokens) {
|
|
3274
|
+
this.tokens = tokens;
|
|
3275
|
+
this.index = 0;
|
|
3276
|
+
this.nodes = 0;
|
|
3277
|
+
this.depth = 0;
|
|
3278
|
+
this.count = () => {
|
|
3279
|
+
this.nodes += 1;
|
|
3280
|
+
if (this.nodes > LIMITS.templateAstNodes) {
|
|
3281
|
+
throw new TemplateError(
|
|
3282
|
+
"LIMIT_EXCEEDED",
|
|
3283
|
+
`A template has at most ${String(LIMITS.templateAstNodes)} nodes`,
|
|
3284
|
+
this.tokens[this.index - 1]?.pos ?? 0
|
|
3285
|
+
);
|
|
3286
|
+
}
|
|
3287
|
+
};
|
|
3288
|
+
}
|
|
3289
|
+
enter(pos) {
|
|
3290
|
+
this.depth += 1;
|
|
3291
|
+
if (this.depth > LIMITS.templateAstDepth) {
|
|
3292
|
+
throw new TemplateError(
|
|
3293
|
+
"LIMIT_EXCEEDED",
|
|
3294
|
+
`A template nests at most ${String(LIMITS.templateAstDepth)} levels deep`,
|
|
3295
|
+
pos
|
|
3296
|
+
);
|
|
3297
|
+
}
|
|
3298
|
+
}
|
|
3299
|
+
parseAll() {
|
|
3300
|
+
const { nodes, end } = this.parseNodes("root", [], 0, []);
|
|
3301
|
+
if (end !== null) throw this.unexpected(end);
|
|
3302
|
+
return nodes;
|
|
3303
|
+
}
|
|
3304
|
+
unexpected(token) {
|
|
3305
|
+
if (token.kind === "close") {
|
|
3306
|
+
return syntax(`Unexpected </${token.name}>`, token.pos);
|
|
3307
|
+
}
|
|
3308
|
+
if (token.kind === "mustache") {
|
|
3309
|
+
return syntax(`Unexpected {{${token.body.trim()}}}`, token.pos);
|
|
3310
|
+
}
|
|
3311
|
+
return syntax("Unexpected token", token.pos);
|
|
3312
|
+
}
|
|
3313
|
+
/**
|
|
3314
|
+
* Reads nodes until the frame's terminator. The terminator (a closing tag,
|
|
3315
|
+
* `{{else}}` or `{{/x}}`) is consumed and returned for the caller to check.
|
|
3316
|
+
*/
|
|
3317
|
+
parseNodes(frame, scope, eachDepth, ancestors) {
|
|
3318
|
+
const nodes = [];
|
|
3319
|
+
while (this.index < this.tokens.length) {
|
|
3320
|
+
const token = this.tokens[this.index];
|
|
3321
|
+
this.index += 1;
|
|
3322
|
+
if (token.kind === "text") {
|
|
3323
|
+
this.count();
|
|
3324
|
+
nodes.push({ type: "text", value: token.value });
|
|
3325
|
+
continue;
|
|
3326
|
+
}
|
|
3327
|
+
if (token.kind === "close") {
|
|
3328
|
+
return { nodes, end: token };
|
|
3329
|
+
}
|
|
3330
|
+
if (token.kind === "open") {
|
|
3331
|
+
nodes.push(this.parseElement(token, scope, eachDepth, ancestors));
|
|
3332
|
+
continue;
|
|
3333
|
+
}
|
|
3334
|
+
const tag = classify(token.body, token.pos, scope);
|
|
3335
|
+
if (tag.kind === "output") {
|
|
3336
|
+
this.count();
|
|
3337
|
+
nodes.push(tag.node);
|
|
3338
|
+
} else if (tag.kind === "else" || tag.kind === "close") {
|
|
3339
|
+
return { nodes, end: token };
|
|
3340
|
+
} else {
|
|
3341
|
+
nodes.push(this.parseBlock(tag, token.pos, scope, eachDepth, ancestors));
|
|
3342
|
+
}
|
|
3343
|
+
}
|
|
3344
|
+
if (frame !== "root") {
|
|
3345
|
+
const last = this.tokens[this.tokens.length - 1];
|
|
3346
|
+
throw syntax("A tag or block is never closed", last?.pos ?? 0);
|
|
3347
|
+
}
|
|
3348
|
+
return { nodes, end: null };
|
|
3349
|
+
}
|
|
3350
|
+
expectClose(end, block, pos) {
|
|
3351
|
+
if (end === null || end.kind !== "mustache" || end.body.trim() !== `/${block}`) {
|
|
3352
|
+
throw syntax(`{{#${block}}} needs a matching {{/${block}}}`, end?.pos ?? pos);
|
|
3353
|
+
}
|
|
3354
|
+
}
|
|
3355
|
+
parseBlock(tag, pos, scope, eachDepth, ancestors) {
|
|
3356
|
+
this.count();
|
|
3357
|
+
this.enter(pos);
|
|
3358
|
+
let node;
|
|
3359
|
+
if (tag.block === "each") {
|
|
3360
|
+
if (eachDepth >= LIMITS.eachDepth) {
|
|
3361
|
+
throw new TemplateError(
|
|
3362
|
+
"LIMIT_EXCEEDED",
|
|
3363
|
+
`{{#each}} can be nested ${String(LIMITS.eachDepth)} levels deep at most`,
|
|
3364
|
+
pos
|
|
3365
|
+
);
|
|
3366
|
+
}
|
|
3367
|
+
const { nodes, end } = this.parseNodes("block", [...scope, tag.as], eachDepth + 1, ancestors);
|
|
3368
|
+
this.expectClose(end, "each", pos);
|
|
3369
|
+
node = { type: "each", path: tag.path, as: tag.as, body: nodes };
|
|
3370
|
+
} else if (tag.block === "unless") {
|
|
3371
|
+
const { nodes, end } = this.parseNodes("block", scope, eachDepth, ancestors);
|
|
3372
|
+
this.expectClose(end, "unless", pos);
|
|
3373
|
+
node = { type: "unless", path: tag.path, body: nodes };
|
|
3374
|
+
} else {
|
|
3375
|
+
const first = this.parseNodes("block", scope, eachDepth, ancestors);
|
|
3376
|
+
let otherwise = [];
|
|
3377
|
+
let end = first.end;
|
|
3378
|
+
if (end?.kind === "mustache" && end.body.trim() === "else") {
|
|
3379
|
+
const second = this.parseNodes("block", scope, eachDepth, ancestors);
|
|
3380
|
+
otherwise = second.nodes;
|
|
3381
|
+
end = second.end;
|
|
3382
|
+
}
|
|
3383
|
+
this.expectClose(end, "if", pos);
|
|
3384
|
+
node = { type: "if", path: tag.path, then: first.nodes, else: otherwise };
|
|
3385
|
+
}
|
|
3386
|
+
this.depth -= 1;
|
|
3387
|
+
return node;
|
|
3388
|
+
}
|
|
3389
|
+
parseElement(token, scope, eachDepth, ancestors) {
|
|
3390
|
+
const { name, pos } = token;
|
|
3391
|
+
if (!ELEMENTS.has(name)) {
|
|
3392
|
+
throw new TemplateError(
|
|
3393
|
+
"ELEMENT_NOT_ALLOWED",
|
|
3394
|
+
`<${name.slice(0, 30)}> is not an allowed element (lower case ${ALLOWED_ELEMENTS.join(" ")})`,
|
|
3395
|
+
pos
|
|
3396
|
+
);
|
|
3397
|
+
}
|
|
3398
|
+
if (name === "a" && ancestors.includes("a")) {
|
|
3399
|
+
throw new TemplateError("ELEMENT_NOT_ALLOWED", "A link cannot contain another link", pos);
|
|
3400
|
+
}
|
|
3401
|
+
if (CLOSES_P.has(name) && ancestors.includes("p")) {
|
|
3402
|
+
throw new TemplateError("ELEMENT_NOT_ALLOWED", `<${name}> cannot go inside <p>`, pos);
|
|
3403
|
+
}
|
|
3404
|
+
if (HEADINGS.has(name) && ancestors.some((a) => HEADINGS.has(a))) {
|
|
3405
|
+
throw new TemplateError("ELEMENT_NOT_ALLOWED", `<${name}> cannot go inside a heading`, pos);
|
|
3406
|
+
}
|
|
3407
|
+
if (name === "li") {
|
|
3408
|
+
const lastLi = ancestors.lastIndexOf("li");
|
|
3409
|
+
if (lastLi > Math.max(ancestors.lastIndexOf("ul"), ancestors.lastIndexOf("ol"))) {
|
|
3410
|
+
throw new TemplateError("ELEMENT_NOT_ALLOWED", "<li> cannot go directly inside another <li>", pos);
|
|
3411
|
+
}
|
|
3412
|
+
}
|
|
3413
|
+
this.count();
|
|
3414
|
+
this.enter(pos);
|
|
3415
|
+
const attrs = buildAttrs(name, token.attrs, scope, this.count);
|
|
3416
|
+
const element = { type: "element", tag: name, attrs, children: [] };
|
|
3417
|
+
if (!VOIDS.has(name) && !token.selfClosing) {
|
|
3418
|
+
const { nodes, end } = this.parseNodes("element", scope, eachDepth, [...ancestors, name]);
|
|
3419
|
+
if (end === null || end.kind !== "close" || end.name !== name) {
|
|
3420
|
+
throw syntax(`<${name}> needs a matching </${name}>`, end?.pos ?? pos);
|
|
3421
|
+
}
|
|
3422
|
+
element.children = nodes;
|
|
3423
|
+
}
|
|
3424
|
+
this.depth -= 1;
|
|
3425
|
+
return element;
|
|
3426
|
+
}
|
|
3427
|
+
};
|
|
3428
|
+
function parseTemplate(src) {
|
|
3429
|
+
if (typeof src !== "string") {
|
|
3430
|
+
return { ok: false, issues: [makeIssue("template", "INVALID_SHAPE", "The template must be text")] };
|
|
3431
|
+
}
|
|
3432
|
+
const bytes = utf8Length(src);
|
|
3433
|
+
if (bytes > LIMITS.templateSourceBytes) {
|
|
3434
|
+
return {
|
|
3435
|
+
ok: false,
|
|
3436
|
+
issues: [
|
|
3437
|
+
makeIssue(
|
|
3438
|
+
"template",
|
|
3439
|
+
"LIMIT_EXCEEDED",
|
|
3440
|
+
`Template source is ${String(bytes)} bytes, over the limit of ${String(LIMITS.templateSourceBytes)}`
|
|
3441
|
+
)
|
|
3442
|
+
]
|
|
3443
|
+
};
|
|
3444
|
+
}
|
|
3445
|
+
try {
|
|
3446
|
+
if (/[\u0000-\u0008\u000B\u000E-\u001F\u007F]/.test(src)) {
|
|
3447
|
+
throw syntax("The template contains a control character", 0);
|
|
3448
|
+
}
|
|
3449
|
+
if (BIDI_CONTROL_RE.test(src)) {
|
|
3450
|
+
throw syntax("The template contains a bidirectional override character", src.search(BIDI_CONTROL_RE));
|
|
3451
|
+
}
|
|
3452
|
+
const ast = new Parser(lex(src)).parseAll();
|
|
3453
|
+
const weight = templateWeight(ast);
|
|
3454
|
+
if (weight > MAX_TEMPLATE_WEIGHT) {
|
|
3455
|
+
return {
|
|
3456
|
+
ok: false,
|
|
3457
|
+
issues: [
|
|
3458
|
+
makeIssue(
|
|
3459
|
+
"template",
|
|
3460
|
+
"LIMIT_EXCEEDED",
|
|
3461
|
+
`A template can render at most ${String(MAX_TEMPLATE_WEIGHT)} nodes (each node counts 100 times per enclosing {{#each}}); this one can render ${String(weight)}`
|
|
3462
|
+
)
|
|
3463
|
+
]
|
|
3464
|
+
};
|
|
3465
|
+
}
|
|
3466
|
+
return { ok: true, ast };
|
|
3467
|
+
} catch (error) {
|
|
3468
|
+
if (error instanceof TemplateError) {
|
|
3469
|
+
const { line, col } = lineCol(src, error.pos);
|
|
3470
|
+
return {
|
|
3471
|
+
ok: false,
|
|
3472
|
+
issues: [makeIssue(`template:${String(line)}:${String(col)}`, error.code, error.message)]
|
|
3473
|
+
};
|
|
3474
|
+
}
|
|
3475
|
+
throw error;
|
|
3476
|
+
}
|
|
3477
|
+
}
|
|
3478
|
+
|
|
3479
|
+
// src/template/validate.ts
|
|
3480
|
+
var MONEY_PARTS = ["display", "currency"];
|
|
3481
|
+
function inputInfo(accepts, sources) {
|
|
3482
|
+
const fields = /* @__PURE__ */ new Map();
|
|
3483
|
+
for (const source of sources) {
|
|
3484
|
+
if (!accepts.includes(source.result)) continue;
|
|
3485
|
+
for (const spec of source.fields) if (!fields.has(spec.name)) fields.set(spec.name, spec);
|
|
3486
|
+
}
|
|
3487
|
+
return { listLike: accepts.some((a) => a.endsWith("[]")), fields };
|
|
3488
|
+
}
|
|
3489
|
+
var hasChildren = (fields, name) => {
|
|
3490
|
+
const prefix = `${name}.`;
|
|
3491
|
+
for (const key of fields.keys()) if (key.startsWith(prefix)) return true;
|
|
3492
|
+
return false;
|
|
3493
|
+
};
|
|
3494
|
+
function lookup(input, info, binding, rest) {
|
|
3495
|
+
const base = binding === null || binding.prefix === "" ? [] : binding.prefix.split(".");
|
|
3496
|
+
const full = [...base, ...rest];
|
|
3497
|
+
const shown = `${binding === null ? `data.${input}` : input}${rest.length > 0 ? `.${rest.join(".")}` : ""}`;
|
|
3498
|
+
if (rest.length === 0) {
|
|
3499
|
+
if (binding !== null) return binding.scalar ? { kind: "value", type: "string" } : { kind: "record" };
|
|
3500
|
+
return info.listLike ? { kind: "list", binding: { input, prefix: "", scalar: false } } : { kind: "record" };
|
|
3501
|
+
}
|
|
3502
|
+
const name = full.join(".");
|
|
3503
|
+
const leaf = full[full.length - 1];
|
|
3504
|
+
const parent = full.slice(0, -1).join(".");
|
|
3505
|
+
const parentSpec = parent === "" ? void 0 : info.fields.get(parent);
|
|
3506
|
+
if (leaf === "amount" && parent !== "" && (parentSpec?.type === "money" || info.fields.has(`${parent}.display`))) {
|
|
3507
|
+
return {
|
|
3508
|
+
kind: "error",
|
|
3509
|
+
code: "FIELD_NOT_ALLOWED",
|
|
3510
|
+
message: `"${shown}" is a money amount. Templates print money only as ${parent}.display or ${parent}.currency, never as a number.`
|
|
3511
|
+
};
|
|
3512
|
+
}
|
|
3513
|
+
const spec = info.fields.get(name);
|
|
3514
|
+
if (spec !== void 0) {
|
|
3515
|
+
if (spec.type === "list") {
|
|
3516
|
+
return {
|
|
3517
|
+
kind: "list",
|
|
3518
|
+
binding: { input, prefix: name, scalar: !hasChildren(info.fields, name) }
|
|
3519
|
+
};
|
|
3520
|
+
}
|
|
3521
|
+
if (spec.type === "money") {
|
|
3522
|
+
return MONEY_PARTS.includes(leaf) ? { kind: "value", type: "string" } : { kind: "record" };
|
|
3523
|
+
}
|
|
3524
|
+
return { kind: "value", type: spec.type };
|
|
3525
|
+
}
|
|
3526
|
+
if (parentSpec?.type === "money" && MONEY_PARTS.includes(leaf)) {
|
|
3527
|
+
return { kind: "value", type: "string" };
|
|
3528
|
+
}
|
|
3529
|
+
if (hasChildren(info.fields, name)) return { kind: "record" };
|
|
3530
|
+
return {
|
|
3531
|
+
kind: "error",
|
|
3532
|
+
code: "FIELD_NOT_ALLOWED",
|
|
3533
|
+
message: `"${shown}" is not a field of the data sources this input accepts`
|
|
3534
|
+
};
|
|
3535
|
+
}
|
|
3536
|
+
var Checker = class {
|
|
3537
|
+
constructor(ctx) {
|
|
3538
|
+
this.issues = [];
|
|
3539
|
+
this.settings = /* @__PURE__ */ new Map();
|
|
3540
|
+
this.inputs = /* @__PURE__ */ new Map();
|
|
3541
|
+
for (const field2 of ctx.settings) this.settings.set(field2.key, field2);
|
|
3542
|
+
for (const [name, input] of Object.entries(ctx.data)) {
|
|
3543
|
+
this.inputs.set(name, inputInfo(input.accepts, ctx.sources));
|
|
3544
|
+
}
|
|
3545
|
+
}
|
|
3546
|
+
fail(code, message) {
|
|
3547
|
+
this.issues.push(makeIssue("template", code, message));
|
|
3548
|
+
}
|
|
3549
|
+
resolve(path, scope) {
|
|
3550
|
+
const [root, ...rest] = path;
|
|
3551
|
+
const shown = path.join(".");
|
|
3552
|
+
if (root === "locale") return { kind: "locale" };
|
|
3553
|
+
if (root === "settings") {
|
|
3554
|
+
const field2 = this.settings.get(rest[0]);
|
|
3555
|
+
if (field2 === void 0) {
|
|
3556
|
+
return {
|
|
3557
|
+
kind: "error",
|
|
3558
|
+
code: "UNKNOWN_REFERENCE",
|
|
3559
|
+
message: `"${shown}" is not a setting of this widget`
|
|
3560
|
+
};
|
|
3561
|
+
}
|
|
3562
|
+
if (field2.output !== "markup") {
|
|
3563
|
+
return {
|
|
3564
|
+
kind: "error",
|
|
3565
|
+
code: "FIELD_NOT_ALLOWED",
|
|
3566
|
+
message: `"${shown}" is a css setting. It reaches the CSS as var(--w-${field2.key}), not the template.`
|
|
3567
|
+
};
|
|
3568
|
+
}
|
|
3569
|
+
return { kind: "setting", field: field2 };
|
|
3570
|
+
}
|
|
3571
|
+
if (root === "data") {
|
|
3572
|
+
const name = rest[0];
|
|
3573
|
+
const info2 = this.inputs.get(name);
|
|
3574
|
+
if (info2 === void 0) {
|
|
3575
|
+
return {
|
|
3576
|
+
kind: "error",
|
|
3577
|
+
code: "UNKNOWN_REFERENCE",
|
|
3578
|
+
message: `"data.${name}" is not a data input of this widget`
|
|
3579
|
+
};
|
|
3580
|
+
}
|
|
3581
|
+
return lookup(name, info2, null, rest.slice(1));
|
|
3582
|
+
}
|
|
3583
|
+
const binding = scope.get(root);
|
|
3584
|
+
const info = binding === void 0 ? void 0 : this.inputs.get(binding.input);
|
|
3585
|
+
if (binding === void 0 || info === void 0) {
|
|
3586
|
+
return { kind: "error", code: "UNKNOWN_REFERENCE", message: `"${root}" is not in scope` };
|
|
3587
|
+
}
|
|
3588
|
+
return lookup(binding.input, info, binding, rest);
|
|
3589
|
+
}
|
|
3590
|
+
/** Returns the resolved path after checking it is usable at `position`. */
|
|
3591
|
+
use(path, position, scope) {
|
|
3592
|
+
const resolved = this.resolve(path, scope);
|
|
3593
|
+
const shown = path.join(".");
|
|
3594
|
+
const bad = (message) => this.fail("FIELD_NOT_ALLOWED", message);
|
|
3595
|
+
if (resolved.kind === "error") {
|
|
3596
|
+
this.fail(resolved.code, resolved.message);
|
|
3597
|
+
return resolved;
|
|
3598
|
+
}
|
|
3599
|
+
if (position === "test") return resolved;
|
|
3600
|
+
if (position === "href") {
|
|
3601
|
+
const ok = resolved.kind === "setting" && resolved.field.control === "link" || resolved.kind === "value" && resolved.type === "url";
|
|
3602
|
+
if (!ok) bad(`href takes a link setting or a url field, not "${shown}"`);
|
|
3603
|
+
} else if (position === "src") {
|
|
3604
|
+
const ok = resolved.kind === "setting" && resolved.field.control === "image" || resolved.kind === "value" && resolved.type === "image";
|
|
3605
|
+
if (!ok) bad(`src takes an image setting or an image field, not "${shown}"`);
|
|
3606
|
+
} else if (resolved.kind === "list" || resolved.kind === "record") {
|
|
3607
|
+
bad(`"${shown}" is not a single value. Print one of its fields, or loop over it with {{#each}}.`);
|
|
3608
|
+
} else if (resolved.kind === "setting" && (resolved.field.control === "link" || resolved.field.control === "image")) {
|
|
3609
|
+
bad(`"${shown}" is a ${resolved.field.control} setting. Use it as ${resolved.field.control === "link" ? "href" : "src"}.`);
|
|
3610
|
+
}
|
|
3611
|
+
return resolved;
|
|
3612
|
+
}
|
|
3613
|
+
walk(nodes, scope, depth) {
|
|
3614
|
+
if (depth > LIMITS.templateAstDepth) {
|
|
3615
|
+
this.fail("LIMIT_EXCEEDED", `A template nests at most ${String(LIMITS.templateAstDepth)} levels deep`);
|
|
3616
|
+
return;
|
|
3617
|
+
}
|
|
3618
|
+
for (const node of nodes) {
|
|
3619
|
+
switch (node.type) {
|
|
3620
|
+
case "text":
|
|
3621
|
+
break;
|
|
3622
|
+
case "output":
|
|
3623
|
+
this.use(node.path, "text", scope);
|
|
3624
|
+
break;
|
|
3625
|
+
case "if":
|
|
3626
|
+
this.use(node.path, "test", scope);
|
|
3627
|
+
this.walk(node.then, scope, depth + 1);
|
|
3628
|
+
this.walk(node.else, scope, depth + 1);
|
|
3629
|
+
break;
|
|
3630
|
+
case "unless":
|
|
3631
|
+
this.use(node.path, "test", scope);
|
|
3632
|
+
this.walk(node.body, scope, depth + 1);
|
|
3633
|
+
break;
|
|
3634
|
+
case "each": {
|
|
3635
|
+
const resolved = this.use(node.path, "test", scope);
|
|
3636
|
+
const inner = new Map(scope);
|
|
3637
|
+
if (resolved.kind === "list") inner.set(node.as, resolved.binding);
|
|
3638
|
+
else if (resolved.kind !== "error") {
|
|
3639
|
+
this.fail("FIELD_NOT_ALLOWED", `"${node.path.join(".")}" is not a list, so {{#each}} cannot loop over it`);
|
|
3640
|
+
}
|
|
3641
|
+
this.walk(node.body, inner, depth + 1);
|
|
3642
|
+
break;
|
|
3643
|
+
}
|
|
3644
|
+
case "element":
|
|
3645
|
+
for (const attr of node.attrs) {
|
|
3646
|
+
const position = attr.name === "href" ? "href" : attr.name === "src" ? "src" : "text";
|
|
3647
|
+
for (const part of attr.value) {
|
|
3648
|
+
if (part.type === "output") this.use(part.path, position, scope);
|
|
3649
|
+
}
|
|
3650
|
+
}
|
|
3651
|
+
this.walk(node.children, scope, depth + 1);
|
|
3652
|
+
break;
|
|
3653
|
+
}
|
|
3654
|
+
}
|
|
3655
|
+
}
|
|
3656
|
+
};
|
|
3657
|
+
function validateTemplate(ast, ctx) {
|
|
3658
|
+
const checker = new Checker(ctx);
|
|
3659
|
+
checker.walk(ast, /* @__PURE__ */ new Map(), 1);
|
|
3660
|
+
return capIssues(checker.issues);
|
|
3661
|
+
}
|
|
3662
|
+
|
|
3663
|
+
// src/css/sanitize.ts
|
|
3664
|
+
var MAX_NESTING = 12;
|
|
3665
|
+
var MAX_STRING = 200;
|
|
3666
|
+
var MAX_SELECTORS = 20;
|
|
3667
|
+
var MAX_COMPOUNDS = 8;
|
|
3668
|
+
var MAX_PSEUDOS = 4;
|
|
3669
|
+
var CLASS_RE = /^[a-z][a-z0-9-]{0,40}$/;
|
|
3670
|
+
var VAR_NAME_RE = /^--w-[a-z][A-Za-z0-9]{0,39}$/;
|
|
3671
|
+
var WIDGET_TYPE = /^custom\.[a-z][a-z0-9-]{0,63}$/;
|
|
3672
|
+
var CssError = class extends Error {
|
|
3673
|
+
constructor(code, message, pos) {
|
|
3674
|
+
super(message);
|
|
3675
|
+
this.code = code;
|
|
3676
|
+
this.pos = pos;
|
|
3677
|
+
}
|
|
3678
|
+
};
|
|
3679
|
+
var forbidden2 = (message, pos) => new CssError("CSS_FORBIDDEN", message, pos);
|
|
3680
|
+
var syntax2 = (message, pos) => new CssError("CSS_SYNTAX", message, pos);
|
|
3681
|
+
var sides = (base, extra = []) => [
|
|
3682
|
+
base,
|
|
3683
|
+
...["top", "right", "bottom", "left"].map((s) => `${base}-${s}`),
|
|
3684
|
+
...extra
|
|
3685
|
+
];
|
|
3686
|
+
var PROPERTIES = /* @__PURE__ */ new Set([
|
|
3687
|
+
// box model
|
|
3688
|
+
"display",
|
|
3689
|
+
"box-sizing",
|
|
3690
|
+
"width",
|
|
3691
|
+
"min-width",
|
|
3692
|
+
"max-width",
|
|
3693
|
+
"height",
|
|
3694
|
+
"min-height",
|
|
3695
|
+
"max-height",
|
|
3696
|
+
"aspect-ratio",
|
|
3697
|
+
"overflow",
|
|
3698
|
+
"overflow-x",
|
|
3699
|
+
"overflow-y",
|
|
3700
|
+
"object-fit",
|
|
3701
|
+
"object-position",
|
|
3702
|
+
"float",
|
|
3703
|
+
"clear",
|
|
3704
|
+
"vertical-align",
|
|
3705
|
+
"visibility",
|
|
3706
|
+
...sides("margin"),
|
|
3707
|
+
...sides("padding"),
|
|
3708
|
+
// flex and grid
|
|
3709
|
+
"flex",
|
|
3710
|
+
"flex-direction",
|
|
3711
|
+
"flex-wrap",
|
|
3712
|
+
"flex-flow",
|
|
3713
|
+
"flex-grow",
|
|
3714
|
+
"flex-shrink",
|
|
3715
|
+
"flex-basis",
|
|
3716
|
+
"order",
|
|
3717
|
+
"gap",
|
|
3718
|
+
"row-gap",
|
|
3719
|
+
"column-gap",
|
|
3720
|
+
"justify-content",
|
|
3721
|
+
"justify-items",
|
|
3722
|
+
"justify-self",
|
|
3723
|
+
"align-items",
|
|
3724
|
+
"align-content",
|
|
3725
|
+
"align-self",
|
|
3726
|
+
"place-items",
|
|
3727
|
+
"place-content",
|
|
3728
|
+
"place-self",
|
|
3729
|
+
"grid-template",
|
|
3730
|
+
"grid-template-columns",
|
|
3731
|
+
"grid-template-rows",
|
|
3732
|
+
"grid-template-areas",
|
|
3733
|
+
"grid-auto-columns",
|
|
3734
|
+
"grid-auto-rows",
|
|
3735
|
+
"grid-auto-flow",
|
|
3736
|
+
"grid-column",
|
|
3737
|
+
"grid-row",
|
|
3738
|
+
"grid-area",
|
|
3739
|
+
"grid-column-start",
|
|
3740
|
+
"grid-column-end",
|
|
3741
|
+
"grid-row-start",
|
|
3742
|
+
"grid-row-end",
|
|
3743
|
+
// typography
|
|
3744
|
+
"font-family",
|
|
3745
|
+
"font-size",
|
|
3746
|
+
"font-weight",
|
|
3747
|
+
"font-style",
|
|
3748
|
+
"line-height",
|
|
3749
|
+
"letter-spacing",
|
|
3750
|
+
"word-spacing",
|
|
3751
|
+
"text-align",
|
|
3752
|
+
"text-decoration",
|
|
3753
|
+
"text-decoration-line",
|
|
3754
|
+
"text-decoration-color",
|
|
3755
|
+
"text-decoration-style",
|
|
3756
|
+
"text-decoration-thickness",
|
|
3757
|
+
"text-transform",
|
|
3758
|
+
"text-overflow",
|
|
3759
|
+
"text-indent",
|
|
3760
|
+
"white-space",
|
|
3761
|
+
"word-break",
|
|
3762
|
+
"overflow-wrap",
|
|
3763
|
+
"list-style",
|
|
3764
|
+
"list-style-type",
|
|
3765
|
+
"list-style-position",
|
|
3766
|
+
// colour
|
|
3767
|
+
"color",
|
|
3768
|
+
"opacity",
|
|
3769
|
+
"background",
|
|
3770
|
+
"background-color",
|
|
3771
|
+
"background-image",
|
|
3772
|
+
"background-position",
|
|
3773
|
+
"background-size",
|
|
3774
|
+
"background-repeat",
|
|
3775
|
+
// border, radius, shadow
|
|
3776
|
+
"border",
|
|
3777
|
+
"border-width",
|
|
3778
|
+
"border-style",
|
|
3779
|
+
"border-color",
|
|
3780
|
+
"border-collapse",
|
|
3781
|
+
"border-spacing",
|
|
3782
|
+
...sides("border"),
|
|
3783
|
+
...["top", "right", "bottom", "left"].flatMap((s) => [
|
|
3784
|
+
`border-${s}-width`,
|
|
3785
|
+
`border-${s}-style`,
|
|
3786
|
+
`border-${s}-color`
|
|
3787
|
+
]),
|
|
3788
|
+
"border-radius",
|
|
3789
|
+
"border-top-left-radius",
|
|
3790
|
+
"border-top-right-radius",
|
|
3791
|
+
"border-bottom-left-radius",
|
|
3792
|
+
"border-bottom-right-radius",
|
|
3793
|
+
"outline",
|
|
3794
|
+
"outline-width",
|
|
3795
|
+
"outline-style",
|
|
3796
|
+
"outline-color",
|
|
3797
|
+
"outline-offset",
|
|
3798
|
+
"box-shadow",
|
|
3799
|
+
"text-shadow",
|
|
3800
|
+
// transform and transition
|
|
3801
|
+
"transform",
|
|
3802
|
+
"transform-origin",
|
|
3803
|
+
"translate",
|
|
3804
|
+
"rotate",
|
|
3805
|
+
"scale",
|
|
3806
|
+
"transition",
|
|
3807
|
+
"transition-property",
|
|
3808
|
+
"transition-duration",
|
|
3809
|
+
"transition-timing-function",
|
|
3810
|
+
"transition-delay"
|
|
3811
|
+
]);
|
|
3812
|
+
var STRING_PROPERTIES = /* @__PURE__ */ new Set([
|
|
3813
|
+
"font-family",
|
|
3814
|
+
"grid-template",
|
|
3815
|
+
"grid-template-areas"
|
|
3816
|
+
]);
|
|
3817
|
+
var FUNCTIONS = /* @__PURE__ */ new Set([
|
|
3818
|
+
"calc",
|
|
3819
|
+
"min",
|
|
3820
|
+
"max",
|
|
3821
|
+
"clamp",
|
|
3822
|
+
"var",
|
|
3823
|
+
"rgb",
|
|
3824
|
+
"rgba",
|
|
3825
|
+
"hsl",
|
|
3826
|
+
"hsla",
|
|
3827
|
+
"linear-gradient",
|
|
3828
|
+
"radial-gradient",
|
|
3829
|
+
"conic-gradient",
|
|
3830
|
+
"repeating-linear-gradient",
|
|
3831
|
+
"repeating-radial-gradient",
|
|
3832
|
+
"translate",
|
|
3833
|
+
"translatex",
|
|
3834
|
+
"translatey",
|
|
3835
|
+
"translate3d",
|
|
3836
|
+
"rotate",
|
|
3837
|
+
"rotatex",
|
|
3838
|
+
"rotatey",
|
|
3839
|
+
"rotatez",
|
|
3840
|
+
"scale",
|
|
3841
|
+
"scalex",
|
|
3842
|
+
"scaley",
|
|
3843
|
+
"skew",
|
|
3844
|
+
"skewx",
|
|
3845
|
+
"skewy",
|
|
3846
|
+
"cubic-bezier",
|
|
3847
|
+
"steps",
|
|
3848
|
+
"minmax",
|
|
3849
|
+
"repeat",
|
|
3850
|
+
"fit-content"
|
|
3851
|
+
]);
|
|
3852
|
+
var LOADING_FUNCTIONS = /* @__PURE__ */ new Set([
|
|
3853
|
+
"url",
|
|
3854
|
+
"src",
|
|
3855
|
+
"image",
|
|
3856
|
+
"image-set",
|
|
3857
|
+
"-webkit-image-set",
|
|
3858
|
+
"cross-fade",
|
|
3859
|
+
"element",
|
|
3860
|
+
"paint",
|
|
3861
|
+
"expression",
|
|
3862
|
+
"attr",
|
|
3863
|
+
"env"
|
|
3864
|
+
]);
|
|
3865
|
+
var FORBIDDEN_IDENTS = /* @__PURE__ */ new Set([
|
|
3866
|
+
"url",
|
|
3867
|
+
"expression",
|
|
3868
|
+
"image-set",
|
|
3869
|
+
"javascript",
|
|
3870
|
+
"vbscript",
|
|
3871
|
+
"behavior",
|
|
3872
|
+
"binding"
|
|
3873
|
+
]);
|
|
3874
|
+
var UNITS = /* @__PURE__ */ new Set([
|
|
3875
|
+
"px",
|
|
3876
|
+
"em",
|
|
3877
|
+
"rem",
|
|
3878
|
+
"vh",
|
|
3879
|
+
"vw",
|
|
3880
|
+
"vmin",
|
|
3881
|
+
"vmax",
|
|
3882
|
+
"svh",
|
|
3883
|
+
"lvh",
|
|
3884
|
+
"dvh",
|
|
3885
|
+
"ch",
|
|
3886
|
+
"ex",
|
|
3887
|
+
"fr",
|
|
3888
|
+
"deg",
|
|
3889
|
+
"rad",
|
|
3890
|
+
"turn",
|
|
3891
|
+
"s",
|
|
3892
|
+
"ms"
|
|
3893
|
+
]);
|
|
3894
|
+
var PSEUDO_CLASSES = /* @__PURE__ */ new Set([
|
|
3895
|
+
"hover",
|
|
3896
|
+
"focus",
|
|
3897
|
+
"focus-visible",
|
|
3898
|
+
"focus-within",
|
|
3899
|
+
"active",
|
|
3900
|
+
"first-child",
|
|
3901
|
+
"last-child",
|
|
3902
|
+
"only-child",
|
|
3903
|
+
"first-of-type",
|
|
3904
|
+
"last-of-type",
|
|
3905
|
+
"empty"
|
|
3906
|
+
]);
|
|
3907
|
+
var NTH_FUNCTIONS = /* @__PURE__ */ new Set([
|
|
3908
|
+
"nth-child",
|
|
3909
|
+
"nth-last-child",
|
|
3910
|
+
"nth-of-type",
|
|
3911
|
+
"nth-last-of-type"
|
|
3912
|
+
]);
|
|
3913
|
+
var ELEMENTS2 = new Set(ALLOWED_ELEMENTS);
|
|
3914
|
+
var MIN_WIDTHS = [768, 1025];
|
|
3915
|
+
var MAX_WIDTHS = [767, 1024];
|
|
3916
|
+
var isAlpha = (c) => c !== void 0 && (c >= "a" && c <= "z" || c >= "A" && c <= "Z");
|
|
3917
|
+
var isDigit2 = (c) => c !== void 0 && c >= "0" && c <= "9";
|
|
3918
|
+
var isWs = (c) => c === " " || c === " " || c === "\n" || c === "\r" || c === "\f";
|
|
3919
|
+
var identStart = (css, i) => {
|
|
3920
|
+
const c = css[i];
|
|
3921
|
+
if (isAlpha(c) || c === "_") return true;
|
|
3922
|
+
if (c === "-") return isAlpha(css[i + 1]) || css[i + 1] === "_" || css[i + 1] === "-";
|
|
3923
|
+
return false;
|
|
3924
|
+
};
|
|
3925
|
+
var startsNumber = (css, i) => {
|
|
3926
|
+
const c = css[i];
|
|
3927
|
+
if (isDigit2(c)) return true;
|
|
3928
|
+
if (c === ".") return isDigit2(css[i + 1]);
|
|
3929
|
+
if (c === "+" || c === "-") return isDigit2(css[i + 1]) || css[i + 1] === "." && isDigit2(css[i + 2]);
|
|
3930
|
+
return false;
|
|
3931
|
+
};
|
|
3932
|
+
function tokenize(css) {
|
|
3933
|
+
const out = [];
|
|
3934
|
+
let i = 0;
|
|
3935
|
+
while (i < css.length) {
|
|
3936
|
+
const c = css[i];
|
|
3937
|
+
const code = c.charCodeAt(0);
|
|
3938
|
+
const pos = i;
|
|
3939
|
+
if (c === "\\") {
|
|
3940
|
+
throw forbidden2("Backslash escapes are not allowed", pos);
|
|
3941
|
+
}
|
|
3942
|
+
if (code > 126 || code < 32 && !isWs(c)) {
|
|
3943
|
+
throw syntax2("Only printable ASCII is allowed outside comments", pos);
|
|
3944
|
+
}
|
|
3945
|
+
if (isWs(c)) {
|
|
3946
|
+
while (isWs(css[i])) i += 1;
|
|
3947
|
+
out.push({ t: "ws", pos });
|
|
3948
|
+
} else if (c === "/" && css[i + 1] === "*") {
|
|
3949
|
+
const end = css.indexOf("*/", i + 2);
|
|
3950
|
+
if (end === -1) throw syntax2("This comment is never closed", pos);
|
|
3951
|
+
i = end + 2;
|
|
3952
|
+
out.push({ t: "ws", pos });
|
|
3953
|
+
} else if (c === '"' || c === "'") {
|
|
3954
|
+
i += 1;
|
|
3955
|
+
let value = "";
|
|
3956
|
+
for (; ; ) {
|
|
3957
|
+
const ch = css[i];
|
|
3958
|
+
if (ch === void 0 || ch === "\n" || ch === "\r" || ch === "\f") {
|
|
3959
|
+
throw syntax2("This string is never closed", pos);
|
|
3960
|
+
}
|
|
3961
|
+
if (ch === c) break;
|
|
3962
|
+
const cc = ch.charCodeAt(0);
|
|
3963
|
+
if (ch === "\\") throw forbidden2("Backslash escapes are not allowed", i);
|
|
3964
|
+
if (cc < 32 || cc > 126 || ch === "<" || ch === ">" || ch === '"') {
|
|
3965
|
+
throw forbidden2('A string can hold printable ASCII except < > and "', i);
|
|
3966
|
+
}
|
|
3967
|
+
value += ch;
|
|
3968
|
+
i += 1;
|
|
3969
|
+
}
|
|
3970
|
+
i += 1;
|
|
3971
|
+
if (value.length > MAX_STRING) throw syntax2("This string is too long", pos);
|
|
3972
|
+
out.push({ t: "string", v: value, pos });
|
|
3973
|
+
} else if (c === "#") {
|
|
3974
|
+
let value = "";
|
|
3975
|
+
i += 1;
|
|
3976
|
+
while (isAlpha(css[i]) || isDigit2(css[i]) || css[i] === "_" || css[i] === "-") {
|
|
3977
|
+
value += css[i];
|
|
3978
|
+
i += 1;
|
|
3979
|
+
}
|
|
3980
|
+
out.push({ t: "hash", v: value, pos });
|
|
3981
|
+
} else if (c === "@") {
|
|
3982
|
+
i += 1;
|
|
3983
|
+
if (!identStart(css, i)) throw syntax2("A stray @", pos);
|
|
3984
|
+
let value = "";
|
|
3985
|
+
while (isAlpha(css[i]) || isDigit2(css[i]) || css[i] === "_" || css[i] === "-") {
|
|
3986
|
+
value += css[i];
|
|
3987
|
+
i += 1;
|
|
3988
|
+
}
|
|
3989
|
+
out.push({ t: "atkw", v: value, pos });
|
|
3990
|
+
} else if (startsNumber(css, i)) {
|
|
3991
|
+
let value = "";
|
|
3992
|
+
if (css[i] === "+" || css[i] === "-") {
|
|
3993
|
+
value += css[i];
|
|
3994
|
+
i += 1;
|
|
3995
|
+
}
|
|
3996
|
+
while (isDigit2(css[i])) {
|
|
3997
|
+
value += css[i];
|
|
3998
|
+
i += 1;
|
|
3999
|
+
}
|
|
4000
|
+
if (css[i] === "." && isDigit2(css[i + 1])) {
|
|
4001
|
+
value += ".";
|
|
4002
|
+
i += 1;
|
|
4003
|
+
while (isDigit2(css[i])) {
|
|
4004
|
+
value += css[i];
|
|
4005
|
+
i += 1;
|
|
4006
|
+
}
|
|
4007
|
+
}
|
|
4008
|
+
if (value.length > 16) throw syntax2("This number is too long", pos);
|
|
4009
|
+
if (css[i] === "%") {
|
|
4010
|
+
i += 1;
|
|
4011
|
+
out.push({ t: "pct", v: value, pos });
|
|
4012
|
+
} else if (isAlpha(css[i])) {
|
|
4013
|
+
let unit = "";
|
|
4014
|
+
while (isAlpha(css[i]) || isDigit2(css[i]) || css[i] === "_" || css[i] === "-") {
|
|
4015
|
+
unit += css[i];
|
|
4016
|
+
i += 1;
|
|
4017
|
+
}
|
|
4018
|
+
out.push({ t: "dim", v: value, unit, pos });
|
|
4019
|
+
} else out.push({ t: "num", v: value, pos });
|
|
4020
|
+
} else if (identStart(css, i)) {
|
|
4021
|
+
let value = "";
|
|
4022
|
+
while (isAlpha(css[i]) || isDigit2(css[i]) || css[i] === "_" || css[i] === "-") {
|
|
4023
|
+
value += css[i];
|
|
4024
|
+
i += 1;
|
|
4025
|
+
}
|
|
4026
|
+
if (css[i] === "(") {
|
|
4027
|
+
const name = value.toLowerCase();
|
|
4028
|
+
if (LOADING_FUNCTIONS.has(name)) {
|
|
4029
|
+
throw forbidden2(`${name}( can load a resource or run code and is not allowed`, pos);
|
|
4030
|
+
}
|
|
4031
|
+
i += 1;
|
|
4032
|
+
out.push({ t: "func", v: value, pos });
|
|
4033
|
+
} else out.push({ t: "ident", v: value, pos });
|
|
4034
|
+
} else {
|
|
4035
|
+
i += 1;
|
|
4036
|
+
if (c === ":") out.push({ t: "colon", pos });
|
|
4037
|
+
else if (c === ";") out.push({ t: "semi", pos });
|
|
4038
|
+
else if (c === ",") out.push({ t: "comma", pos });
|
|
4039
|
+
else if (c === "(" || c === "[" || c === "{") out.push({ t: "open", v: c, pos });
|
|
4040
|
+
else if (c === ")" || c === "]" || c === "}") out.push({ t: "close", v: c, pos });
|
|
4041
|
+
else out.push({ t: "delim", v: c, pos });
|
|
4042
|
+
}
|
|
4043
|
+
}
|
|
4044
|
+
return out;
|
|
4045
|
+
}
|
|
4046
|
+
var PAIRS = { "(": ")", "[": "]", "{": "}" };
|
|
4047
|
+
function buildTree(tokens) {
|
|
4048
|
+
const root = [];
|
|
4049
|
+
const stack = [];
|
|
4050
|
+
let current = root;
|
|
4051
|
+
for (const tok of tokens) {
|
|
4052
|
+
if (tok.t === "open" || tok.t === "func") {
|
|
4053
|
+
if (stack.length >= MAX_NESTING) throw syntax2("Too many nested brackets", tok.pos);
|
|
4054
|
+
const children = [];
|
|
4055
|
+
const node = tok.t === "open" ? { t: "block", v: tok.v, children, pos: tok.pos } : { t: "fn", v: tok.v, children, pos: tok.pos };
|
|
4056
|
+
current.push(node);
|
|
4057
|
+
stack.push({ node, close: tok.t === "open" ? PAIRS[tok.v] : ")", children: current });
|
|
4058
|
+
current = children;
|
|
4059
|
+
} else if (tok.t === "close") {
|
|
4060
|
+
const top = stack.pop();
|
|
4061
|
+
if (top === void 0 || top.close !== tok.v) {
|
|
4062
|
+
throw syntax2(`Unbalanced "${tok.v}"`, tok.pos);
|
|
4063
|
+
}
|
|
4064
|
+
current = top.children;
|
|
4065
|
+
} else current.push(tok);
|
|
4066
|
+
}
|
|
4067
|
+
const open = stack[stack.length - 1];
|
|
4068
|
+
if (open !== void 0) throw syntax2(`Unbalanced: "${open.close === ")" ? "(" : open.close === "}" ? "{" : "["}" is never closed`, open.node.pos);
|
|
4069
|
+
return root;
|
|
4070
|
+
}
|
|
4071
|
+
var isWsNode = (n) => n.t === "ws";
|
|
4072
|
+
var trimWs = (nodes) => {
|
|
4073
|
+
let start = 0;
|
|
4074
|
+
let end = nodes.length;
|
|
4075
|
+
while (start < end && isWsNode(nodes[start])) start += 1;
|
|
4076
|
+
while (end > start && isWsNode(nodes[end - 1])) end -= 1;
|
|
4077
|
+
return nodes.slice(start, end);
|
|
4078
|
+
};
|
|
4079
|
+
function raw(node) {
|
|
4080
|
+
switch (node.t) {
|
|
4081
|
+
case "ident":
|
|
4082
|
+
case "num":
|
|
4083
|
+
return node.v;
|
|
4084
|
+
case "dim":
|
|
4085
|
+
return node.v + node.unit;
|
|
4086
|
+
case "delim":
|
|
4087
|
+
return node.v;
|
|
4088
|
+
default:
|
|
4089
|
+
return "?";
|
|
4090
|
+
}
|
|
4091
|
+
}
|
|
4092
|
+
function parseCompoundList(nodes, pos) {
|
|
4093
|
+
const selectors = [];
|
|
4094
|
+
let part = [];
|
|
4095
|
+
const parts = [];
|
|
4096
|
+
for (const node of nodes) {
|
|
4097
|
+
if (node.t === "comma") {
|
|
4098
|
+
parts.push(part);
|
|
4099
|
+
part = [];
|
|
4100
|
+
} else part.push(node);
|
|
4101
|
+
}
|
|
4102
|
+
parts.push(part);
|
|
4103
|
+
if (parts.length > MAX_SELECTORS) throw syntax2(`At most ${String(MAX_SELECTORS)} selectors per rule`, pos);
|
|
4104
|
+
for (const p of parts) selectors.push(parseSelector(trimWs(p), pos));
|
|
4105
|
+
return selectors;
|
|
4106
|
+
}
|
|
4107
|
+
function parseSelector(nodes, pos) {
|
|
4108
|
+
if (nodes.length === 0) throw syntax2("An empty selector", pos);
|
|
4109
|
+
let out = "";
|
|
4110
|
+
let compounds = 0;
|
|
4111
|
+
let inCompound = false;
|
|
4112
|
+
let expectCompound = true;
|
|
4113
|
+
let pseudos = 0;
|
|
4114
|
+
const pseudo = (at2) => {
|
|
4115
|
+
pseudos += 1;
|
|
4116
|
+
if (pseudos > MAX_PSEUDOS) {
|
|
4117
|
+
throw forbidden2(`At most ${String(MAX_PSEUDOS)} pseudo-classes per compound selector`, at2);
|
|
4118
|
+
}
|
|
4119
|
+
};
|
|
4120
|
+
const begin = (at2) => {
|
|
4121
|
+
if (inCompound) return;
|
|
4122
|
+
if (!expectCompound) out += " ";
|
|
4123
|
+
compounds += 1;
|
|
4124
|
+
if (compounds > MAX_COMPOUNDS) {
|
|
4125
|
+
throw syntax2(`At most ${String(MAX_COMPOUNDS)} parts in a selector`, at2);
|
|
4126
|
+
}
|
|
4127
|
+
inCompound = true;
|
|
4128
|
+
expectCompound = false;
|
|
4129
|
+
pseudos = 0;
|
|
4130
|
+
};
|
|
4131
|
+
for (let i = 0; i < nodes.length; i += 1) {
|
|
4132
|
+
const node = nodes[i];
|
|
4133
|
+
const next = nodes[i + 1];
|
|
4134
|
+
if (node.t === "ws") {
|
|
4135
|
+
inCompound = false;
|
|
4136
|
+
continue;
|
|
4137
|
+
}
|
|
4138
|
+
if (node.t === "delim" && (node.v === ">" || node.v === "+" || node.v === "~")) {
|
|
4139
|
+
if (expectCompound) throw syntax2(`Unexpected "${node.v}" in a selector`, node.pos);
|
|
4140
|
+
out += ` ${node.v} `;
|
|
4141
|
+
expectCompound = true;
|
|
4142
|
+
inCompound = false;
|
|
4143
|
+
continue;
|
|
4144
|
+
}
|
|
4145
|
+
if (node.t === "ident" || node.t === "delim" && node.v === "*") {
|
|
4146
|
+
if (inCompound) throw syntax2("A type selector must start a compound selector", node.pos);
|
|
4147
|
+
const name = node.t === "ident" ? node.v.toLowerCase() : "*";
|
|
4148
|
+
if (node.t === "ident" && !ELEMENTS2.has(name)) {
|
|
4149
|
+
throw forbidden2(`"${node.v}" is not an element a template can use`, node.pos);
|
|
4150
|
+
}
|
|
4151
|
+
begin(node.pos);
|
|
4152
|
+
out += name;
|
|
4153
|
+
continue;
|
|
4154
|
+
}
|
|
4155
|
+
if (node.t === "delim" && node.v === ".") {
|
|
4156
|
+
if (next?.t !== "ident") throw syntax2("A class selector is .name", node.pos);
|
|
4157
|
+
if (!CLASS_RE.test(next.v)) {
|
|
4158
|
+
throw forbidden2(`The class "${next.v}" must be lower case letters, digits and -`, next.pos);
|
|
4159
|
+
}
|
|
4160
|
+
if (isReservedClass(next.v)) {
|
|
4161
|
+
throw forbidden2(`The class "${next.v}" starts with a prefix reserved for the platform`, next.pos);
|
|
4162
|
+
}
|
|
4163
|
+
begin(node.pos);
|
|
4164
|
+
out += `.${next.v}`;
|
|
4165
|
+
i += 1;
|
|
4166
|
+
continue;
|
|
4167
|
+
}
|
|
4168
|
+
if (node.t === "colon") {
|
|
4169
|
+
if (next?.t === "colon") throw forbidden2("Pseudo-elements (::) are not allowed", node.pos);
|
|
4170
|
+
if (next?.t === "ident") {
|
|
4171
|
+
const name = next.v.toLowerCase();
|
|
4172
|
+
if (!PSEUDO_CLASSES.has(name)) throw forbidden2(`:${next.v} is not allowed`, next.pos);
|
|
4173
|
+
begin(node.pos);
|
|
4174
|
+
pseudo(node.pos);
|
|
4175
|
+
out += `:${name}`;
|
|
4176
|
+
i += 1;
|
|
4177
|
+
continue;
|
|
4178
|
+
}
|
|
4179
|
+
if (next?.t === "fn") {
|
|
4180
|
+
const name = next.v.toLowerCase();
|
|
4181
|
+
const arg = trimWs(next.children).map(raw).join("");
|
|
4182
|
+
if (!NTH_FUNCTIONS.has(name) || !/^(?:odd|even|[+-]?[0-9]*n(?:[+-][0-9]+)?|[+-]?[0-9]+)$/i.test(arg)) {
|
|
4183
|
+
throw forbidden2(`:${next.v}(...) is not allowed`, next.pos);
|
|
4184
|
+
}
|
|
4185
|
+
begin(node.pos);
|
|
4186
|
+
pseudo(node.pos);
|
|
4187
|
+
out += `:${name}(${arg.toLowerCase()})`;
|
|
4188
|
+
i += 1;
|
|
4189
|
+
continue;
|
|
4190
|
+
}
|
|
4191
|
+
throw syntax2("A pseudo-class is :name", node.pos);
|
|
4192
|
+
}
|
|
4193
|
+
if (node.t === "hash") throw forbidden2("ID selectors are not allowed", node.pos);
|
|
4194
|
+
if (node.t === "block" && node.v === "[") {
|
|
4195
|
+
throw forbidden2("Attribute selectors are not allowed", node.pos);
|
|
4196
|
+
}
|
|
4197
|
+
throw forbidden2("This is not allowed in a selector", node.pos);
|
|
4198
|
+
}
|
|
4199
|
+
if (expectCompound) {
|
|
4200
|
+
throw syntax2("A selector cannot end with a combinator", nodes[nodes.length - 1]?.pos ?? pos);
|
|
4201
|
+
}
|
|
4202
|
+
return out;
|
|
4203
|
+
}
|
|
4204
|
+
function serializeValue(nodes, property, vars, nested) {
|
|
4205
|
+
let out = "";
|
|
4206
|
+
let space = false;
|
|
4207
|
+
let afterComma = false;
|
|
4208
|
+
const emit = (text) => {
|
|
4209
|
+
if (space && out !== "" && !afterComma) out += " ";
|
|
4210
|
+
out += text;
|
|
4211
|
+
space = false;
|
|
4212
|
+
afterComma = false;
|
|
4213
|
+
};
|
|
4214
|
+
for (const node of trimWs(nodes)) {
|
|
4215
|
+
switch (node.t) {
|
|
4216
|
+
case "ws":
|
|
4217
|
+
space = true;
|
|
4218
|
+
break;
|
|
4219
|
+
case "ident": {
|
|
4220
|
+
const lower = node.v.toLowerCase();
|
|
4221
|
+
if (node.v.startsWith("--")) throw forbidden2("Custom properties are only read through var(--w-<setting>)", node.pos);
|
|
4222
|
+
if (FORBIDDEN_IDENTS.has(lower)) throw forbidden2(`"${node.v}" is not allowed in a value`, node.pos);
|
|
4223
|
+
emit(node.v);
|
|
4224
|
+
break;
|
|
4225
|
+
}
|
|
4226
|
+
case "num":
|
|
4227
|
+
emit(node.v);
|
|
4228
|
+
break;
|
|
4229
|
+
case "pct":
|
|
4230
|
+
emit(`${node.v}%`);
|
|
4231
|
+
break;
|
|
4232
|
+
case "dim": {
|
|
4233
|
+
const unit = node.unit.toLowerCase();
|
|
4234
|
+
if (!UNITS.has(unit)) throw forbidden2(`The unit "${node.unit}" is not allowed`, node.pos);
|
|
4235
|
+
emit(node.v + unit);
|
|
4236
|
+
break;
|
|
4237
|
+
}
|
|
4238
|
+
case "hash":
|
|
4239
|
+
if (!/^(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(node.v)) {
|
|
4240
|
+
throw forbidden2("A # value must be a hex colour", node.pos);
|
|
4241
|
+
}
|
|
4242
|
+
emit(`#${node.v.toLowerCase()}`);
|
|
4243
|
+
break;
|
|
4244
|
+
case "string":
|
|
4245
|
+
if (!STRING_PROPERTIES.has(property)) {
|
|
4246
|
+
throw forbidden2(`${property} does not take a string`, node.pos);
|
|
4247
|
+
}
|
|
4248
|
+
emit(`"${node.v}"`);
|
|
4249
|
+
break;
|
|
4250
|
+
case "comma":
|
|
4251
|
+
if (out === "" || afterComma) throw syntax2("Unexpected ,", node.pos);
|
|
4252
|
+
out += ",";
|
|
4253
|
+
space = false;
|
|
4254
|
+
afterComma = true;
|
|
4255
|
+
break;
|
|
4256
|
+
case "delim":
|
|
4257
|
+
if (node.v === "!") throw forbidden2("!important is not allowed", node.pos);
|
|
4258
|
+
if (node.v !== "/" && node.v !== "+" && node.v !== "-" && node.v !== "*") {
|
|
4259
|
+
throw syntax2(`"${node.v}" is not allowed in a value`, node.pos);
|
|
4260
|
+
}
|
|
4261
|
+
emit(node.v);
|
|
4262
|
+
break;
|
|
4263
|
+
case "colon":
|
|
4264
|
+
throw forbidden2('":" is not allowed in a value', node.pos);
|
|
4265
|
+
case "semi":
|
|
4266
|
+
throw syntax2("Unexpected ;", node.pos);
|
|
4267
|
+
case "atkw":
|
|
4268
|
+
throw forbidden2("@ rules are not allowed in a value", node.pos);
|
|
4269
|
+
case "block":
|
|
4270
|
+
if (node.v !== "(" || !nested) {
|
|
4271
|
+
throw forbidden2(`"${node.v}" blocks are not allowed in a value`, node.pos);
|
|
4272
|
+
}
|
|
4273
|
+
emit(`(${serializeValue(node.children, property, vars, true)})`);
|
|
4274
|
+
break;
|
|
4275
|
+
case "fn": {
|
|
4276
|
+
const name = node.v.toLowerCase();
|
|
4277
|
+
if (!FUNCTIONS.has(name)) throw forbidden2(`The function ${node.v}() is not allowed`, node.pos);
|
|
4278
|
+
if (name === "var") emit(`var(${serializeVar(node, property, vars)})`);
|
|
4279
|
+
else emit(`${name}(${serializeValue(node.children, property, vars, true)})`);
|
|
4280
|
+
break;
|
|
4281
|
+
}
|
|
4282
|
+
}
|
|
4283
|
+
}
|
|
4284
|
+
return out;
|
|
4285
|
+
}
|
|
4286
|
+
function serializeVar(node, property, vars) {
|
|
4287
|
+
const children = trimWs(node.children);
|
|
4288
|
+
const first = children[0];
|
|
4289
|
+
if (first?.t !== "ident" || !VAR_NAME_RE.test(first.v)) {
|
|
4290
|
+
throw forbidden2("var() reads only --w-<setting key> custom properties", node.pos);
|
|
4291
|
+
}
|
|
4292
|
+
vars.add(first.v.slice("--w-".length));
|
|
4293
|
+
const rest = children.slice(1);
|
|
4294
|
+
if (rest.length === 0) return first.v;
|
|
4295
|
+
const comma = trimWs(rest);
|
|
4296
|
+
if (comma[0]?.t !== "comma") throw syntax2("var() takes a name and an optional fallback", node.pos);
|
|
4297
|
+
return `${first.v},${serializeValue(comma.slice(1), property, vars, true)}`;
|
|
4298
|
+
}
|
|
4299
|
+
function parseDeclarations(nodes, vars, issues, css) {
|
|
4300
|
+
const out = [];
|
|
4301
|
+
const decls = [];
|
|
4302
|
+
let current = [];
|
|
4303
|
+
for (const node of nodes) {
|
|
4304
|
+
if (node.t === "semi") {
|
|
4305
|
+
decls.push(current);
|
|
4306
|
+
current = [];
|
|
4307
|
+
} else current.push(node);
|
|
4308
|
+
}
|
|
4309
|
+
decls.push(current);
|
|
4310
|
+
for (const decl of decls) {
|
|
4311
|
+
const body = trimWs(decl);
|
|
4312
|
+
if (body.length === 0) continue;
|
|
4313
|
+
try {
|
|
4314
|
+
const name = body[0];
|
|
4315
|
+
if (name?.t !== "ident") throw syntax2("Expected a property name", body[0]?.pos ?? 0);
|
|
4316
|
+
const rest = trimWs(body.slice(1));
|
|
4317
|
+
if (rest[0]?.t !== "colon") throw syntax2(`Expected ":" after ${name.v}`, name.pos);
|
|
4318
|
+
const property = name.v.toLowerCase();
|
|
4319
|
+
if (name.v.startsWith("--")) {
|
|
4320
|
+
throw forbidden2("Custom properties cannot be declared. Settings arrive as --w-<key>.", name.pos);
|
|
4321
|
+
}
|
|
4322
|
+
if (!PROPERTIES.has(property)) {
|
|
4323
|
+
throw new CssError("CSS_PROPERTY_NOT_ALLOWED", `The property "${name.v}" is not allowed`, name.pos);
|
|
4324
|
+
}
|
|
4325
|
+
const value = serializeValue(rest.slice(1), property, vars, false);
|
|
4326
|
+
if (value === "") throw syntax2(`${name.v} has no value`, name.pos);
|
|
4327
|
+
out.push(`${property}:${value}`);
|
|
4328
|
+
} catch (error) {
|
|
4329
|
+
if (!(error instanceof CssError)) throw error;
|
|
4330
|
+
issues.push(cssIssue(css, error));
|
|
4331
|
+
}
|
|
4332
|
+
}
|
|
4333
|
+
return out;
|
|
4334
|
+
}
|
|
4335
|
+
function parseMedia(prelude, pos) {
|
|
4336
|
+
const nodes = trimWs(prelude).filter((n) => n.t !== "ws");
|
|
4337
|
+
const conditions = /* @__PURE__ */ new Map();
|
|
4338
|
+
if (nodes.length % 2 === 0) throw syntax2('@media takes conditions joined with "and"', pos);
|
|
4339
|
+
for (let i = 0; i < nodes.length; i += 1) {
|
|
4340
|
+
const node = nodes[i];
|
|
4341
|
+
if (i % 2 === 1) {
|
|
4342
|
+
if (node.t !== "ident" || node.v.toLowerCase() !== "and") {
|
|
4343
|
+
throw forbidden2("@media takes (min-width: ...) and (max-width: ...) only", node.pos);
|
|
4344
|
+
}
|
|
4345
|
+
continue;
|
|
4346
|
+
}
|
|
4347
|
+
if (node.t !== "block" || node.v !== "(") {
|
|
4348
|
+
throw forbidden2("@media takes (min-width: ...) and (max-width: ...) only", node.pos);
|
|
4349
|
+
}
|
|
4350
|
+
const inner = trimWs(node.children).filter((n) => n.t !== "ws");
|
|
4351
|
+
const [feature, colon, value] = inner;
|
|
4352
|
+
if (inner.length !== 3 || feature?.t !== "ident" || colon?.t !== "colon" || value?.t !== "dim" || value.unit.toLowerCase() !== "px") {
|
|
4353
|
+
throw forbidden2("@media takes (min-width: Npx) and (max-width: Npx) only", node.pos);
|
|
4354
|
+
}
|
|
4355
|
+
const name = feature.v.toLowerCase();
|
|
4356
|
+
const px = Number(value.v);
|
|
4357
|
+
const allowed = name === "min-width" ? MIN_WIDTHS : name === "max-width" ? MAX_WIDTHS : [];
|
|
4358
|
+
if (!allowed.includes(px) || conditions.has(name)) {
|
|
4359
|
+
throw forbidden2(
|
|
4360
|
+
"Only the breakpoints (min-width: 1025px), (min-width: 768px), (max-width: 1024px) and (max-width: 767px) are allowed",
|
|
4361
|
+
node.pos
|
|
4362
|
+
);
|
|
4363
|
+
}
|
|
4364
|
+
conditions.set(name, px);
|
|
4365
|
+
}
|
|
4366
|
+
const min = conditions.get("min-width");
|
|
4367
|
+
const max = conditions.get("max-width");
|
|
4368
|
+
if (min === void 0 && max === void 0) throw syntax2("@media needs a condition", pos);
|
|
4369
|
+
if (min !== void 0 && max !== void 0 && !(min === 768 && max === 1024)) {
|
|
4370
|
+
throw forbidden2("Combine breakpoints only as (min-width: 768px) and (max-width: 1024px)", pos);
|
|
4371
|
+
}
|
|
4372
|
+
const parts = [];
|
|
4373
|
+
if (min !== void 0) parts.push(`(min-width:${String(min)}px)`);
|
|
4374
|
+
if (max !== void 0) parts.push(`(max-width:${String(max)}px)`);
|
|
4375
|
+
return parts.join(" and ");
|
|
4376
|
+
}
|
|
4377
|
+
function cssIssue(css, error) {
|
|
4378
|
+
const { line, col } = lineCol(css, error.pos);
|
|
4379
|
+
return makeIssue(`css:${String(line)}:${String(col)}`, error.code, error.message);
|
|
4380
|
+
}
|
|
4381
|
+
function parseRules(nodes, media, vars, issues, css, rules) {
|
|
4382
|
+
let i = 0;
|
|
4383
|
+
while (i < nodes.length) {
|
|
4384
|
+
const node = nodes[i];
|
|
4385
|
+
if (node.t === "ws") {
|
|
4386
|
+
i += 1;
|
|
4387
|
+
continue;
|
|
4388
|
+
}
|
|
4389
|
+
if (node.t === "atkw" && node.v.toLowerCase() !== "media") {
|
|
4390
|
+
throw forbidden2(`@${node.v} is not allowed. Only @media with the three breakpoints is.`, node.pos);
|
|
4391
|
+
}
|
|
4392
|
+
const start = i;
|
|
4393
|
+
while (i < nodes.length && !(nodes[i].t === "block" && nodes[i].v === "{")) {
|
|
4394
|
+
if (nodes[i].t === "semi") throw syntax2("Unexpected ; outside a rule", nodes[i].pos);
|
|
4395
|
+
i += 1;
|
|
4396
|
+
}
|
|
4397
|
+
const block = nodes[i];
|
|
4398
|
+
if (block === void 0) throw syntax2("A rule needs a { } block", node.pos);
|
|
4399
|
+
const prelude = nodes.slice(start, i);
|
|
4400
|
+
i += 1;
|
|
4401
|
+
if (node.t === "atkw") {
|
|
4402
|
+
if (media !== null) throw forbidden2("@media cannot be nested", node.pos);
|
|
4403
|
+
const condition = parseMedia(prelude.slice(1), node.pos);
|
|
4404
|
+
parseRules(block.children, condition, vars, issues, css, rules);
|
|
4405
|
+
continue;
|
|
4406
|
+
}
|
|
4407
|
+
try {
|
|
4408
|
+
const selectors = parseCompoundList(prelude, node.pos);
|
|
4409
|
+
const declarations = parseDeclarations(block.children, vars, issues, css);
|
|
4410
|
+
if (declarations.length > 0) rules.push({ media, selectors, declarations });
|
|
4411
|
+
} catch (error) {
|
|
4412
|
+
if (!(error instanceof CssError)) throw error;
|
|
4413
|
+
issues.push(cssIssue(css, error));
|
|
4414
|
+
}
|
|
4415
|
+
}
|
|
4416
|
+
}
|
|
4417
|
+
function serialize(rules, scope) {
|
|
4418
|
+
let out = "";
|
|
4419
|
+
let open = null;
|
|
4420
|
+
for (const rule of rules) {
|
|
4421
|
+
if (rule.media !== open) {
|
|
4422
|
+
if (open !== null) out += "}";
|
|
4423
|
+
if (rule.media !== null) out += `@media ${rule.media}{`;
|
|
4424
|
+
open = rule.media;
|
|
4425
|
+
}
|
|
4426
|
+
const selector = rule.selectors.map((s) => scope === "" ? s : `${scope} ${s}`).join(",");
|
|
4427
|
+
out += `${selector}{${rule.declarations.join(";")}}`;
|
|
4428
|
+
}
|
|
4429
|
+
if (open !== null) out += "}";
|
|
4430
|
+
return out;
|
|
4431
|
+
}
|
|
4432
|
+
function sanitizeWidgetCss(css, type) {
|
|
4433
|
+
if (typeof css !== "string" || !WIDGET_TYPE.test(type)) {
|
|
4434
|
+
return {
|
|
4435
|
+
ok: false,
|
|
4436
|
+
issues: [makeIssue("css", "INVALID_VALUE", "The css must be text and the type custom.<slug>")]
|
|
4437
|
+
};
|
|
4438
|
+
}
|
|
4439
|
+
const bytes = utf8Length(css);
|
|
4440
|
+
if (bytes > LIMITS.widgetCssBytes) {
|
|
4441
|
+
return {
|
|
4442
|
+
ok: false,
|
|
4443
|
+
issues: [
|
|
4444
|
+
makeIssue(
|
|
4445
|
+
"css",
|
|
4446
|
+
"LIMIT_EXCEEDED",
|
|
4447
|
+
`CSS is ${String(bytes)} bytes, over the limit of ${String(LIMITS.widgetCssBytes)}`
|
|
4448
|
+
)
|
|
4449
|
+
]
|
|
4450
|
+
};
|
|
4451
|
+
}
|
|
4452
|
+
const issues = [];
|
|
4453
|
+
const vars = /* @__PURE__ */ new Set();
|
|
4454
|
+
const rules = [];
|
|
4455
|
+
try {
|
|
4456
|
+
parseRules(buildTree(tokenize(css)), null, vars, issues, css, rules);
|
|
4457
|
+
} catch (error) {
|
|
4458
|
+
if (!(error instanceof CssError)) throw error;
|
|
4459
|
+
issues.push(cssIssue(css, error));
|
|
4460
|
+
}
|
|
4461
|
+
if (issues.length > 0) return { ok: false, issues: issues.slice(0, 50) };
|
|
4462
|
+
return {
|
|
4463
|
+
ok: true,
|
|
4464
|
+
css: serialize(rules, `[data-b-w="${type}"]`),
|
|
4465
|
+
source: serialize(rules, ""),
|
|
4466
|
+
vars: [...vars].sort()
|
|
4467
|
+
};
|
|
4468
|
+
}
|
|
4469
|
+
|
|
4470
|
+
// src/custom-widget.ts
|
|
4471
|
+
var import_zod7 = require("zod");
|
|
4472
|
+
var SETTING_KEY_RE = /^[a-z][A-Za-z0-9]{0,39}$/;
|
|
4473
|
+
var settingKey = import_zod7.z.string().regex(SETTING_KEY_RE);
|
|
4474
|
+
var inputName = import_zod7.z.string().regex(/^[a-z][A-Za-z0-9]{0,39}$/);
|
|
4475
|
+
var dataInputShape = {
|
|
4476
|
+
accepts: import_zod7.z.array(import_zod7.z.enum(DATA_RESULT_TYPES)).min(1).max(DATA_RESULT_TYPES.length),
|
|
4477
|
+
required: import_zod7.z.boolean(),
|
|
4478
|
+
label: import_zod7.z.string().min(1).max(100)
|
|
4479
|
+
};
|
|
4480
|
+
var common = {
|
|
4481
|
+
type: import_zod7.z.string().regex(/^custom\.[a-z][a-z0-9-]{0,63}$/),
|
|
4482
|
+
v: import_zod7.z.number().int().min(1).max(9999),
|
|
4483
|
+
title: import_zod7.z.string().min(1).max(100),
|
|
4484
|
+
description: import_zod7.z.string().min(1).max(500),
|
|
4485
|
+
icon: import_zod7.z.string().min(1).max(40),
|
|
4486
|
+
group: import_zod7.z.literal("custom"),
|
|
4487
|
+
// The common props (hidden, anchor, spacing) are added on normalisation and do not count.
|
|
4488
|
+
settings: import_zod7.z.array(settingFieldSchema).max(LIMITS.widgetSettings + COMMON_SETTING_KEYS.length),
|
|
4489
|
+
renamed: safeRecord(settingKey, settingKey).optional(),
|
|
4490
|
+
removed: import_zod7.z.array(settingKey).max(LIMITS.widgetSettings).optional()
|
|
4491
|
+
};
|
|
4492
|
+
function limitInputs(data, ctx) {
|
|
4493
|
+
if (data !== void 0 && Object.keys(data).length > LIMITS.customWidgetDataInputs) {
|
|
4494
|
+
customIssue(
|
|
4495
|
+
ctx,
|
|
4496
|
+
"LIMIT_EXCEEDED",
|
|
4497
|
+
`A custom widget has at most ${String(LIMITS.customWidgetDataInputs)} data inputs`,
|
|
4498
|
+
["data"]
|
|
4499
|
+
);
|
|
4500
|
+
}
|
|
4501
|
+
}
|
|
4502
|
+
var templateSchema = import_zod7.z.object({
|
|
4503
|
+
kind: import_zod7.z.literal("template"),
|
|
4504
|
+
...common,
|
|
4505
|
+
data: safeRecord(import_zod7.z.object(dataInputShape).strict(), inputName).optional(),
|
|
4506
|
+
template: import_zod7.z.string(),
|
|
4507
|
+
css: import_zod7.z.string().optional()
|
|
4508
|
+
}).strict().superRefine((def, ctx) => limitInputs(def.data, ctx));
|
|
4509
|
+
var targetNode = nodeIdSchema;
|
|
4510
|
+
var compositeSchema = import_zod7.z.object({
|
|
4511
|
+
kind: import_zod7.z.literal("composite"),
|
|
4512
|
+
...common,
|
|
4513
|
+
body: import_zod7.z.object({
|
|
4514
|
+
columns: import_zod7.z.array(import_zod7.z.unknown()).optional(),
|
|
4515
|
+
stack: import_zod7.z.array(import_zod7.z.unknown()).optional()
|
|
4516
|
+
}).strict(),
|
|
4517
|
+
bindings: import_zod7.z.array(
|
|
4518
|
+
import_zod7.z.object({
|
|
4519
|
+
setting: settingKey,
|
|
4520
|
+
targets: import_zod7.z.array(import_zod7.z.object({ node: targetNode, prop: settingKey }).strict()).min(1).max(20)
|
|
4521
|
+
}).strict()
|
|
4522
|
+
).max(LIMITS.widgetSettings),
|
|
4523
|
+
data: safeRecord(
|
|
4524
|
+
import_zod7.z.object({
|
|
4525
|
+
...dataInputShape,
|
|
4526
|
+
targets: import_zod7.z.array(import_zod7.z.object({ node: targetNode, input: inputName }).strict()).min(1).max(20)
|
|
4527
|
+
}).strict(),
|
|
4528
|
+
inputName
|
|
4529
|
+
).optional()
|
|
4530
|
+
}).strict().superRefine((def, ctx) => {
|
|
4531
|
+
limitInputs(def.data, ctx);
|
|
4532
|
+
const kinds = [def.body.columns, def.body.stack].filter((b) => b !== void 0).length;
|
|
4533
|
+
if (kinds !== 1) {
|
|
4534
|
+
customIssue(ctx, "INVALID_VALUE", "A composite body has exactly one of columns or stack", ["body"]);
|
|
4535
|
+
}
|
|
4536
|
+
});
|
|
4537
|
+
var MAX_DEPTH = 24;
|
|
4538
|
+
var MAX_VALUES = 1e5;
|
|
4539
|
+
function pathOf(frame) {
|
|
4540
|
+
const out = [];
|
|
4541
|
+
for (let f = frame; f !== null && f.parent !== null; f = f.parent) out.unshift(f.key);
|
|
4542
|
+
return out;
|
|
4543
|
+
}
|
|
4544
|
+
function scan(input) {
|
|
4545
|
+
const issues = [];
|
|
4546
|
+
const stack = [{ value: input, key: "", parent: null, depth: 0 }];
|
|
4547
|
+
let seen = 0;
|
|
4548
|
+
while (stack.length > 0 && issues.length < 50) {
|
|
4549
|
+
const frame = stack.pop();
|
|
4550
|
+
const { value } = frame;
|
|
4551
|
+
seen += 1;
|
|
4552
|
+
if (seen > MAX_VALUES) {
|
|
4553
|
+
issues.push(makeIssue("", "WIDGET_TOO_LARGE", "The definition has far too many values"));
|
|
4554
|
+
break;
|
|
4555
|
+
}
|
|
4556
|
+
const at2 = () => pathOf(frame);
|
|
4557
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") continue;
|
|
4558
|
+
if (value === void 0) {
|
|
4559
|
+
issues.push(makeIssue(at2(), "INVALID_SHAPE", "Only JSON values are allowed"));
|
|
4560
|
+
continue;
|
|
4561
|
+
}
|
|
4562
|
+
if (typeof value === "number") {
|
|
4563
|
+
if (!Number.isFinite(value)) issues.push(makeIssue(at2(), "INVALID_SHAPE", "Numbers must be finite"));
|
|
4564
|
+
continue;
|
|
4565
|
+
}
|
|
4566
|
+
if (typeof value !== "object") {
|
|
4567
|
+
issues.push(makeIssue(at2(), "INVALID_SHAPE", "Only JSON values are allowed"));
|
|
4568
|
+
continue;
|
|
4569
|
+
}
|
|
4570
|
+
if (frame.depth >= MAX_DEPTH) {
|
|
4571
|
+
issues.push(makeIssue(at2(), "LIMIT_EXCEEDED", `Nested more than ${String(MAX_DEPTH)} levels`));
|
|
4572
|
+
continue;
|
|
4573
|
+
}
|
|
4574
|
+
if (Array.isArray(value)) {
|
|
4575
|
+
value.forEach((item, i) => stack.push({ value: item, key: i, parent: frame, depth: frame.depth + 1 }));
|
|
4576
|
+
continue;
|
|
4577
|
+
}
|
|
4578
|
+
const proto = Object.getPrototypeOf(value);
|
|
4579
|
+
if (proto !== Object.prototype && proto !== null) {
|
|
4580
|
+
issues.push(makeIssue(at2(), "INVALID_SHAPE", "Expected a plain JSON object"));
|
|
4581
|
+
continue;
|
|
4582
|
+
}
|
|
4583
|
+
for (const key of Object.getOwnPropertyNames(value)) {
|
|
4584
|
+
if (FORBIDDEN_KEYS.has(key)) {
|
|
4585
|
+
issues.push(makeIssue([...at2(), key], "FORBIDDEN_KEY", `The key "${key}" is not allowed`));
|
|
4586
|
+
continue;
|
|
4587
|
+
}
|
|
4588
|
+
const item = Object.getOwnPropertyDescriptor(value, key)?.value;
|
|
4589
|
+
if (item === void 0) continue;
|
|
4590
|
+
stack.push({ value: item, key, parent: frame, depth: frame.depth + 1 });
|
|
4591
|
+
}
|
|
4592
|
+
}
|
|
4593
|
+
return issues;
|
|
4594
|
+
}
|
|
4595
|
+
var Collector = class {
|
|
4596
|
+
constructor() {
|
|
4597
|
+
this.issues = [];
|
|
4598
|
+
}
|
|
4599
|
+
add(path, code, message) {
|
|
4600
|
+
this.issues.push(makeIssue(path, code, message));
|
|
4601
|
+
}
|
|
4602
|
+
};
|
|
4603
|
+
var COMMON_BY_KEY = new Map(COMMON_WIDGET_SETTINGS.map((f) => [f.key, f]));
|
|
4604
|
+
function sameField(a, b) {
|
|
4605
|
+
const entries = (f) => Object.entries(f).filter(([, v]) => v !== void 0);
|
|
4606
|
+
const left = entries(a);
|
|
4607
|
+
const right = new Map(entries(b));
|
|
4608
|
+
return left.length === right.size && left.every(([k, v]) => right.has(k) && JSON.stringify(v) === JSON.stringify(right.get(k)));
|
|
4609
|
+
}
|
|
4610
|
+
function withCommonSettings(settings, out) {
|
|
4611
|
+
let own = 0;
|
|
4612
|
+
settings.forEach((field2, i) => {
|
|
4613
|
+
const common2 = COMMON_BY_KEY.get(field2.key);
|
|
4614
|
+
if (common2 === void 0) own += 1;
|
|
4615
|
+
else if (!sameField(field2, common2)) {
|
|
4616
|
+
out.add(["settings", i, "key"], "DUPLICATE_KEY", `"${field2.key}" is a common prop and cannot be redefined`);
|
|
4617
|
+
}
|
|
4618
|
+
});
|
|
4619
|
+
if (own > LIMITS.widgetSettings) {
|
|
4620
|
+
out.add("settings", "LIMIT_EXCEEDED", `A custom widget has at most ${String(LIMITS.widgetSettings)} settings, not ${String(own)}`);
|
|
4621
|
+
}
|
|
4622
|
+
const present = new Set(settings.map((f) => f.key));
|
|
4623
|
+
return [...settings, ...COMMON_WIDGET_SETTINGS.filter((f) => !present.has(f.key))];
|
|
4624
|
+
}
|
|
4625
|
+
function checkText(def, out) {
|
|
4626
|
+
for (const key of ["title", "description"]) {
|
|
4627
|
+
if (BIDI_CONTROL_RE.test(def[key])) {
|
|
4628
|
+
out.add(key, "INVALID_VALUE", `The ${key} cannot contain bidirectional override characters`);
|
|
4629
|
+
}
|
|
4630
|
+
}
|
|
4631
|
+
}
|
|
4632
|
+
function checkSettings(settings, out) {
|
|
4633
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4634
|
+
settings.forEach((field2, i) => {
|
|
4635
|
+
if (seen.has(field2.key)) {
|
|
4636
|
+
out.add(["settings", i, "key"], "DUPLICATE_KEY", `The setting key "${field2.key}" is used twice`);
|
|
4637
|
+
}
|
|
4638
|
+
seen.add(field2.key);
|
|
4639
|
+
if (field2.control === "select" && (field2.options === void 0 || field2.options.length === 0)) {
|
|
4640
|
+
out.add(["settings", i, "options"], "REQUIRED", "A select setting needs options");
|
|
4641
|
+
}
|
|
4642
|
+
if (BIDI_CONTROL_RE.test(JSON.stringify(field2))) {
|
|
4643
|
+
out.add(["settings", i], "INVALID_VALUE", "A setting cannot contain bidirectional override characters");
|
|
4644
|
+
}
|
|
4645
|
+
const problem = validateSettingValue(field2, field2.default);
|
|
4646
|
+
if (problem !== null) out.add(["settings", i, "default"], "INVALID_VALUE", `The default is invalid: ${problem}`);
|
|
4647
|
+
});
|
|
4648
|
+
}
|
|
4649
|
+
function checkVersion(def, registry, out) {
|
|
4650
|
+
const renamed = def.renamed ?? {};
|
|
4651
|
+
const removed = def.removed ?? [];
|
|
4652
|
+
const renameEntries = Object.entries(renamed);
|
|
4653
|
+
if (def.v === 1) {
|
|
4654
|
+
if (renameEntries.length > 0 || removed.length > 0) {
|
|
4655
|
+
out.add("renamed", "VERSION_RULE", "Version 1 has nothing to rename or remove");
|
|
4656
|
+
}
|
|
4657
|
+
return;
|
|
4658
|
+
}
|
|
4659
|
+
const previous = registry.widget(def.type, def.v - 1);
|
|
4660
|
+
if (previous === void 0) {
|
|
4661
|
+
out.add("v", "VERSION_RULE", `Version ${String(def.v - 1)} of ${def.type} is not registered`);
|
|
4662
|
+
return;
|
|
4663
|
+
}
|
|
4664
|
+
if (previous.kind !== void 0 && previous.kind !== def.kind) {
|
|
4665
|
+
out.add("kind", "VERSION_RULE", `A new version stays a ${previous.kind} widget`);
|
|
4666
|
+
}
|
|
4667
|
+
const before = new Map(previous.settings.map((f) => [f.key, f]));
|
|
4668
|
+
const after = new Map(def.settings.map((f) => [f.key, f]));
|
|
4669
|
+
const renamedTo = /* @__PURE__ */ new Set();
|
|
4670
|
+
for (const [from, to] of renameEntries) {
|
|
4671
|
+
const old = before.get(from);
|
|
4672
|
+
const next = after.get(to);
|
|
4673
|
+
if (old === void 0) out.add(["renamed", from], "VERSION_RULE", `"${from}" is not a setting of version ${String(previous.v)}`);
|
|
4674
|
+
else if (next === void 0) out.add(["renamed", from], "VERSION_RULE", `"${to}" is not a setting of this version`);
|
|
4675
|
+
else if (after.has(from) || before.has(to)) {
|
|
4676
|
+
out.add(["renamed", from], "VERSION_RULE", `Renaming "${from}" to "${to}" collides with another setting`);
|
|
4677
|
+
} else if (old.control !== next.control) {
|
|
4678
|
+
out.add(["renamed", from], "VERSION_RULE", `"${from}" and "${to}" must use the same control`);
|
|
4679
|
+
}
|
|
4680
|
+
if (renamedTo.has(to)) out.add(["renamed", from], "VERSION_RULE", `"${to}" is the target of two renames`);
|
|
4681
|
+
renamedTo.add(to);
|
|
4682
|
+
}
|
|
4683
|
+
removed.forEach((key, i) => {
|
|
4684
|
+
if (!before.has(key)) out.add(["removed", i], "VERSION_RULE", `"${key}" is not a setting of version ${String(previous.v)}`);
|
|
4685
|
+
else if (after.has(key)) out.add(["removed", i], "VERSION_RULE", `"${key}" is removed but still declared`);
|
|
4686
|
+
});
|
|
4687
|
+
for (const key of before.keys()) {
|
|
4688
|
+
if (!after.has(key) && !removed.includes(key) && !renameEntries.some(([from]) => from === key)) {
|
|
4689
|
+
out.add("settings", "VERSION_RULE", `The setting "${key}" is gone. List it in removed, or rename it with renamed.`);
|
|
4690
|
+
}
|
|
4691
|
+
}
|
|
4692
|
+
for (const [key, field2] of after) {
|
|
4693
|
+
const old = before.get(key);
|
|
4694
|
+
if (old !== void 0 && old.control !== field2.control) {
|
|
4695
|
+
out.add("settings", "VERSION_RULE", `The setting "${key}" cannot change its control. Add a new key and remove the old one.`);
|
|
4696
|
+
}
|
|
4697
|
+
}
|
|
4698
|
+
}
|
|
4699
|
+
function controlMismatch(outer, inner) {
|
|
4700
|
+
if (outer.control !== inner.control) {
|
|
4701
|
+
return `"${outer.key}" is a ${outer.control} setting but ${inner.key} takes ${inner.control}`;
|
|
4702
|
+
}
|
|
4703
|
+
if (outer.translatable === true !== (inner.translatable === true)) {
|
|
4704
|
+
return `"${outer.key}" and ${inner.key} must both be translatable, or neither`;
|
|
4705
|
+
}
|
|
4706
|
+
if (outer.control === "select") {
|
|
4707
|
+
const allowed = new Set((inner.options ?? []).map((o) => o.value));
|
|
4708
|
+
const missing = (outer.options ?? []).find((o) => !allowed.has(o.value));
|
|
4709
|
+
if (missing !== void 0) {
|
|
4710
|
+
return `The option "${missing.value}" of "${outer.key}" is not an option of ${inner.key}`;
|
|
4711
|
+
}
|
|
4712
|
+
}
|
|
4713
|
+
return null;
|
|
4714
|
+
}
|
|
4715
|
+
var WRAP = { section: "zzwrapsection01", column: "zzwrapcolumn001", inner: "zzwrapinner0001" };
|
|
4716
|
+
var COLUMNS_PREFIX = "sections[0].columns[0].children[0].columns";
|
|
4717
|
+
var STACK_PREFIX = "sections[0].columns[0].children";
|
|
4718
|
+
function checkBody(def, out) {
|
|
4719
|
+
const useColumns = def.body.columns !== void 0;
|
|
4720
|
+
const children = useColumns ? [{ id: WRAP.inner, type: "inner-section", props: {}, columns: def.body.columns }] : def.body.stack ?? [];
|
|
4721
|
+
if (!useColumns) {
|
|
4722
|
+
(def.body.stack ?? []).forEach((node, i) => {
|
|
4723
|
+
if (node?.type === "inner-section") {
|
|
4724
|
+
out.add(["body", "stack", i, "type"], "INVALID_WIDGET_TYPE", "A stack holds widgets, not sections");
|
|
4725
|
+
}
|
|
4726
|
+
});
|
|
4727
|
+
}
|
|
4728
|
+
const wrapped = pageDocumentSchema.safeParse({
|
|
4729
|
+
schemaVersion: 1,
|
|
4730
|
+
sections: [
|
|
4731
|
+
{
|
|
4732
|
+
id: WRAP.section,
|
|
4733
|
+
type: "section",
|
|
4734
|
+
props: {},
|
|
4735
|
+
columns: [{ id: WRAP.column, type: "column", props: { width: 100 }, children }]
|
|
4736
|
+
}
|
|
4737
|
+
]
|
|
4738
|
+
});
|
|
4739
|
+
if (!wrapped.success) {
|
|
4740
|
+
const prefix = useColumns ? COLUMNS_PREFIX : STACK_PREFIX;
|
|
4741
|
+
const to = useColumns ? "body.columns" : "body.stack";
|
|
4742
|
+
for (const issue2 of zodIssues(wrapped.error)) {
|
|
4743
|
+
out.add(
|
|
4744
|
+
issue2.path.startsWith(prefix) ? `${to}${issue2.path.slice(prefix.length)}` : "body",
|
|
4745
|
+
issue2.code,
|
|
4746
|
+
issue2.message
|
|
4747
|
+
);
|
|
4748
|
+
}
|
|
4749
|
+
return null;
|
|
4750
|
+
}
|
|
4751
|
+
const column = wrapped.data.sections[0]?.columns[0];
|
|
4752
|
+
const first = column?.children[0];
|
|
4753
|
+
if (useColumns) {
|
|
4754
|
+
return first?.type === "inner-section" ? { columns: first.columns } : null;
|
|
4755
|
+
}
|
|
4756
|
+
return { stack: (column?.children ?? []).filter((c) => c.type !== "inner-section") };
|
|
4757
|
+
}
|
|
4758
|
+
function innerNodes(body) {
|
|
4759
|
+
const out = [];
|
|
4760
|
+
body.columns?.forEach(
|
|
4761
|
+
(column, i) => column.children.forEach(
|
|
4762
|
+
(node, j) => out.push({ node, path: ["body", "columns", i, "children", j] })
|
|
4763
|
+
)
|
|
4764
|
+
);
|
|
4765
|
+
body.stack?.forEach((node, i) => out.push({ node, path: ["body", "stack", i] }));
|
|
4766
|
+
return out;
|
|
4767
|
+
}
|
|
4768
|
+
function checkComposite(def, registry, out) {
|
|
4769
|
+
const body = checkBody(def, out);
|
|
4770
|
+
if (body === null) return null;
|
|
4771
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
4772
|
+
for (const ref of innerNodes(body)) {
|
|
4773
|
+
const { node, path } = ref;
|
|
4774
|
+
if (node.data !== void 0) {
|
|
4775
|
+
out.add([...path, "data"], "INVALID_VALUE", "Inner nodes get data through the widget's data inputs, not their own");
|
|
4776
|
+
}
|
|
4777
|
+
let inner;
|
|
4778
|
+
if (node.type === def.type) {
|
|
4779
|
+
out.add([...path, "type"], "NO_RECURSION", "A composite cannot contain itself");
|
|
4780
|
+
} else {
|
|
4781
|
+
inner = registry.widget(node.type, node.v);
|
|
4782
|
+
if (inner === void 0) {
|
|
4783
|
+
out.add([...path, "type"], "INVALID_WIDGET_TYPE", `${node.type}@${String(node.v)} is not an available widget`);
|
|
4784
|
+
} else if (node.type.startsWith("custom.") && inner.kind !== "template") {
|
|
4785
|
+
out.add(
|
|
4786
|
+
[...path, "type"],
|
|
4787
|
+
"NO_RECURSION",
|
|
4788
|
+
"A composite can contain basic, pack and template widgets, but not another composite"
|
|
4789
|
+
);
|
|
4790
|
+
} else {
|
|
4791
|
+
for (const problem of validateWidgetProps(inner, node.props)) {
|
|
4792
|
+
out.add([...path, "props", problem.path], "INVALID_VALUE", problem.message);
|
|
4793
|
+
}
|
|
4794
|
+
}
|
|
4795
|
+
}
|
|
4796
|
+
nodes.set(node.id, { ref, def: inner });
|
|
4797
|
+
}
|
|
4798
|
+
const settings = new Map(def.settings.map((f) => [f.key, f]));
|
|
4799
|
+
const bound = /* @__PURE__ */ new Set();
|
|
4800
|
+
def.bindings.forEach((binding, i) => {
|
|
4801
|
+
const field2 = settings.get(binding.setting);
|
|
4802
|
+
if (field2 === void 0) {
|
|
4803
|
+
out.add(["bindings", i, "setting"], "BINDING_TARGET_MISSING", `"${binding.setting}" is not one of this widget's settings`);
|
|
4804
|
+
}
|
|
4805
|
+
binding.targets.forEach((target2, j) => {
|
|
4806
|
+
const at2 = ["bindings", i, "targets", j];
|
|
4807
|
+
const entry = nodes.get(target2.node);
|
|
4808
|
+
if (entry === void 0) {
|
|
4809
|
+
out.add([...at2, "node"], "BINDING_TARGET_MISSING", `No node with id "${target2.node}" in the body`);
|
|
4810
|
+
return;
|
|
4811
|
+
}
|
|
4812
|
+
const key = `${target2.node}.${target2.prop}`;
|
|
4813
|
+
if (bound.has(key)) out.add(at2, "DUPLICATE_KEY", `${key} is bound by two settings`);
|
|
4814
|
+
bound.add(key);
|
|
4815
|
+
if (entry.def === void 0) return;
|
|
4816
|
+
const innerField = entry.def.settings.find((f) => f.key === target2.prop);
|
|
4817
|
+
if (innerField === void 0) {
|
|
4818
|
+
out.add([...at2, "prop"], "BINDING_TARGET_MISSING", `${entry.def.type} has no setting "${target2.prop}"`);
|
|
4819
|
+
} else if (field2 !== void 0) {
|
|
4820
|
+
const problem = controlMismatch(field2, innerField);
|
|
4821
|
+
if (problem !== null) out.add([...at2, "prop"], "BINDING_TYPE_MISMATCH", problem);
|
|
4822
|
+
}
|
|
4823
|
+
});
|
|
4824
|
+
});
|
|
4825
|
+
const claimed = /* @__PURE__ */ new Set();
|
|
4826
|
+
for (const [name, input] of Object.entries(def.data ?? {})) {
|
|
4827
|
+
input.targets.forEach((target2, j) => {
|
|
4828
|
+
const at2 = ["data", name, "targets", j];
|
|
4829
|
+
const entry = nodes.get(target2.node);
|
|
4830
|
+
if (entry === void 0) {
|
|
4831
|
+
out.add([...at2, "node"], "BINDING_TARGET_MISSING", `No node with id "${target2.node}" in the body`);
|
|
4832
|
+
return;
|
|
4833
|
+
}
|
|
4834
|
+
const key = `${target2.node}.${target2.input}`;
|
|
4835
|
+
if (claimed.has(key)) out.add(at2, "DUPLICATE_KEY", `${key} is fed by two data inputs`);
|
|
4836
|
+
claimed.add(key);
|
|
4837
|
+
if (entry.def === void 0) return;
|
|
4838
|
+
const innerInput = Object.entries(entry.def.data ?? {}).find(([n]) => n === target2.input)?.[1];
|
|
4839
|
+
if (innerInput === void 0) {
|
|
4840
|
+
out.add([...at2, "input"], "BINDING_TARGET_MISSING", `${entry.def.type} has no data input "${target2.input}"`);
|
|
4841
|
+
} else if (!input.accepts.every((a) => innerInput.accepts.includes(a))) {
|
|
4842
|
+
out.add(
|
|
4843
|
+
[...at2, "input"],
|
|
4844
|
+
"BINDING_TYPE_MISMATCH",
|
|
4845
|
+
`"${name}" accepts ${input.accepts.join(", ")} but ${target2.input} takes ${innerInput.accepts.join(", ")}`
|
|
4846
|
+
);
|
|
4847
|
+
}
|
|
4848
|
+
});
|
|
4849
|
+
}
|
|
4850
|
+
const normalized = {
|
|
4851
|
+
...def,
|
|
4852
|
+
body
|
|
4853
|
+
};
|
|
4854
|
+
if (body.columns !== void 0) normalized.placement = { topLevelOnly: true };
|
|
4855
|
+
return normalized;
|
|
4856
|
+
}
|
|
4857
|
+
function checkTemplate(def, registry, out) {
|
|
4858
|
+
const normalized = { ...def };
|
|
4859
|
+
if (def.css !== void 0) {
|
|
4860
|
+
const css = sanitizeWidgetCss(def.css, def.type);
|
|
4861
|
+
if (!css.ok) out.issues.push(...css.issues);
|
|
4862
|
+
else {
|
|
4863
|
+
const byKey = new Map(def.settings.map((f) => [f.key, f]));
|
|
4864
|
+
for (const key of css.vars) {
|
|
4865
|
+
const field2 = byKey.get(key);
|
|
4866
|
+
if (field2 === void 0) out.add("css", "UNKNOWN_REFERENCE", `var(--w-${key}) is not a setting of this widget`);
|
|
4867
|
+
else if (field2.output !== "css") {
|
|
4868
|
+
out.add("css", "FIELD_NOT_ALLOWED", `var(--w-${key}) reads a markup setting. Only css settings reach the CSS.`);
|
|
4869
|
+
}
|
|
4870
|
+
}
|
|
4871
|
+
normalized.css = css.source;
|
|
4872
|
+
}
|
|
4873
|
+
}
|
|
4874
|
+
const parsed = parseTemplate(def.template);
|
|
4875
|
+
if (!parsed.ok) {
|
|
4876
|
+
out.issues.push(...parsed.issues);
|
|
4877
|
+
return null;
|
|
4878
|
+
}
|
|
4879
|
+
out.issues.push(
|
|
4880
|
+
...validateTemplate(parsed.ast, {
|
|
4881
|
+
settings: def.settings,
|
|
4882
|
+
data: def.data ?? {},
|
|
4883
|
+
sources: registry.sources()
|
|
4884
|
+
})
|
|
4885
|
+
);
|
|
4886
|
+
return { normalized, ast: parsed.ast };
|
|
4887
|
+
}
|
|
4888
|
+
var plain2 = (value) => JSON.parse(JSON.stringify(value));
|
|
4889
|
+
function validateCustomWidget(def, registry) {
|
|
4890
|
+
const shape = scan(def);
|
|
4891
|
+
if (shape.length > 0) return { ok: false, issues: capIssues(shape) };
|
|
4892
|
+
let bytes;
|
|
4893
|
+
try {
|
|
4894
|
+
bytes = utf8Length(JSON.stringify(def));
|
|
4895
|
+
} catch {
|
|
4896
|
+
return { ok: false, issues: [makeIssue("", "INVALID_SHAPE", "The definition must be JSON")] };
|
|
4897
|
+
}
|
|
4898
|
+
if (bytes > LIMITS.widgetDefinitionBytes) {
|
|
4899
|
+
return {
|
|
4900
|
+
ok: false,
|
|
4901
|
+
issues: [
|
|
4902
|
+
makeIssue(
|
|
4903
|
+
"",
|
|
4904
|
+
"WIDGET_TOO_LARGE",
|
|
4905
|
+
`The definition is ${String(bytes)} bytes, over the limit of ${String(LIMITS.widgetDefinitionBytes)}`
|
|
4906
|
+
)
|
|
4907
|
+
]
|
|
4908
|
+
};
|
|
4909
|
+
}
|
|
4910
|
+
const kind = def?.kind;
|
|
4911
|
+
if (typeof def !== "object" || def === null || Array.isArray(def)) {
|
|
4912
|
+
return { ok: false, issues: [makeIssue("", "INVALID_SHAPE", "Expected a widget definition object")] };
|
|
4913
|
+
}
|
|
4914
|
+
if (kind !== "composite" && kind !== "template") {
|
|
4915
|
+
return { ok: false, issues: [makeIssue("kind", "INVALID_VALUE", 'kind is "composite" or "template"')] };
|
|
4916
|
+
}
|
|
4917
|
+
const out = new Collector();
|
|
4918
|
+
if (kind === "template") {
|
|
4919
|
+
const parsed2 = templateSchema.safeParse(def);
|
|
4920
|
+
if (!parsed2.success) return { ok: false, issues: zodIssues(parsed2.error) };
|
|
4921
|
+
const full2 = { ...parsed2.data, settings: withCommonSettings(parsed2.data.settings, out) };
|
|
4922
|
+
checkText(full2, out);
|
|
4923
|
+
checkSettings(full2.settings, out);
|
|
4924
|
+
const result = checkTemplate(full2, registry, out);
|
|
4925
|
+
checkVersion(full2, registry, out);
|
|
4926
|
+
if (out.issues.length > 0 || result === null) return { ok: false, issues: capIssues(out.issues) };
|
|
4927
|
+
return { ok: true, normalized: plain2(result.normalized), ast: result.ast };
|
|
4928
|
+
}
|
|
4929
|
+
const parsed = compositeSchema.safeParse(def);
|
|
4930
|
+
if (!parsed.success) return { ok: false, issues: zodIssues(parsed.error) };
|
|
4931
|
+
const full = { ...parsed.data, settings: withCommonSettings(parsed.data.settings, out) };
|
|
4932
|
+
checkText(full, out);
|
|
4933
|
+
checkSettings(full.settings, out);
|
|
4934
|
+
const normalized = checkComposite(full, registry, out);
|
|
4935
|
+
checkVersion(full, registry, out);
|
|
4936
|
+
if (out.issues.length > 0 || normalized === null) return { ok: false, issues: capIssues(out.issues) };
|
|
4937
|
+
return { ok: true, normalized: plain2(normalized) };
|
|
4938
|
+
}
|