@perkyzz/emvb 0.0.0-stage → 0.2.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.
Files changed (217) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +59 -2
  3. package/THIRD_PARTY_NOTICES.md +32 -0
  4. package/licenses/fontawesome-free.LICENSE +165 -0
  5. package/licenses/lucide-static.LICENSE +43 -0
  6. package/licenses/remixicon.LICENSE +201 -0
  7. package/licenses/tabler-icons.LICENSE +21 -0
  8. package/package.json +76 -4
  9. package/src/admin/access/RequireEditor.tsx +39 -0
  10. package/src/admin/access/role.ts +36 -0
  11. package/src/admin/access/sidebar-shim.ts +108 -0
  12. package/src/admin/api.ts +75 -0
  13. package/src/admin/content-api.ts +210 -0
  14. package/src/admin/editor/Editor.tsx +532 -0
  15. package/src/admin/editor/EditorOverlay.tsx +66 -0
  16. package/src/admin/editor/ShortcutsDialog.tsx +30 -0
  17. package/src/admin/editor/SmallScreenNotice.tsx +21 -0
  18. package/src/admin/editor/TopBar.tsx +243 -0
  19. package/src/admin/editor/canvas/CanvasFrame.tsx +536 -0
  20. package/src/admin/editor/canvas/CanvasTextEdit.tsx +87 -0
  21. package/src/admin/editor/canvas/DeviceBar.tsx +91 -0
  22. package/src/admin/editor/canvas/EmptyCanvas.tsx +49 -0
  23. package/src/admin/editor/canvas/SelectionOverlay.tsx +239 -0
  24. package/src/admin/editor/canvas/canvas-events.ts +245 -0
  25. package/src/admin/editor/canvas/state-preview.ts +19 -0
  26. package/src/admin/editor/canvas/tab-reveal.ts +101 -0
  27. package/src/admin/editor/canvas/vnode-react.tsx +69 -0
  28. package/src/admin/editor/clipboard-store.ts +44 -0
  29. package/src/admin/editor/dialogs.tsx +164 -0
  30. package/src/admin/editor/dnd/drag-stash.ts +27 -0
  31. package/src/admin/editor/dnd/drop-target.ts +148 -0
  32. package/src/admin/editor/dnd/new-element.ts +107 -0
  33. package/src/admin/editor/editor-css.ts +625 -0
  34. package/src/admin/editor/exit.ts +27 -0
  35. package/src/admin/editor/host.ts +30 -0
  36. package/src/admin/editor/icons/IconLibraryDialog.tsx +850 -0
  37. package/src/admin/editor/icons/library.ts +243 -0
  38. package/src/admin/editor/icons/set-format.ts +29 -0
  39. package/src/admin/editor/icons/sets/fontawesome.json +1 -0
  40. package/src/admin/editor/icons/sets/lucide.json +1 -0
  41. package/src/admin/editor/icons/sets/remix.json +1 -0
  42. package/src/admin/editor/icons/sets/tabler.json +1 -0
  43. package/src/admin/editor/panels/AddPanel.tsx +146 -0
  44. package/src/admin/editor/panels/ClassesSection.tsx +372 -0
  45. package/src/admin/editor/panels/ColorControl.tsx +477 -0
  46. package/src/admin/editor/panels/ConditionsEditor.tsx +201 -0
  47. package/src/admin/editor/panels/ElementPanel.tsx +773 -0
  48. package/src/admin/editor/panels/LayersPanel.tsx +573 -0
  49. package/src/admin/editor/panels/LeftPanel.tsx +81 -0
  50. package/src/admin/editor/panels/PageSettings.tsx +255 -0
  51. package/src/admin/editor/panels/SettingsPanel.tsx +113 -0
  52. package/src/admin/editor/panels/SiteStylesDrawer.tsx +548 -0
  53. package/src/admin/editor/panels/TriggerSettings.tsx +132 -0
  54. package/src/admin/editor/panels/TriggersEditor.tsx +214 -0
  55. package/src/admin/editor/panels/VariableSection.tsx +530 -0
  56. package/src/admin/editor/panels/color-space.ts +195 -0
  57. package/src/admin/editor/panels/settings/AdvancedFields.tsx +119 -0
  58. package/src/admin/editor/panels/settings/BackgroundControls.tsx +418 -0
  59. package/src/admin/editor/panels/settings/BackgroundFill.tsx +221 -0
  60. package/src/admin/editor/panels/settings/BoxControl.tsx +271 -0
  61. package/src/admin/editor/panels/settings/ChoiceGroup.tsx +138 -0
  62. package/src/admin/editor/panels/settings/ClassChip.tsx +167 -0
  63. package/src/admin/editor/panels/settings/ClassChipInput.tsx +451 -0
  64. package/src/admin/editor/panels/settings/DraftTextField.tsx +242 -0
  65. package/src/admin/editor/panels/settings/EffectsControls.tsx +354 -0
  66. package/src/admin/editor/panels/settings/FieldBindControl.tsx +119 -0
  67. package/src/admin/editor/panels/settings/FieldControl.tsx +270 -0
  68. package/src/admin/editor/panels/settings/FormBindControl.tsx +106 -0
  69. package/src/admin/editor/panels/settings/IconControls.tsx +438 -0
  70. package/src/admin/editor/panels/settings/IconPicker.tsx +170 -0
  71. package/src/admin/editor/panels/settings/ItemList.tsx +199 -0
  72. package/src/admin/editor/panels/settings/LengthRow.tsx +217 -0
  73. package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +130 -0
  74. package/src/admin/editor/panels/settings/MediaPicker.tsx +220 -0
  75. package/src/admin/editor/panels/settings/NumberRow.tsx +154 -0
  76. package/src/admin/editor/panels/settings/StateSwitcher.tsx +87 -0
  77. package/src/admin/editor/panels/settings/StyleRow.tsx +563 -0
  78. package/src/admin/editor/panels/settings/VariableBinding.tsx +171 -0
  79. package/src/admin/editor/panels/settings/advanced-rules.ts +63 -0
  80. package/src/admin/editor/panels/settings/background-mode.ts +112 -0
  81. package/src/admin/editor/panels/settings/box-link-name.ts +38 -0
  82. package/src/admin/editor/panels/settings/box-sides.ts +153 -0
  83. package/src/admin/editor/panels/settings/button-url.ts +27 -0
  84. package/src/admin/editor/panels/settings/class-suggest.ts +53 -0
  85. package/src/admin/editor/panels/settings/form-names.ts +38 -0
  86. package/src/admin/editor/panels/settings/heading-warning.ts +25 -0
  87. package/src/admin/editor/panels/settings/image-alt.ts +25 -0
  88. package/src/admin/editor/panels/settings/length-units.ts +116 -0
  89. package/src/admin/editor/panels/settings/loop-context.ts +26 -0
  90. package/src/admin/editor/panels/settings/node-props.ts +11 -0
  91. package/src/admin/editor/panels/settings/style-sections.ts +394 -0
  92. package/src/admin/editor/panels/settings/svg-images.ts +14 -0
  93. package/src/admin/editor/panels/site-list.tsx +209 -0
  94. package/src/admin/editor/panels/variable-values.ts +73 -0
  95. package/src/admin/editor/restore-deleted.ts +80 -0
  96. package/src/admin/editor/section-templates.ts +50 -0
  97. package/src/admin/editor/shortcuts.ts +20 -0
  98. package/src/admin/editor/store.ts +240 -0
  99. package/src/admin/editor/useClipboardActions.ts +136 -0
  100. package/src/admin/editor/useEditorCommands.ts +203 -0
  101. package/src/admin/editor/useEditorData.ts +193 -0
  102. package/src/admin/editor/useEditorShortcuts.ts +180 -0
  103. package/src/admin/editor/useMediaQuery.ts +20 -0
  104. package/src/admin/editor/useNodeActions.ts +184 -0
  105. package/src/admin/editor/useSave.ts +242 -0
  106. package/src/admin/entry-status.ts +19 -0
  107. package/src/admin/forms-api.ts +73 -0
  108. package/src/admin/icon-uploads-api.ts +44 -0
  109. package/src/admin/index.tsx +20 -0
  110. package/src/admin/media-api.ts +68 -0
  111. package/src/admin/pages/CreateDialog.tsx +150 -0
  112. package/src/admin/pages/EditorPage.tsx +57 -0
  113. package/src/admin/pages/NewPageDialog.tsx +83 -0
  114. package/src/admin/pages/NewThemePartDialog.tsx +61 -0
  115. package/src/admin/pages/PageList.tsx +91 -0
  116. package/src/admin/pages/PagesPage.tsx +25 -0
  117. package/src/admin/pages/SetupShell.tsx +90 -0
  118. package/src/admin/pages/ThemeBuilderPage.tsx +25 -0
  119. package/src/admin/pages/ThemePartList.tsx +221 -0
  120. package/src/admin/pages/list-kit.tsx +125 -0
  121. package/src/admin/setup/plan.ts +150 -0
  122. package/src/admin/setup/run.ts +61 -0
  123. package/src/admin/setup/spec.ts +107 -0
  124. package/src/admin/setup/useSetupStatus.ts +62 -0
  125. package/src/admin/theme-api.ts +233 -0
  126. package/src/admin/ui.ts +244 -0
  127. package/src/admin/widgets/LayoutField.tsx +67 -0
  128. package/src/astro/EmVBPage.astro +63 -0
  129. package/src/astro/env.d.ts +5 -0
  130. package/src/astro/floats/EmVBFloatsRuntime.astro +9 -0
  131. package/src/astro/floats/index.ts +1 -0
  132. package/src/astro/floats/runtime.ts +78 -0
  133. package/src/astro/forms/EmVBFormsRuntime.astro +9 -0
  134. package/src/astro/forms/index.ts +1 -0
  135. package/src/astro/forms-definitions.ts +126 -0
  136. package/src/astro/index.ts +16 -0
  137. package/src/astro/menu/EmVBMenuRuntime.astro +9 -0
  138. package/src/astro/menu/index.ts +1 -0
  139. package/src/astro/menu/runtime.ts +37 -0
  140. package/src/astro/popups/EmVBPopupsRuntime.astro +9 -0
  141. package/src/astro/popups/index.ts +1 -0
  142. package/src/astro/popups/runtime.ts +296 -0
  143. package/src/astro/render.ts +108 -0
  144. package/src/astro/resolve-theme.ts +515 -0
  145. package/src/astro/resolve.ts +65 -0
  146. package/src/astro/tabs/EmVBTabsRuntime.astro +10 -0
  147. package/src/astro/tabs/index.ts +1 -0
  148. package/src/astro/tabs/runtime.ts +116 -0
  149. package/src/astro/theme-context.ts +89 -0
  150. package/src/astro/theme-posts.ts +84 -0
  151. package/src/constants.ts +12 -0
  152. package/src/core/arrange.ts +448 -0
  153. package/src/core/clipboard.ts +504 -0
  154. package/src/core/css/generate.ts +236 -0
  155. package/src/core/css/scope.ts +88 -0
  156. package/src/core/design/cascade.ts +28 -0
  157. package/src/core/design/classes.ts +224 -0
  158. package/src/core/design/devices.ts +56 -0
  159. package/src/core/design/states.ts +49 -0
  160. package/src/core/design/transfer.ts +132 -0
  161. package/src/core/design/variables.ts +359 -0
  162. package/src/core/elements/definition.ts +12 -0
  163. package/src/core/elements/dynamic-elements.ts +225 -0
  164. package/src/core/elements/field-id.ts +6 -0
  165. package/src/core/elements/form-elements.ts +219 -0
  166. package/src/core/elements/index.ts +960 -0
  167. package/src/core/elements/menu.ts +190 -0
  168. package/src/core/elements/plain-text.ts +32 -0
  169. package/src/core/forms/binding.ts +61 -0
  170. package/src/core/forms/definition.ts +45 -0
  171. package/src/core/forms/success-layout.ts +63 -0
  172. package/src/core/icons/catalog.ts +273 -0
  173. package/src/core/index.ts +380 -0
  174. package/src/core/limits.ts +9 -0
  175. package/src/core/migrate/index.ts +126 -0
  176. package/src/core/nesting.ts +51 -0
  177. package/src/core/render/context.ts +21 -0
  178. package/src/core/render/dynamic-post.ts +175 -0
  179. package/src/core/render/extras.ts +98 -0
  180. package/src/core/render/form.ts +235 -0
  181. package/src/core/render/index.ts +372 -0
  182. package/src/core/render/loop.ts +43 -0
  183. package/src/core/render/pagination.ts +76 -0
  184. package/src/core/render/section.ts +37 -0
  185. package/src/core/render/tabs.ts +76 -0
  186. package/src/core/render/vnode.ts +87 -0
  187. package/src/core/sanitize/css.ts +771 -0
  188. package/src/core/sanitize/embed-url.ts +160 -0
  189. package/src/core/sanitize/escape.ts +10 -0
  190. package/src/core/sanitize/href.ts +6 -0
  191. package/src/core/sanitize/media-url.ts +7 -0
  192. package/src/core/sanitize/svg-upload.ts +203 -0
  193. package/src/core/sanitize/svg.ts +374 -0
  194. package/src/core/sanitize/url.ts +31 -0
  195. package/src/core/schema/descriptors.ts +37 -0
  196. package/src/core/schema/design.ts +93 -0
  197. package/src/core/schema/layout.ts +1008 -0
  198. package/src/core/schema/state-names.ts +4 -0
  199. package/src/core/schema/style.ts +336 -0
  200. package/src/core/stats.ts +18 -0
  201. package/src/core/tabs/presence.ts +25 -0
  202. package/src/core/theme/conditions.ts +324 -0
  203. package/src/core/theme/dynamic.ts +143 -0
  204. package/src/core/theme/float.ts +113 -0
  205. package/src/core/theme/pagination.ts +97 -0
  206. package/src/core/theme/parse-doc.ts +36 -0
  207. package/src/core/theme/part-types.ts +81 -0
  208. package/src/core/theme/popup-markup.ts +41 -0
  209. package/src/core/theme/popup-rules.ts +62 -0
  210. package/src/core/theme/triggers.ts +104 -0
  211. package/src/core/tree-ops.ts +139 -0
  212. package/src/core/validate.ts +185 -0
  213. package/src/index.ts +18 -0
  214. package/src/server/design-routes.ts +125 -0
  215. package/src/server/hooks.ts +118 -0
  216. package/src/server/icon-routes.ts +79 -0
  217. package/src/server/plugin.ts +43 -0
@@ -0,0 +1,771 @@
1
+ import { STYLE_STATES, type StyleStateName } from "../schema/state-names.ts";
2
+ import type { StyleProps } from "../schema/style.ts";
3
+ import { sanitizeMediaUrl } from "./media-url.ts";
4
+
5
+ const HEX_COLOR = /^#(?:[0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
6
+ const VAR_ID = /^[a-z0-9-]{1,40}$/;
7
+ const UNITS = new Set(["px", "rem", "em", "%", "vw", "vh"]);
8
+ const FLEX_DIRECTIONS = new Set(["row", "column", "row-reverse", "column-reverse"]);
9
+ const FLEX_WRAPS = new Set(["nowrap", "wrap", "wrap-reverse"]);
10
+ const JUSTIFY = new Set([
11
+ "flex-start",
12
+ "flex-end",
13
+ "center",
14
+ "space-between",
15
+ "space-around",
16
+ "space-evenly",
17
+ ]);
18
+ const ALIGN = new Set(["stretch", "flex-start", "flex-end", "center", "baseline"]);
19
+ const TEXT_TRANSFORM = new Set(["none", "uppercase", "lowercase", "capitalize"]);
20
+ const TEXT_DECORATION = new Set(["none", "underline", "overline", "line-through"]);
21
+ const BORDER_STYLE = new Set(["none", "solid", "dashed", "dotted"]);
22
+ const OVERFLOW = new Set(["visible", "hidden", "clip", "scroll", "auto"]);
23
+ const OBJECT_FIT = new Set(["fill", "contain", "cover", "none", "scale-down"]);
24
+ const POSITION = new Set(["static", "relative", "absolute", "fixed", "sticky"]);
25
+ const CURSOR = new Set([
26
+ "default",
27
+ "pointer",
28
+ "text",
29
+ "move",
30
+ "grab",
31
+ "not-allowed",
32
+ "help",
33
+ "crosshair",
34
+ "zoom-in",
35
+ ]);
36
+ const ASPECT_RATIO = /^([1-9]\d{0,3})\/([1-9]\d{0,3})$/;
37
+ const FONT_WEIGHT = new Set([
38
+ "normal",
39
+ "bold",
40
+ "100",
41
+ "200",
42
+ "300",
43
+ "400",
44
+ "500",
45
+ "600",
46
+ "700",
47
+ "800",
48
+ "900",
49
+ ]);
50
+ const FORBIDDEN = /[{};<>\\"'`]|\/\*|url\(|expression\(|@import|javascript:/i;
51
+
52
+ /** Last line of defence on every emitted value, even ones built by the typed validators below. */
53
+ export function isSafeCssValue(value: string): boolean {
54
+ return value.length > 0 && value.length <= 200 && !FORBIDDEN.test(value) && !/[\n\r]/.test(value);
55
+ }
56
+
57
+ const FAMILY = String.raw`(?:'[\p{L}\p{N} _.-]+'|"[\p{L}\p{N} _.-]+"|[\p{L}\p{N}_-]+(?: [\p{L}\p{N}_-]+)*)`;
58
+ const FONT_STACK = new RegExp(String.raw`^${FAMILY}(?:\s*,\s*${FAMILY})*$`, "u");
59
+
60
+ /**
61
+ * A comma-separated font stack whose family names may be quoted. Quoted names hold only letters,
62
+ * digits, spaces, `_`, `.` and `-`, so a quote can never open or close anything else.
63
+ */
64
+ export function isSafeFontStack(value: string): boolean {
65
+ return (
66
+ isSafeCssValue(value) ||
67
+ (value.length <= 200 && !/[\n\r]/.test(value) && FONT_STACK.test(value))
68
+ );
69
+ }
70
+
71
+ export function colorVariableName(id: string): string | undefined {
72
+ return VAR_ID.test(id) ? `--emvb-c-${id}` : undefined;
73
+ }
74
+
75
+ export function fontVariableName(id: string): string | undefined {
76
+ return VAR_ID.test(id) ? `--emvb-f-${id}` : undefined;
77
+ }
78
+
79
+ export function fontSizeVariableName(id: string): string | undefined {
80
+ return VAR_ID.test(id) ? `--emvb-fs-${id}` : undefined;
81
+ }
82
+
83
+ export function spacingVariableName(id: string): string | undefined {
84
+ return VAR_ID.test(id) ? `--emvb-s-${id}` : undefined;
85
+ }
86
+
87
+ /** Public class selector fragment `emvb-k-<id>` (never unprefixed). */
88
+ export function styleClassName(id: string): string | undefined {
89
+ if (!/^[a-z0-9-]{1,40}$/.test(id)) return undefined;
90
+ return `emvb-k-${id}`;
91
+ }
92
+
93
+ type VarFrom = "color" | "font" | "fontSize" | "spacing";
94
+
95
+ function refOf(value: object): { id: string; from: VarFrom } | undefined {
96
+ if (!("var" in value) || typeof (value as { var?: unknown }).var !== "string") return undefined;
97
+ const id = (value as { var: string }).var;
98
+ if (!VAR_ID.test(id)) return undefined;
99
+ const from = (value as { from?: unknown }).from;
100
+ if (from === undefined || from === "color") return { id, from: "color" };
101
+ if (from === "font" || from === "fontSize" || from === "spacing") return { id, from };
102
+ return undefined;
103
+ }
104
+
105
+ export function cssColor(value: unknown): string | undefined {
106
+ if (typeof value === "string") return HEX_COLOR.test(value) ? value.toLowerCase() : undefined;
107
+ if (typeof value === "object" && value !== null) {
108
+ const ref = refOf(value);
109
+ if (!ref || ref.from !== "color") return undefined;
110
+ const name = colorVariableName(ref.id);
111
+ return name ? `var(${name})` : undefined;
112
+ }
113
+ return undefined;
114
+ }
115
+
116
+ function cssFontFamily(value: unknown): string | undefined {
117
+ if (typeof value === "string") {
118
+ return isSafeFontStack(value) && !HEX_COLOR.test(value) ? value : undefined;
119
+ }
120
+ if (typeof value === "object" && value !== null) {
121
+ const ref = refOf(value);
122
+ if (!ref || ref.from !== "font") return undefined;
123
+ const name = fontVariableName(ref.id);
124
+ return name ? `var(${name})` : undefined;
125
+ }
126
+ return undefined;
127
+ }
128
+
129
+ export function cssLength(value: unknown): string | undefined {
130
+ if (typeof value !== "object" || value === null) return undefined;
131
+ const ref = refOf(value);
132
+ if (ref) {
133
+ if (ref.from === "fontSize") {
134
+ const name = fontSizeVariableName(ref.id);
135
+ return name ? `var(${name})` : undefined;
136
+ }
137
+ if (ref.from === "spacing") {
138
+ const name = spacingVariableName(ref.id);
139
+ return name ? `var(${name})` : undefined;
140
+ }
141
+ return undefined;
142
+ }
143
+ const { value: n, unit } = value as { value?: unknown; unit?: unknown };
144
+ if (typeof n !== "number" || !Number.isFinite(n) || n < 0 || n > 10_000) return undefined;
145
+ if (typeof unit !== "string" || !UNITS.has(unit)) return undefined;
146
+ return `${n}${unit}`;
147
+ }
148
+
149
+ /** A length, or `auto` where the property allows it (W-088). */
150
+ const cssLengthOrAuto = (value: unknown): string | undefined =>
151
+ value === "auto" ? "auto" : cssLength(value);
152
+
153
+ /** An offset: `auto`, a variable, or a length from -10000 to 10000 (W-088). */
154
+ function cssOffset(value: unknown): string | undefined {
155
+ if (value === "auto") return "auto";
156
+ if (typeof value !== "object" || value === null || "var" in value) return cssLength(value);
157
+ const { value: n, unit } = value as { value?: unknown; unit?: unknown };
158
+ if (typeof n !== "number" || !Number.isFinite(n) || n < -10_000 || n > 10_000) return undefined;
159
+ if (typeof unit !== "string" || !UNITS.has(unit)) return undefined;
160
+ return `${n}${unit}`;
161
+ }
162
+
163
+ const cssZIndex = (value: unknown): string | undefined =>
164
+ Number.isInteger(value) && (value as number) >= -9999 && (value as number) <= 9999
165
+ ? String(value)
166
+ : undefined;
167
+
168
+ const ENTRANCES = new Set(["fade", "fade-up", "fade-down", "slide-up", "slide-down", "scale"]);
169
+
170
+ /** `emvb-<type> <ms> ease-out [delay] both`. A bad trigger drops the whole entrance. */
171
+ function cssEntrance(value: unknown): string | undefined {
172
+ if (typeof value !== "object" || value === null) return undefined;
173
+ const { type, duration, delay, trigger } = value as {
174
+ type?: unknown;
175
+ duration?: unknown;
176
+ delay?: unknown;
177
+ trigger?: unknown;
178
+ };
179
+ if (typeof type !== "string" || !ENTRANCES.has(type)) return undefined;
180
+ if (!Number.isInteger(duration) || (duration as number) < 0 || (duration as number) > 2000) {
181
+ return undefined;
182
+ }
183
+ if (trigger !== undefined && trigger !== "load" && trigger !== "view") return undefined;
184
+ if (
185
+ delay !== undefined &&
186
+ (!Number.isInteger(delay) || (delay as number) < 0 || (delay as number) > 2000)
187
+ ) {
188
+ return undefined;
189
+ }
190
+ const wait = typeof delay === "number" && delay > 0 ? ` ${delay}ms` : "";
191
+ return `emvb-${type} ${duration}ms ease-out${wait} both`;
192
+ }
193
+
194
+ function cssTextAlign(value: unknown): string | undefined {
195
+ if (value === "left") return "start";
196
+ if (value === "right") return "end";
197
+ if (value === "center" || value === "justify") return value;
198
+ return undefined;
199
+ }
200
+
201
+ const cssGridSpan = (value: unknown): string | undefined =>
202
+ Number.isInteger(value) && (value as number) >= 1 && (value as number) <= 12
203
+ ? `span ${value}`
204
+ : undefined;
205
+
206
+ const inRange = (value: unknown, min: number, max: number): value is number =>
207
+ typeof value === "number" && Number.isFinite(value) && value >= min && value <= max;
208
+
209
+ const cssOpacity = (value: unknown) => (inRange(value, 0, 1) ? String(value) : undefined);
210
+
211
+ const SHADOW_KEYS = new Set(["x", "y", "blur", "spread", "color", "inset"]);
212
+
213
+ /** `[inset] x y blur spread [colour]`, built only from checked numbers and a checked colour. */
214
+ function cssBoxShadow(value: unknown): string | undefined {
215
+ if (typeof value !== "object" || value === null) return undefined;
216
+ if (Object.keys(value).some((key) => !SHADOW_KEYS.has(key))) return undefined;
217
+ const { x, y, blur, spread, color, inset } = value as Record<string, unknown>;
218
+ if (!inRange(x, -1000, 1000) || !inRange(y, -1000, 1000)) return undefined;
219
+ if (!inRange(blur, 0, 1000) || !inRange(spread, -1000, 1000)) return undefined;
220
+ if (inset !== undefined && typeof inset !== "boolean") return undefined;
221
+ const colour = color === undefined ? undefined : cssColor(color);
222
+ if (color !== undefined && colour === undefined) return undefined;
223
+ const parts = [inset ? "inset" : "", `${x}px ${y}px ${blur}px ${spread}px`, colour ?? ""];
224
+ return parts.filter(Boolean).join(" ");
225
+ }
226
+
227
+ /** Filter functions in a fixed order, each with its own range and unit. */
228
+ const FILTERS: ReadonlyArray<[key: string, fn: string, max: number, unit: string]> = [
229
+ ["blur", "blur", 100, "px"],
230
+ ["brightness", "brightness", 300, "%"],
231
+ ["contrast", "contrast", 300, "%"],
232
+ ["saturate", "saturate", 300, "%"],
233
+ ["grayscale", "grayscale", 100, "%"],
234
+ ["hueRotate", "hue-rotate", 360, "deg"],
235
+ ];
236
+ const FILTER_KEYS = new Set(FILTERS.map(([key]) => key));
237
+
238
+ function cssFilter(value: unknown): string | undefined {
239
+ if (typeof value !== "object" || value === null) return undefined;
240
+ const filter = value as Record<string, unknown>;
241
+ if (Object.keys(filter).some((key) => !FILTER_KEYS.has(key))) return undefined;
242
+ const parts: string[] = [];
243
+ for (const [key, fn, max, unit] of FILTERS) {
244
+ const n = filter[key];
245
+ if (n === undefined) continue;
246
+ if (!inRange(n, 0, max)) return undefined;
247
+ parts.push(`${fn}(${n}${unit})`);
248
+ }
249
+ return parts.length > 0 ? parts.join(" ") : undefined;
250
+ }
251
+
252
+ function cssAspectRatio(value: unknown): string | undefined {
253
+ if (value === "auto") return "auto";
254
+ const match = typeof value === "string" ? ASPECT_RATIO.exec(value) : null;
255
+ return match ? `${match[1]} / ${match[2]}` : undefined;
256
+ }
257
+
258
+ export function cssFlexDirection(value: unknown): string | undefined {
259
+ return typeof value === "string" && FLEX_DIRECTIONS.has(value) ? value : undefined;
260
+ }
261
+
262
+ const keyword =
263
+ (allowed: Set<string>) =>
264
+ (value: unknown): string | undefined =>
265
+ typeof value === "string" && allowed.has(value) ? value : undefined;
266
+
267
+ /** W-213: a length, or a unitless number 0–100. */
268
+ function cssLineHeight(value: unknown): string | undefined {
269
+ if (typeof value === "number")
270
+ return Number.isFinite(value) && value >= 0 && value <= 100 ? String(value) : undefined;
271
+ return cssLength(value);
272
+ }
273
+
274
+ /** W-213: a length that may be negative (never `auto`). */
275
+ function cssLetterSpacing(value: unknown): string | undefined {
276
+ return value === "auto" ? undefined : cssOffset(value);
277
+ }
278
+
279
+ function cssFontWeight(value: unknown): string | undefined {
280
+ if (typeof value === "number" && FONT_WEIGHT.has(String(value))) return String(value);
281
+ if (typeof value === "string" && FONT_WEIGHT.has(value)) return value;
282
+ return undefined;
283
+ }
284
+
285
+ const TRANSITION_EASINGS = new Set(["ease", "ease-in", "ease-out", "ease-in-out", "linear"]);
286
+ const TRANSITION_PROPERTIES: Readonly<Record<string, readonly string[]>> = {
287
+ all: ["all"],
288
+ colors: ["color", "background-color", "border-color"],
289
+ opacity: ["opacity"],
290
+ shadow: ["box-shadow"],
291
+ filter: ["filter"],
292
+ };
293
+ const TRANSITION_KEYS = new Set(["duration", "delay", "easing", "property"]);
294
+
295
+ const ms = (n: unknown) => (Number.isInteger(n) && inRange(n, 0, 2000) ? `${n}ms` : undefined);
296
+
297
+ /** W-089: `{ duration, delay?, easing, property }` to one transition entry per CSS property. */
298
+ function cssTransition(value: unknown): string | undefined {
299
+ if (typeof value !== "object" || value === null) return undefined;
300
+ const t = value as Record<string, unknown>;
301
+ if (Object.keys(t).some((key) => !TRANSITION_KEYS.has(key))) return undefined;
302
+ const duration = ms(t["duration"]);
303
+ const delay = t["delay"] === undefined ? "" : ms(t["delay"]);
304
+ const easing = typeof t["easing"] === "string" && TRANSITION_EASINGS.has(t["easing"]);
305
+ const properties =
306
+ typeof t["property"] === "string" && Object.hasOwn(TRANSITION_PROPERTIES, t["property"])
307
+ ? TRANSITION_PROPERTIES[t["property"]]
308
+ : undefined;
309
+ if (!duration || delay === undefined || !easing || !properties) return undefined;
310
+ const timing = `${duration} ${t["easing"] as string}${delay ? ` ${delay}` : ""}`;
311
+ return properties.map((property) => `${property} ${timing}`).join(",");
312
+ }
313
+
314
+ const inRangeNumber = (value: unknown, min: number, max: number): value is number =>
315
+ typeof value === "number" && Number.isFinite(value) && value >= min && value <= max;
316
+
317
+ /** Up to 3 decimals, without float noise or a trailing zero (`1.5`, not `1.5000000001`). */
318
+ const decimal = (n: number) => String(Math.round(n * 1000) / 1000);
319
+
320
+ /** W-237: glyph rotation in degrees, -360 to 360. */
321
+ const cssIconRotate = (value: unknown): string | undefined =>
322
+ Number.isInteger(value) && inRangeNumber(value, -360, 360) ? `${value}deg` : undefined;
323
+
324
+ const ICON_FLIPS: Readonly<Record<string, string>> = {
325
+ none: "1 1",
326
+ horizontal: "-1 1",
327
+ vertical: "1 -1",
328
+ both: "-1 -1",
329
+ };
330
+
331
+ /** W-237: a flip as the svg's `scale` (`none` too, so a device can undo a Desktop flip). */
332
+ const cssIconFlip = (value: unknown): string | undefined =>
333
+ typeof value === "string" && Object.hasOwn(ICON_FLIPS, value) ? ICON_FLIPS[value] : undefined;
334
+
335
+ const cssIconScale = (value: unknown): string | undefined =>
336
+ inRangeNumber(value, 0.1, 4) ? decimal(value) : undefined;
337
+
338
+ const cssIconStroke = (value: unknown): string | undefined =>
339
+ inRangeNumber(value, 0.25, 6) ? decimal(value) : undefined;
340
+
341
+ /** W-237: `drop-shadow(x y blur [color])` in px; no colour means the icon's own colour. */
342
+ function cssIconShadow(value: unknown): string | undefined {
343
+ if (!isRecord(value)) return undefined;
344
+ if (Object.keys(value).some((key) => !["x", "y", "blur", "color"].includes(key)))
345
+ return undefined;
346
+ const { x, y, blur } = value;
347
+ if (!inRangeNumber(x, -100, 100) || !inRangeNumber(y, -100, 100) || !inRangeNumber(blur, 0, 100))
348
+ return undefined;
349
+ const color = value["color"] === undefined ? "" : cssColor(value["color"]);
350
+ if (color === undefined) return undefined;
351
+ return `drop-shadow(${decimal(x)}px ${decimal(y)}px ${decimal(blur)}px${color ? ` ${color}` : ""})`;
352
+ }
353
+
354
+ const ICON_ANIMATIONS: Readonly<Record<string, string>> = {
355
+ spin: "emvb-icon-spin {ms} linear infinite",
356
+ pulse: "emvb-icon-pulse {ms} ease-in-out infinite alternate",
357
+ };
358
+
359
+ /** W-237: a looping spin or pulse; the Icon's base CSS turns it off for reduced motion. */
360
+ function cssIconAnimation(value: unknown): string | undefined {
361
+ if (!isRecord(value)) return undefined;
362
+ if (Object.keys(value).some((key) => key !== "type" && key !== "duration")) return undefined;
363
+ const { type, duration } = value;
364
+ // W-248: stops an animation inherited from a wider device.
365
+ if (type === "none") return duration === undefined ? "none" : undefined;
366
+ if (typeof type !== "string" || !Object.hasOwn(ICON_ANIMATIONS, type)) return undefined;
367
+ if (!Number.isInteger(duration) || !inRangeNumber(duration, 200, 10_000)) return undefined;
368
+ return ICON_ANIMATIONS[type]?.replace("{ms}", `${duration}ms`);
369
+ }
370
+
371
+ export type Declaration = { property: string; value: string };
372
+
373
+ const BACKGROUND_SIZE = new Set(["auto", "cover", "contain"]);
374
+ const BACKGROUND_POSITION = new Set([
375
+ "center",
376
+ "top",
377
+ "bottom",
378
+ "left",
379
+ "right",
380
+ "top left",
381
+ "top right",
382
+ "bottom left",
383
+ "bottom right",
384
+ ]);
385
+ const BACKGROUND_REPEAT = new Set(["no-repeat", "repeat", "repeat-x", "repeat-y"]);
386
+ const BACKGROUND_KEYS = new Set([
387
+ "backgroundImage",
388
+ "backgroundSize",
389
+ "backgroundPosition",
390
+ "backgroundRepeat",
391
+ "gradient",
392
+ "overlay",
393
+ "backgroundVideo",
394
+ ]);
395
+
396
+ /** Backstop for values that must contain `url()` or quotes, which `isSafeCssValue` refuses. */
397
+ function isSafeBackground(value: string): boolean {
398
+ return value.length > 0 && value.length <= 4096 && !/[{};<>\\`]|\/\*|\n|\r/.test(value);
399
+ }
400
+
401
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
402
+ typeof value === "object" && value !== null && !Array.isArray(value);
403
+
404
+ /** A media URL wrapped for CSS. The schema already refuses quotes, spaces and parentheses. */
405
+ function cssBackgroundImage(value: unknown): string | undefined {
406
+ if (typeof value !== "string") return undefined;
407
+ if (sanitizeMediaUrl(value) !== value || /[()\\\s"'`]/.test(value)) return undefined;
408
+ const css = `url("${value}")`;
409
+ return isSafeBackground(css) ? css : undefined;
410
+ }
411
+
412
+ const GRADIENT_POSITION = new Set([
413
+ "center",
414
+ "top",
415
+ "bottom",
416
+ "left",
417
+ "right",
418
+ "top left",
419
+ "top right",
420
+ "bottom left",
421
+ "bottom right",
422
+ ]);
423
+
424
+ function cssAngle(value: unknown, fallback: number): number | undefined {
425
+ const angle = value === undefined ? fallback : value;
426
+ if (!Number.isInteger(angle) || (angle as number) < 0 || (angle as number) > 360)
427
+ return undefined;
428
+ return angle as number;
429
+ }
430
+
431
+ function cssGradient(value: unknown): string | undefined {
432
+ if (!isRecord(value)) return undefined;
433
+ if (
434
+ Object.keys(value).some(
435
+ (key) => key !== "type" && key !== "angle" && key !== "position" && key !== "stops",
436
+ )
437
+ )
438
+ return undefined;
439
+ const { type, stops } = value;
440
+ if (type !== "linear" && type !== "radial" && type !== "conic") return undefined;
441
+ if (!Array.isArray(stops) || stops.length < 2 || stops.length > 10) return undefined;
442
+ const parts: { color: string; at: number }[] = [];
443
+ for (const stop of stops) {
444
+ if (!isRecord(stop) || Object.keys(stop).some((key) => key !== "color" && key !== "at"))
445
+ return undefined;
446
+ const color = cssColor(stop["color"]);
447
+ const at = stop["at"];
448
+ if (!color || !Number.isInteger(at) || (at as number) < 0 || (at as number) > 100)
449
+ return undefined;
450
+ parts.push({ color, at: at as number });
451
+ }
452
+ // Stops are kept in the order they were added; CSS needs them by location (W-172).
453
+ parts.sort((a, b) => a.at - b.at);
454
+ const list = parts.map((part) => `${part.color} ${part.at}%`).join(", ");
455
+ let css: string;
456
+ if (type === "radial") {
457
+ const position = value["position"] === undefined ? "center" : value["position"];
458
+ if (typeof position !== "string" || !GRADIENT_POSITION.has(position)) return undefined;
459
+ css = `radial-gradient(circle at ${position}, ${list})`;
460
+ } else {
461
+ const angle = cssAngle(value["angle"], type === "conic" ? 0 : 180);
462
+ if (angle === undefined) return undefined;
463
+ css =
464
+ type === "conic"
465
+ ? `conic-gradient(from ${angle}deg, ${list})`
466
+ : `linear-gradient(${angle}deg, ${list})`;
467
+ }
468
+ return isSafeBackground(css) ? css : undefined;
469
+ }
470
+
471
+ /** `#rgb` / `#rrggbb` (and an existing alpha) times `opacity`, as 8-digit hex. */
472
+ function hexWithAlpha(hex: string, opacity: number): string | undefined {
473
+ let body = hex.slice(1).toLowerCase();
474
+ if (body.length === 3 || body.length === 4) body = [...body].map((ch) => ch + ch).join("");
475
+ if (body.length !== 6 && body.length !== 8) return undefined;
476
+ const existing = body.length === 8 ? Number.parseInt(body.slice(6), 16) / 255 : 1;
477
+ const alpha = Math.round(existing * opacity * 255)
478
+ .toString(16)
479
+ .padStart(2, "0");
480
+ return `#${body.slice(0, 6)}${alpha}`;
481
+ }
482
+
483
+ function overlayPaint(color: string, opacity: number): string | undefined {
484
+ if (opacity === 1) return color;
485
+ if (color.startsWith("var(")) {
486
+ return `color-mix(in srgb, ${color} ${Math.round(opacity * 100)}%, transparent)`;
487
+ }
488
+ return HEX_COLOR.test(color) ? hexWithAlpha(color, opacity) : undefined;
489
+ }
490
+
491
+ function cssOverlay(value: unknown): string | undefined {
492
+ if (!isRecord(value)) return undefined;
493
+ if (Object.keys(value).some((key) => key !== "color" && key !== "opacity")) return undefined;
494
+ const color = cssColor(value["color"]);
495
+ const opacity = value["opacity"];
496
+ if (
497
+ !color ||
498
+ typeof opacity !== "number" ||
499
+ !Number.isFinite(opacity) ||
500
+ opacity < 0 ||
501
+ opacity > 1
502
+ )
503
+ return undefined;
504
+ const paint = overlayPaint(color, opacity);
505
+ if (!paint) return undefined;
506
+ const css = `linear-gradient(${paint}, ${paint})`;
507
+ return isSafeBackground(css) ? css : undefined;
508
+ }
509
+
510
+ /**
511
+ * Image, gradient and overlay share `background-image`. The overlay is the top layer, then the
512
+ * image, then the gradient. Size, position and repeat apply to the image; the other layers fill.
513
+ */
514
+ function composeBackground(style: Record<string, unknown>): {
515
+ declarations: Declaration[];
516
+ rejected: string[];
517
+ } {
518
+ const rejected: string[] = [];
519
+ const take = (key: string, css: string | undefined) => {
520
+ if (style[key] === undefined) return undefined;
521
+ if (!css) rejected.push(key);
522
+ return css;
523
+ };
524
+ const image = take("backgroundImage", cssBackgroundImage(style["backgroundImage"]));
525
+ const size = take("backgroundSize", keyword(BACKGROUND_SIZE)(style["backgroundSize"]));
526
+ const position = take(
527
+ "backgroundPosition",
528
+ keyword(BACKGROUND_POSITION)(style["backgroundPosition"]),
529
+ );
530
+ const repeat = take("backgroundRepeat", keyword(BACKGROUND_REPEAT)(style["backgroundRepeat"]));
531
+ const gradient = take("gradient", cssGradient(style["gradient"]));
532
+ const overlay = take("overlay", cssOverlay(style["overlay"]));
533
+
534
+ const layers: string[] = [];
535
+ const sizes: string[] = [];
536
+ const positions: string[] = [];
537
+ const repeats: string[] = [];
538
+ const push = (layer: string, layerSize: string, layerPosition: string, layerRepeat: string) => {
539
+ layers.push(layer);
540
+ sizes.push(layerSize);
541
+ positions.push(layerPosition);
542
+ repeats.push(layerRepeat);
543
+ };
544
+ if (overlay) push(overlay, "auto", "center", "no-repeat");
545
+ if (image) push(image, size ?? "cover", position ?? "center", repeat ?? "no-repeat");
546
+ if (gradient) push(gradient, "auto", "center", "no-repeat");
547
+
548
+ const declarations: Declaration[] = [];
549
+ if (layers.length > 0) {
550
+ const value = layers.join(",");
551
+ if (!isSafeBackground(value)) {
552
+ for (const key of ["overlay", "backgroundImage", "gradient"]) {
553
+ if (style[key] !== undefined && !rejected.includes(key)) rejected.push(key);
554
+ }
555
+ } else {
556
+ declarations.push({ property: "background-image", value });
557
+ if (layers.length > 1) {
558
+ declarations.push(
559
+ { property: "background-size", value: sizes.join(",") },
560
+ { property: "background-position", value: positions.join(",") },
561
+ { property: "background-repeat", value: repeats.join(",") },
562
+ );
563
+ } else if (image) {
564
+ declarations.push(
565
+ { property: "background-size", value: size ?? "cover" },
566
+ { property: "background-position", value: position ?? "center" },
567
+ { property: "background-repeat", value: repeat ?? "no-repeat" },
568
+ );
569
+ }
570
+ }
571
+ }
572
+ if (!image) {
573
+ if (size) declarations.push({ property: "background-size", value: size });
574
+ if (position) declarations.push({ property: "background-position", value: position });
575
+ if (repeat) declarations.push({ property: "background-repeat", value: repeat });
576
+ }
577
+ return { declarations, rejected };
578
+ }
579
+
580
+ const PROPERTY_MAP: {
581
+ [K in keyof Required<StyleProps>]: { css: string; toValue: (v: unknown) => string | undefined };
582
+ } = {
583
+ flexDirection: { css: "flex-direction", toValue: cssFlexDirection },
584
+ flexWrap: { css: "flex-wrap", toValue: keyword(FLEX_WRAPS) },
585
+ justifyContent: { css: "justify-content", toValue: keyword(JUSTIFY) },
586
+ alignItems: { css: "align-items", toValue: keyword(ALIGN) },
587
+ gap: { css: "gap", toValue: cssLength },
588
+ gridColumnSpan: { css: "grid-column", toValue: cssGridSpan },
589
+ gridRowSpan: { css: "grid-row", toValue: cssGridSpan },
590
+ width: { css: "width", toValue: cssLengthOrAuto },
591
+ minWidth: { css: "min-width", toValue: cssLength },
592
+ maxWidth: { css: "max-width", toValue: cssLength },
593
+ height: { css: "height", toValue: cssLengthOrAuto },
594
+ minHeight: { css: "min-height", toValue: cssLength },
595
+ maxHeight: { css: "max-height", toValue: cssLength },
596
+ overflow: { css: "overflow", toValue: keyword(OVERFLOW) },
597
+ aspectRatio: { css: "aspect-ratio", toValue: cssAspectRatio },
598
+ objectFit: { css: "object-fit", toValue: keyword(OBJECT_FIT) },
599
+ paddingTop: { css: "padding-top", toValue: cssLength },
600
+ paddingRight: { css: "padding-inline-end", toValue: cssLength },
601
+ paddingBottom: { css: "padding-bottom", toValue: cssLength },
602
+ paddingLeft: { css: "padding-inline-start", toValue: cssLength },
603
+ marginTop: { css: "margin-top", toValue: cssLengthOrAuto },
604
+ marginRight: { css: "margin-inline-end", toValue: cssLengthOrAuto },
605
+ marginBottom: { css: "margin-bottom", toValue: cssLengthOrAuto },
606
+ marginLeft: { css: "margin-inline-start", toValue: cssLengthOrAuto },
607
+ position: { css: "position", toValue: keyword(POSITION) },
608
+ top: { css: "top", toValue: cssOffset },
609
+ right: { css: "inset-inline-end", toValue: cssOffset },
610
+ bottom: { css: "bottom", toValue: cssOffset },
611
+ left: { css: "inset-inline-start", toValue: cssOffset },
612
+ zIndex: { css: "z-index", toValue: cssZIndex },
613
+ fontFamily: { css: "font-family", toValue: cssFontFamily },
614
+ fontSize: { css: "font-size", toValue: cssLength },
615
+ fontWeight: { css: "font-weight", toValue: cssFontWeight },
616
+ lineHeight: { css: "line-height", toValue: cssLineHeight },
617
+ letterSpacing: { css: "letter-spacing", toValue: cssLetterSpacing },
618
+ textAlign: { css: "text-align", toValue: cssTextAlign },
619
+ textTransform: { css: "text-transform", toValue: keyword(TEXT_TRANSFORM) },
620
+ textDecoration: { css: "text-decoration", toValue: keyword(TEXT_DECORATION) },
621
+ color: { css: "color", toValue: cssColor },
622
+ backgroundColor: { css: "background-color", toValue: cssColor },
623
+ backgroundImage: { css: "background-image", toValue: cssBackgroundImage },
624
+ backgroundSize: { css: "background-size", toValue: keyword(BACKGROUND_SIZE) },
625
+ backgroundPosition: { css: "background-position", toValue: keyword(BACKGROUND_POSITION) },
626
+ backgroundRepeat: { css: "background-repeat", toValue: keyword(BACKGROUND_REPEAT) },
627
+ backgroundVideo: { css: "background-image", toValue: () => undefined },
628
+ gradient: { css: "background-image", toValue: cssGradient },
629
+ overlay: { css: "background-image", toValue: cssOverlay },
630
+ borderWidth: { css: "border-width", toValue: cssLength },
631
+ borderTopWidth: { css: "border-top-width", toValue: cssLength },
632
+ borderRightWidth: { css: "border-inline-end-width", toValue: cssLength },
633
+ borderBottomWidth: { css: "border-bottom-width", toValue: cssLength },
634
+ borderLeftWidth: { css: "border-inline-start-width", toValue: cssLength },
635
+ borderStyle: { css: "border-style", toValue: keyword(BORDER_STYLE) },
636
+ borderColor: { css: "border-color", toValue: cssColor },
637
+ borderRadius: { css: "border-radius", toValue: cssLength },
638
+ borderTopLeftRadius: { css: "border-start-start-radius", toValue: cssLength },
639
+ borderTopRightRadius: { css: "border-start-end-radius", toValue: cssLength },
640
+ borderBottomRightRadius: { css: "border-end-end-radius", toValue: cssLength },
641
+ borderBottomLeftRadius: { css: "border-end-start-radius", toValue: cssLength },
642
+ opacity: { css: "opacity", toValue: cssOpacity },
643
+ boxShadow: { css: "box-shadow", toValue: cssBoxShadow },
644
+ filter: { css: "filter", toValue: cssFilter },
645
+ cursor: { css: "cursor", toValue: keyword(CURSOR) },
646
+ transition: { css: "transition", toValue: cssTransition },
647
+ entrance: { css: "animation", toValue: cssEntrance },
648
+ iconRotate: { css: "--emvb-icon-rotate", toValue: cssIconRotate },
649
+ iconFlip: { css: "--emvb-icon-flip", toValue: cssIconFlip },
650
+ iconScale: { css: "scale", toValue: cssIconScale },
651
+ iconStrokeWidth: { css: "--emvb-icon-stroke", toValue: cssIconStroke },
652
+ iconShadow: { css: "--emvb-icon-shadow", toValue: cssIconShadow },
653
+ iconAnimation: { css: "--emvb-icon-animation", toValue: cssIconAnimation },
654
+ };
655
+
656
+ /** Exported for table-driven tests (W-017). */
657
+ export const STYLE_PROPERTY_MAP = PROPERTY_MAP;
658
+
659
+ /** All-sides keys whose CSS shorthand must come before the per-side longhands (W-138). */
660
+ const SHORTHANDS = new Set(["borderWidth", "borderRadius"]);
661
+
662
+ /** Entries with `borderWidth` and `borderRadius` first, so a side or corner always wins. */
663
+ const shorthandsFirst = (record: Record<string, unknown>): [string, unknown][] => {
664
+ const entries = Object.entries(record);
665
+ return [
666
+ ...entries.filter(([key]) => SHORTHANDS.has(key)),
667
+ ...entries.filter(([key]) => !SHORTHANDS.has(key)),
668
+ ];
669
+ };
670
+
671
+ /** Converts style props into safe declarations; anything that fails validation is dropped and reported. */
672
+ export function styleDeclarations(style: unknown): {
673
+ declarations: Declaration[];
674
+ rejected: string[];
675
+ } {
676
+ const declarations: Declaration[] = [];
677
+ const rejected: string[] = [];
678
+ if (typeof style !== "object" || style === null) return { declarations, rejected };
679
+ const record = style as Record<string, unknown>;
680
+ for (const [key, raw] of shorthandsFirst(record)) {
681
+ if (BACKGROUND_KEYS.has(key)) continue;
682
+ const entry = Object.hasOwn(PROPERTY_MAP, key)
683
+ ? PROPERTY_MAP[key as keyof StyleProps]
684
+ : undefined;
685
+ const value = entry?.toValue(raw);
686
+ const safe =
687
+ value !== undefined &&
688
+ (entry?.css === "font-family" ? isSafeFontStack(value) : isSafeCssValue(value));
689
+ if (entry && safe) {
690
+ declarations.push({ property: entry.css, value });
691
+ if (key === "entrance") {
692
+ const trigger = (raw as { trigger?: unknown }).trigger;
693
+ if (trigger === "view") {
694
+ declarations.push(
695
+ { property: "animation-timeline", value: "view()" },
696
+ { property: "animation-range", value: "entry 0% cover 40%" },
697
+ );
698
+ }
699
+ }
700
+ } else {
701
+ rejected.push(key);
702
+ }
703
+ }
704
+ const background = composeBackground(record);
705
+ declarations.push(...background.declarations);
706
+ rejected.push(...background.rejected);
707
+ return { declarations, rejected };
708
+ }
709
+
710
+ /** Normal only (W-089, W-237): a state's `transition` and `iconAnimation` are dropped and reported. */
711
+ const NORMAL_ONLY = ["transition", "iconAnimation"] as const;
712
+ const withoutTransition = (style: unknown): unknown => {
713
+ if (typeof style !== "object" || style === null) return style;
714
+ if (!NORMAL_ONLY.some((key) => key in style)) return style;
715
+ const { transition: _drop, iconAnimation: _loop, ...rest } = style as Record<string, unknown>;
716
+ return rest;
717
+ };
718
+
719
+ export type StateDeclarations = Partial<Record<StyleStateName, Declaration[]>>;
720
+
721
+ /**
722
+ * `states` (W-089) through the same mappers and gates as `style`. Rejected keys are reported as
723
+ * `<state>.<key>`; an unknown state name is reported whole.
724
+ */
725
+ export function stateDeclarations(states: unknown): {
726
+ states: StateDeclarations;
727
+ rejected: string[];
728
+ } {
729
+ const result: StateDeclarations = {};
730
+ const rejected: string[] = [];
731
+ if (typeof states !== "object" || states === null) return { states: result, rejected };
732
+ for (const key of Object.keys(states)) {
733
+ if (!STYLE_STATES.includes(key as StyleStateName)) rejected.push(key);
734
+ }
735
+ for (const state of STYLE_STATES) {
736
+ const style = (states as Record<string, unknown>)[state];
737
+ if (style === undefined) continue;
738
+ const { declarations, rejected: bad } = styleDeclarations(withoutTransition(style));
739
+ for (const key of NORMAL_ONLY) {
740
+ if (typeof style === "object" && style !== null && key in style) bad.push(key);
741
+ }
742
+ if (declarations.length > 0) result[state] = declarations;
743
+ for (const key of bad) rejected.push(`${state}.${key}`);
744
+ }
745
+ return { states: result, rejected };
746
+ }
747
+
748
+ /** The style keys drawn together as one `background-image` stack (image, gradient, overlay). */
749
+ const BACKGROUND_LAYER_KEYS = [
750
+ "backgroundImage",
751
+ "gradient",
752
+ "overlay",
753
+ "backgroundSize",
754
+ "backgroundPosition",
755
+ "backgroundRepeat",
756
+ ] as const;
757
+
758
+ /**
759
+ * W-210: a tablet or mobile style that sets one background layer (say only the overlay) writes the
760
+ * whole `background-image` stack for that device, so the layers it inherits (the desktop image or
761
+ * gradient) are filled in from `inherited` here. Otherwise they vanish on that device.
762
+ */
763
+ export function withInheritedLayers(device: unknown, inherited: unknown): unknown {
764
+ if (!isRecord(device) || !isRecord(inherited)) return device;
765
+ if (!BACKGROUND_LAYER_KEYS.some((key) => device[key] !== undefined)) return device;
766
+ const next: Record<string, unknown> = { ...device };
767
+ for (const key of BACKGROUND_LAYER_KEYS) {
768
+ if (next[key] === undefined && inherited[key] !== undefined) next[key] = inherited[key];
769
+ }
770
+ return next;
771
+ }