@perkyzz/emvb 0.8.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/package.json +4 -4
- package/src/admin/agents-api.ts +45 -0
- package/src/admin/editor/A11yCopilot.tsx +1 -1
- package/src/admin/editor/Editor.tsx +325 -239
- package/src/admin/editor/FieldPreview.tsx +164 -0
- package/src/admin/editor/Timeline.tsx +2 -1
- package/src/admin/editor/TopBar.tsx +36 -3
- package/src/admin/editor/canvas/CanvasFrame.tsx +2 -2
- package/src/admin/editor/canvas/DeviceBar.tsx +17 -9
- package/src/admin/editor/canvas/canvas-scale.ts +2 -2
- package/src/admin/editor/canvas/use-canvas-session.ts +2 -2
- package/src/admin/editor/co-design.ts +190 -0
- package/src/admin/editor/dnd/new-element.ts +26 -4
- package/src/admin/editor/editor-chrome.tsx +3 -3
- package/src/admin/editor/editor-commands.ts +8 -3
- package/src/admin/editor/editor-css.ts +3 -0
- package/src/admin/editor/element-chrome.ts +3 -1
- package/src/admin/editor/extension-manifests.ts +20 -0
- package/src/admin/editor/extensions/ExtensionPanels.tsx +26 -0
- package/src/admin/editor/extensions-context.tsx +27 -0
- package/src/admin/editor/feature-css.ts +17 -0
- package/src/admin/editor/host.ts +6 -0
- package/src/admin/editor/palette-items.ts +2 -2
- package/src/admin/editor/panels/AddPanel.tsx +31 -1
- package/src/admin/editor/panels/AgentRequestBox.tsx +116 -0
- package/src/admin/editor/panels/ClassesSection.tsx +9 -6
- package/src/admin/editor/panels/CoDesignPanel.tsx +170 -0
- package/src/admin/editor/panels/ColorControl.tsx +2 -2
- package/src/admin/editor/panels/ElementPanel.tsx +53 -15
- package/src/admin/editor/panels/LanguagesSection.tsx +152 -0
- package/src/admin/editor/panels/LayersPanel.tsx +10 -6
- package/src/admin/editor/panels/LeftPanel.tsx +33 -5
- package/src/admin/editor/panels/PageSettings.tsx +4 -0
- package/src/admin/editor/panels/SettingsPanel.tsx +21 -2
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +7 -7
- package/src/admin/editor/panels/settings/BackgroundControls.tsx +76 -20
- package/src/admin/editor/panels/settings/BackgroundFill.tsx +8 -6
- package/src/admin/editor/panels/settings/BoxControl.tsx +5 -5
- package/src/admin/editor/panels/settings/EffectsControls.tsx +104 -9
- package/src/admin/editor/panels/settings/FieldControl.tsx +9 -3
- package/src/admin/editor/panels/settings/IconControls.tsx +14 -11
- package/src/admin/editor/panels/settings/LengthRow.tsx +27 -16
- package/src/admin/editor/panels/settings/LoopControl.tsx +269 -0
- package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +1 -1
- package/src/admin/editor/panels/settings/MediaPicker.tsx +5 -5
- package/src/admin/editor/panels/settings/MotionControl.tsx +12 -11
- package/src/admin/editor/panels/settings/QueryStyleRows.tsx +7 -4
- package/src/admin/editor/panels/settings/StyleRow.tsx +113 -13
- package/src/admin/editor/panels/settings/TextMarksControl.tsx +247 -0
- package/src/admin/editor/panels/settings/VariableBinding.tsx +2 -2
- package/src/admin/editor/panels/settings/background-mode.ts +14 -11
- package/src/admin/editor/panels/settings/box-link-name.ts +1 -1
- package/src/admin/editor/panels/settings/box-sides.ts +22 -14
- package/src/admin/editor/panels/settings/heading-warning.ts +5 -1
- package/src/admin/editor/panels/settings/length-units.ts +55 -2
- package/src/admin/editor/panels/settings/query-style.ts +3 -3
- package/src/admin/editor/panels/settings/style-sections.ts +51 -10
- package/src/admin/editor/panels/style-target.ts +9 -7
- package/src/admin/editor/presence.ts +60 -0
- package/src/admin/editor/store.ts +6 -2
- package/src/admin/editor/useCoDesign.ts +177 -0
- package/src/admin/editor/useEditorCommands.ts +4 -4
- package/src/admin/editor/useExtensions.ts +45 -0
- package/src/admin/editor/useNodeActions.ts +15 -6
- package/src/admin/editor/useProposals.ts +81 -0
- package/src/admin/index.tsx +5 -1
- package/src/admin/languages-api.ts +61 -0
- package/src/admin/media-api.ts +27 -0
- package/src/admin/pages/AgentSetup.tsx +148 -0
- package/src/admin/pages/AgentsPage.tsx +11 -28
- package/src/admin/pages/CollectionsPage.tsx +215 -0
- package/src/admin/pages/CreateDialog.tsx +15 -0
- package/src/admin/pages/ImportButton.tsx +18 -0
- package/src/admin/pages/ImportElementorDialog.tsx +669 -0
- package/src/admin/pages/ImportPageLinks.tsx +86 -0
- package/src/admin/pages/PageList.tsx +15 -1
- package/src/admin/pages/ThemePartList.tsx +15 -1
- package/src/admin/pages/section-head.tsx +27 -0
- package/src/admin/proposals-api.ts +91 -0
- package/src/admin/setup/host-collections.ts +100 -0
- package/src/admin/ui.ts +39 -2
- package/src/admin/widgets/CanvasField.tsx +202 -0
- package/src/admin/widgets/convert-body.ts +54 -0
- package/src/admin/widgets/field-fetcher.ts +79 -0
- package/src/astro/content-port.ts +14 -1
- package/src/astro/index.ts +2 -0
- package/src/astro/page-count.ts +77 -0
- package/src/astro/resolve-theme.ts +58 -3
- package/src/astro/resolve.ts +128 -5
- package/src/astro/responsive-images.ts +99 -0
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +44 -14
- package/src/core/arrange.ts +5 -1
- package/src/core/bounded-array.ts +31 -0
- package/src/core/canvas-collections.ts +20 -0
- package/src/core/clipboard.ts +9 -0
- package/src/core/contract.ts +285 -0
- package/src/core/css/generate.ts +89 -20
- package/src/core/css/trace.ts +18 -9
- package/src/core/css-keywords.ts +51 -1
- package/src/core/design/cascade.ts +12 -9
- package/src/core/design/classes.ts +4 -5
- package/src/core/design/devices.ts +13 -13
- package/src/core/design/states.ts +5 -3
- package/src/core/design/transfer.ts +5 -0
- package/src/core/design/variables.ts +17 -4
- package/src/core/elements/dynamic-elements.ts +19 -4
- package/src/core/elements/index.ts +73 -9
- package/src/core/extensions/define.ts +13 -0
- package/src/core/extensions/index.ts +41 -0
- package/src/core/extensions/namespace.ts +82 -0
- package/src/core/extensions/normalize.ts +141 -0
- package/src/core/extensions/placeholder.ts +41 -0
- package/src/core/extensions/registry.ts +94 -0
- package/src/core/extensions/render-ext.ts +118 -0
- package/src/core/extensions/safe-nodes.ts +93 -0
- package/src/core/extensions/schema.ts +112 -0
- package/src/core/extensions/types.ts +159 -0
- package/src/core/forms/binding.ts +1 -1
- package/src/core/history/diff.ts +1 -1
- package/src/core/import/elementor/atomic.ts +1595 -0
- package/src/core/import/elementor/classes.ts +127 -0
- package/src/core/import/elementor/context.ts +429 -0
- package/src/core/import/elementor/counter-steps.ts +72 -0
- package/src/core/import/elementor/css.ts +445 -0
- package/src/core/import/elementor/html.ts +500 -0
- package/src/core/import/elementor/index.ts +949 -0
- package/src/core/import/elementor/inline-marks.ts +81 -0
- package/src/core/import/elementor/snapshots.ts +48 -0
- package/src/core/import/elementor/style.ts +409 -0
- package/src/core/import/elementor/values.ts +241 -0
- package/src/core/import/elementor/widgets.ts +1897 -0
- package/src/core/index.ts +84 -0
- package/src/core/json-schema.ts +107 -0
- package/src/core/limits.ts +19 -0
- package/src/core/migrate/index.ts +6 -1
- package/src/core/migrate/v18.ts +222 -0
- package/src/core/perf/meter.ts +5 -5
- package/src/core/portable-text.ts +127 -0
- package/src/core/proposals-safety.ts +102 -0
- package/src/core/proposals.ts +357 -0
- package/src/core/recipes/index.ts +2 -2
- package/src/core/render/extras.ts +3 -3
- package/src/core/render/index.ts +140 -20
- package/src/core/render/loop.ts +1 -1
- package/src/core/render/pagination.ts +20 -1
- package/src/core/render/section.ts +4 -4
- package/src/core/render/vnode.ts +1 -1
- package/src/core/sanitize/css.ts +125 -10
- package/src/core/sanitize/motion.ts +116 -0
- package/src/core/saved-sections.ts +6 -0
- package/src/core/schema/descriptors.ts +3 -1
- package/src/core/schema/design.ts +2 -1
- package/src/core/schema/layout.ts +87 -14
- package/src/core/schema/style.ts +251 -114
- package/src/core/text-marks.ts +227 -0
- package/src/core/theme/dynamic.ts +9 -4
- package/src/core/theme/float.ts +1 -0
- package/src/core/theme/pagination.ts +9 -0
- package/src/core/theme/popup-rules.ts +17 -1
- package/src/core/translatable.ts +34 -0
- package/src/core/validate.ts +13 -1
- package/src/index.ts +35 -1
- package/src/mcp/errors.ts +3 -1
- package/src/mcp/ports.ts +67 -0
- package/src/mcp/prompts.ts +1 -11
- package/src/mcp/proposals.ts +281 -0
- package/src/mcp/read.ts +10 -9
- package/src/mcp/tools.ts +71 -3
- package/src/mcp/write.ts +66 -11
- package/src/server/agent-settings.ts +21 -2
- package/src/server/hooks.ts +81 -0
- package/src/server/mcp-routes.ts +15 -0
- package/src/server/plugin.ts +53 -5
- package/src/server/presence.ts +78 -0
- package/src/server/proposals.ts +261 -0
- package/src/server/requests.ts +196 -0
- package/src/server/tools-served.ts +45 -0
package/src/constants.ts
CHANGED
package/src/core/a11y/audit.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import { flattenMotion } from "../migrate/v18.ts";
|
|
1
2
|
import { clipText } from "../limits.ts";
|
|
2
3
|
import type { DesignSystem } from "../schema/design.ts";
|
|
3
4
|
import { isParentNode, type Layout, type LayoutNode } from "../schema/layout.ts";
|
|
4
5
|
import { boxBecomesLink } from "../render/extras.ts";
|
|
5
6
|
import { ICON_ANIMATION_LIMIT_MS, iconAnimationRuns } from "../icons/animation-runs.ts";
|
|
6
7
|
import { updateNode } from "../tree-ops.ts";
|
|
8
|
+
import { DEVICES } from "../theme/popup-rules.ts";
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* W-572: text as a screen reader says it: invisible format characters (zero-width space, word
|
|
@@ -161,13 +163,19 @@ function effectiveStyle(
|
|
|
161
163
|
const tagDefaults =
|
|
162
164
|
level === undefined
|
|
163
165
|
? undefined
|
|
164
|
-
: (design.defaults?.[`h${level}` as "h1"] as
|
|
166
|
+
: (flattenMotion(design.defaults?.[`h${level}` as "h1"]) as
|
|
167
|
+
| Record<string, unknown>
|
|
168
|
+
| undefined);
|
|
165
169
|
const classIds = new Set(node.classes ?? []);
|
|
166
|
-
|
|
170
|
+
// W-925: read by the flat motion keys (`iconAnimation`), as the renderer does.
|
|
171
|
+
const own = flattenMotion(node.style) as Record<string, unknown> | undefined;
|
|
172
|
+
if (classIds.size === 0 && !tagDefaults) return own;
|
|
167
173
|
const fromClasses = (design.classes ?? [])
|
|
168
174
|
.filter((c) => classIds.has(c.id))
|
|
169
|
-
.reduce<Record<string, unknown>>((acc, c) => Object.assign(acc, c.style), {
|
|
170
|
-
|
|
175
|
+
.reduce<Record<string, unknown>>((acc, c) => Object.assign(acc, flattenMotion(c.style)), {
|
|
176
|
+
...tagDefaults,
|
|
177
|
+
});
|
|
178
|
+
return { ...fromClasses, ...own };
|
|
171
179
|
}
|
|
172
180
|
|
|
173
181
|
/** Each device's style with the node's classes under the node's own (W-882). */
|
|
@@ -180,7 +188,7 @@ function deviceStyles(node: LayoutNode, design: DesignSystem): Record<string, un
|
|
|
180
188
|
];
|
|
181
189
|
for (const devices of layers) {
|
|
182
190
|
for (const [key, st] of Object.entries(devices ?? {})) {
|
|
183
|
-
merged[key] = { ...merged[key], ...(st as Record<string, unknown>) };
|
|
191
|
+
merged[key] = { ...merged[key], ...(flattenMotion(st) as Record<string, unknown>) };
|
|
184
192
|
}
|
|
185
193
|
}
|
|
186
194
|
return Object.values(merged);
|
|
@@ -210,7 +218,12 @@ function largeText(node: LayoutNode, design: DesignSystem): boolean {
|
|
|
210
218
|
* its text isn't known here.
|
|
211
219
|
*/
|
|
212
220
|
function boxHasName(node: LayoutNode): boolean {
|
|
213
|
-
if (
|
|
221
|
+
if (
|
|
222
|
+
node.bind ||
|
|
223
|
+
node.type.startsWith("post-") ||
|
|
224
|
+
node.type === "loop" ||
|
|
225
|
+
node.type === "part-ref"
|
|
226
|
+
)
|
|
214
227
|
return true;
|
|
215
228
|
const props = node.props as Record<string, unknown>;
|
|
216
229
|
const decorative = props["decorative"] === true;
|
|
@@ -228,9 +241,9 @@ export function auditPage(layout: Layout, design: DesignSystem): A11yReport {
|
|
|
228
241
|
issues.push({ ...issue, id: `${issue.rule}:${issue.nodeId}` });
|
|
229
242
|
let lastLevel = 0;
|
|
230
243
|
// W-444: H1s counted per device, so a desktop H1 and a mobile H1 that swap places aren't two.
|
|
231
|
-
|
|
232
|
-
let h1s =
|
|
233
|
-
let shownOn =
|
|
244
|
+
// W-956: Laptop counts as its own device.
|
|
245
|
+
let h1s = DEVICES.map(() => 0);
|
|
246
|
+
let shownOn = DEVICES.map(() => true);
|
|
234
247
|
// W-446: aria-hidden on a box hides everything inside it from screen readers, links included.
|
|
235
248
|
let ariaHiddenAbove = false;
|
|
236
249
|
// W-312 variants are alternatives: B is judged as if it stood where A does, so a page whose
|
|
@@ -239,13 +252,10 @@ export function auditPage(layout: Layout, design: DesignSystem): A11yReport {
|
|
|
239
252
|
const afterA = new Map<string, [number[], number]>();
|
|
240
253
|
let fade = 1;
|
|
241
254
|
// W-445: per device, like H1s, so a desktop menu and a mobile menu may share a name.
|
|
242
|
-
const menuNames =
|
|
255
|
+
const menuNames = DEVICES.map(() => new Set<string>());
|
|
243
256
|
const walk = (node: LayoutNode, bg: string | undefined, color: string | undefined): void => {
|
|
244
257
|
// W-413: an element hidden on every device (and what's inside it) is never shown or read.
|
|
245
|
-
if (
|
|
246
|
-
node !== layout.root &&
|
|
247
|
-
(["desktop", "tablet", "mobile"] as const).every((d) => node.hiddenOn?.includes(d))
|
|
248
|
-
) {
|
|
258
|
+
if (node !== layout.root && DEVICES.every((d) => node.hiddenOn?.includes(d))) {
|
|
249
259
|
return;
|
|
250
260
|
}
|
|
251
261
|
const variant = node.variant;
|
|
@@ -348,6 +358,26 @@ export function auditPage(layout: Layout, design: DesignSystem): A11yReport {
|
|
|
348
358
|
"This icon animates for more than 5 seconds and visitors can't pause it. Shorten the duration, or keep it only if the motion is essential.",
|
|
349
359
|
});
|
|
350
360
|
}
|
|
361
|
+
// W-938: an element's loop is held to the same rule: it stops after about 5 s unless it loops
|
|
362
|
+
// forever, so only Loop forever or a single run longer than 5 s is flagged.
|
|
363
|
+
const loopsTooLong = [effectiveStyle(node, design), ...deviceStyles(node, design)].some(
|
|
364
|
+
(st) => {
|
|
365
|
+
const loop = (st as { loopMotion?: { duration?: unknown; loop?: unknown } } | undefined)
|
|
366
|
+
?.loopMotion;
|
|
367
|
+
if (!loop || !("duration" in loop)) return false;
|
|
368
|
+
if (loop.loop === true) return true;
|
|
369
|
+
return typeof loop.duration === "number" && loop.duration > ICON_ANIMATION_LIMIT_MS;
|
|
370
|
+
},
|
|
371
|
+
);
|
|
372
|
+
if (loopsTooLong) {
|
|
373
|
+
add({
|
|
374
|
+
nodeId: node.id,
|
|
375
|
+
rule: "motion",
|
|
376
|
+
severity: "warning",
|
|
377
|
+
message:
|
|
378
|
+
"This element's loop keeps moving for more than 5 seconds and visitors can't pause it. Turn off Loop forever, or keep it only if the motion is essential.",
|
|
379
|
+
});
|
|
380
|
+
}
|
|
351
381
|
// W-403: with extra sizes but no Width, the main image is left out of srcset, so wide screens
|
|
352
382
|
// can only get the listed copies.
|
|
353
383
|
if (node.type === "image" && Array.isArray(props["sources"]) && props["sources"].length > 0) {
|
package/src/core/arrange.ts
CHANGED
|
@@ -336,7 +336,11 @@ export const wrapInThemeSection = (
|
|
|
336
336
|
partId: string,
|
|
337
337
|
random: () => number = Math.random,
|
|
338
338
|
): Arranged =>
|
|
339
|
-
wrapWith(layout, id, {
|
|
339
|
+
wrapWith(layout, id, {
|
|
340
|
+
id: newNodeId(random),
|
|
341
|
+
type: "part-ref",
|
|
342
|
+
props: { partId },
|
|
343
|
+
} as LayoutNode);
|
|
340
344
|
|
|
341
345
|
function wrapWith(layout: Layout, id: string, shell: LayoutNode): Arranged {
|
|
342
346
|
const found = locate(layout, id);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* D-070 (GHSA-mrhx-c7xc-m3w5): zod before 4.6.6 parses every element of an array, collecting an
|
|
5
|
+
* issue for each bad one, before `.max()` is checked. A 10 MB request of bad items held about 2 GB.
|
|
6
|
+
* EmDash 1.4 pins zod 4.5.4, and it parses EmVB's route and MCP tool inputs before any EmVB code
|
|
7
|
+
* runs, so every array in such an input checks its length first and stops with a single issue.
|
|
8
|
+
* JSON Schema output (MCP `tools/list`) is the plain array, with minItems/maxItems.
|
|
9
|
+
*/
|
|
10
|
+
export function boundedArray<T extends z.ZodType>(item: T, max: number, min = 0) {
|
|
11
|
+
return z
|
|
12
|
+
.unknown()
|
|
13
|
+
.check((ctx) => {
|
|
14
|
+
const value = ctx.value;
|
|
15
|
+
if (!Array.isArray(value)) return;
|
|
16
|
+
if (value.length > max || value.length < min) {
|
|
17
|
+
ctx.issues.push({
|
|
18
|
+
code: value.length > max ? "too_big" : "too_small",
|
|
19
|
+
origin: "array",
|
|
20
|
+
...(value.length > max ? { maximum: max } : { minimum: min }),
|
|
21
|
+
inclusive: true,
|
|
22
|
+
input: value,
|
|
23
|
+
message:
|
|
24
|
+
value.length > max
|
|
25
|
+
? `Too many items: at most ${max}.`
|
|
26
|
+
: `Too few items: at least ${min}.`,
|
|
27
|
+
} as never);
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
.pipe(z.array(item).min(min).max(max));
|
|
31
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* W-897 (W-P02): host collections whose entries carry an `emvb:canvas` field. Only EmDash-style
|
|
3
|
+
* slugs are kept; EmVB's own collections are left out because their hooks already cover them.
|
|
4
|
+
*/
|
|
5
|
+
export const MAX_CANVAS_COLLECTIONS = 20;
|
|
6
|
+
const OWN = new Set(["emvb_pages", "emvb_theme_parts"]);
|
|
7
|
+
const SLUG = /^[a-z][a-z0-9_]{0,62}$/;
|
|
8
|
+
|
|
9
|
+
export function canvasCollectionSlugs(input: unknown): string[] {
|
|
10
|
+
if (!Array.isArray(input)) return [];
|
|
11
|
+
const out: string[] = [];
|
|
12
|
+
for (const item of input) {
|
|
13
|
+
if (typeof item !== "string") continue;
|
|
14
|
+
const slug = item.trim();
|
|
15
|
+
if (!SLUG.test(slug) || OWN.has(slug) || out.includes(slug)) continue;
|
|
16
|
+
out.push(slug);
|
|
17
|
+
if (out.length === MAX_CANVAS_COLLECTIONS) break;
|
|
18
|
+
}
|
|
19
|
+
return out;
|
|
20
|
+
}
|
package/src/core/clipboard.ts
CHANGED
|
@@ -203,6 +203,7 @@ function refsInStyle(style: StyleProps | undefined): Ref[] {
|
|
|
203
203
|
const refsOf = (owner: CopiedStyle): Ref[] => [
|
|
204
204
|
...refsInStyle(owner.style),
|
|
205
205
|
...STYLE_STATES.flatMap((state) => refsInStyle(owner.states?.[state])),
|
|
206
|
+
...refsInStyle(owner.devices?.laptop),
|
|
206
207
|
...refsInStyle(owner.devices?.tablet),
|
|
207
208
|
...refsInStyle(owner.devices?.mobile),
|
|
208
209
|
// W-643: a container-query override (W-346) can use a variable too.
|
|
@@ -377,6 +378,14 @@ function withoutIconKeys(style: object | undefined): object | undefined {
|
|
|
377
378
|
removed = true;
|
|
378
379
|
}
|
|
379
380
|
}
|
|
381
|
+
// W-925: the Icon animation is `motion.icon` since schema 18.
|
|
382
|
+
const motion = next["motion"];
|
|
383
|
+
if (isRecord(motion) && "icon" in motion) {
|
|
384
|
+
const { icon: _icon, ...rest } = motion;
|
|
385
|
+
if (Object.keys(rest).length > 0) next["motion"] = rest;
|
|
386
|
+
else delete next["motion"];
|
|
387
|
+
removed = true;
|
|
388
|
+
}
|
|
380
389
|
if (!removed) return style;
|
|
381
390
|
return Object.keys(next).length > 0 ? next : undefined;
|
|
382
391
|
}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { MAX_UNKNOWN_DEPTH, MAX_UNKNOWN_KEYS, UNKNOWN_KEY_PATTERN } from "./limits.ts";
|
|
2
|
+
import { flattenMotion, isFlatMotionKey, withoutStrayMotionKeys } from "./migrate/v18.ts";
|
|
3
|
+
import { isRecord } from "./record.ts";
|
|
4
|
+
import { KNOWN_NODE_SCHEMAS } from "./schema/layout.ts";
|
|
5
|
+
import { FlatStyleProps, StyleProps } from "./schema/style.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* D-065 (W-925, contract 1.0): a 1.x document may carry keys this version does not know, on a
|
|
9
|
+
* node, its props, or a style record (style, states, devices, query style; a class or tag
|
|
10
|
+
* default in the design). They are set aside before the strict parse, checked against the
|
|
11
|
+
* limits in limits.ts, put back untouched after it, and never rendered: `renderableLayout` and
|
|
12
|
+
* `renderableDesign` keep known keys only. Everywhere else stays strict.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export type Path = readonly (string | number)[];
|
|
16
|
+
export type UnknownKey = { path: Path; key: string; value: unknown };
|
|
17
|
+
type ContractIssue = { path: string; code: string; message: string };
|
|
18
|
+
|
|
19
|
+
function shapeKeys(schema: unknown): Set<string> {
|
|
20
|
+
const shape = (schema as { shape?: Record<string, unknown> }).shape;
|
|
21
|
+
return new Set(shape ? Object.keys(shape) : []);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const NODE_KEYS = new Set<string>(["children"]);
|
|
25
|
+
const PROP_KEYS = new Map<string, Set<string>>();
|
|
26
|
+
for (const schema of KNOWN_NODE_SCHEMAS) {
|
|
27
|
+
const shape = (schema as unknown as { shape: Record<string, unknown> }).shape;
|
|
28
|
+
for (const key of Object.keys(shape)) NODE_KEYS.add(key);
|
|
29
|
+
const type = (shape["type"] as { value?: unknown }).value;
|
|
30
|
+
if (typeof type === "string") PROP_KEYS.set(type, shapeKeys(shape["props"]));
|
|
31
|
+
}
|
|
32
|
+
const STYLE_KEYS = shapeKeys(StyleProps);
|
|
33
|
+
const FLAT_STYLE_KEYS = shapeKeys(FlatStyleProps);
|
|
34
|
+
const STATES = ["hover", "focus", "active"] as const;
|
|
35
|
+
const DEVICES = ["laptop", "tablet", "mobile"] as const;
|
|
36
|
+
|
|
37
|
+
function formatPath(path: Path): string {
|
|
38
|
+
return path.reduce<string>((out, key) => {
|
|
39
|
+
if (typeof key === "number") return `${out}[${key}]`;
|
|
40
|
+
return out ? `${out}.${key}` : key;
|
|
41
|
+
}, "");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function depthOf(value: unknown, limit: number): number {
|
|
45
|
+
let max = 0;
|
|
46
|
+
const stack: { value: unknown; depth: number }[] = [{ value, depth: 1 }];
|
|
47
|
+
while (stack.length > 0) {
|
|
48
|
+
const { value: v, depth } = stack.pop() as { value: unknown; depth: number };
|
|
49
|
+
if (typeof v !== "object" || v === null) continue;
|
|
50
|
+
max = Math.max(max, depth);
|
|
51
|
+
if (max > limit) return max;
|
|
52
|
+
for (const child of Object.values(v)) stack.push({ value: child, depth: depth + 1 });
|
|
53
|
+
}
|
|
54
|
+
return max;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
class Splitter {
|
|
58
|
+
readonly extras: UnknownKey[] = [];
|
|
59
|
+
readonly issues: ContractIssue[] = [];
|
|
60
|
+
|
|
61
|
+
/** Moves keys not in `knownKeys` out of `record` (mutated: it is the upgrader's private copy). */
|
|
62
|
+
take(record: unknown, knownKeys: ReadonlySet<string>, path: Path): void {
|
|
63
|
+
if (!isRecord(record)) return;
|
|
64
|
+
let count = 0;
|
|
65
|
+
for (const key of Object.keys(record)) {
|
|
66
|
+
if (knownKeys.has(key)) continue;
|
|
67
|
+
const where = formatPath([...path, key]);
|
|
68
|
+
count++;
|
|
69
|
+
if (!UNKNOWN_KEY_PATTERN.test(key)) {
|
|
70
|
+
this.issues.push({
|
|
71
|
+
path: where,
|
|
72
|
+
code: "unknown_key",
|
|
73
|
+
message: `"${key.slice(0, 40)}" is not a key EmVB can keep. Names start with a letter and use letters, digits and _ . : / @ -.`,
|
|
74
|
+
});
|
|
75
|
+
} else if (count > MAX_UNKNOWN_KEYS) {
|
|
76
|
+
this.issues.push({
|
|
77
|
+
path: where,
|
|
78
|
+
code: "unknown_key",
|
|
79
|
+
message: `At most ${MAX_UNKNOWN_KEYS} keys this EmVB does not know can be kept in one place.`,
|
|
80
|
+
});
|
|
81
|
+
} else if (depthOf(record[key], MAX_UNKNOWN_DEPTH) > MAX_UNKNOWN_DEPTH) {
|
|
82
|
+
this.issues.push({
|
|
83
|
+
path: where,
|
|
84
|
+
code: "unknown_key",
|
|
85
|
+
message: `A key this EmVB does not know can nest at most ${MAX_UNKNOWN_DEPTH} levels.`,
|
|
86
|
+
});
|
|
87
|
+
} else {
|
|
88
|
+
this.extras.push({ path, key, value: record[key] });
|
|
89
|
+
}
|
|
90
|
+
delete record[key];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
styleRecords(owner: Record<string, unknown>, path: Path): void {
|
|
95
|
+
this.take(owner["style"], STYLE_KEYS, [...path, "style"]);
|
|
96
|
+
const states = owner["states"];
|
|
97
|
+
if (isRecord(states)) {
|
|
98
|
+
// `motion` is a known key here so the schema refuses it in a state, not kept as unknown.
|
|
99
|
+
for (const state of STATES) this.take(states[state], STYLE_KEYS, [...path, "states", state]);
|
|
100
|
+
}
|
|
101
|
+
const devices = owner["devices"];
|
|
102
|
+
if (isRecord(devices)) {
|
|
103
|
+
for (const device of DEVICES)
|
|
104
|
+
this.take(devices[device], STYLE_KEYS, [...path, "devices", device]);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
node(node: unknown, path: Path): void {
|
|
109
|
+
if (!isRecord(node)) return;
|
|
110
|
+
const type = typeof node["type"] === "string" ? node["type"] : "";
|
|
111
|
+
const props = PROP_KEYS.get(type);
|
|
112
|
+
if (props) this.take(node["props"], props, [...path, "props"]);
|
|
113
|
+
this.styleRecords(node, path);
|
|
114
|
+
const query = node["query"];
|
|
115
|
+
if (isRecord(query)) this.take(query["style"], STYLE_KEYS, [...path, "query", "style"]);
|
|
116
|
+
this.take(node, NODE_KEYS, path);
|
|
117
|
+
const children = node["children"];
|
|
118
|
+
if (Array.isArray(children)) {
|
|
119
|
+
children.forEach((child, i) => this.node(child, [...path, "children", i]));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Sets aside a layout's unknown keys. `doc` must be a private copy; it is changed in place. */
|
|
125
|
+
export function splitLayoutExtras(doc: Record<string, unknown>): Splitter {
|
|
126
|
+
const splitter = new Splitter();
|
|
127
|
+
splitter.node(doc["root"], ["root"]);
|
|
128
|
+
return splitter;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Sets aside a design's unknown keys (class and tag-default style records). Changes `doc`. */
|
|
132
|
+
export function splitDesignExtras(doc: Record<string, unknown>): Splitter {
|
|
133
|
+
const splitter = new Splitter();
|
|
134
|
+
const classes = doc["classes"];
|
|
135
|
+
if (Array.isArray(classes)) {
|
|
136
|
+
classes.forEach((cls, i) => {
|
|
137
|
+
if (isRecord(cls)) splitter.styleRecords(cls, ["classes", i]);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
const defaults = doc["defaults"];
|
|
141
|
+
if (isRecord(defaults)) {
|
|
142
|
+
for (const [tag, style] of Object.entries(defaults))
|
|
143
|
+
splitter.take(style, STYLE_KEYS, ["defaults", tag]);
|
|
144
|
+
}
|
|
145
|
+
return splitter;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Puts the set-aside keys back on the parsed document, untouched. Changes `doc`. */
|
|
149
|
+
export function restoreExtras<T>(doc: T, extras: readonly UnknownKey[]): T {
|
|
150
|
+
for (const { path, key, value } of extras) {
|
|
151
|
+
let target: unknown = doc;
|
|
152
|
+
for (const step of path) target = (target as Record<string | number, unknown>)?.[step];
|
|
153
|
+
if (isRecord(target)) target[key] = value;
|
|
154
|
+
}
|
|
155
|
+
return doc;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function known<T>(record: T, keys: ReadonlySet<string>): T {
|
|
159
|
+
if (!isRecord(record)) return record;
|
|
160
|
+
let extra = false;
|
|
161
|
+
for (const key of Object.keys(record)) if (!keys.has(key)) extra = true;
|
|
162
|
+
if (!extra) return record;
|
|
163
|
+
const out: Record<string, unknown> = {};
|
|
164
|
+
for (const [key, value] of Object.entries(record)) if (keys.has(key)) out[key] = value;
|
|
165
|
+
return out as T;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const FLAT_RANK = new Map([...FLAT_STYLE_KEYS].map((key, i) => [key, i]));
|
|
169
|
+
const rankOf = (key: string): number => FLAT_RANK.get(key) ?? -1;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* The motion parts back where 0.x listed them among the other keys. A parsed style is in schema
|
|
173
|
+
* order, so this gives exactly the 0.x order, and with it the same CSS.
|
|
174
|
+
*/
|
|
175
|
+
function inFlatOrder<T>(style: T, nested: unknown): T {
|
|
176
|
+
if (!isRecord(style) || !isRecord(nested) || !("motion" in nested)) return style;
|
|
177
|
+
const parts = Object.entries(style)
|
|
178
|
+
.filter(([key]) => isFlatMotionKey(key))
|
|
179
|
+
.toSorted(([a], [b]) => rankOf(a) - rankOf(b));
|
|
180
|
+
const out: Record<string, unknown> = {};
|
|
181
|
+
let next = 0;
|
|
182
|
+
for (const [key, value] of Object.entries(style)) {
|
|
183
|
+
if (isFlatMotionKey(key)) continue;
|
|
184
|
+
while (next < parts.length && rankOf((parts[next] as [string, unknown])[0]) < rankOf(key)) {
|
|
185
|
+
const [part, partValue] = parts[next++] as [string, unknown];
|
|
186
|
+
out[part] = partValue;
|
|
187
|
+
}
|
|
188
|
+
out[key] = value;
|
|
189
|
+
}
|
|
190
|
+
for (; next < parts.length; next++) {
|
|
191
|
+
const [part, partValue] = parts[next] as [string, unknown];
|
|
192
|
+
out[part] = partValue;
|
|
193
|
+
}
|
|
194
|
+
return out as T;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** A stored style as the renderer reads it: 0.x flat motion keys, known keys only. */
|
|
198
|
+
export function renderableStyle<T>(style: T): T {
|
|
199
|
+
return inFlatOrder(known(flattenMotion(withoutStrayMotionKeys(style)), FLAT_STYLE_KEYS), style);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function renderableStyles<T extends Record<string, unknown>>(owner: T): T {
|
|
203
|
+
const out: Record<string, unknown> = { ...owner };
|
|
204
|
+
if ("style" in owner) out["style"] = renderableStyle(owner["style"]);
|
|
205
|
+
const states = owner["states"];
|
|
206
|
+
if (isRecord(states)) {
|
|
207
|
+
const next: Record<string, unknown> = {};
|
|
208
|
+
// A state never takes motion; a forged `motion` is flattened so the CSS writer refuses its
|
|
209
|
+
// parts with a warning, as it did for the 0.x flat keys.
|
|
210
|
+
for (const [state, style] of Object.entries(states))
|
|
211
|
+
next[state] = known(flattenMotion(withoutStrayMotionKeys(style)), FLAT_STYLE_KEYS);
|
|
212
|
+
out["states"] = next;
|
|
213
|
+
}
|
|
214
|
+
const devices = owner["devices"];
|
|
215
|
+
if (isRecord(devices)) {
|
|
216
|
+
const next: Record<string, unknown> = {};
|
|
217
|
+
for (const [device, style] of Object.entries(devices)) next[device] = renderableStyle(style);
|
|
218
|
+
out["devices"] = next;
|
|
219
|
+
}
|
|
220
|
+
return out as T;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** A node as the renderer reads it: known keys only, motion flattened (D-065). */
|
|
224
|
+
export function renderableNode<T>(input: T): T {
|
|
225
|
+
if (!isRecord(input)) return input;
|
|
226
|
+
const node: Record<string, unknown> = input;
|
|
227
|
+
const type = typeof node["type"] === "string" ? node["type"] : "";
|
|
228
|
+
const propKeys = PROP_KEYS.get(type);
|
|
229
|
+
const out: Record<string, unknown> = renderableStyles(propKeys ? known(node, NODE_KEYS) : node);
|
|
230
|
+
if (propKeys && isRecord(out["props"])) out["props"] = known(out["props"], propKeys);
|
|
231
|
+
const query = out["query"];
|
|
232
|
+
if (isRecord(query) && "style" in query)
|
|
233
|
+
out["query"] = { ...query, style: renderableStyle(query["style"]) };
|
|
234
|
+
const children = out["children"];
|
|
235
|
+
if (Array.isArray(children)) out["children"] = children.map((child) => renderableNode(child));
|
|
236
|
+
return out as T;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function renderableLayout<T extends { root: unknown }>(layout: T): T {
|
|
240
|
+
return { ...layout, root: renderableNode(layout.root) };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export function renderableDesign<T>(design: T): T {
|
|
244
|
+
if (!isRecord(design)) return design;
|
|
245
|
+
const out: Record<string, unknown> = { ...design };
|
|
246
|
+
if (Array.isArray(design["classes"])) {
|
|
247
|
+
out["classes"] = design["classes"].map((cls: unknown) =>
|
|
248
|
+
isRecord(cls) ? renderableStyles(cls) : cls,
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
if (isRecord(design["defaults"])) {
|
|
252
|
+
const defaults: Record<string, unknown> = {};
|
|
253
|
+
for (const [tag, style] of Object.entries(design["defaults"]))
|
|
254
|
+
defaults[tag] = renderableStyle(style);
|
|
255
|
+
out["defaults"] = defaults;
|
|
256
|
+
}
|
|
257
|
+
return out as T;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function lazyTemplates<T>(templates: Record<string, T> | undefined): Record<string, T> | undefined {
|
|
261
|
+
if (!templates) return templates;
|
|
262
|
+
const out: Record<string, T> = {};
|
|
263
|
+
for (const id of Object.keys(templates)) {
|
|
264
|
+
let cached: T | undefined;
|
|
265
|
+
Object.defineProperty(out, id, {
|
|
266
|
+
enumerable: true,
|
|
267
|
+
get: () => (cached ??= renderableLayout(templates[id] as T & { root: unknown })),
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
return out;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Theme templates as the renderer reads them, converted when first used. */
|
|
274
|
+
export function renderableDynamic<
|
|
275
|
+
T extends { sectionTemplates?: Record<string, unknown>; loopTemplates?: Record<string, unknown> },
|
|
276
|
+
>(dynamic: T | undefined): T | undefined {
|
|
277
|
+
if (!dynamic || (!dynamic.sectionTemplates && !dynamic.loopTemplates)) return dynamic;
|
|
278
|
+
return {
|
|
279
|
+
...dynamic,
|
|
280
|
+
...(dynamic.sectionTemplates
|
|
281
|
+
? { sectionTemplates: lazyTemplates(dynamic.sectionTemplates) }
|
|
282
|
+
: {}),
|
|
283
|
+
...(dynamic.loopTemplates ? { loopTemplates: lazyTemplates(dynamic.loopTemplates) } : {}),
|
|
284
|
+
};
|
|
285
|
+
}
|