@astryxdesign/cli 0.7.16 → 0.7.17-canary.g335a172
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 +76 -86
- package/api/build/_adapter.d.mts +12 -0
- package/api/build/_adapter.mjs +51 -34
- package/api/build/blocks/blocks.d.mts +7 -5
- package/api/build/blocks/blocks.mjs +113 -26
- package/api/build/build.doc.mjs +4 -3
- package/api/build/build.type.d.mts +2 -2
- package/api/build/build.type.mjs +3 -3
- package/api/build/kit/kind.d.mts +2 -1
- package/api/build/kit/kind.mjs +199 -10
- package/api/build/kit/kit.mjs +6 -4
- package/api/build/kit/rank.mjs +69 -16
- package/api/build/kit/start.d.mts +5 -4
- package/api/build/kit/start.mjs +12 -8
- package/api/build/kit/weights.d.mts +16 -3
- package/api/build/kit/weights.json +1 -1
- package/api/build/kit/weights.mjs +251 -35
- package/api/build/kit/word-model.d.mts +23 -0
- package/api/build/kit/word-model.mjs +46 -0
- package/api/build/kit/words.d.mts +48 -0
- package/api/build/kit/words.mjs +90 -1
- package/api/component/__fixtures__/resolutions/modules.txt +4 -5
- package/api/component/list/list.d.mts +1 -1
- package/api/component/list/list.mjs +48 -6
- package/api/discover/_adapter.d.mts +1 -1
- package/api/discover/search/search.mjs +3 -2
- package/api/docs/_adapter.d.mts +26 -2
- package/api/docs/_adapter.mjs +219 -6
- package/api/docs/docs.d.mts +3 -0
- package/api/docs/docs.doc.mjs +7 -1
- package/api/docs/docs.mjs +12 -0
- package/api/docs/docs.type.d.mts +5 -0
- package/api/docs/docs.type.mjs +2 -0
- package/api/docs/node/node.mjs +1 -1
- package/api/doctor/_adapter.d.mts +27 -5
- package/api/doctor/_adapter.mjs +83 -10
- package/api/doctor/built-checks.mjs +40 -14
- package/api/doctor/doctor.d.mts +4 -3
- package/api/doctor/doctor.mjs +14 -7
- package/api/doctor/theme-checks.mjs +121 -17
- package/api/icon/_records.mjs +2 -1
- package/api/index.d.mts +2 -0
- package/api/index.mjs +2 -0
- package/api/integration/_adapter.d.mts +44 -0
- package/api/integration/_adapter.mjs +77 -1
- package/api/integration/authoring-checks.mjs +6 -6
- package/api/integration/example-scope.d.mts +14 -0
- package/api/integration/example-scope.mjs +44 -0
- package/api/integration/example-scope.type.d.mts +29 -0
- package/api/integration/example-scope.type.mjs +25 -0
- package/api/integration/integrationExampleScope.doc.d.mts +11 -0
- package/api/integration/integrationExampleScope.doc.mjs +59 -0
- package/api/integration/validate-integration.mjs +19 -1
- package/api/integration/validate-integration.type.d.mts +9 -0
- package/api/integration/validate-integration.type.mjs +1 -1
- package/api/integration/validateIntegration.doc.mjs +1 -1
- package/api/render/_adapter.mjs +591 -195
- package/api/render/render.doc.mjs +11 -3
- package/api/render/render.type.d.mts +52 -0
- package/api/render/render.type.mjs +31 -1
- package/api/search/ORDERING.md +1 -1
- package/api/search/_candidate-score.mjs +51 -32
- package/api/search/_fit-order.mjs +3 -3
- package/api/search/_gather-components.d.mts +3 -2
- package/api/search/_gather-components.mjs +12 -3
- package/api/search/_lexical.d.mts +5 -0
- package/api/search/_lexical.mjs +8 -2
- package/api/search/_result.d.mts +11 -9
- package/api/search/_result.mjs +4 -4
- package/api/search/_score.d.mts +6 -1
- package/api/search/_score.mjs +15 -3
- package/api/search/search-index.mjs +7 -1
- package/api/search/search.d.mts +1 -1
- package/api/theme/_adapter.mjs +12 -1
- package/api/theme/build/build.d.mts +60 -0
- package/api/theme/build/build.mjs +210 -54
- package/api/theme/build/font-warning.d.mts +8 -0
- package/api/theme/build/font-warning.mjs +19 -0
- package/api/theme/theme.type.d.mts +10 -0
- package/api/theme/theme.type.mjs +7 -1
- package/api/theme-edit/_adapter.d.mts +55 -0
- package/api/theme-edit/_adapter.mjs +673 -42
- package/api/theme-edit/_check.d.mts +28 -22
- package/api/theme-edit/_check.mjs +1858 -312
- package/api/theme-edit/_color.d.mts +10 -0
- package/api/theme-edit/_color.mjs +49 -1
- package/api/theme-edit/_compiler.mjs +9 -0
- package/api/theme-edit/_context.mjs +60 -3
- package/api/theme-edit/_css.mjs +21 -1
- package/api/theme-edit/_edit.d.mts +22 -0
- package/api/theme-edit/_edit.mjs +1533 -92
- package/api/theme-edit/_how-commands.d.mts +57 -0
- package/api/theme-edit/_how-commands.mjs +587 -1
- package/api/theme-edit/_how-question.d.mts +78 -0
- package/api/theme-edit/_how-question.mjs +4716 -235
- package/api/theme-edit/_how.mjs +134 -8
- package/api/theme-edit/_marks.d.mts +31 -5
- package/api/theme-edit/_marks.mjs +57 -5
- package/api/theme-edit/_normal.d.mts +219 -0
- package/api/theme-edit/_normal.mjs +1041 -0
- package/api/theme-edit/_palette.mjs +10 -4
- package/api/theme-edit/_parts.d.mts +8 -0
- package/api/theme-edit/_parts.mjs +103 -2
- package/api/theme-edit/_playwright-picture.d.mts +4 -0
- package/api/theme-edit/_playwright-picture.mjs +34 -0
- package/api/theme-edit/_preview-app.d.mts +7 -1
- package/api/theme-edit/_preview-app.mjs +394 -74
- package/api/theme-edit/_preview.d.mts +69 -8
- package/api/theme-edit/_preview.mjs +531 -108
- package/api/theme-edit/_settings.d.mts +243 -6
- package/api/theme-edit/_settings.mjs +2415 -147
- package/api/theme-edit/_tables.d.mts +6 -0
- package/api/theme-edit/_tables.mjs +44 -1
- package/api/theme-edit/_vet.d.mts +18 -0
- package/api/theme-edit/_vet.mjs +32 -0
- package/api/theme-edit/theme-edit.type.d.mts +10 -1
- package/api/theme-edit/theme-edit.type.mjs +4 -1
- package/api/theme-edit/themeEditHow.doc.mjs +1 -1
- package/api/theme-edit/themeEditNew.doc.mjs +1 -1
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +3 -3
- package/api/theme-edit/themeEditPreviewRemove.doc.mjs +2 -2
- package/api/upgrade/run/run.mjs +4 -3
- package/assets/codemods/integration-discovery.mjs +2 -1
- package/assets/codemods/run-codemod.mjs +87 -7
- package/assets/codemods/transforms/next/index.mjs +11 -1
- package/assets/codemods/transforms/next/migrate-item-swipe-actions-to-swipe.mjs +394 -0
- package/assets/component-list.generated.json +1 -0
- package/assets/docs/author-a-theme.doc.mjs +1 -0
- package/assets/docs/theme.doc.dense.mjs +2 -2
- package/assets/docs/theme.doc.mjs +1 -1
- package/assets/docs/tree/check-your-docs.doc.mjs +1 -1
- package/assets/docs/tree/checks.doc.mjs +1 -1
- package/assets/docs/tree/cli-command.doc.mjs +2 -3
- package/assets/docs/tree/render-pages.doc.mjs +44 -3
- package/assets/docs/tree/single-component.doc.mjs +4 -0
- package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
- package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
- package/assets/docs/use-a-theme.doc.mjs +1 -1
- package/assets/docs.generated.json +2 -2
- package/assets/self-audit.generated.json +1 -1
- package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.tsx +38 -0
- package/assets/templates/blocks/components/Hooks/useContainerRevealHookUsage.tsx +2 -2
- package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.tsx +36 -0
- package/assets/templates/components.generated.json +1 -1
- package/assets/templates/pages/ai-chat/template.doc.mjs +5 -4
- package/assets/templates/pages/ai-chat-landing/template.doc.mjs +2 -1
- package/assets/templates/pages/blank/template.doc.mjs +1 -1
- package/assets/templates/pages/canvas-editor/template.doc.mjs +4 -2
- package/assets/templates/pages/centered-hero/template.doc.mjs +9 -1
- package/assets/templates/pages/classic-gallery/template.doc.mjs +1 -1
- package/assets/templates/pages/contact-form/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard/template.doc.mjs +3 -2
- package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard-progress/template.doc.mjs +6 -3
- package/assets/templates/pages/detail-page/template.doc.mjs +10 -3
- package/assets/templates/pages/documentation/template.doc.mjs +4 -1
- package/assets/templates/pages/documentation-design/template.doc.mjs +7 -1
- package/assets/templates/pages/documentation-technical/template.doc.mjs +7 -1
- package/assets/templates/pages/editor/template.doc.mjs +2 -8
- package/assets/templates/pages/file-explorer/template.doc.mjs +1 -1
- package/assets/templates/pages/form-two-column/template.doc.mjs +4 -3
- package/assets/templates/pages/form-wizard/template.doc.mjs +5 -3
- package/assets/templates/pages/gallery-hero/template.doc.mjs +4 -2
- package/assets/templates/pages/ide/page.tsx +24 -2
- package/assets/templates/pages/kanban-board/template.doc.mjs +1 -9
- package/assets/templates/pages/library/template.doc.mjs +4 -1
- package/assets/templates/pages/login/template.doc.mjs +2 -1
- package/assets/templates/pages/login-card/template.doc.mjs +2 -1
- package/assets/templates/pages/login-split/template.doc.mjs +2 -1
- package/assets/templates/pages/login-sso/template.doc.mjs +4 -2
- package/assets/templates/pages/mixed-gallery/template.doc.mjs +2 -1
- package/assets/templates/pages/product-detail/template.doc.mjs +4 -2
- package/assets/templates/pages/product-gallery/template.doc.mjs +2 -1
- package/assets/templates/pages/settings-dialog/template.doc.mjs +2 -1
- package/assets/templates/pages/settings-sidebar/template.doc.mjs +2 -1
- package/assets/templates/pages/shell-nav/template.doc.mjs +4 -2
- package/assets/templates/pages/shell-side-nav/template.doc.mjs +5 -3
- package/assets/templates/pages/shell-top-nav/template.doc.mjs +2 -1
- package/assets/templates/pages/side-gallery/template.doc.mjs +9 -1
- package/assets/templates/pages/table-filter/template.doc.mjs +9 -8
- package/assets/templates/pages/table-grouped/template.doc.mjs +4 -2
- package/assets/templates/pages/table-page/template.doc.mjs +5 -4
- package/assets/templates/pages/table-tree/template.doc.mjs +6 -3
- package/assets/templates/pages/theme-showcase/template.doc.mjs +2 -9
- package/assets/templates/pages/work-item-detail/template.doc.mjs +1 -0
- package/assets/theme.template.ts +6 -0
- package/authoring/debug/parse.d.mts +3 -3
- package/authoring/doctypes/_schema.d.mts +3 -3
- package/authoring/doctypes/command/command.doc.mjs +1 -1
- package/authoring/doctypes/component/categories.d.mts +37 -0
- package/authoring/doctypes/component/categories.mjs +54 -0
- package/authoring/doctypes/template/template.doc.mjs +1 -1
- package/authoring/doctypes/template/type.ts +9 -0
- package/authoring/example/example.doc.d.mts +12 -0
- package/authoring/example/example.doc.mjs +69 -0
- package/authoring/example/preview.d.mts +92 -0
- package/authoring/example/preview.mjs +994 -0
- package/authoring/example/type.ts +102 -0
- package/authoring/index.d.mts +1 -0
- package/authoring/index.d.ts +12 -0
- package/authoring/index.mjs +3 -1
- package/clients/cli/bin/astryx.mjs +6 -9
- package/clients/cli/build-id.generated.json +1 -1
- package/clients/cli/commands/component.doc.mjs +1 -1
- package/clients/cli/commands/component.mjs +22 -3
- package/clients/cli/commands/docs.doc.mjs +7 -1
- package/clients/cli/commands/docs.mjs +3 -1
- package/clients/cli/commands/doctor.mjs +32 -14
- package/clients/cli/commands/experimental/render.mjs +42 -1
- package/clients/cli/commands/experimental/theme.mjs +145 -16
- package/clients/cli/commands/experimental-render.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
- package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +87 -1
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +2 -2
- package/clients/cli/commands/search.doc.mjs +2 -1
- package/clients/cli/commands/search.mjs +6 -1
- package/clients/cli/commands/template.mjs +4 -2
- package/clients/cli/commands/theme-build.doc.mjs +5 -1
- package/clients/cli/commands/theme.doc.mjs +1 -1
- package/clients/cli/commands/theme.mjs +183 -6
- package/clients/cli/commands/upgrade.mjs +42 -7
- package/clients/cli/index.mjs +1 -1
- package/clients/cli/lib/cli-error.mjs +2 -1
- package/clients/cli/lib/component-format.mjs +205 -98
- package/clients/cli/lib/hook-format.mjs +9 -19
- package/clients/cli/lib/json-shim.mjs +77 -21
- package/clients/cli/lib/manifest.mjs +3 -2
- package/clients/cli/manifest.generated.json +1 -1
- package/foundation/agent-docs/agent-docs.mjs +1 -1
- package/foundation/config/theme-state.d.mts +13 -1
- package/foundation/config/theme-state.mjs +47 -7
- package/foundation/discovery/authoring-self-docs.mjs +1 -0
- package/foundation/discovery/component-discovery.d.mts +11 -0
- package/foundation/discovery/component-discovery.mjs +56 -8
- package/foundation/discovery/component-replacement.mjs +130 -1
- package/foundation/discovery/copied-passages.mjs +2 -1
- package/foundation/discovery/doc-types.mjs +4 -3
- package/foundation/discovery/entry-name-files.d.mts +43 -0
- package/foundation/discovery/entry-name-files.mjs +264 -0
- package/foundation/discovery/hook-discovery.d.mts +8 -2
- package/foundation/discovery/hook-discovery.mjs +22 -8
- package/foundation/discovery/icon-definition-discovery.mjs +2 -1
- package/foundation/discovery/icon-set-checks.mjs +2 -1
- package/foundation/discovery/icon-set-discovery.mjs +2 -1
- package/foundation/discovery/integration-doc-files.mjs +2 -1
- package/foundation/discovery/package-exports.d.mts +10 -0
- package/foundation/discovery/package-exports.mjs +56 -3
- package/foundation/discovery/prop-drift.mjs +9 -9
- package/foundation/discovery/runner-allowlist.d.mts +1 -0
- package/foundation/discovery/runner-allowlist.mjs +2 -0
- package/foundation/discovery/stored-component-list.d.mts +43 -0
- package/foundation/discovery/stored-component-list.mjs +90 -0
- package/foundation/discovery/stored-self-audit.d.mts +26 -10
- package/foundation/discovery/stored-self-audit.mjs +38 -18
- package/foundation/discovery/template-adapter.d.mts +52 -0
- package/foundation/discovery/template-adapter.mjs +176 -12
- package/foundation/discovery/template-icons.mjs +2 -1
- package/foundation/discovery/theme-discovery.mjs +3 -2
- package/foundation/discovery/theme-imports.d.mts +14 -0
- package/foundation/discovery/theme-imports.mjs +43 -1
- package/foundation/discovery/theming-targets.mjs +3 -2
- package/foundation/doc-compiler/import.mjs +7 -1
- package/foundation/doc-compiler/inputs.mjs +3 -2
- package/foundation/doc-compiler/parse-readable.mjs +4 -1
- package/foundation/doc-compiler/stored-docs.d.mts +22 -0
- package/foundation/doc-compiler/stored-docs.mjs +49 -0
- package/foundation/env/semver.mjs +3 -1
- package/foundation/fonts/font-stylesheet.d.mts +31 -0
- package/foundation/fonts/font-stylesheet.mjs +115 -0
- package/foundation/fs/cache-eviction.d.mts +21 -0
- package/foundation/fs/cache-eviction.mjs +73 -0
- package/foundation/fs/file-protection.mjs +4 -3
- package/foundation/fs/gather-cache.d.mts +25 -4
- package/foundation/fs/gather-cache.mjs +164 -55
- package/foundation/fs/jiti-cache.d.mts +19 -1
- package/foundation/fs/jiti-cache.mjs +130 -6
- package/foundation/fs/kept-tree.d.mts +60 -0
- package/foundation/fs/kept-tree.mjs +154 -0
- package/foundation/fs/listing-cache.d.mts +7 -0
- package/foundation/fs/listing-cache.mjs +10 -0
- package/foundation/fs/module-loader.d.mts +24 -2
- package/foundation/fs/module-loader.mjs +94 -21
- package/foundation/fs/paths.d.mts +18 -0
- package/foundation/fs/paths.mjs +15 -2
- package/foundation/identity/provider-identity.d.mts +6 -0
- package/foundation/identity/provider-identity.mjs +10 -1
- package/foundation/integrations/contribution-inventory.mjs +4 -3
- package/foundation/integrations/example-code.d.mts +63 -0
- package/foundation/integrations/example-code.mjs +416 -0
- package/foundation/integrations/public-imports.d.mts +9 -0
- package/foundation/integrations/public-imports.mjs +3 -2
- package/foundation/integrations/validate-contributions.mjs +17 -45
- package/foundation/render/embedded-files.d.mts +6 -0
- package/foundation/render/embedded-files.mjs +25 -5
- package/foundation/render/embedded-imports.d.mts +39 -7
- package/foundation/render/embedded-imports.mjs +66 -27
- package/foundation/render/file-header.d.mts +66 -0
- package/foundation/render/file-header.mjs +118 -0
- package/foundation/render/host-answer.d.mts +18 -0
- package/foundation/render/host-answer.mjs +46 -0
- package/foundation/render/host-exit.d.mts +1 -0
- package/foundation/render/host-exit.mjs +6 -1
- package/foundation/render/html-reader.d.mts +55 -2
- package/foundation/render/html-reader.mjs +145 -26
- package/foundation/render/markup-edits.d.mts +57 -0
- package/foundation/render/markup-edits.mjs +182 -0
- package/foundation/render/page-host.mjs +29 -12
- package/foundation/render/page-worker.mjs +121 -13
- package/foundation/render/rebuild.mjs +2 -1
- package/foundation/render/render-page.d.mts +35 -3
- package/foundation/render/render-page.mjs +316 -42
- package/foundation/render/source-block.d.mts +85 -6
- package/foundation/render/source-block.mjs +211 -20
- package/foundation/render/ts-paths.d.mts +69 -6
- package/foundation/render/ts-paths.mjs +326 -40
- package/foundation/response/error-codes.d.mts +3 -1
- package/foundation/response/error-codes.d.ts +1 -0
- package/foundation/response/error-codes.doc.mjs +12 -1
- package/foundation/response/error-codes.mjs +11 -1
- package/foundation/response/response-types.doc.mjs +2 -2
- package/foundation/text/compare-text.d.mts +14 -0
- package/foundation/text/compare-text.mjs +68 -0
- package/foundation/text/string-utils.d.mts +3 -2
- package/foundation/text/string-utils.mjs +18 -4
- package/foundation/text/suggest-order.d.mts +9 -0
- package/foundation/text/suggest-order.mjs +15 -1
- package/foundation/xle/expand.mjs +2 -1
- package/host/index.mjs +8 -6
- package/package.json +7 -6
|
@@ -0,0 +1,1041 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file The theme editor's input normaliser: how a typed or saved thing and
|
|
5
|
+
* value are read before any check (Unicode spaces, delimiting quotes, a
|
|
6
|
+
* pasted declaration, JSON and copy debris, a font stack's families). Typed
|
|
7
|
+
* input (`set`, `undo`) and saved settings (`loadTheme`, the stored checks)
|
|
8
|
+
* both read through it.
|
|
9
|
+
* @position packages/cli/api/theme-edit — pure helpers, no environment access
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {DIALS, wordTable} from './_tables.mjs';
|
|
13
|
+
import {namedColorHex} from './_color.mjs';
|
|
14
|
+
import {levenshteinDistance} from '../../foundation/text/levenshtein.mjs';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A value as CSS reads it, for every value type: every Unicode space
|
|
18
|
+
* (no-break, thin, ideographic…) a plain space and zero-width and format
|
|
19
|
+
* characters dropped, and quotes that delimit a value (curly ones, «», ‹›,
|
|
20
|
+
* full-width, backticks) straight. A straight-quoted CSS string keeps what
|
|
21
|
+
* it holds as written (`content: "it’s"`). Curly quotes pair by kind
|
|
22
|
+
* (double with double, single with single, in any of their shapes: „Inter“,
|
|
23
|
+
* ‚Inter‘); one left unpaired is read as the straight quote it stands for.
|
|
24
|
+
* @param {string} v
|
|
25
|
+
* @returns {string}
|
|
26
|
+
*/
|
|
27
|
+
export function normalValueText(v) {
|
|
28
|
+
let out = '';
|
|
29
|
+
let plain = '';
|
|
30
|
+
const flush = () => {
|
|
31
|
+
out += plain
|
|
32
|
+
.replace(/[\u200B-\u200D\u2060\uFEFF]/g, '')
|
|
33
|
+
.replace(/[\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]/g, ' ');
|
|
34
|
+
plain = '';
|
|
35
|
+
};
|
|
36
|
+
for (let i = 0; i < v.length;) {
|
|
37
|
+
const c = v[i];
|
|
38
|
+
// A straight-quoted CSS string keeps everything inside it.
|
|
39
|
+
if (c === '"' || c === "'") {
|
|
40
|
+
let j = i + 1;
|
|
41
|
+
while (j < v.length && v[j] !== c) j += v[j] === '\\' ? 2 : 1;
|
|
42
|
+
if (j < v.length) {
|
|
43
|
+
flush();
|
|
44
|
+
out += v.slice(i, j + 1);
|
|
45
|
+
i = j + 1;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const kind = CURLY_DOUBLE.has(c)
|
|
50
|
+
? CURLY_DOUBLE
|
|
51
|
+
: CURLY_SINGLE.has(c)
|
|
52
|
+
? CURLY_SINGLE
|
|
53
|
+
: null;
|
|
54
|
+
/** @type {number} */
|
|
55
|
+
let j = -1;
|
|
56
|
+
if (kind) {
|
|
57
|
+
for (let k = i + 1; k < v.length; k++)
|
|
58
|
+
if (kind.has(v[k])) {
|
|
59
|
+
j = k;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
} else if (QUOTE_PAIRS[c]) j = v.indexOf(QUOTE_PAIRS[c], i + 1);
|
|
63
|
+
if (j > i) {
|
|
64
|
+
flush();
|
|
65
|
+
// What the quotes hold, as typed (`“ / ”` is a slash between
|
|
66
|
+
// spaces); a font family trims it (normalizeEntry).
|
|
67
|
+
const inner = v.slice(i + 1, j);
|
|
68
|
+
const single = kind === CURLY_SINGLE || c === '\uFF07';
|
|
69
|
+
const mark = single
|
|
70
|
+
? inner.includes("'")
|
|
71
|
+
? '"'
|
|
72
|
+
: "'"
|
|
73
|
+
: inner.includes('"')
|
|
74
|
+
? "'"
|
|
75
|
+
: '"';
|
|
76
|
+
out += `${mark}${inner}${mark}`;
|
|
77
|
+
i = j + 1;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
// An unpaired curly quote is the straight one it stands for.
|
|
81
|
+
plain += kind === CURLY_DOUBLE ? '"' : kind === CURLY_SINGLE ? "'" : c;
|
|
82
|
+
i += 1;
|
|
83
|
+
}
|
|
84
|
+
flush();
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Curly double quotes, in every shape; any one closes any other. */
|
|
89
|
+
const CURLY_DOUBLE = new Set(['\u201C', '\u201D', '\u201E', '\u201F']);
|
|
90
|
+
/** Curly single quotes, in every shape. */
|
|
91
|
+
const CURLY_SINGLE = new Set(['\u2018', '\u2019', '\u201A', '\u201B']);
|
|
92
|
+
/** The other delimiting quotes and the one that closes each. */
|
|
93
|
+
/** @type {Record<string, string>} */
|
|
94
|
+
const QUOTE_PAIRS = {
|
|
95
|
+
'\u300C': '\u300D',
|
|
96
|
+
'\u300E': '\u300F',
|
|
97
|
+
'\u00AB': '\u00BB',
|
|
98
|
+
'\u00BB': '\u00AB',
|
|
99
|
+
'\u2039': '\u203A',
|
|
100
|
+
'\u203A': '\u2039',
|
|
101
|
+
'\uFF02': '\uFF02',
|
|
102
|
+
'\uFF07': '\uFF07',
|
|
103
|
+
'`': '`',
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/** CSS functions whose names a value may write in any case. */
|
|
107
|
+
const CSS_FUNCTIONS =
|
|
108
|
+
/\b(light-dark|rgba?|hsla?|hwb|lab|lch|oklab|oklch|color|color-mix|calc|min|max|clamp|var)\(/gi;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* A value as meant when typed with escapes or in another notation: CSS
|
|
112
|
+
* function names in lower case (`LIGHT-DARK(` is `light-dark(`, as CSS reads
|
|
113
|
+
* it); a shell or JSON backslash before `#`, a quote or a space taken off;
|
|
114
|
+
* a JavaScript escape in a `content` string written as CSS writes it
|
|
115
|
+
* (`\u2022` is `\2022 `, `\n` a line break, `\t` a tab); and the JSON
|
|
116
|
+
* shapes a design-tokens file or an array writes (`{"$value": "#e11d48"}`,
|
|
117
|
+
* `{"value": 18, "unit": "px"}`, `["#e11d48"]`, `["Inter", "sans-serif"]`).
|
|
118
|
+
* @param {string} thing
|
|
119
|
+
* @param {string} value
|
|
120
|
+
* @returns {string}
|
|
121
|
+
*/
|
|
122
|
+
export function escapesMeant(thing, value) {
|
|
123
|
+
let v = value;
|
|
124
|
+
const content = /^content$/i.test(thing);
|
|
125
|
+
if (content)
|
|
126
|
+
return (
|
|
127
|
+
v
|
|
128
|
+
.replace(
|
|
129
|
+
/\\u\{?([0-9a-f]{2,6})\}?/gi,
|
|
130
|
+
(_, h) => `\\${h.toUpperCase()} `,
|
|
131
|
+
)
|
|
132
|
+
// `\x` with four hex digits is written for the code point (as
|
|
133
|
+
// `\u`), with two for that character.
|
|
134
|
+
.replace(
|
|
135
|
+
/\\x([0-9a-f]{4}|[0-9a-f]{2})/gi,
|
|
136
|
+
(_, h) => `\\${h.toUpperCase()} `,
|
|
137
|
+
)
|
|
138
|
+
.replace(/\\n/g, '\\A ')
|
|
139
|
+
.replace(/\\t/g, '\\9 ')
|
|
140
|
+
.replace(/\\r/g, '\\D ')
|
|
141
|
+
);
|
|
142
|
+
v = v.replace(CSS_FUNCTIONS, f => f.toLowerCase());
|
|
143
|
+
// A JS escape of a printable character in a font family (`Inter\u0020
|
|
144
|
+
// Tight`) is that character, as CSS would read its own escape.
|
|
145
|
+
if (/font/i.test(thing))
|
|
146
|
+
v = v.replace(
|
|
147
|
+
/\\(?:u\{?([0-9a-f]{4,6})\}?|x([0-9a-f]{2}))/gi,
|
|
148
|
+
(m, u, x) => {
|
|
149
|
+
const c = String.fromCodePoint(parseInt(u ?? x, 16));
|
|
150
|
+
return /^[\p{L}\p{N} _-]$/u.test(c) ? c : m;
|
|
151
|
+
},
|
|
152
|
+
);
|
|
153
|
+
// (A JSON string holding escaped quotes is unwrapped whole later.)
|
|
154
|
+
if (!/^\s*"(?:[^"\\]|\\.)*"\s*$/.test(v)) v = v.replace(/\\([#"' ])/g, '$1');
|
|
155
|
+
else v = v.replace(/\\([# ])/g, '$1');
|
|
156
|
+
const t = v.trim();
|
|
157
|
+
if (/^[[{]/.test(t)) {
|
|
158
|
+
/** @type {any} */
|
|
159
|
+
let j;
|
|
160
|
+
try {
|
|
161
|
+
j = JSON.parse(t);
|
|
162
|
+
} catch {
|
|
163
|
+
// A list in single quotes, as a JS config writes it (`['Inter',
|
|
164
|
+
// 'serif']`): read like the JSON one.
|
|
165
|
+
const items =
|
|
166
|
+
/^\[\s*('(?:[^'\\]|\\.)*'\s*(?:,\s*'(?:[^'\\]|\\.)*'\s*)*),?\s*\]$/.exec(
|
|
167
|
+
t,
|
|
168
|
+
);
|
|
169
|
+
if (!items) return v;
|
|
170
|
+
j = [...items[1].matchAll(/'((?:[^'\\]|\\.)*)'/g)].map(x =>
|
|
171
|
+
x[1].replace(/\\(.)/g, '$1'),
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
/** @param {any} x */
|
|
175
|
+
const text = x =>
|
|
176
|
+
typeof x === 'string' || typeof x === 'number' ? String(x) : null;
|
|
177
|
+
if (Array.isArray(j) && j.length && j.every(x => text(x) != null)) {
|
|
178
|
+
if (j.length === 1) return String(j[0]);
|
|
179
|
+
if (/font/i.test(thing))
|
|
180
|
+
return j
|
|
181
|
+
.map(x =>
|
|
182
|
+
/\s/.test(String(x)) && !/^["']/.test(String(x))
|
|
183
|
+
? `"${x}"`
|
|
184
|
+
: String(x),
|
|
185
|
+
)
|
|
186
|
+
.join(', ');
|
|
187
|
+
return v;
|
|
188
|
+
}
|
|
189
|
+
if (j && typeof j === 'object' && !Array.isArray(j)) {
|
|
190
|
+
const inner =
|
|
191
|
+
'$value' in j ? j.$value : 'value' in j ? j.value : undefined;
|
|
192
|
+
if (text(inner) != null)
|
|
193
|
+
return `${inner}${typeof j.unit === 'string' ? j.unit : ''}`;
|
|
194
|
+
// One labeled entry (`{"color": "#e11d48"}`) is read as the same
|
|
195
|
+
// label in its CSS form (`color: #e11d48`): one reader for both.
|
|
196
|
+
const keys = Object.keys(j);
|
|
197
|
+
if (
|
|
198
|
+
keys.length === 1 &&
|
|
199
|
+
/^(--)?[a-z][a-z0-9-]*$/i.test(keys[0]) &&
|
|
200
|
+
text(j[keys[0]]) != null
|
|
201
|
+
)
|
|
202
|
+
return `${keys[0]}: ${j[keys[0]]}`;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return v;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The thing and value a setting was typed as, put right before any check: a
|
|
210
|
+
* declaration pasted whole or split (`accent: #e11d48`, `--color-accent=red`,
|
|
211
|
+
* `font:inter`, `accent:` with the value next), a token typed with an em or
|
|
212
|
+
* en dash (`—color-accent`) or as `var(--color-accent)`, and the value's
|
|
213
|
+
* Unicode spaces. A thing it cannot read is left as typed.
|
|
214
|
+
* @param {string} thing
|
|
215
|
+
* @param {string | undefined} value
|
|
216
|
+
* @returns {{thing: string, value: string | undefined, dropped?: string}} `dropped`: a second declaration pasted after the first, which is not set
|
|
217
|
+
*/
|
|
218
|
+
export function normalizeEntry(thing, value) {
|
|
219
|
+
let t = normalValueText(String(thing ?? '')).trim();
|
|
220
|
+
let v = value == null ? value : normalValueText(String(value));
|
|
221
|
+
// A pasted declaration (the whole of it in the thing, or the thing with
|
|
222
|
+
// its colon and the value next): its `;` and `!important` go with it.
|
|
223
|
+
let pasted = false;
|
|
224
|
+
// A JSON object around one entry (`{"accent": "#e11d48"}`), whole or
|
|
225
|
+
// split at its colon.
|
|
226
|
+
if ((v == null || v.trim() === '') && /^\{[\s\S]*\}$/.test(t))
|
|
227
|
+
t = t.slice(1, -1).trim().replace(/,\s*$/, '');
|
|
228
|
+
else if (t.startsWith('{') && typeof v === 'string' && /\}\s*$/.test(v)) {
|
|
229
|
+
t = t.slice(1).trim();
|
|
230
|
+
v = v.replace(/\s*\}\s*$/, '');
|
|
231
|
+
}
|
|
232
|
+
if (v == null || v.trim() === '') {
|
|
233
|
+
const m = /^(\S+?)\s*[:=]\s*(.+?)\s*;?\s*$/.exec(t);
|
|
234
|
+
if (m && !/^https?$/i.test(m[1])) {
|
|
235
|
+
t = m[1];
|
|
236
|
+
v = m[2];
|
|
237
|
+
pasted = true;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
// A thing ending in its colon or equals sign, the value given next.
|
|
241
|
+
if (/\s*[:=]+$/.test(t)) {
|
|
242
|
+
t = t.replace(/\s*[:=]+$/, '');
|
|
243
|
+
pasted = true;
|
|
244
|
+
}
|
|
245
|
+
// A thing in quotes, as a JSON or design-tokens file writes it
|
|
246
|
+
// (`"accent": "#e11d48"`).
|
|
247
|
+
const quotedThing = /^(["'])(.+)\1$/.exec(t);
|
|
248
|
+
if (quotedThing) {
|
|
249
|
+
t = quotedThing[2].trim();
|
|
250
|
+
pasted = true;
|
|
251
|
+
// Its value in JSON's double quotes is the value (`"#e11d48"`); a font
|
|
252
|
+
// list keeps its own quotes.
|
|
253
|
+
if (typeof v === 'string' && !/font/i.test(t)) {
|
|
254
|
+
const jv = /^\s*"([^"]*)"\s*,?\s*$/.exec(v);
|
|
255
|
+
if (jv) v = jv[1];
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
// Dash-like characters where a token's two dashes go (an em or en dash,
|
|
259
|
+
// a non-breaking hyphen, a minus sign, a full-width hyphen, alone or
|
|
260
|
+
// mixed with hyphens): `--`.
|
|
261
|
+
t = t.replace(/^[-\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]+/, run =>
|
|
262
|
+
/[^-]/.test(run) ? '--' : run,
|
|
263
|
+
);
|
|
264
|
+
// A dash-like character inside a token's name is its hyphen.
|
|
265
|
+
if (t.startsWith('--'))
|
|
266
|
+
t = `--${t.slice(2).replace(/[\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]/g, '-')}`;
|
|
267
|
+
// A token typed as a var() reference.
|
|
268
|
+
const ref = /^var\(\s*(--[a-z0-9-]+)\s*\)$/i.exec(t);
|
|
269
|
+
if (ref) t = ref[1];
|
|
270
|
+
// A dial name written in another style (textSize, text_size, a dash-like
|
|
271
|
+
// character for its hyphen) is the dial.
|
|
272
|
+
if (
|
|
273
|
+
!t.startsWith('-') &&
|
|
274
|
+
/[_\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]|[a-z][A-Z]/.test(t)
|
|
275
|
+
) {
|
|
276
|
+
const dial = t
|
|
277
|
+
.replace(/[_\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]/g, '-')
|
|
278
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
|
|
279
|
+
.toLowerCase();
|
|
280
|
+
if (DIALS[dial]) t = dial;
|
|
281
|
+
}
|
|
282
|
+
if (typeof v === 'string') v = escapesMeant(t, v);
|
|
283
|
+
// A second declaration pasted after the first (`accent: red; text-size:
|
|
284
|
+
// 18`): the first is this setting.
|
|
285
|
+
/** A second declaration pasted after the first, which this does not set. @type {string | null} */
|
|
286
|
+
let dropped = null;
|
|
287
|
+
if (typeof v === 'string') {
|
|
288
|
+
const cut = withoutSecondDeclaration(v);
|
|
289
|
+
if (cut !== v)
|
|
290
|
+
dropped = v
|
|
291
|
+
.slice(cut.length)
|
|
292
|
+
.replace(/^\s*;\s*/, '')
|
|
293
|
+
.trim();
|
|
294
|
+
v = cut;
|
|
295
|
+
}
|
|
296
|
+
if (pasted && typeof v === 'string') {
|
|
297
|
+
v = v.replace(/\s*!\s*important\s*;?\s*$/i, '').replace(/;+\s*$/, '');
|
|
298
|
+
}
|
|
299
|
+
if (typeof v === 'string') v = withoutDebris(v);
|
|
300
|
+
// A font stack written as a JSON string (`"\\"Open Sans\\", serif"`): the
|
|
301
|
+
// stack it holds.
|
|
302
|
+
if (
|
|
303
|
+
typeof v === 'string' &&
|
|
304
|
+
/font/i.test(t) &&
|
|
305
|
+
/^"(?:[^"\\]|\\.)*"$/.test(v.trim()) &&
|
|
306
|
+
v.includes('\\"')
|
|
307
|
+
) {
|
|
308
|
+
try {
|
|
309
|
+
const inner = JSON.parse(v.trim());
|
|
310
|
+
if (typeof inner === 'string') v = inner;
|
|
311
|
+
} catch {
|
|
312
|
+
// Not a JSON string.
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
// A font stack with its quotes escaped, as it sits inside a JSON or JS
|
|
316
|
+
// string (`\"Inter\", sans-serif`): the stack.
|
|
317
|
+
if (typeof v === 'string' && /font/i.test(t) && /\\["']/.test(v))
|
|
318
|
+
v = v.replace(/\\(["'])/g, '$1');
|
|
319
|
+
// A font family's name never starts or ends with a space: French quotes
|
|
320
|
+
// (« Inter », with a no-break or narrow no-break space) hold the name.
|
|
321
|
+
if (typeof v === 'string' && /font/i.test(t)) v = trimInsideQuotes(v);
|
|
322
|
+
return {thing: t, value: v, ...(dropped ? {dropped} : {})};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* A font stack's pieces, split on the commas outside quotes, with each
|
|
327
|
+
* piece's name and whether it was quoted; null when a quote is left open.
|
|
328
|
+
* @param {string} v
|
|
329
|
+
* @returns {Array<{name: string, quoted: boolean}> | null}
|
|
330
|
+
*/
|
|
331
|
+
export function familyPieces(v) {
|
|
332
|
+
/** @type {Array<{name: string, quoted: boolean}>} */
|
|
333
|
+
const out = [];
|
|
334
|
+
let cur = '';
|
|
335
|
+
let quoted = false;
|
|
336
|
+
/** @type {string | null} */
|
|
337
|
+
let q = null;
|
|
338
|
+
let closed = false;
|
|
339
|
+
for (const c of String(v)) {
|
|
340
|
+
if (q) {
|
|
341
|
+
if (c === q) {
|
|
342
|
+
q = null;
|
|
343
|
+
closed = true;
|
|
344
|
+
} else cur += c;
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
if (c === '"' || c === "'") {
|
|
348
|
+
// A quote opening after text, or a second quoted name before a
|
|
349
|
+
// comma: not a stack of whole pieces.
|
|
350
|
+
if (cur.trim() || closed) return null;
|
|
351
|
+
q = c;
|
|
352
|
+
quoted = true;
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
// Text after a quoted name before its comma (`"Open Sans" serif`).
|
|
356
|
+
if (closed && c !== ',' && c.trim()) return null;
|
|
357
|
+
if (c === ',') {
|
|
358
|
+
out.push({name: cur.trim(), quoted});
|
|
359
|
+
cur = '';
|
|
360
|
+
quoted = false;
|
|
361
|
+
closed = false;
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
cur += c;
|
|
365
|
+
}
|
|
366
|
+
if (q) return null;
|
|
367
|
+
out.push({name: cur.trim(), quoted});
|
|
368
|
+
return out;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* The families a typed font stack names: a trailing comma and empty pieces
|
|
373
|
+
* dropped, and a stack wrapped in one more pair of the quotes it uses
|
|
374
|
+
* inside (`""SF Mono", Monaco, monospace"`, as a JSON string) unwrapped.
|
|
375
|
+
* Null when the stack does not read.
|
|
376
|
+
* @param {string} v
|
|
377
|
+
* @returns {string[] | null}
|
|
378
|
+
*/
|
|
379
|
+
export function familiesOf(v) {
|
|
380
|
+
let s = String(v)
|
|
381
|
+
.trim()
|
|
382
|
+
.replace(/,+\s*$/, '');
|
|
383
|
+
const w = /^(["'])([\s\S]*)\1$/.exec(s);
|
|
384
|
+
if (w && w[2].includes(w[1])) s = w[2].trim().replace(/,+\s*$/, '');
|
|
385
|
+
const pieces = familyPieces(s);
|
|
386
|
+
if (!pieces) return null;
|
|
387
|
+
return pieces.map(f => f.name).filter(Boolean);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* A value with the debris of a bad copy taken off: a leading `=` or `:`, a
|
|
392
|
+
* trailing comma (a JSON or CSS list), and a quote or a closing bracket left
|
|
393
|
+
* over at either end. A value without any is returned as it was.
|
|
394
|
+
* @param {string} v
|
|
395
|
+
* @returns {string}
|
|
396
|
+
*/
|
|
397
|
+
export function withoutDebris(v) {
|
|
398
|
+
let s = v.replace(/^\s*[=:]\s*/, '').replace(/\s*,+\s*$/, '');
|
|
399
|
+
// Quotes and brackets counted as CSS reads them: an escaped quote (\")
|
|
400
|
+
// is no quote, and a bracket inside a string is the string's.
|
|
401
|
+
const scan = (/** @type {string} */ t) => {
|
|
402
|
+
/** @type {Record<string, number>} */
|
|
403
|
+
const quotes = {'"': 0, "'": 0};
|
|
404
|
+
let open = 0;
|
|
405
|
+
let close = 0;
|
|
406
|
+
/** @type {string | null} */
|
|
407
|
+
let q = null;
|
|
408
|
+
for (let i = 0; i < t.length; i++) {
|
|
409
|
+
const c = t[i];
|
|
410
|
+
if (c === '\\') {
|
|
411
|
+
i++;
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
if (q) {
|
|
415
|
+
if (c === q) {
|
|
416
|
+
q = null;
|
|
417
|
+
quotes[c]++;
|
|
418
|
+
}
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
if (c === '"' || c === "'") {
|
|
422
|
+
q = c;
|
|
423
|
+
quotes[c]++;
|
|
424
|
+
} else if (c === '(') open++;
|
|
425
|
+
else if (c === ')') close++;
|
|
426
|
+
}
|
|
427
|
+
return {quotes, open, close};
|
|
428
|
+
};
|
|
429
|
+
for (const mark of /** @type {const} */ (['"', "'"])) {
|
|
430
|
+
if (scan(s).quotes[mark] % 2 === 1) {
|
|
431
|
+
if (s.endsWith(mark) && !s.endsWith(`\\${mark}`)) s = s.slice(0, -1);
|
|
432
|
+
else if (s.startsWith(mark)) s = s.slice(1);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
const {open, close} = scan(s);
|
|
436
|
+
if (close > open && s.endsWith(')')) s = s.slice(0, -1);
|
|
437
|
+
return s === v.trim() ? v : s.trim();
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* A value without a second declaration pasted after it (`#e11d48;
|
|
442
|
+
* text-size: 18`): cut at the first `;` outside quotes that a `name:`
|
|
443
|
+
* follows. A `;` inside a quoted string (`"Tip; note: x"`) is the string's.
|
|
444
|
+
* @param {string} v
|
|
445
|
+
* @returns {string}
|
|
446
|
+
*/
|
|
447
|
+
export function withoutSecondDeclaration(v) {
|
|
448
|
+
/** @type {string | null} */
|
|
449
|
+
let q = null;
|
|
450
|
+
for (let i = 0; i < v.length; i++) {
|
|
451
|
+
const c = v[i];
|
|
452
|
+
if (q) {
|
|
453
|
+
if (c === '\\') i++;
|
|
454
|
+
else if (c === q) q = null;
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
if (c === '"' || c === "'") q = c;
|
|
458
|
+
else if (
|
|
459
|
+
c === ';' &&
|
|
460
|
+
/^\s*[a-z-]+\s*:/i.test(v.slice(i + 1)) &&
|
|
461
|
+
// `light: red; dark: blue` is one value for two modes.
|
|
462
|
+
!/^\s*(?:light|dark)\s*:/i.test(v.slice(i + 1))
|
|
463
|
+
)
|
|
464
|
+
return v.slice(0, i);
|
|
465
|
+
}
|
|
466
|
+
return v;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* A saved dial value read as typed input is (invisible or stray spaces,
|
|
471
|
+
* quotes around it, a pasted `corners: pill`, a trailing `;`, other
|
|
472
|
+
* letters): the value the dial may have meant.
|
|
473
|
+
* @param {string} dial
|
|
474
|
+
* @param {string} value
|
|
475
|
+
* @returns {string}
|
|
476
|
+
*/
|
|
477
|
+
export function cleanDialValue(dial, value) {
|
|
478
|
+
const read = normalizeEntry(dial, value).value ?? '';
|
|
479
|
+
return read
|
|
480
|
+
.trim()
|
|
481
|
+
.replace(new RegExp(`^${dial.replace(/-/g, '\\-')}\\s*[:=]\\s*`, 'i'), '')
|
|
482
|
+
.replace(/\s*!\s*important\s*$/i, '')
|
|
483
|
+
.replace(/;+\s*$/, '')
|
|
484
|
+
.replace(/^(["'])(.*)\1$/, '$2')
|
|
485
|
+
.trim()
|
|
486
|
+
.toLowerCase();
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* A value with the spaces just inside each straight-quoted string taken off
|
|
491
|
+
* (`" Inter "` is "Inter"), for a font family, whose name holds none there.
|
|
492
|
+
* @param {string} v
|
|
493
|
+
* @returns {string}
|
|
494
|
+
*/
|
|
495
|
+
export function trimInsideQuotes(v) {
|
|
496
|
+
const SP = '[\\s\\u00A0\\u202F\\u2000-\\u200A\\u3000]*';
|
|
497
|
+
return v.replace(
|
|
498
|
+
new RegExp(`(["'])${SP}((?:(?!\\1)[^\\\\]|\\\\.)*?)${SP}\\1`, 'g'),
|
|
499
|
+
'$1$2$1',
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* The other setting a dial's value is labeled with, when pasted with a
|
|
505
|
+
* label that names a different dial or a token (`neutral: #64748b` given
|
|
506
|
+
* to accent): that label and the value after it, else null. A label that
|
|
507
|
+
* names this dial, or a CSS property (`color: #e11d48`), is no other
|
|
508
|
+
* setting.
|
|
509
|
+
* @param {string} dial
|
|
510
|
+
* @param {string} value
|
|
511
|
+
* @returns {{thing: string, value: string} | null}
|
|
512
|
+
*/
|
|
513
|
+
export function otherSettingLabel(dial, value) {
|
|
514
|
+
const m =
|
|
515
|
+
/^\s*["']?([a-z][a-z0-9_-]*|--[a-z0-9-]+)["']?\s*[:=]\s*["']?(.+?)["']?\s*;?\s*$/i.exec(
|
|
516
|
+
String(value),
|
|
517
|
+
);
|
|
518
|
+
if (!m) return null;
|
|
519
|
+
// A label in camelCase or snake_case (textSize, text_size) is the dial.
|
|
520
|
+
const label = m[1]
|
|
521
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
|
|
522
|
+
.replace(/_/g, '-')
|
|
523
|
+
.toLowerCase();
|
|
524
|
+
if (label === dial) return null;
|
|
525
|
+
if (!DIALS[label] && !label.startsWith('--')) return null;
|
|
526
|
+
return {thing: label, value: m[2]};
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* A light and a dark color typed as a pair: labeled (`light: red; dark:
|
|
531
|
+
* blue`), a JSON object (`{"light": "red", "dark": "blue"}`), or two colors
|
|
532
|
+
* with a slash or a comma between (`red / blue`, `red, blue`). Null when the
|
|
533
|
+
* value is no such pair; the caller checks that both are colors.
|
|
534
|
+
* @param {string} value
|
|
535
|
+
* @returns {{light: string, dark: string} | null}
|
|
536
|
+
*/
|
|
537
|
+
export function colorPair(value) {
|
|
538
|
+
const v = String(value).trim();
|
|
539
|
+
const unq = (/** @type {string} */ x) =>
|
|
540
|
+
x
|
|
541
|
+
.trim()
|
|
542
|
+
.replace(/^(["'])(.*)\1$/, '$2')
|
|
543
|
+
.trim();
|
|
544
|
+
const labeled =
|
|
545
|
+
/^\{?\s*["']?(light|dark)["']?\s*[:=]\s*(.+?)\s*(?:[;,]\s*|\s+)["']?(light|dark)["']?\s*[:=]\s*(.+?)\s*;?\s*\}?$/i.exec(
|
|
546
|
+
v,
|
|
547
|
+
);
|
|
548
|
+
if (labeled && labeled[1].toLowerCase() !== labeled[3].toLowerCase()) {
|
|
549
|
+
const a = unq(labeled[2]);
|
|
550
|
+
const b = unq(labeled[4]);
|
|
551
|
+
return labeled[1].toLowerCase() === 'light'
|
|
552
|
+
? {light: a, dark: b}
|
|
553
|
+
: {light: b, dark: a};
|
|
554
|
+
}
|
|
555
|
+
const two = /^([^\s,/()]+)\s*(?:\s\/\s|,)\s*([^\s,/()]+)$/.exec(v);
|
|
556
|
+
if (two) return {light: unq(two[1]), dark: unq(two[2])};
|
|
557
|
+
return null;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* A value without the debris a copy brings along: a trailing `;`, a
|
|
562
|
+
* `!important`, a comment. One of the exact cleanups a repair may offer.
|
|
563
|
+
* @param {string} v
|
|
564
|
+
* @returns {string}
|
|
565
|
+
*/
|
|
566
|
+
export function withoutCopyDebris(v) {
|
|
567
|
+
return String(v)
|
|
568
|
+
.replace(/\/\*[\s\S]*?\*\//g, '')
|
|
569
|
+
.replace(/\s*!\s*important\b/gi, '')
|
|
570
|
+
.replace(/[\s;]+$/, '')
|
|
571
|
+
.trim();
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Words with a documented reading on a CSS property: the exact value each
|
|
576
|
+
* one means. A repair may offer these for the word typed.
|
|
577
|
+
* @type {Record<string, Record<string, string>>}
|
|
578
|
+
*/
|
|
579
|
+
export const PROPERTY_WORDS = wordTable({
|
|
580
|
+
opacity: {
|
|
581
|
+
half: '0.5',
|
|
582
|
+
'half-transparent': '0.5',
|
|
583
|
+
opaque: '1',
|
|
584
|
+
solid: '1',
|
|
585
|
+
},
|
|
586
|
+
'text-transform': {
|
|
587
|
+
caps: 'uppercase',
|
|
588
|
+
'all-caps': 'uppercase',
|
|
589
|
+
upper: 'uppercase',
|
|
590
|
+
lower: 'lowercase',
|
|
591
|
+
title: 'capitalize',
|
|
592
|
+
'no-caps': 'none',
|
|
593
|
+
},
|
|
594
|
+
'border-radius': {
|
|
595
|
+
round: '9999px',
|
|
596
|
+
rounded: '9999px',
|
|
597
|
+
pill: '9999px',
|
|
598
|
+
circle: '50%',
|
|
599
|
+
square: '0',
|
|
600
|
+
},
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
/** Weight words beyond the headings dial's, as their numbers. */
|
|
604
|
+
export const WEIGHT_WORDS = wordTable(
|
|
605
|
+
/** @type {Record<string, string>} */ ({
|
|
606
|
+
thin: '100',
|
|
607
|
+
extralight: '200',
|
|
608
|
+
light: '300',
|
|
609
|
+
regular: '400',
|
|
610
|
+
normal: '400',
|
|
611
|
+
medium: '500',
|
|
612
|
+
semibold: '600',
|
|
613
|
+
bold: '700',
|
|
614
|
+
heavy: '800',
|
|
615
|
+
extrabold: '800',
|
|
616
|
+
ultrabold: '800',
|
|
617
|
+
black: '900',
|
|
618
|
+
}),
|
|
619
|
+
);
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Tailwind's radius classes, as the corners value each one is.
|
|
623
|
+
* @type {Record<string, string>}
|
|
624
|
+
*/
|
|
625
|
+
export const TAILWIND_RADIUS = wordTable({
|
|
626
|
+
'rounded-none': 'square',
|
|
627
|
+
'rounded-sm': '2px',
|
|
628
|
+
rounded: '4px',
|
|
629
|
+
'rounded-md': '6px',
|
|
630
|
+
'rounded-lg': '8px',
|
|
631
|
+
'rounded-xl': '12px',
|
|
632
|
+
'rounded-2xl': '16px',
|
|
633
|
+
'rounded-3xl': '24px',
|
|
634
|
+
'rounded-full': 'pill',
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
/** Words for more or less room, as the spacing rung each one means. */
|
|
638
|
+
export const DENSITY_WORDS = wordTable(
|
|
639
|
+
/** @type {Record<string, string>} */ ({
|
|
640
|
+
airy: 'roomy',
|
|
641
|
+
loose: 'roomy',
|
|
642
|
+
relaxed: 'roomy',
|
|
643
|
+
comfortable: 'roomy',
|
|
644
|
+
open: 'roomy',
|
|
645
|
+
sparse: 'roomy',
|
|
646
|
+
generous: 'roomy',
|
|
647
|
+
dense: 'compact',
|
|
648
|
+
cramped: 'compact',
|
|
649
|
+
condensed: 'compact',
|
|
650
|
+
packed: 'compact',
|
|
651
|
+
}),
|
|
652
|
+
);
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* The words of a typed value, as a repair may carry them: lower case, with
|
|
656
|
+
* quotes, escapes (`\u0020`), a `#` or `0x` before a hex, and a unit after
|
|
657
|
+
* a number taken off; and each run of neighbor words joined (`dark red` is
|
|
658
|
+
* also `darkred`).
|
|
659
|
+
* @param {string} typed
|
|
660
|
+
* @returns {Set<string>}
|
|
661
|
+
*/
|
|
662
|
+
function typedWords(typed) {
|
|
663
|
+
const text = String(typed)
|
|
664
|
+
.replace(/\\(?:u\{?([0-9a-f]{4,6})\}?|x([0-9a-f]{2}))/gi, (_, u, x) =>
|
|
665
|
+
String.fromCodePoint(parseInt(u ?? x, 16)),
|
|
666
|
+
)
|
|
667
|
+
.toLowerCase();
|
|
668
|
+
const words = text.match(/[a-z0-9.%]+/g) ?? [];
|
|
669
|
+
/** @type {Set<string>} */
|
|
670
|
+
const out = new Set();
|
|
671
|
+
const add = (/** @type {string} */ w) => {
|
|
672
|
+
out.add(w);
|
|
673
|
+
out.add(w.replace(/^0x/, ''));
|
|
674
|
+
const n = /^([0-9.]+)(px|rem|em|%)?$/.exec(w);
|
|
675
|
+
if (n) out.add(n[1]);
|
|
676
|
+
};
|
|
677
|
+
words.forEach(add);
|
|
678
|
+
for (let i = 0; i < words.length; i += 1)
|
|
679
|
+
for (let j = i + 2; j <= Math.min(words.length, i + 4); j += 1)
|
|
680
|
+
out.add(words.slice(i, j).join(''));
|
|
681
|
+
return out;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Whether a suggested value carries what was typed: each of its words and
|
|
686
|
+
* numbers is a word of the typed text (cleaned: case, quotes, escapes), or
|
|
687
|
+
* neighbor words of it joined, apart from a unit added to a bare number
|
|
688
|
+
* and the words of the CSS form it is written in (`light-dark()`, `var()`,
|
|
689
|
+
* `calc()`). A word only inside a typed word does not count (`round` is not
|
|
690
|
+
* in `rounded-lg`, `red` is not `darkred`).
|
|
691
|
+
* @param {string} offered
|
|
692
|
+
* @param {string} typed
|
|
693
|
+
*/
|
|
694
|
+
export function carriesTyped(offered, typed) {
|
|
695
|
+
const have = typedWords(typed);
|
|
696
|
+
// The words of the CSS form a value is written in.
|
|
697
|
+
const FORM = new Set([
|
|
698
|
+
'light',
|
|
699
|
+
'dark',
|
|
700
|
+
'var',
|
|
701
|
+
'calc',
|
|
702
|
+
'px',
|
|
703
|
+
'rem',
|
|
704
|
+
'em',
|
|
705
|
+
'linear',
|
|
706
|
+
'radial',
|
|
707
|
+
'conic',
|
|
708
|
+
'gradient',
|
|
709
|
+
]);
|
|
710
|
+
return (
|
|
711
|
+
String(offered)
|
|
712
|
+
.toLowerCase()
|
|
713
|
+
.match(/[a-z0-9.%]+/g) ?? []
|
|
714
|
+
)
|
|
715
|
+
.map(w => w.replace(/^([0-9.]+)(px|rem|em)$/, '$1'))
|
|
716
|
+
.filter(w => !FORM.has(w))
|
|
717
|
+
.every(w => have.has(w));
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/**
|
|
721
|
+
* Whether a repair may offer `offered` for `typed`: only through the
|
|
722
|
+
* allowlist of exact readings that keep the meaning. The value carries
|
|
723
|
+
* what was typed (quotes, labels, debris and a unit's absence aside), or
|
|
724
|
+
* is the documented reading of the word typed (a Tailwind radius class, a
|
|
725
|
+
* property word, a weight word, a density word, a share of one as a
|
|
726
|
+
* percent, rounding said with a negation as square). Anything else (a
|
|
727
|
+
* stock example, a reset, a nearest spelling) is not offered.
|
|
728
|
+
* @param {string} offered
|
|
729
|
+
* @param {string} typed
|
|
730
|
+
*/
|
|
731
|
+
export function allowedOffer(offered, typed) {
|
|
732
|
+
const o = String(offered)
|
|
733
|
+
.trim()
|
|
734
|
+
.replace(/^(["'])(.*)\1$/, '$2')
|
|
735
|
+
.toLowerCase();
|
|
736
|
+
const t = String(typed)
|
|
737
|
+
.trim()
|
|
738
|
+
.replace(/^(["'])(.*)\1$/, '$2')
|
|
739
|
+
.replace(/;+\s*$/, '')
|
|
740
|
+
.trim()
|
|
741
|
+
.toLowerCase();
|
|
742
|
+
if (carriesTyped(o, t)) return true;
|
|
743
|
+
const key = t.replace(/^[a-z-]+\s*:\s*/, '');
|
|
744
|
+
// A documented reading of the whole value or of one of its words.
|
|
745
|
+
const keys = [
|
|
746
|
+
key,
|
|
747
|
+
key.replace(/\s+/g, '-'),
|
|
748
|
+
key.replace(/[\s-]+/g, ''),
|
|
749
|
+
...key.split(/\s+/),
|
|
750
|
+
];
|
|
751
|
+
if (keys.some(k => READINGS.get(k)?.has(o))) return true;
|
|
752
|
+
if (numbersEqual(o, t)) return true;
|
|
753
|
+
// An rgb() color typed, as its opaque hex.
|
|
754
|
+
const rgb = /rgba?\(\s*(\d+)[\s,]+(\d+)[\s,]+(\d+)/.exec(t);
|
|
755
|
+
if (
|
|
756
|
+
rgb &&
|
|
757
|
+
o ===
|
|
758
|
+
`#${rgb
|
|
759
|
+
.slice(1, 4)
|
|
760
|
+
.map(x => Math.min(255, Number(x)).toString(16).padStart(2, '0'))
|
|
761
|
+
.join('')}`
|
|
762
|
+
)
|
|
763
|
+
return true;
|
|
764
|
+
// A CSS color name typed, as its hex.
|
|
765
|
+
if (
|
|
766
|
+
/^#[0-9a-f]{3,8}$/.test(o) &&
|
|
767
|
+
(t.match(/[a-z]+/g) ?? []).some(w => namedColorHex(w) === o)
|
|
768
|
+
)
|
|
769
|
+
return true;
|
|
770
|
+
// A radius at or below zero is square; at or past half, or 1000px, pill.
|
|
771
|
+
const len = /^([+-]?\d*\.?\d+)(px|%)?$/.exec(key);
|
|
772
|
+
if (len) {
|
|
773
|
+
const n = Number(len[1]);
|
|
774
|
+
if (o === 'square' && n <= 0) return true;
|
|
775
|
+
if (o === 'pill' && ((len[2] === '%' && n >= 50) || n >= 1000)) return true;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
// Rounding said with a negation (not round, unrounded, rounded-none).
|
|
779
|
+
if (
|
|
780
|
+
o === 'square' &&
|
|
781
|
+
/round/.test(key) &&
|
|
782
|
+
/(^|[\s-])(not|no|non|without|never|less)([\s-]|$)|^un|-none$/.test(key)
|
|
783
|
+
)
|
|
784
|
+
return true;
|
|
785
|
+
return false;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* Documented readings: each typed word and the exact value it means,
|
|
790
|
+
* registered by the table that documents it (a dial's words, a Tailwind
|
|
791
|
+
* class, a property word). A repair may offer only these for a word that
|
|
792
|
+
* was not typed as the value.
|
|
793
|
+
* @type {Map<string, Set<string>>}
|
|
794
|
+
*/
|
|
795
|
+
const READINGS = new Map();
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* Register a documented table's readings with the repair allowlist.
|
|
799
|
+
* @param {Iterable<[string, string]>} pairs typed word, value it means
|
|
800
|
+
*/
|
|
801
|
+
export function documentReadings(pairs) {
|
|
802
|
+
for (const [w, v] of pairs) {
|
|
803
|
+
const k = String(w).trim().toLowerCase();
|
|
804
|
+
if (!READINGS.has(k)) READINGS.set(k, new Set());
|
|
805
|
+
/** @type {Set<string>} */ (READINGS.get(k)).add(String(v).toLowerCase());
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
/**
|
|
810
|
+
* Documented numeric tables (a rung and its number), each for the settings
|
|
811
|
+
* that document it, registered where it is defined: a number typed for one
|
|
812
|
+
* of those settings may be offered as its nearest rung.
|
|
813
|
+
* @type {Array<{things: string[], table: Array<[string, number]>, domain: [number, number]}>}
|
|
814
|
+
*/
|
|
815
|
+
const NUMERIC_TABLES = [];
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* Register a documented numeric table with the repair allowlist.
|
|
819
|
+
* @param {string[]} things the settings whose values the table reads (a
|
|
820
|
+
* name ending in `*` names every setting it starts)
|
|
821
|
+
* @param {Array<[string, number]>} table
|
|
822
|
+
* @param {[number, number]} [domain] the numbers the table reads (by
|
|
823
|
+
* default every number)
|
|
824
|
+
*/
|
|
825
|
+
export function documentNumericTable(
|
|
826
|
+
things,
|
|
827
|
+
table,
|
|
828
|
+
domain = [-Infinity, Infinity],
|
|
829
|
+
) {
|
|
830
|
+
if (table.length) NUMERIC_TABLES.push({things, table, domain});
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* Documented ranges: a number typed past either end of a setting's range
|
|
835
|
+
* may be offered as that end.
|
|
836
|
+
* @type {Array<{things: string[], min: number, max: number}>}
|
|
837
|
+
*/
|
|
838
|
+
const RANGES = [];
|
|
839
|
+
|
|
840
|
+
/**
|
|
841
|
+
* Register a setting's documented range with the repair allowlist.
|
|
842
|
+
* @param {string[]} things as for documentNumericTable
|
|
843
|
+
* @param {number} min
|
|
844
|
+
* @param {number} max
|
|
845
|
+
*/
|
|
846
|
+
export function documentRange(things, min, max) {
|
|
847
|
+
RANGES.push({things, min, max});
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
/**
|
|
851
|
+
* Whether a registered setting list names a setting.
|
|
852
|
+
* @param {string[]} things
|
|
853
|
+
* @param {string} key
|
|
854
|
+
*/
|
|
855
|
+
const names = (things, key) =>
|
|
856
|
+
things.some(t =>
|
|
857
|
+
t.endsWith('*') ? key.startsWith(t.slice(0, -1)) : t === key,
|
|
858
|
+
);
|
|
859
|
+
|
|
860
|
+
/**
|
|
861
|
+
* Values a setting documents as its own fallback or other direction, which
|
|
862
|
+
* a refusal may offer whatever was typed: no base, the modes, a step the
|
|
863
|
+
* other way.
|
|
864
|
+
* @type {Record<string, string[]>}
|
|
865
|
+
*/
|
|
866
|
+
const DECLARED_VALUES = wordTable({
|
|
867
|
+
base: ['none'],
|
|
868
|
+
mode: ['both', 'light-only', 'dark-only'],
|
|
869
|
+
});
|
|
870
|
+
|
|
871
|
+
/**
|
|
872
|
+
* The one gate a suggested line's value goes through. Its value comes from
|
|
873
|
+
* one of the declared sources: (1) the value typed, under the exact
|
|
874
|
+
* transformations of allowedOffer; (2) a documented table registered with
|
|
875
|
+
* documentReadings or documentNumericTable; (3) the theme's own saved
|
|
876
|
+
* values (a keep or unlock line); (4) the setting's documented fallback or
|
|
877
|
+
* other-direction step; (5) the correction of a typo of the value typed
|
|
878
|
+
* (typoOf: underlin is underline).
|
|
879
|
+
* @param {{offered: string, typed: string, thing: string, saved?: string}} line
|
|
880
|
+
*/
|
|
881
|
+
export function allowedLine({offered, typed, thing, saved = ''}) {
|
|
882
|
+
const o = String(offered)
|
|
883
|
+
.trim()
|
|
884
|
+
.replace(/^(["'])(.*)\1$/, '$2')
|
|
885
|
+
.toLowerCase();
|
|
886
|
+
if (allowedOffer(o, typed)) return true;
|
|
887
|
+
if (saved && allowedOffer(o, saved)) return true;
|
|
888
|
+
const key = String(thing).toLowerCase();
|
|
889
|
+
if (Object.hasOwn(DECLARED_VALUES, key) && DECLARED_VALUES[key].includes(o))
|
|
890
|
+
return true;
|
|
891
|
+
const t = String(typed).trim().toLowerCase();
|
|
892
|
+
// A step (more, less) is worked out from the theme as it is: the value
|
|
893
|
+
// it gives is the theme's own, never a stock one.
|
|
894
|
+
if (/(^|\s)(more|less)$/.test(t)) return true;
|
|
895
|
+
// A number typed for a setting with a documented numeric table, read as
|
|
896
|
+
// the rung nearest it (900 is the heavy weight, 1.3 the editorial type
|
|
897
|
+
// scale, 101 the most vivid 100).
|
|
898
|
+
for (const {things, table, domain} of NUMERIC_TABLES)
|
|
899
|
+
if (names(things, key))
|
|
900
|
+
for (const m of t.matchAll(/\d*\.?\d+/g)) {
|
|
901
|
+
const n = Number(m[0]);
|
|
902
|
+
if (n < domain[0] || n > domain[1]) continue;
|
|
903
|
+
const [rung] = table.reduce((a, b) =>
|
|
904
|
+
Math.abs(b[1] - n) < Math.abs(a[1] - n) ? b : a,
|
|
905
|
+
);
|
|
906
|
+
if (rung === o) return true;
|
|
907
|
+
}
|
|
908
|
+
// A number past a documented range, as the end it passed.
|
|
909
|
+
for (const {things, min, max} of RANGES)
|
|
910
|
+
if (names(things, key))
|
|
911
|
+
for (const m of t.matchAll(/-?\d*\.?\d+/g)) {
|
|
912
|
+
const n = Number(m[0]);
|
|
913
|
+
if ((n > max && o === String(max)) || (n < min && o === String(min)))
|
|
914
|
+
return true;
|
|
915
|
+
}
|
|
916
|
+
return typoOf(t, o);
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
documentReadings([
|
|
920
|
+
['more', 'less'],
|
|
921
|
+
['less', 'more'],
|
|
922
|
+
]);
|
|
923
|
+
documentReadings(Object.entries(TAILWIND_RADIUS));
|
|
924
|
+
documentReadings(Object.values(PROPERTY_WORDS).flatMap(m => Object.entries(m)));
|
|
925
|
+
documentReadings(Object.entries(WEIGHT_WORDS));
|
|
926
|
+
documentReadings(Object.entries(WEIGHT_WORDS).map(([w, n]) => [n, w]));
|
|
927
|
+
documentReadings(Object.entries(DENSITY_WORDS));
|
|
928
|
+
|
|
929
|
+
/**
|
|
930
|
+
* Whether every number of `offered` is a number of `typed` written another
|
|
931
|
+
* exact way: other digits (full-width), a comma for the decimal point, an
|
|
932
|
+
* exponent, rem or em as px (x16), pt as px (x4/3), a percent as a share
|
|
933
|
+
* of one, or a share of one as a percent. The rest of `offered` is units
|
|
934
|
+
* and form words only.
|
|
935
|
+
* @param {string} offered
|
|
936
|
+
* @param {string} typed
|
|
937
|
+
*/
|
|
938
|
+
function numbersEqual(offered, typed) {
|
|
939
|
+
const NUM = /[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?/gi;
|
|
940
|
+
const rest = offered
|
|
941
|
+
.replace(NUM, '')
|
|
942
|
+
.replace(/px|rem|em|%|[\s(),/*+-]|calc|var/g, '');
|
|
943
|
+
const want = offered.match(NUM);
|
|
944
|
+
if (!want || rest) return false;
|
|
945
|
+
// A letter O written for a zero inside a number (7OO) is the zero.
|
|
946
|
+
const nfkc = typed
|
|
947
|
+
.normalize('NFKC')
|
|
948
|
+
.replace(/(\d)([oO]+)/g, (_, d, os) => d + '0'.repeat(os.length));
|
|
949
|
+
// A comma is a decimal point (1,5rem), or before three digits a
|
|
950
|
+
// thousands mark (1,000): both readings.
|
|
951
|
+
const text = `${nfkc.replace(/(\d),(\d)/g, '$1.$2')} ${nfkc.replace(/(\d),(\d{3})\b/g, '$1$2')}`;
|
|
952
|
+
/** @type {Set<string>} */
|
|
953
|
+
const have = new Set();
|
|
954
|
+
const r2 = (/** @type {number} */ x) => String(Math.round(x * 100) / 100);
|
|
955
|
+
for (const m of text.matchAll(
|
|
956
|
+
/([+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?)\s*(px|rem|em|pt|%)?/gi,
|
|
957
|
+
)) {
|
|
958
|
+
const n = Number(m[1]);
|
|
959
|
+
if (!Number.isFinite(n)) continue;
|
|
960
|
+
const unit = (m[2] ?? '').toLowerCase();
|
|
961
|
+
have.add(r2(n));
|
|
962
|
+
if (unit === 'rem' || unit === 'em') have.add(r2(n * 16));
|
|
963
|
+
if (unit === 'pt') have.add(r2((n * 4) / 3));
|
|
964
|
+
// A percent as a share of one, or of the 16px base size.
|
|
965
|
+
if (unit === '%') {
|
|
966
|
+
have.add(r2(n / 100));
|
|
967
|
+
have.add(r2((n * 16) / 100));
|
|
968
|
+
}
|
|
969
|
+
if (!unit && n > 0 && n < 1) have.add(r2(n * 100));
|
|
970
|
+
}
|
|
971
|
+
// A calc() of lengths that adds and subtracts only, worked out in px.
|
|
972
|
+
const sum = /^\s*calc\(([^()]*)\)/i.exec(nfkc);
|
|
973
|
+
// One length times or over a plain number (calc(4px*2)).
|
|
974
|
+
const scale = sum
|
|
975
|
+
? /^\s*(\d*\.?\d+)\s*(px|rem|em)?\s*([*/])\s*(\d*\.?\d+)\s*$/i.exec(sum[1])
|
|
976
|
+
: null;
|
|
977
|
+
if (scale) {
|
|
978
|
+
const a = Number(scale[1]) * (/r?em/i.test(scale[2] ?? '') ? 16 : 1);
|
|
979
|
+
have.add(
|
|
980
|
+
r2(scale[3] === '*' ? a * Number(scale[4]) : a / Number(scale[4])),
|
|
981
|
+
);
|
|
982
|
+
}
|
|
983
|
+
if (sum && !/[*/]/.test(sum[1])) {
|
|
984
|
+
let total = 0;
|
|
985
|
+
for (const m of sum[1].matchAll(/([+-]?)\s*(\d*\.?\d+)\s*(px|rem|em)?/gi)) {
|
|
986
|
+
const x = Number(m[2]) * (/r?em/i.test(m[3] ?? '') ? 16 : 1);
|
|
987
|
+
total += m[1] === '-' ? -x : x;
|
|
988
|
+
}
|
|
989
|
+
have.add(r2(total));
|
|
990
|
+
}
|
|
991
|
+
return want.every(
|
|
992
|
+
w => have.has(r2(Math.abs(Number(w)))) || have.has(r2(Number(w))),
|
|
993
|
+
);
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
/**
|
|
997
|
+
* Whether a repair may offer `offered` for `typed`: the one gate every
|
|
998
|
+
* suggested value goes through (the allowlist above).
|
|
999
|
+
* @param {string} offered
|
|
1000
|
+
* @param {string} typed
|
|
1001
|
+
*/
|
|
1002
|
+
export const offersTyped = (offered, typed) => allowedOffer(offered, typed);
|
|
1003
|
+
|
|
1004
|
+
/**
|
|
1005
|
+
* Whether a typed word is a typo of a word: both words of four letters or
|
|
1006
|
+
* more, within one edit for a typed word of five letters or fewer and two
|
|
1007
|
+
* for a longer one (`nnone` is none, `normall` normal; `line` is no none).
|
|
1008
|
+
* @param {string} typed
|
|
1009
|
+
* @param {string} word
|
|
1010
|
+
*/
|
|
1011
|
+
export function typoOf(typed, word) {
|
|
1012
|
+
const t = String(typed).trim().toLowerCase();
|
|
1013
|
+
const w = String(word).trim().toLowerCase();
|
|
1014
|
+
return (
|
|
1015
|
+
/^[a-z][a-z-]{3,}$/.test(t) && /^[a-z][a-z-]{3,}$/.test(w) && typoNear(t, w)
|
|
1016
|
+
);
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/**
|
|
1020
|
+
* The edits a typo of a typed word may hold: one for a word of five letters
|
|
1021
|
+
* or fewer, two for a longer one. Every typo reading of the editor (a value,
|
|
1022
|
+
* a setting, a part, a state, a theme or token name) uses this limit.
|
|
1023
|
+
* @param {string} typed
|
|
1024
|
+
*/
|
|
1025
|
+
export const typoLimit = typed => (String(typed).trim().length <= 5 ? 1 : 2);
|
|
1026
|
+
|
|
1027
|
+
/**
|
|
1028
|
+
* Whether `word` is within the typo limit of `typed` (two letters swapped
|
|
1029
|
+
* count as one edit, as typed: flxe is flex).
|
|
1030
|
+
* @param {string} typed
|
|
1031
|
+
* @param {string} word
|
|
1032
|
+
*/
|
|
1033
|
+
export function typoNear(typed, word) {
|
|
1034
|
+
const t = String(typed).trim().toLowerCase();
|
|
1035
|
+
const limit = typoLimit(t);
|
|
1036
|
+
return (
|
|
1037
|
+
levenshteinDistance(t, String(word).trim().toLowerCase(), limit, {
|
|
1038
|
+
swaps: true,
|
|
1039
|
+
}) <= limit
|
|
1040
|
+
);
|
|
1041
|
+
}
|