@linxin666/dsh-client-ui-skin-center 0.4.12 → 0.4.13

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.
@@ -0,0 +1,825 @@
1
+ import { transform } from "lightningcss";
2
+ //#region src/core/css-safety/errors.ts
3
+ /**
4
+ * CSS safety error types (lightningcss-free).
5
+ *
6
+ * The host half and the worker half must agree on what a failure means, but
7
+ * the host half may not import anything that reaches lightningcss (issue
8
+ * #98). These error classes therefore live in their own module: the worker
9
+ * raises them, the host re-raises them from the IPC answer, and the HTTP
10
+ * layer maps them (whitelist violation -> 422, overloaded -> 503, everything
11
+ * else -> 500).
12
+ * @module @linxin666/dsh-client-ui-skin-center/css-safety/errors
13
+ */
14
+ /**
15
+ * The stable kind marker of a whitelist violation.
16
+ *
17
+ * The worker and the transform live in DIFFERENT bundles, so an
18
+ * `instanceof` check cannot see the same class identity. The kind marker is
19
+ * a plain string that both bundles agree on, which is what lets the worker
20
+ * tell a whitelist violation from any other error.
21
+ */
22
+ const SKIN_CSS_SAFETY_ERROR_KIND = "SkinCssSafetyError";
23
+ /** Violation of the CSS whitelist. Always fatal (fail-closed). */
24
+ var SkinCssSafetyError = class extends Error {
25
+ name = SKIN_CSS_SAFETY_ERROR_KIND;
26
+ /** The stable marker the worker classifies on (bundle-independent). */
27
+ kind = SKIN_CSS_SAFETY_ERROR_KIND;
28
+ violations;
29
+ constructor(message, violations) {
30
+ super(message);
31
+ this.violations = violations;
32
+ }
33
+ };
34
+ //#endregion
35
+ //#region src/core/css-safety/official-tokens.generated.ts
36
+ /**
37
+ * GENERATED by scripts/official-tokens-snapshot.mjs — do not edit.
38
+ * Official shell custom-property surface (--dsw-*, static palette excluded).
39
+ */
40
+ const OFFICIAL_TOKENS = [
41
+ "--dsw-alias-bg-base",
42
+ "--dsw-alias-bg-document-preview",
43
+ "--dsw-alias-bg-document-selection",
44
+ "--dsw-alias-bg-layer-1",
45
+ "--dsw-alias-bg-layer-2",
46
+ "--dsw-alias-bg-layer-3",
47
+ "--dsw-alias-bg-layer-4",
48
+ "--dsw-alias-bg-mask-1",
49
+ "--dsw-alias-bg-mask-2",
50
+ "--dsw-alias-bg-mask-3",
51
+ "--dsw-alias-bg-mask-drop",
52
+ "--dsw-alias-bg-mask-photo",
53
+ "--dsw-alias-bg-module-platform",
54
+ "--dsw-alias-bg-multi-select",
55
+ "--dsw-alias-bg-overlay",
56
+ "--dsw-alias-bg-skeleton",
57
+ "--dsw-alias-border-inverted",
58
+ "--dsw-alias-border-inverted2",
59
+ "--dsw-alias-border-l1",
60
+ "--dsw-alias-border-l2",
61
+ "--dsw-alias-border-l2-darkmode-thin",
62
+ "--dsw-alias-border-l3",
63
+ "--dsw-alias-border-l4",
64
+ "--dsw-alias-brand-primary",
65
+ "--dsw-alias-brand-primary-invert",
66
+ "--dsw-alias-brand-primary-new-colorprimary-new-color",
67
+ "--dsw-alias-brand-text",
68
+ "--dsw-alias-button-contrast-fill",
69
+ "--dsw-alias-button-elevated-fill",
70
+ "--dsw-alias-button-floating-fill",
71
+ "--dsw-alias-button-floating-hover",
72
+ "--dsw-alias-button-ghost-active-border",
73
+ "--dsw-alias-button-ghost-active-fill",
74
+ "--dsw-alias-button-ghost-active-hover",
75
+ "--dsw-alias-button-info-fill",
76
+ "--dsw-alias-button-info-hover",
77
+ "--dsw-alias-button-primary-dimmed",
78
+ "--dsw-alias-button-primary-fill",
79
+ "--dsw-alias-button-primary-hover",
80
+ "--dsw-alias-button-tool-bar-fill",
81
+ "--dsw-alias-button-tool-bar-fill-invisible",
82
+ "--dsw-alias-button-tool-bar-hover",
83
+ "--dsw-alias-code-diff-added",
84
+ "--dsw-alias-code-diff-deleted",
85
+ "--dsw-alias-file-diff-added-bg",
86
+ "--dsw-alias-file-diff-added-gutter",
87
+ "--dsw-alias-file-diff-added-marker",
88
+ "--dsw-alias-file-diff-deleted-bg",
89
+ "--dsw-alias-file-diff-deleted-gutter",
90
+ "--dsw-alias-file-diff-deleted-marker",
91
+ "--dsw-alias-interactive-bg-active",
92
+ "--dsw-alias-interactive-bg-hover",
93
+ "--dsw-alias-interactive-bg-hover-accent",
94
+ "--dsw-alias-interactive-bg-hover-danger",
95
+ "--dsw-alias-interactive-bg-hover-solid",
96
+ "--dsw-alias-label-caption",
97
+ "--dsw-alias-label-deep-diving",
98
+ "--dsw-alias-label-deep-diving-shimmer",
99
+ "--dsw-alias-label-dimmed",
100
+ "--dsw-alias-label-document-preview",
101
+ "--dsw-alias-label-error",
102
+ "--dsw-alias-label-primary",
103
+ "--dsw-alias-label-primary-bluish",
104
+ "--dsw-alias-label-primary-dimmed",
105
+ "--dsw-alias-label-primary-foreground",
106
+ "--dsw-alias-label-primary-inverted",
107
+ "--dsw-alias-label-secondary",
108
+ "--dsw-alias-label-shimmer",
109
+ "--dsw-alias-label-tertiary",
110
+ "--dsw-alias-link",
111
+ "--dsw-alias-markdown-citation",
112
+ "--dsw-alias-markdown-code-block",
113
+ "--dsw-alias-markdown-code-block-banner",
114
+ "--dsw-alias-markdown-code-segment-selected",
115
+ "--dsw-alias-markdown-code-segment-unselected",
116
+ "--dsw-alias-markdown-inline-code",
117
+ "--dsw-alias-markdown-placeholder",
118
+ "--dsw-alias-markdown-tag",
119
+ "--dsw-alias-menu-group-header-fill",
120
+ "--dsw-alias-menu-icon",
121
+ "--dsw-alias-onboarding-accent",
122
+ "--dsw-alias-onboarding-card-fill",
123
+ "--dsw-alias-onboarding-checkbox-border",
124
+ "--dsw-alias-onboarding-secondary-fill",
125
+ "--dsw-alias-scrollbar-bg-l1",
126
+ "--dsw-alias-scrollbar-bg-l2",
127
+ "--dsw-alias-scrollbar-hover-l1",
128
+ "--dsw-alias-scrollbar-hover-l2",
129
+ "--dsw-alias-settings-card-fill",
130
+ "--dsw-alias-settings-card-stroke",
131
+ "--dsw-alias-state-business-primary",
132
+ "--dsw-alias-state-business-tertiary",
133
+ "--dsw-alias-state-error-primary",
134
+ "--dsw-alias-state-error-secondary",
135
+ "--dsw-alias-state-idle-primary",
136
+ "--dsw-alias-state-success-primary",
137
+ "--dsw-alias-state-success-secondary",
138
+ "--dsw-alias-state-success-tertiary",
139
+ "--dsw-alias-state-warn-label",
140
+ "--dsw-alias-state-warn-primary",
141
+ "--dsw-alias-state-warn-secondary",
142
+ "--dsw-alias-state-warn-tertiary",
143
+ "--dsw-alias-switch-thumb",
144
+ "--dsw-alias-toast-bg",
145
+ "--dsw-alias-toast-label",
146
+ "--dsw-alias-tooltip-bg",
147
+ "--dsw-alias-tooltip-key-bg",
148
+ "--dsw-alias-turn-trigger-bg",
149
+ "--dsw-alias-turn-trigger-bg-hover",
150
+ "--dsw-corner-shape",
151
+ "--dsw-elevation-panel",
152
+ "--dsw-elevation-prominent",
153
+ "--dsw-elevation-soft",
154
+ "--dsw-elevation-stroke",
155
+ "--dsw-elevation-stroke-color",
156
+ "--dsw-focus-ring-color",
157
+ "--dsw-focus-ring-width",
158
+ "--dsw-font-base-16",
159
+ "--dsw-font-base-16-font-family",
160
+ "--dsw-font-base-16-font-size",
161
+ "--dsw-font-base-16-font-style",
162
+ "--dsw-font-base-16-font-weight",
163
+ "--dsw-font-base-16-line-height",
164
+ "--dsw-font-base-strong-16",
165
+ "--dsw-font-base-strong-16-font-family",
166
+ "--dsw-font-base-strong-16-font-size",
167
+ "--dsw-font-base-strong-16-font-style",
168
+ "--dsw-font-base-strong-16-font-weight",
169
+ "--dsw-font-base-strong-16-line-height",
170
+ "--dsw-font-family",
171
+ "--dsw-font-family-brand",
172
+ "--dsw-font-l-20",
173
+ "--dsw-font-l-20-font-family",
174
+ "--dsw-font-l-20-font-size",
175
+ "--dsw-font-l-20-font-style",
176
+ "--dsw-font-l-20-font-weight",
177
+ "--dsw-font-l-20-line-height",
178
+ "--dsw-font-m-18",
179
+ "--dsw-font-m-18-font-family",
180
+ "--dsw-font-m-18-font-size",
181
+ "--dsw-font-m-18-font-style",
182
+ "--dsw-font-m-18-font-weight",
183
+ "--dsw-font-m-18-line-height",
184
+ "--dsw-font-markdown-base",
185
+ "--dsw-font-markdown-base-font-family",
186
+ "--dsw-font-markdown-base-font-size",
187
+ "--dsw-font-markdown-base-font-style",
188
+ "--dsw-font-markdown-base-font-weight",
189
+ "--dsw-font-markdown-base-italic",
190
+ "--dsw-font-markdown-base-italic-font-family",
191
+ "--dsw-font-markdown-base-italic-font-size",
192
+ "--dsw-font-markdown-base-italic-font-style",
193
+ "--dsw-font-markdown-base-italic-font-weight",
194
+ "--dsw-font-markdown-base-italic-line-height",
195
+ "--dsw-font-markdown-base-line-height",
196
+ "--dsw-font-markdown-base-strong",
197
+ "--dsw-font-markdown-base-strong-font-family",
198
+ "--dsw-font-markdown-base-strong-font-size",
199
+ "--dsw-font-markdown-base-strong-font-style",
200
+ "--dsw-font-markdown-base-strong-font-weight",
201
+ "--dsw-font-markdown-base-strong-italic",
202
+ "--dsw-font-markdown-base-strong-italic-font-family",
203
+ "--dsw-font-markdown-base-strong-italic-font-size",
204
+ "--dsw-font-markdown-base-strong-italic-font-style",
205
+ "--dsw-font-markdown-base-strong-italic-font-weight",
206
+ "--dsw-font-markdown-base-strong-italic-line-height",
207
+ "--dsw-font-markdown-base-strong-line-height",
208
+ "--dsw-font-markdown-code",
209
+ "--dsw-font-markdown-code-block",
210
+ "--dsw-font-markdown-code-block-font-family",
211
+ "--dsw-font-markdown-code-block-font-size",
212
+ "--dsw-font-markdown-code-block-font-style",
213
+ "--dsw-font-markdown-code-block-font-weight",
214
+ "--dsw-font-markdown-code-block-line-height",
215
+ "--dsw-font-markdown-code-block-small",
216
+ "--dsw-font-markdown-code-block-small-font-family",
217
+ "--dsw-font-markdown-code-block-small-font-size",
218
+ "--dsw-font-markdown-code-block-small-font-style",
219
+ "--dsw-font-markdown-code-block-small-font-weight",
220
+ "--dsw-font-markdown-code-block-small-line-height",
221
+ "--dsw-font-markdown-code-font-family",
222
+ "--dsw-font-markdown-code-font-size",
223
+ "--dsw-font-markdown-code-font-style",
224
+ "--dsw-font-markdown-code-font-weight",
225
+ "--dsw-font-markdown-code-line-height",
226
+ "--dsw-font-markdown-h1",
227
+ "--dsw-font-markdown-h1-font-family",
228
+ "--dsw-font-markdown-h1-font-size",
229
+ "--dsw-font-markdown-h1-font-style",
230
+ "--dsw-font-markdown-h1-font-weight",
231
+ "--dsw-font-markdown-h1-line-height",
232
+ "--dsw-font-markdown-h2",
233
+ "--dsw-font-markdown-h2-font-family",
234
+ "--dsw-font-markdown-h2-font-size",
235
+ "--dsw-font-markdown-h2-font-style",
236
+ "--dsw-font-markdown-h2-font-weight",
237
+ "--dsw-font-markdown-h2-line-height",
238
+ "--dsw-font-markdown-h3",
239
+ "--dsw-font-markdown-h3-font-family",
240
+ "--dsw-font-markdown-h3-font-size",
241
+ "--dsw-font-markdown-h3-font-style",
242
+ "--dsw-font-markdown-h3-font-weight",
243
+ "--dsw-font-markdown-h3-line-height",
244
+ "--dsw-font-markdown-h4",
245
+ "--dsw-font-markdown-h4-font-family",
246
+ "--dsw-font-markdown-h4-font-size",
247
+ "--dsw-font-markdown-h4-font-style",
248
+ "--dsw-font-markdown-h4-font-weight",
249
+ "--dsw-font-markdown-h4-line-height",
250
+ "--dsw-font-markdown-small",
251
+ "--dsw-font-markdown-small-font-family",
252
+ "--dsw-font-markdown-small-font-size",
253
+ "--dsw-font-markdown-small-font-style",
254
+ "--dsw-font-markdown-small-font-weight",
255
+ "--dsw-font-markdown-small-italic",
256
+ "--dsw-font-markdown-small-italic-font-family",
257
+ "--dsw-font-markdown-small-italic-font-size",
258
+ "--dsw-font-markdown-small-italic-font-style",
259
+ "--dsw-font-markdown-small-italic-font-weight",
260
+ "--dsw-font-markdown-small-italic-line-height",
261
+ "--dsw-font-markdown-small-line-height",
262
+ "--dsw-font-markdown-small-strong",
263
+ "--dsw-font-markdown-small-strong-font-family",
264
+ "--dsw-font-markdown-small-strong-font-size",
265
+ "--dsw-font-markdown-small-strong-font-style",
266
+ "--dsw-font-markdown-small-strong-font-weight",
267
+ "--dsw-font-markdown-small-strong-italic",
268
+ "--dsw-font-markdown-small-strong-italic-font-family",
269
+ "--dsw-font-markdown-small-strong-italic-font-size",
270
+ "--dsw-font-markdown-small-strong-italic-font-style",
271
+ "--dsw-font-markdown-small-strong-italic-font-weight",
272
+ "--dsw-font-markdown-small-strong-italic-line-height",
273
+ "--dsw-font-markdown-small-strong-line-height",
274
+ "--dsw-font-markdown-table",
275
+ "--dsw-font-markdown-table-font-family",
276
+ "--dsw-font-markdown-table-font-size",
277
+ "--dsw-font-markdown-table-font-style",
278
+ "--dsw-font-markdown-table-font-weight",
279
+ "--dsw-font-markdown-table-head",
280
+ "--dsw-font-markdown-table-head-font-family",
281
+ "--dsw-font-markdown-table-head-font-size",
282
+ "--dsw-font-markdown-table-head-font-style",
283
+ "--dsw-font-markdown-table-head-font-weight",
284
+ "--dsw-font-markdown-table-head-line-height",
285
+ "--dsw-font-markdown-table-line-height",
286
+ "--dsw-font-s-14",
287
+ "--dsw-font-s-14-font-family",
288
+ "--dsw-font-s-14-font-size",
289
+ "--dsw-font-s-14-font-style",
290
+ "--dsw-font-s-14-font-weight",
291
+ "--dsw-font-s-14-line-height",
292
+ "--dsw-font-s-strong-14",
293
+ "--dsw-font-s-strong-14-font-family",
294
+ "--dsw-font-s-strong-14-font-size",
295
+ "--dsw-font-s-strong-14-font-style",
296
+ "--dsw-font-s-strong-14-font-weight",
297
+ "--dsw-font-s-strong-14-line-height",
298
+ "--dsw-font-xl-24",
299
+ "--dsw-font-xl-24-font-family",
300
+ "--dsw-font-xl-24-font-size",
301
+ "--dsw-font-xl-24-font-style",
302
+ "--dsw-font-xl-24-font-weight",
303
+ "--dsw-font-xl-24-line-height",
304
+ "--dsw-font-xs-13",
305
+ "--dsw-font-xs-13-font-family",
306
+ "--dsw-font-xs-13-font-size",
307
+ "--dsw-font-xs-13-font-style",
308
+ "--dsw-font-xs-13-font-weight",
309
+ "--dsw-font-xs-13-line-height",
310
+ "--dsw-font-xs-strong-13",
311
+ "--dsw-font-xs-strong-13-font-family",
312
+ "--dsw-font-xs-strong-13-font-size",
313
+ "--dsw-font-xs-strong-13-font-style",
314
+ "--dsw-font-xs-strong-13-font-weight",
315
+ "--dsw-font-xs-strong-13-line-height",
316
+ "--dsw-font-xxs-12",
317
+ "--dsw-font-xxs-12-font-family",
318
+ "--dsw-font-xxs-12-font-size",
319
+ "--dsw-font-xxs-12-font-style",
320
+ "--dsw-font-xxs-12-font-weight",
321
+ "--dsw-font-xxs-12-line-height",
322
+ "--dsw-font-xxs-strong-12",
323
+ "--dsw-font-xxs-strong-12-font-family",
324
+ "--dsw-font-xxs-strong-12-font-size",
325
+ "--dsw-font-xxs-strong-12-font-style",
326
+ "--dsw-font-xxs-strong-12-font-weight",
327
+ "--dsw-font-xxs-strong-12-line-height",
328
+ "--dsw-font-xxxs-11",
329
+ "--dsw-font-xxxs-11-font-family",
330
+ "--dsw-font-xxxs-11-font-size",
331
+ "--dsw-font-xxxs-11-font-style",
332
+ "--dsw-font-xxxs-11-font-weight",
333
+ "--dsw-font-xxxs-11-line-height",
334
+ "--dsw-font-xxxs-strong-11",
335
+ "--dsw-font-xxxs-strong-11-font-family",
336
+ "--dsw-font-xxxs-strong-11-font-size",
337
+ "--dsw-font-xxxs-strong-11-font-style",
338
+ "--dsw-font-xxxs-strong-11-font-weight",
339
+ "--dsw-font-xxxs-strong-11-line-height",
340
+ "--dsw-gradient-onboarding-blue-stops",
341
+ "--dsw-gradient-onboarding-cyan-stops",
342
+ "--dsw-gradient-onboarding-violet-stops",
343
+ "--dsw-hovercard-bg",
344
+ "--dsw-linear-gradient-think",
345
+ "--dsw-linear-think-select",
346
+ "--dsw-mask-blur",
347
+ "--dsw-menu-backdrop-filter",
348
+ "--dsw-menu-surface-fill",
349
+ "--dsw-radius-lg",
350
+ "--dsw-radius-md",
351
+ "--dsw-radius-panel",
352
+ "--dsw-radius-sm",
353
+ "--dsw-radius-xl",
354
+ "--dsw-radius-xs",
355
+ "--dsw-shadow-lv1",
356
+ "--dsw-shadow-lv1-blur",
357
+ "--dsw-shadow-lv2",
358
+ "--dsw-shadow-lv3",
359
+ "--dsw-specific-bubble",
360
+ "--dsw-specific-bubble-highlight",
361
+ "--dsw-specific-input-major",
362
+ "--dsw-specific-login-input",
363
+ "--dsw-specific-menu",
364
+ "--dsw-specific-selector",
365
+ "--dsw-specific-sidebar-fill",
366
+ "--dsw-specific-sidebar-nav-item-active",
367
+ "--dsw-specific-sidebar-nav-item-active-accent",
368
+ "--dsw-specific-sidebar-nav-item-hover",
369
+ "--dsw-specific-tip"
370
+ ];
371
+ //#endregion
372
+ //#region src/core/css-safety/fallback.ts
373
+ /**
374
+ * Automatic token fallbacks (issue #506 follow-up): for every official
375
+ * --dsw-* token a skin does NOT remap, derive a translucent tint of the
376
+ * skin's own palette — the skin's main color, "blurred" over whatever sits
377
+ * behind the surface. The official shell keeps adding surfaces (e.g. the
378
+ * composer's --dsw-specific-input-major); without this, an uncovered
379
+ * surface snaps back to the official default gray-blue and breaks the
380
+ * skin's palette. The fallback keeps skins future-proof across official
381
+ * upgrades: any new token simply inherits the skin's tint instead of the
382
+ * stock look.
383
+ *
384
+ * Rules (fail-closed, conservative):
385
+ * - never touch the static palette (not in the registry at all);
386
+ * - never override a token the skin defines;
387
+ * - never derive when the skin defines no anchor for the group;
388
+ * - semantic / structural groups (buttons, state colors, masks, shadows,
389
+ * inverted/foreground labels, fonts, easing) are skipped: a tint there
390
+ * would break contrast or layout instead of filling a gap. State colors
391
+ * are matched by role word, not by prefix, because the official surface
392
+ * names them inconsistently (--dsw-alias-state-error-primary but also
393
+ * --dsw-alias-label-error and --dsw-alias-interactive-bg-hover-danger).
394
+ *
395
+ * The derivation is textual (color-mix with a var() reference), so it
396
+ * resolves against the skin's own remap — including the dark-theme block —
397
+ * and stays theme-aware with zero runtime logic.
398
+ */
399
+ /**
400
+ * Roles that must never be tinted: a translucent skin tint would break
401
+ * contrast, layout, or the meaning of a state instead of filling a gap.
402
+ * State roles get one word each — the official surface is not uniformly
403
+ * prefixed (state-error-primary, label-error, interactive-bg-hover-danger),
404
+ * so matching the role word is the only reliable rule.
405
+ */
406
+ const EXCLUDED = /(^|-)(mask|shadow|button|state|error|warning|success|danger|info|caution|brand|scrollbar|foreground|inverted|dimmed)(-|$)|-font-|linear-|ease|duration|transition/;
407
+ /** Matched in order; the first group whose pattern hits wins. */
408
+ const GROUPS = [
409
+ {
410
+ skip: /-bg-/,
411
+ anchors: ["--dsw-alias-bg-layer-1", "--dsw-alias-bg-base"],
412
+ alpha: 65
413
+ },
414
+ {
415
+ skip: /-label-/,
416
+ anchors: ["--dsw-alias-label-primary"],
417
+ alpha: 70
418
+ },
419
+ {
420
+ skip: /-border-/,
421
+ anchors: ["--dsw-alias-border-l2", "--dsw-alias-border-l1"],
422
+ alpha: 55
423
+ },
424
+ {
425
+ skip: /-interactive-/,
426
+ anchors: ["--dsw-alias-bg-layer-1"],
427
+ alpha: 50
428
+ },
429
+ {
430
+ skip: /-specific-/,
431
+ anchors: ["--dsw-alias-bg-layer-1", "--dsw-alias-bg-base"],
432
+ alpha: 60
433
+ }
434
+ ];
435
+ function groupFor(token) {
436
+ if (EXCLUDED.test(token)) return null;
437
+ for (const group of GROUPS) if (group.skip.test(token)) return group;
438
+ return null;
439
+ }
440
+ /**
441
+ * Build fallback declarations for the official tokens the skin does not
442
+ * define. Returns declaration strings ("--x: color-mix(...);" per token).
443
+ */
444
+ function deriveFallbackTokens(defined) {
445
+ const out = [];
446
+ for (const token of OFFICIAL_TOKENS) {
447
+ if (defined.has(token)) continue;
448
+ const group = groupFor(token);
449
+ if (group === null) continue;
450
+ const anchor = group.anchors.find((candidate) => defined.has(candidate));
451
+ if (anchor === void 0) continue;
452
+ out.push(`${token}: color-mix(in srgb, var(${anchor}) ${group.alpha}%, transparent);`);
453
+ }
454
+ return out;
455
+ }
456
+ /**
457
+ * Primary-action completion (issue #506 follow-up): filled primary buttons
458
+ * render from one matched set — button-primary-fill, button-primary-hover,
459
+ * label-primary-foreground. The official theme itself wires
460
+ * button-primary-fill to brand-primary, so a skin that remaps the brand
461
+ * already colors the fill; hover and foreground do NOT follow the brand and
462
+ * would snap to the shell's static values. To keep a partially-declared or
463
+ * legacy (brand-primary + brand-primary-invert) skin coherent, the loader
464
+ * completes the set here:
465
+ *
466
+ * - fill: derive from brand-primary when the skin declares its brand but
467
+ * no explicit fill (the shell chain does this anyway; the derivation
468
+ * makes the intent explicit and keeps the textual derivation table
469
+ * self-contained);
470
+ * - hover / dimmed: blend the fill toward the surface (color-mix) — a
471
+ * direction-agnostic press/disabled tint that works in both themes;
472
+ * - foreground: inherit the skin's own brand-primary-invert ONLY when the
473
+ * skin declares both brand tokens (the legacy matched convention); the
474
+ * shell foreground stands in otherwise.
475
+ *
476
+ * Never overrides a token the skin defines, and never derives without an
477
+ * anchor: a skin with no brand and no button tokens keeps the official
478
+ * shell's own matched CTA.
479
+ */
480
+ /** The primary-action token family (see ./token-audit.ts for the audit). */
481
+ const PRIMARY_ACTION_FILL = "--dsw-alias-button-primary-fill";
482
+ const PRIMARY_ACTION_HOVER = "--dsw-alias-button-primary-hover";
483
+ const PRIMARY_ACTION_DIMMED = "--dsw-alias-button-primary-dimmed";
484
+ const PRIMARY_ACTION_FOREGROUND = "--dsw-alias-label-primary-foreground";
485
+ const PRIMARY_ACTION_BRAND = "--dsw-alias-brand-primary";
486
+ const PRIMARY_ACTION_BRAND_INVERT = "--dsw-alias-brand-primary-invert";
487
+ /** Derive the primary-action tokens the skin did not define. */
488
+ function derivePrimaryActionFallbacks(defined) {
489
+ const out = [];
490
+ const hasBrand = defined.has(PRIMARY_ACTION_BRAND);
491
+ const branded = hasBrand || defined.has("--dsw-alias-button-primary-fill");
492
+ if (!hasBrand && !defined.has("--dsw-alias-button-primary-fill")) return out;
493
+ if (!defined.has("--dsw-alias-button-primary-fill") && hasBrand) out.push(`${PRIMARY_ACTION_FILL}: var(${PRIMARY_ACTION_BRAND});`);
494
+ if (branded && !defined.has("--dsw-alias-button-primary-hover")) out.push(`${PRIMARY_ACTION_HOVER}: color-mix(in srgb, var(${PRIMARY_ACTION_FILL}) 82%, var(--dsw-alias-bg-layer-1));`);
495
+ if (branded && !defined.has("--dsw-alias-button-primary-dimmed")) out.push(`${PRIMARY_ACTION_DIMMED}: color-mix(in srgb, var(${PRIMARY_ACTION_FILL}) 60%, var(--dsw-alias-bg-layer-1));`);
496
+ if (!defined.has("--dsw-alias-label-primary-foreground") && hasBrand && defined.has("--dsw-alias-brand-primary-invert")) out.push(`${PRIMARY_ACTION_FOREGROUND}: var(${PRIMARY_ACTION_BRAND_INVERT});`);
497
+ return out;
498
+ }
499
+ //#endregion
500
+ //#region src/core/css-safety/transform.ts
501
+ /**
502
+ * Skin CSS safety pipeline (issue #506, contract section "校验纪律").
503
+ *
504
+ * Every skin stylesheet passes through this transform before it is served or
505
+ * injected — built-in or community, skin.css or patches.css. It is the
506
+ * technical enforcement of the coupling boundary:
507
+ *
508
+ * - SCOPING: every selector is force-scoped under
509
+ * `html[data-dsh-skin="<id>"]`. Root-ish heads are rewritten, not nested:
510
+ * `:root` / `html` merge into the scope; `body` and bare official
511
+ * `[data-ds-*]` heads (the official dark-theme attribute lives on BODY)
512
+ * become descendants of the scope; everything else becomes a descendant.
513
+ * - ROOT THEME TOKENS: per-theme `--dsw-alias-*` and
514
+ * `--dsw-specific-*` declarations from bare `:root` / `html` are reset
515
+ * on the scope and cloned to body. Root-level shell variables therefore
516
+ * cannot capture a light token while its dark variant belongs on body (#646).
517
+ * - WHITELIST (fail-closed): no `@import`, no remote or protocol-relative
518
+ * URLs, no absolute paths escaping the skin directory; only relative
519
+ * in-directory assets (and `data:`, which warns — prefer assets/ files).
520
+ * - WARNINGS: reliance on CSS-Modules hash class names (`[class*=...]`)
521
+ * warns; generic @keyframes names warn.
522
+ *
523
+ * Two-pass design (do NOT collapse): selector scoping is a text-level
524
+ * surgery guided by lightningcss rule locations, and lightningcss itself is
525
+ * only used to PARSE/validate (read-only visitors). Returning mutated rules
526
+ * from a lightningcss 1.32/1.33 style visitor crashes declaration
527
+ * deserialization on any var() declaration ("failed to deserialize; expected
528
+ * an object-like struct named Specifier") — an upstream serialization defect
529
+ * the text-level pass sidesteps entirely. A side benefit: the output keeps
530
+ * the author's formatting and values byte-for-byte outside selector heads.
531
+ *
532
+ * NOTE: this module runs in the CSS safety worker process, never in the
533
+ * DSH host process (issue #98): lightningcss is a native dependency, and a
534
+ * host that imports it maps the addon into its own process, which blocks
535
+ * package installs on Windows. The host half reaches this algorithm through
536
+ * the ./css-safety bundle; the browser bundle never carries it.
537
+ * @module @linxin666/dsh-client-ui-skin-center/css-safety/transform
538
+ */
539
+ /** Convert a lightningcss Location2 (0-based line, 1-based column) to a char offset. */
540
+ function locToOffset(source, line, column) {
541
+ let offset = 0;
542
+ let currentLine = 0;
543
+ while (currentLine < line) {
544
+ const next = source.indexOf("\n", offset);
545
+ if (next === -1) return source.length;
546
+ offset = next + 1;
547
+ currentLine += 1;
548
+ }
549
+ return offset + column - 1;
550
+ }
551
+ /**
552
+ * Find the opening '{' of a rule whose selector starts at `start`,
553
+ * tracking parens/brackets/strings so :is(...), [title="{"] etc. cannot
554
+ * fake an early brace.
555
+ */
556
+ function findOpenBrace(source, start) {
557
+ let parens = 0;
558
+ let brackets = 0;
559
+ let quote = null;
560
+ for (let i = start; i < source.length; i += 1) {
561
+ const ch = source[i];
562
+ if (quote !== null) {
563
+ if (ch === "\\") i += 1;
564
+ else if (ch === quote) quote = null;
565
+ continue;
566
+ }
567
+ if (ch === "\"" || ch === "'") {
568
+ quote = ch;
569
+ continue;
570
+ }
571
+ if (ch === "(") parens += 1;
572
+ else if (ch === ")") parens -= 1;
573
+ else if (ch === "[") brackets += 1;
574
+ else if (ch === "]") brackets -= 1;
575
+ else if (ch === "{" && parens === 0 && brackets === 0) return i;
576
+ else if (ch === ";" && parens === 0 && brackets === 0) return -1;
577
+ }
578
+ return -1;
579
+ }
580
+ /** Split a selector list on top-level commas (paren/bracket/string aware). */
581
+ function splitSelectors(selectorText) {
582
+ const parts = [];
583
+ let parens = 0;
584
+ let brackets = 0;
585
+ let quote = null;
586
+ let current = "";
587
+ for (let i = 0; i < selectorText.length; i += 1) {
588
+ const ch = selectorText[i];
589
+ if (quote !== null) {
590
+ current += ch;
591
+ if (ch === "\\") {
592
+ current += selectorText[i + 1] ?? "";
593
+ i += 1;
594
+ } else if (ch === quote) quote = null;
595
+ continue;
596
+ }
597
+ if (ch === "\"" || ch === "'") {
598
+ quote = ch;
599
+ current += ch;
600
+ continue;
601
+ }
602
+ if (ch === "(") parens += 1;
603
+ else if (ch === ")") parens -= 1;
604
+ else if (ch === "[") brackets += 1;
605
+ else if (ch === "]") brackets -= 1;
606
+ if (ch === "," && parens === 0 && brackets === 0) {
607
+ parts.push(current);
608
+ current = "";
609
+ continue;
610
+ }
611
+ current += ch;
612
+ }
613
+ parts.push(current);
614
+ return parts;
615
+ }
616
+ const HEAD_DATA_DS = /^\[data-ds-[a-z0-9-]+/;
617
+ /**
618
+ * Scope one selector under html[data-dsh-skin="<id>"]. Text-level and
619
+ * conservative: only the well-defined root-ish heads get rewritten; any
620
+ * other selector simply becomes a descendant of the scope.
621
+ */
622
+ function scopeSelectorText(selector, skinId) {
623
+ const scope = `html[data-dsh-skin="${skinId}"]`;
624
+ const trimmed = selector.trim();
625
+ const leading = selector.slice(0, selector.length - selector.trimStart().length);
626
+ const trailing = selector.slice(leading.length + trimmed.length);
627
+ if (trimmed === ":root" || trimmed.startsWith(":root ") || trimmed.startsWith(":root,")) return leading + scope + trimmed.slice(5) + trailing;
628
+ if (/^html\[data-ds-/.test(trimmed)) return `${leading}${scope} body${trimmed.slice(4)}${trailing}`;
629
+ if (trimmed === "html" || trimmed.startsWith("html ")) return leading + scope + trimmed.slice(4) + trailing;
630
+ if (trimmed === "body" || trimmed.startsWith("body ") || trimmed.startsWith("body[") || trimmed.startsWith("body:")) return `${leading}${scope} ${trimmed}${trailing}`;
631
+ if (HEAD_DATA_DS.test(trimmed)) return `${leading}${scope} body${trimmed}${trailing}`;
632
+ return `${leading}${scope} ${trimmed}${trailing}`;
633
+ }
634
+ /** Scope every selector in one selector-list text, preserving separators. */
635
+ function scopeSelectorList(selectorText, skinId) {
636
+ return splitSelectors(selectorText).map((sel) => scopeSelectorText(sel, skinId)).join(",");
637
+ }
638
+ const ROOT_BODY_TOKEN = /^(?:--dsw-alias-|--dsw-specific-)/;
639
+ /** A bare root selector owns custom properties evaluated on html itself. */
640
+ function hasBareRootSelector(selectorText) {
641
+ return splitSelectors(selectorText).some((selector) => {
642
+ const trimmed = selector.trim();
643
+ return trimmed === ":root" || trimmed === "html";
644
+ });
645
+ }
646
+ function withoutCssComments(value) {
647
+ return value.replace(/\/\*[\s\S]*?\*\//g, "");
648
+ }
649
+ /** Per-theme root declarations that must instead take effect from body. */
650
+ function rootBodyTokens(block) {
651
+ const tokens = /* @__PURE__ */ new Map();
652
+ const declarations = withoutCssComments(block);
653
+ for (const match of declarations.matchAll(/(?:^|[;{])\s*(--[\w-]+)\s*:\s*([^;}]*)/gm)) {
654
+ const name = match[1];
655
+ if (name !== void 0 && ROOT_BODY_TOKEN.test(name)) tokens.set(name, /!\s*important\s*$/i.test(match[2] ?? ""));
656
+ }
657
+ return [...tokens].map(([name, important]) => ({
658
+ name,
659
+ important
660
+ }));
661
+ }
662
+ /** Normalize cloned root tokens so dark body declarations can override them. */
663
+ function bodyCloneProperty(line) {
664
+ const custom = line.match(/^(--[\w-]+)\s*:/);
665
+ if (custom !== null) {
666
+ const name = custom[1] ?? "";
667
+ return ROOT_BODY_TOKEN.test(name) ? line.replace(/\s*!important(?=\s*;?\s*$)/i, "") : line;
668
+ }
669
+ return /^background-(color|image)\s*:/.test(line) ? line : null;
670
+ }
671
+ /** Check one url() target against the whitelist. */
672
+ function checkUrl(raw, context, violations, warnings) {
673
+ const url = raw.trim().replace(/^["']|["']$/g, "");
674
+ if (/^https?:\/\//i.test(url)) violations.push(`${context}: remote URL "${url}" is not allowed; ship the asset in the skin directory`);
675
+ else if (url.startsWith("//")) violations.push(`${context}: protocol-relative URL "${url}" is not allowed`);
676
+ else if (url.startsWith("/")) violations.push(`${context}: absolute path "${url}" escapes the skin directory`);
677
+ else if (/^(?:\.\.\/)/.test(url)) violations.push(`${context}: path "${url}" escapes the skin directory`);
678
+ else if (/^data:/i.test(url)) warnings.push(`${context}: inline data: URL — prefer a file under assets/`);
679
+ }
680
+ const GENERIC_KEYFRAMES = /* @__PURE__ */ new Set([
681
+ "spin",
682
+ "pulse",
683
+ "fade",
684
+ "fadein",
685
+ "fade-in",
686
+ "fadeout",
687
+ "fade-out",
688
+ "slide",
689
+ "slidein",
690
+ "slide-in",
691
+ "bounce",
692
+ "glow",
693
+ "blink",
694
+ "shake",
695
+ "float"
696
+ ]);
697
+ /**
698
+ * Transform a skin stylesheet: force-scope every selector under
699
+ * html[data-dsh-skin="<id>"] and enforce the whitelist. Throws
700
+ * SkinCssSafetyError on any violation (fail-closed); lightningcss parse
701
+ * errors propagate as-is (malformed CSS is also a hard failure).
702
+ */
703
+ function transformSkinCss(css, options) {
704
+ const { skinId } = options;
705
+ const filename = options.filename ?? "skin.css";
706
+ const violations = [];
707
+ const warnings = [];
708
+ const spans = [];
709
+ const defined = /* @__PURE__ */ new Set();
710
+ transform({
711
+ filename,
712
+ code: Buffer.from(css),
713
+ visitor: {
714
+ Rule: {
715
+ import(rule) {
716
+ violations.push(`${filename}: @import "${rule.value.url}" is not allowed; skins are single-file stylesheets`);
717
+ },
718
+ keyframes(rule) {
719
+ const name = rule.value.name;
720
+ const value = typeof name === "string" ? name : name?.value;
721
+ if (typeof value === "string" && GENERIC_KEYFRAMES.has(value.toLowerCase())) warnings.push(`${filename}: generic @keyframes name "${value}" may collide across skins; prefix it (e.g. ${skinId}-${value})`);
722
+ },
723
+ style(rule) {
724
+ const loc = rule.value.loc;
725
+ if (loc) {
726
+ const start = locToOffset(css, loc.line, loc.column);
727
+ const openBrace = findOpenBrace(css, start);
728
+ if (openBrace !== -1) spans.push({
729
+ start,
730
+ openBrace
731
+ });
732
+ }
733
+ for (const sel of rule.value.selectors) for (const c of sel) if (c.type === "attribute" && c.name === "class" && [
734
+ "substring",
735
+ "prefix",
736
+ "suffix"
737
+ ].includes(c.operation?.operator)) warnings.push(`${filename}: [class*=...]-style attribute matching relies on CSS-Modules hash class names and may break on any official rebuild`);
738
+ }
739
+ },
740
+ Declaration: { custom(property) {
741
+ defined.add(property.name);
742
+ } },
743
+ Url(url) {
744
+ checkUrl(url.url, filename, violations, warnings);
745
+ }
746
+ }
747
+ });
748
+ if (violations.length > 0) throw new SkinCssSafetyError(`skin CSS violates the whitelist:\n - ${violations.join("\n - ")}`, violations);
749
+ const sorted = [...spans].sort((a, b) => a.start - b.start);
750
+ const scope = `html[data-dsh-skin="${skinId}"]`;
751
+ let out = "";
752
+ let cursor = 0;
753
+ for (const span of sorted) {
754
+ const selectorText = css.slice(span.start, span.openBrace);
755
+ const close = findCloseBrace(css, span.openBrace);
756
+ out += css.slice(cursor, span.start);
757
+ const scoped = scopeSelectorList(selectorText, skinId);
758
+ const block = close === -1 ? css.slice(span.openBrace) : css.slice(span.openBrace, close + 1);
759
+ out += scoped + block;
760
+ if (close !== -1 && hasBareRootSelector(selectorText)) {
761
+ const tokens = rootBodyTokens(block);
762
+ if (tokens.length > 0) out += `\n${scope} {\n ${tokens.map(({ name, important }) => `${name}: initial${important ? " !important" : ""};`).join("\n ")}\n}\n`;
763
+ }
764
+ if (hasBareRootSelector(selectorText) && close !== -1) {
765
+ const props = withoutCssComments(css.slice(span.openBrace + 1, close)).split("\n").map((line) => bodyCloneProperty(line.trim())).filter((line) => line !== null);
766
+ if (props.length > 0) out += `\n${scope} body {\n ${props.join("\n ")}\n}\n`;
767
+ }
768
+ cursor = close === -1 ? span.openBrace : close + 1;
769
+ }
770
+ out += css.slice(cursor);
771
+ out += `\n${scope} [id="root"] { background: transparent; }\n`;
772
+ out += `\n${scope} body { --shiki-background: var(--dsw-alias-markdown-code-block); }\n`;
773
+ if (options.deriveFallbacks === true) {
774
+ const fallbacks = [...deriveFallbackTokens(defined), ...derivePrimaryActionFallbacks(defined)];
775
+ if (fallbacks.length > 0) out += `\n${scope} body {\n ${fallbacks.join("\n ")}\n}\n`;
776
+ }
777
+ return {
778
+ code: out,
779
+ warnings
780
+ };
781
+ }
782
+ /**
783
+ * Find the matching closing brace for the block opening at openBrace.
784
+ * Conservative: counts braces, skips strings and comments; returns -1 when
785
+ * the block never closes (callers then keep the remainder as-is).
786
+ */
787
+ function findCloseBrace(css, openBrace) {
788
+ let depth = 0;
789
+ let i = openBrace;
790
+ let inString = null;
791
+ let inComment = false;
792
+ for (; i < css.length; i++) {
793
+ const ch = css[i];
794
+ const next = css[i + 1];
795
+ if (inComment) {
796
+ if (ch === "*" && next === "/") {
797
+ inComment = false;
798
+ i++;
799
+ }
800
+ continue;
801
+ }
802
+ if (inString !== null) {
803
+ if (ch === "\\") i++;
804
+ else if (ch === inString) inString = null;
805
+ continue;
806
+ }
807
+ if (ch === "/" && next === "*") {
808
+ inComment = true;
809
+ i++;
810
+ continue;
811
+ }
812
+ if (ch === "\"" || ch === "'") {
813
+ inString = ch;
814
+ continue;
815
+ }
816
+ if (ch === "{") depth++;
817
+ else if (ch === "}") {
818
+ depth--;
819
+ if (depth === 0) return i;
820
+ }
821
+ }
822
+ return -1;
823
+ }
824
+ //#endregion
825
+ export { SkinCssSafetyError, scopeSelectorList, scopeSelectorText, transformSkinCss };