@astryxdesign/cli 0.7.18-canary.gfca8508 → 0.7.18
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/CHANGELOG.md +185 -19
- package/README.md +19 -19
- package/api/component/_adapter.d.mts +2 -1
- package/api/component/_adapter.mjs +2 -1
- package/api/docs/_adapter.d.mts +6 -5
- package/api/docs/_adapter.mjs +36 -5
- package/api/doctor/_adapter.d.mts +6 -3
- package/api/doctor/_adapter.mjs +13 -5
- package/api/doctor/doctor.mjs +2 -2
- package/api/doctor/font-packages.d.mts +36 -0
- package/api/doctor/font-packages.mjs +124 -0
- package/api/doctor/theme-checks.mjs +110 -49
- package/api/experimental.d.mts +0 -2
- package/api/experimental.mjs +0 -12
- package/api/icon/_adapter.d.mts +1 -1
- package/api/icon/_adapter.mjs +1 -0
- package/api/icon/_authoring.mjs +17 -8
- package/api/icon/_default-look.mjs +15 -15
- package/api/icon/add/add.mjs +9 -4
- package/api/icon/exportIconIndex.doc.mjs +1 -1
- package/api/icon/icon.doc.mjs +1 -1
- package/api/icon/icon.type.d.mts +33 -33
- package/api/icon/icon.type.mjs +45 -45
- package/api/icon/iconAdd.doc.mjs +1 -1
- package/api/icon/iconNewSet.doc.mjs +3 -3
- package/api/icon/iconUse.doc.mjs +5 -5
- package/api/icon/rankIconIndex.doc.mjs +1 -1
- package/api/icon/use/use.mjs +4 -4
- package/api/init/run/run.mjs +0 -1
- package/api/integration/add-helpers.mjs +1 -1
- package/api/render/_adapter.mjs +7 -1
- package/api/search/ORDERING.md +1 -1
- package/api/search/_candidate-score.mjs +20 -0
- package/api/search/_words.d.mts +8 -0
- package/api/search/_words.mjs +15 -0
- package/api/template/template.type.d.mts +2 -2
- package/api/template/template.type.mjs +2 -2
- package/api/theme/_adapter.mjs +19 -11
- package/api/theme/build/build.d.mts +19 -51
- package/api/theme/build/build.mjs +195 -189
- package/api/theme/build/font-warning.d.mts +19 -2
- package/api/theme/build/font-warning.mjs +46 -5
- package/api/theme/generateTonalPalette.doc.mjs +1 -1
- package/api/theme/palette/generate/derive-look.mjs +108 -37
- package/api/theme/palette/generate/mapping.mjs +2 -0
- package/api/theme/template/template.d.mts +0 -17
- package/api/theme/template/template.mjs +1 -42
- package/api/theme/theme.type.d.mts +5 -4
- package/api/theme/theme.type.mjs +5 -4
- package/api/theme/themePaletteGenerate.doc.mjs +1 -1
- package/api/theme/themeRemove.doc.mjs +1 -1
- package/assets/codemods/transforms/v0.7.0/preserve-bottom-sheet-content-padding.mjs +3 -1
- package/assets/component-list.generated.json +1 -1
- package/assets/discover/official-packages.json +26 -2
- package/assets/docs/author-a-theme.doc.mjs +6 -5
- package/assets/docs/browser-support.doc.mjs +1 -0
- package/assets/docs/icons.doc.mjs +8 -7
- package/assets/docs/motion.doc.mjs +46 -0
- package/assets/docs/theme.doc.dense.mjs +6 -17
- package/assets/docs/theme.doc.mjs +6 -16
- package/assets/docs/theme.doc.zh.mjs +6 -16
- package/assets/docs/tree/ci-and-containers.doc.mjs +1 -1
- package/assets/docs/tree/font-setup.doc.mjs +30 -0
- package/assets/docs/tree/icon-sets.doc.mjs +1 -1
- package/assets/docs/tree/template-icons.doc.mjs +3 -3
- package/assets/docs/tree/tokens-color.doc.mjs +5 -0
- package/assets/docs/tree/tokens.doc.mjs +4 -3
- package/assets/docs/tree/updates.doc.mjs +1 -1
- package/assets/docs-routes.generated.json +1 -1
- package/assets/docs.generated.json +2 -2
- package/assets/icons/definitions/drag.doc.mjs +1 -0
- package/assets/search-catalog.generated.json +2 -2
- package/assets/self-audit.generated.json +1 -1
- package/assets/templates/blocks/components/Card/SelectableCardElevated.doc.mjs +1 -1
- package/assets/templates/blocks/components/Card/SelectableCardElevated.tsx +20 -10
- package/assets/templates/blocks/components/Card/SelectableCardShowcase.doc.mjs +1 -1
- package/assets/templates/blocks/components/Card/SelectableCardShowcase.tsx +36 -16
- package/assets/templates/blocks/components/ChatComposerInput/ChatComposerInputMentionTrigger.tsx +6 -0
- package/assets/templates/blocks/components/ChatComposerInput/ChatComposerInputMultipleTriggers.tsx +6 -0
- package/assets/templates/blocks/components/Hooks/useIndicatorFocusRingHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Hooks/useIndicatorFocusRingHookUsage.tsx +63 -0
- package/assets/templates/blocks/components/Hooks/useLongPressHookUsage.doc.mjs +29 -0
- package/assets/templates/blocks/components/Hooks/useLongPressHookUsage.tsx +237 -0
- package/assets/templates/blocks/components/SideNav/SideNavShowcase.tsx +10 -72
- package/assets/templates/blocks/components/SideNav/SideNavWithHeaderMenu.doc.mjs +1 -1
- package/assets/templates/blocks/components/SideNav/SideNavWithHeaderMenu.tsx +7 -7
- package/assets/templates/blocks/components/Table/TableCustomRowTable.doc.mjs +14 -0
- package/assets/templates/blocks/components/Table/TableCustomRowTable.tsx +96 -0
- package/assets/templates/components.generated.json +1 -1
- package/assets/templates/pages/ai-chat-landing/page.tsx +4 -0
- package/assets/templates/pages/dashboard-scorecard/page.tsx +26 -7
- package/assets/templates/pages/editor/page.tsx +13 -3
- package/assets/templates/themes/butter/butterPaletteRefs.generated.ts +2 -2
- package/assets/templates/themes/butter/butterTheme.ts +12 -10
- package/assets/templates/themes/butter/icons.tsx +2 -0
- package/assets/templates/themes/chocolate/chocolateTheme.ts +1 -0
- package/assets/templates/themes/chocolate/icons.tsx +2 -0
- package/assets/templates/themes/gothic/gothicPaletteRefs.generated.ts +1 -1
- package/assets/templates/themes/gothic/gothicTheme.ts +1 -0
- package/assets/templates/themes/gothic/icons.tsx +2 -0
- package/assets/templates/themes/matcha/icons.tsx +2 -0
- package/assets/templates/themes/matcha/matchaPaletteRefs.generated.ts +2 -2
- package/assets/templates/themes/matcha/matchaTheme.ts +1 -0
- package/assets/templates/themes/neutral/icons.tsx +2 -0
- package/assets/templates/themes/neutral/neutralPaletteRefs.generated.ts +2 -2
- package/assets/templates/themes/neutral/neutralTheme.ts +1 -0
- package/assets/templates/themes/stone/icons.tsx +2 -0
- package/assets/templates/themes/stone/stonePaletteRefs.generated.ts +1 -1
- package/assets/templates/themes/stone/stoneTheme.ts +1 -0
- package/assets/templates/themes/y2k/icons.tsx +2 -0
- package/assets/templates/themes/y2k/y2kTheme.ts +1 -0
- package/assets/theme.template.ts +9 -5
- package/authoring/config/parse.mjs +1 -1
- package/authoring/doctypes/base/type.ts +2 -3
- package/authoring/doctypes/component/component.doc.mjs +1 -1
- package/authoring/doctypes/icon-set/icon-names.mjs +1 -0
- package/authoring/integration/integration.doc.mjs +1 -1
- package/authoring/integration/type.ts +1 -1
- package/clients/cli/build-id.generated.json +1 -1
- package/clients/cli/commands/experimental/icon.mjs +7 -7
- package/clients/cli/commands/experimental/index.mjs +0 -5
- package/clients/cli/commands/experimental-icon-new-set.doc.mjs +2 -2
- package/clients/cli/commands/experimental-icon-use.doc.mjs +4 -4
- package/clients/cli/commands/experimental-icon.doc.mjs +1 -1
- package/clients/cli/commands/experimental.doc.mjs +2 -6
- package/clients/cli/commands/template.mjs +1 -1
- package/clients/cli/commands/theme-build.doc.mjs +8 -1
- package/clients/cli/commands/theme-palette-generate.doc.mjs +1 -1
- package/clients/cli/commands/theme-remove.doc.mjs +1 -1
- package/clients/cli/commands/theme.doc.mjs +1 -11
- package/clients/cli/commands/theme.mjs +110 -475
- package/clients/cli/commands.generated.json +1 -1
- package/clients/cli/index.mjs +4 -34
- package/clients/cli/lib/base-gate.mjs +2 -3
- package/clients/cli/lib/cli-error.mjs +8 -72
- package/clients/cli/lib/define-command.mjs +0 -19
- package/clients/cli/lib/json-shim.mjs +11 -633
- package/clients/cli/lib/manifest.mjs +0 -4
- package/clients/cli/lib/stray-option.mjs +1 -2
- package/clients/cli/manifest.generated.json +1 -1
- package/foundation/agent-docs/agent-docs.mjs +1 -1
- package/foundation/color/css-color.mjs +2 -3
- package/foundation/discovery/app-layout.d.mts +13 -0
- package/foundation/discovery/app-layout.mjs +35 -0
- package/foundation/discovery/fixture-refs.d.mts +51 -0
- package/foundation/discovery/fixture-refs.mjs +351 -0
- package/foundation/discovery/icon-definition-discovery.mjs +1 -1
- package/foundation/discovery/icon-ranking.mjs +2 -2
- package/foundation/discovery/icon-set-checks.mjs +1 -1
- package/foundation/discovery/icon-set-discovery.mjs +1 -1
- package/foundation/discovery/icon-set-writer.d.mts +24 -7
- package/foundation/discovery/icon-set-writer.mjs +66 -22
- package/foundation/discovery/static-descriptor.d.mts +49 -0
- package/foundation/discovery/static-descriptor.mjs +548 -0
- package/foundation/discovery/template-adapter.d.mts +0 -47
- package/foundation/discovery/template-adapter.mjs +1 -333
- package/foundation/discovery/theme-discovery.d.mts +6 -47
- package/foundation/discovery/theme-discovery.mjs +15 -537
- package/foundation/doc-compiler/import.mjs +1 -1
- package/foundation/env/user-dirs.d.mts +5 -2
- package/foundation/env/user-dirs.mjs +12 -5
- package/foundation/fonts/next-font.d.mts +26 -0
- package/foundation/fonts/next-font.mjs +149 -0
- package/foundation/fs/empty-folder.mjs +2 -2
- package/foundation/fs/read-record.mjs +11 -4
- package/foundation/pictures/pictures.d.mts +1 -45
- package/foundation/pictures/pictures.mjs +8 -8
- package/foundation/release/folder-lock.mjs +3 -2
- package/foundation/render/cut-short.d.mts +2 -2
- package/foundation/render/cut-short.mjs +2 -2
- package/foundation/render/drawn-contrast.d.mts +2 -2
- package/foundation/render/drawn-contrast.mjs +2 -2
- package/foundation/render/font-probe.d.mts +2 -2
- package/foundation/render/font-probe.mjs +2 -2
- package/foundation/render/layers.mjs +1 -2
- package/foundation/render/package-resolve-hooks.d.mts +25 -5
- package/foundation/render/package-resolve-hooks.mjs +77 -14
- package/foundation/render/page-worker.mjs +24 -12
- package/foundation/render/part-boxes.d.mts +2 -3
- package/foundation/render/part-boxes.mjs +2 -3
- package/foundation/render/picture-chrome.mjs +5 -0
- package/foundation/render/render-page.d.mts +1 -1
- package/foundation/render/render-page.mjs +4 -5
- package/foundation/render/single-copy-hooks.d.mts +2 -0
- package/foundation/render/single-copy-hooks.mjs +2 -0
- package/foundation/response/error-codes.doc.mjs +14 -14
- package/foundation/response/error-codes.mjs +13 -13
- package/foundation/response/response-types.doc.mjs +7 -68
- package/foundation/runtime/css-tree.mjs +2 -2
- package/foundation/runtime/word-table.d.mts +1 -1
- package/foundation/runtime/word-table.mjs +1 -1
- package/foundation/suggest/other-command.d.mts +52 -15
- package/foundation/suggest/other-command.mjs +168 -45
- package/foundation/text/suggest-order.d.mts +7 -0
- package/foundation/text/suggest-order.mjs +1 -1
- package/package.json +7 -8
- package/api/theme-edit/_adapter.d.mts +0 -228
- package/api/theme-edit/_adapter.mjs +0 -2025
- package/api/theme-edit/_catalog.d.mts +0 -172
- package/api/theme-edit/_catalog.mjs +0 -806
- package/api/theme-edit/_check.d.mts +0 -391
- package/api/theme-edit/_check.mjs +0 -9370
- package/api/theme-edit/_color.d.mts +0 -135
- package/api/theme-edit/_color.mjs +0 -480
- package/api/theme-edit/_compiler.d.mts +0 -77
- package/api/theme-edit/_compiler.mjs +0 -1053
- package/api/theme-edit/_context.d.mts +0 -399
- package/api/theme-edit/_context.mjs +0 -1778
- package/api/theme-edit/_css.d.mts +0 -169
- package/api/theme-edit/_css.mjs +0 -1167
- package/api/theme-edit/_edit.d.mts +0 -167
- package/api/theme-edit/_edit.mjs +0 -11220
- package/api/theme-edit/_how-commands.d.mts +0 -315
- package/api/theme-edit/_how-commands.mjs +0 -1664
- package/api/theme-edit/_how-question.d.mts +0 -341
- package/api/theme-edit/_how-question.mjs +0 -33339
- package/api/theme-edit/_how-sides.d.mts +0 -22
- package/api/theme-edit/_how-sides.mjs +0 -173
- package/api/theme-edit/_how-steps.d.mts +0 -42
- package/api/theme-edit/_how-steps.mjs +0 -208
- package/api/theme-edit/_how-values.d.mts +0 -29
- package/api/theme-edit/_how-values.mjs +0 -111
- package/api/theme-edit/_how-words.d.mts +0 -15
- package/api/theme-edit/_how-words.mjs +0 -239
- package/api/theme-edit/_how.d.mts +0 -55
- package/api/theme-edit/_how.mjs +0 -2388
- package/api/theme-edit/_importers.d.mts +0 -38
- package/api/theme-edit/_importers.mjs +0 -188
- package/api/theme-edit/_marks.d.mts +0 -39
- package/api/theme-edit/_marks.mjs +0 -67
- package/api/theme-edit/_missed.d.mts +0 -56
- package/api/theme-edit/_missed.mjs +0 -1128
- package/api/theme-edit/_name-rule.d.mts +0 -11
- package/api/theme-edit/_name-rule.mjs +0 -12
- package/api/theme-edit/_normal.d.mts +0 -365
- package/api/theme-edit/_normal.mjs +0 -1978
- package/api/theme-edit/_overview.d.mts +0 -9
- package/api/theme-edit/_overview.mjs +0 -293
- package/api/theme-edit/_palette.d.mts +0 -616
- package/api/theme-edit/_palette.mjs +0 -2776
- package/api/theme-edit/_part-pieces.d.mts +0 -54
- package/api/theme-edit/_part-pieces.mjs +0 -179
- package/api/theme-edit/_part-words.d.mts +0 -39
- package/api/theme-edit/_part-words.mjs +0 -242
- package/api/theme-edit/_parts.d.mts +0 -402
- package/api/theme-edit/_parts.mjs +0 -2322
- package/api/theme-edit/_picture-traits.d.mts +0 -259
- package/api/theme-edit/_picture-traits.mjs +0 -1030
- package/api/theme-edit/_png.d.mts +0 -94
- package/api/theme-edit/_png.mjs +0 -479
- package/api/theme-edit/_preview-app.d.mts +0 -172
- package/api/theme-edit/_preview-app.mjs +0 -6430
- package/api/theme-edit/_preview.d.mts +0 -484
- package/api/theme-edit/_preview.mjs +0 -2775
- package/api/theme-edit/_project.d.mts +0 -207
- package/api/theme-edit/_project.mjs +0 -643
- package/api/theme-edit/_resolved.d.mts +0 -46
- package/api/theme-edit/_resolved.mjs +0 -800
- package/api/theme-edit/_set-line.d.mts +0 -29
- package/api/theme-edit/_set-line.mjs +0 -100
- package/api/theme-edit/_settings.d.mts +0 -2426
- package/api/theme-edit/_settings.mjs +0 -18449
- package/api/theme-edit/_tables.d.mts +0 -227
- package/api/theme-edit/_tables.mjs +0 -1128
- package/api/theme-edit/_text-looks.d.mts +0 -78
- package/api/theme-edit/_text-looks.mjs +0 -376
- package/api/theme-edit/_vet.d.mts +0 -50
- package/api/theme-edit/_vet.mjs +0 -166
- package/api/theme-edit/how/how.d.mts +0 -17
- package/api/theme-edit/how/how.mjs +0 -28
- package/api/theme-edit/new/new.d.mts +0 -11
- package/api/theme-edit/new/new.mjs +0 -22
- package/api/theme-edit/preview-add/preview-add.d.mts +0 -14
- package/api/theme-edit/preview-add/preview-add.mjs +0 -25
- package/api/theme-edit/preview-app/preview-app.d.mts +0 -13
- package/api/theme-edit/preview-app/preview-app.mjs +0 -24
- package/api/theme-edit/preview-list/preview-list.d.mts +0 -10
- package/api/theme-edit/preview-list/preview-list.mjs +0 -21
- package/api/theme-edit/preview-remove/preview-remove.d.mts +0 -12
- package/api/theme-edit/preview-remove/preview-remove.mjs +0 -23
- package/api/theme-edit/set/set.d.mts +0 -13
- package/api/theme-edit/set/set.mjs +0 -24
- package/api/theme-edit/show/show.d.mts +0 -13
- package/api/theme-edit/show/show.mjs +0 -24
- package/api/theme-edit/theme-edit.d.mts +0 -12
- package/api/theme-edit/theme-edit.mjs +0 -36
- package/api/theme-edit/theme-edit.type.d.mts +0 -730
- package/api/theme-edit/theme-edit.type.mjs +0 -342
- package/api/theme-edit/themeEditHow.doc.d.mts +0 -6
- package/api/theme-edit/themeEditHow.doc.mjs +0 -105
- package/api/theme-edit/themeEditNew.doc.d.mts +0 -5
- package/api/theme-edit/themeEditNew.doc.mjs +0 -112
- package/api/theme-edit/themeEditPreviewAdd.doc.d.mts +0 -6
- package/api/theme-edit/themeEditPreviewAdd.doc.mjs +0 -121
- package/api/theme-edit/themeEditPreviewApp.doc.d.mts +0 -6
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +0 -87
- package/api/theme-edit/themeEditPreviewList.doc.d.mts +0 -6
- package/api/theme-edit/themeEditPreviewList.doc.mjs +0 -62
- package/api/theme-edit/themeEditPreviewRemove.doc.d.mts +0 -6
- package/api/theme-edit/themeEditPreviewRemove.doc.mjs +0 -96
- package/api/theme-edit/themeEditSet.doc.d.mts +0 -6
- package/api/theme-edit/themeEditSet.doc.mjs +0 -175
- package/api/theme-edit/themeEditShow.doc.d.mts +0 -6
- package/api/theme-edit/themeEditShow.doc.mjs +0 -118
- package/api/theme-edit/themeEditUndo.doc.d.mts +0 -6
- package/api/theme-edit/themeEditUndo.doc.mjs +0 -133
- package/api/theme-edit/undo/undo.d.mts +0 -13
- package/api/theme-edit/undo/undo.mjs +0 -24
- package/assets/preview-app/README.md +0 -6
- package/assets/preview-app/css.d.ts +0 -5
- package/assets/preview-app/gitignore +0 -4
- package/assets/preview-app/next.config.mjs +0 -12
- package/assets/preview-app/package.json +0 -24
- package/assets/preview-app/src/app/globals.css +0 -6
- package/assets/preview-app/src/app/layout.tsx +0 -21
- package/assets/preview-app/src/app/page.tsx +0 -94
- package/assets/preview-app/src/app/providers.tsx +0 -16
- package/assets/preview-app/tsconfig.json +0 -27
- package/clients/cli/commands/experimental/theme.mjs +0 -2990
- package/clients/cli/commands/experimental-theme-add.doc.mjs +0 -80
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +0 -98
- package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +0 -196
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +0 -174
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +0 -139
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +0 -77
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +0 -105
- package/clients/cli/commands/experimental-theme-editor.doc.mjs +0 -49
- package/clients/cli/commands/experimental-theme-eject.doc.mjs +0 -72
- package/clients/cli/commands/experimental-theme-list.doc.mjs +0 -49
- package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +0 -88
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +0 -61
- package/clients/cli/commands/experimental-theme-preview-list.doc.mjs +0 -48
- package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +0 -59
- package/clients/cli/commands/experimental-theme-preview.doc.mjs +0 -41
- package/clients/cli/commands/experimental-theme-remove.doc.mjs +0 -57
- package/clients/cli/commands/experimental-theme-targets.doc.mjs +0 -62
- package/clients/cli/commands/experimental-theme-template.doc.mjs +0 -63
- package/clients/cli/commands/experimental-theme-use.doc.mjs +0 -57
- package/clients/cli/commands/experimental-theme.doc.mjs +0 -60
- package/foundation/fonts/fontsource-families.generated.d.mts +0 -5
- package/foundation/fonts/fontsource-families.generated.mjs +0 -2110
|
@@ -1,1978 +0,0 @@
|
|
|
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 {
|
|
13
|
-
DIALS,
|
|
14
|
-
FONTS,
|
|
15
|
-
MODE_WORDS,
|
|
16
|
-
OPEN_FONTS,
|
|
17
|
-
fontStack,
|
|
18
|
-
wordTable,
|
|
19
|
-
} from './_tables.mjs';
|
|
20
|
-
import {colorOpacitiesAsCss, colorProblem, hex, parseColor} from './_color.mjs';
|
|
21
|
-
import {sameKindValues, takesWholeNumbersOnly, unitsJoined} from './_css.mjs';
|
|
22
|
-
import {levenshteinDistance} from '../../foundation/text/levenshtein.mjs';
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* An English noun's plural, as a part's name is said in the plural
|
|
26
|
-
* ("switch" → "switches", "body" → "bodies", "card" → "cards").
|
|
27
|
-
* @param {string} word
|
|
28
|
-
* @returns {string}
|
|
29
|
-
*/
|
|
30
|
-
export function pluralOf(word) {
|
|
31
|
-
const w = String(word);
|
|
32
|
-
if (/(?:s|x|z|ch|sh)$/i.test(w)) return `${w}es`;
|
|
33
|
-
if (/[^aeiou]y$/i.test(w)) return `${w.slice(0, -1)}ies`;
|
|
34
|
-
return `${w}s`;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* A value as CSS reads it, for every value type: every Unicode space
|
|
39
|
-
* (no-break, thin, ideographic…) a plain space and zero-width and format
|
|
40
|
-
* characters dropped, and quotes that delimit a value (curly ones, «», ‹›,
|
|
41
|
-
* full-width, backticks) straight. A straight-quoted CSS string keeps what
|
|
42
|
-
* it holds as written (`content: "it’s"`). Curly quotes pair by kind
|
|
43
|
-
* (double with double, single with single, in any of their shapes: „Inter“,
|
|
44
|
-
* ‚Inter‘); one left unpaired is read as the straight quote it stands for.
|
|
45
|
-
* @param {string} v
|
|
46
|
-
* @returns {string}
|
|
47
|
-
*/
|
|
48
|
-
export function normalValueText(v) {
|
|
49
|
-
let out = '';
|
|
50
|
-
let plain = '';
|
|
51
|
-
const flush = () => {
|
|
52
|
-
out += plain
|
|
53
|
-
.replace(/[\u200B-\u200D\u2060\uFEFF]/g, '')
|
|
54
|
-
.replace(/[\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]/g, ' ');
|
|
55
|
-
plain = '';
|
|
56
|
-
};
|
|
57
|
-
for (let i = 0; i < v.length;) {
|
|
58
|
-
const c = v[i];
|
|
59
|
-
// A straight-quoted CSS string keeps everything inside it.
|
|
60
|
-
if (c === '"' || c === "'") {
|
|
61
|
-
let j = i + 1;
|
|
62
|
-
while (j < v.length && v[j] !== c) j += v[j] === '\\' ? 2 : 1;
|
|
63
|
-
if (j < v.length) {
|
|
64
|
-
flush();
|
|
65
|
-
out += v.slice(i, j + 1);
|
|
66
|
-
i = j + 1;
|
|
67
|
-
continue;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
const kind = CURLY_DOUBLE.has(c)
|
|
71
|
-
? CURLY_DOUBLE
|
|
72
|
-
: CURLY_SINGLE.has(c)
|
|
73
|
-
? CURLY_SINGLE
|
|
74
|
-
: null;
|
|
75
|
-
/** @type {number} */
|
|
76
|
-
let j = -1;
|
|
77
|
-
if (kind) {
|
|
78
|
-
for (let k = i + 1; k < v.length; k++)
|
|
79
|
-
if (kind.has(v[k])) {
|
|
80
|
-
j = k;
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
} else if (QUOTE_PAIRS[c]) j = v.indexOf(QUOTE_PAIRS[c], i + 1);
|
|
84
|
-
if (j > i) {
|
|
85
|
-
flush();
|
|
86
|
-
// What the quotes hold, as typed (`“ / ”` is a slash between
|
|
87
|
-
// spaces); a font family trims it (normalizeEntry).
|
|
88
|
-
const inner = v.slice(i + 1, j);
|
|
89
|
-
const single = kind === CURLY_SINGLE || c === '\uFF07';
|
|
90
|
-
const mark = single
|
|
91
|
-
? inner.includes("'")
|
|
92
|
-
? '"'
|
|
93
|
-
: "'"
|
|
94
|
-
: inner.includes('"')
|
|
95
|
-
? "'"
|
|
96
|
-
: '"';
|
|
97
|
-
out += `${mark}${inner}${mark}`;
|
|
98
|
-
i = j + 1;
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
// An unpaired curly quote is the straight one it stands for.
|
|
102
|
-
plain += kind === CURLY_DOUBLE ? '"' : kind === CURLY_SINGLE ? "'" : c;
|
|
103
|
-
i += 1;
|
|
104
|
-
}
|
|
105
|
-
flush();
|
|
106
|
-
return out;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/** Curly double quotes, in every shape; any one closes any other. */
|
|
110
|
-
const CURLY_DOUBLE = new Set(['\u201C', '\u201D', '\u201E', '\u201F']);
|
|
111
|
-
/** Curly single quotes, in every shape. */
|
|
112
|
-
const CURLY_SINGLE = new Set(['\u2018', '\u2019', '\u201A', '\u201B']);
|
|
113
|
-
/** The other delimiting quotes and the one that closes each. */
|
|
114
|
-
/** @type {Record<string, string>} */
|
|
115
|
-
const QUOTE_PAIRS = {
|
|
116
|
-
'\u300C': '\u300D',
|
|
117
|
-
'\u300E': '\u300F',
|
|
118
|
-
'\u00AB': '\u00BB',
|
|
119
|
-
'\u00BB': '\u00AB',
|
|
120
|
-
'\u2039': '\u203A',
|
|
121
|
-
'\u203A': '\u2039',
|
|
122
|
-
'\uFF02': '\uFF02',
|
|
123
|
-
'\uFF07': '\uFF07',
|
|
124
|
-
'`': '`',
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
/** CSS functions whose names a value may write in any case. */
|
|
128
|
-
const CSS_FUNCTIONS =
|
|
129
|
-
/\b(light-dark|rgba?|hsla?|hwb|lab|lch|oklab|oklch|color|color-mix|calc|min|max|clamp|var)\(/gi;
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* A value as meant when typed with escapes or in another notation: CSS
|
|
133
|
-
* function names in lower case (`LIGHT-DARK(` is `light-dark(`, as CSS reads
|
|
134
|
-
* it); a shell or JSON backslash before `#`, a quote or a space taken off;
|
|
135
|
-
* a JavaScript escape in a `content` string written as CSS writes it
|
|
136
|
-
* (`\u2022` is `\2022 `, `\n` a line break, `\t` a tab); and the JSON
|
|
137
|
-
* shapes a design-tokens file or an array writes (`{"$value": "#e11d48"}`,
|
|
138
|
-
* `{"value": 18, "unit": "px"}`, `["#e11d48"]`, `["Inter", "sans-serif"]`).
|
|
139
|
-
* @param {string} thing
|
|
140
|
-
* @param {string} value
|
|
141
|
-
* @returns {string}
|
|
142
|
-
*/
|
|
143
|
-
export function escapesMeant(thing, value) {
|
|
144
|
-
let v = value;
|
|
145
|
-
const content = /^content$/i.test(thing);
|
|
146
|
-
if (content)
|
|
147
|
-
return (
|
|
148
|
-
v
|
|
149
|
-
.replace(
|
|
150
|
-
/\\u\{?([0-9a-f]{2,6})\}?/gi,
|
|
151
|
-
(_, h) => `\\${h.toUpperCase()} `,
|
|
152
|
-
)
|
|
153
|
-
// `\x` with four hex digits is written for the code point (as
|
|
154
|
-
// `\u`), with two for that character.
|
|
155
|
-
.replace(
|
|
156
|
-
/\\x([0-9a-f]{4}|[0-9a-f]{2})/gi,
|
|
157
|
-
(_, h) => `\\${h.toUpperCase()} `,
|
|
158
|
-
)
|
|
159
|
-
.replace(/\\n/g, '\\A ')
|
|
160
|
-
.replace(/\\t/g, '\\9 ')
|
|
161
|
-
.replace(/\\r/g, '\\D ')
|
|
162
|
-
);
|
|
163
|
-
v = v.replace(CSS_FUNCTIONS, f => f.toLowerCase());
|
|
164
|
-
// A JS escape of a printable character in a font family (`Inter\u0020
|
|
165
|
-
// Tight`) is that character, as CSS would read its own escape.
|
|
166
|
-
if (/font/i.test(thing))
|
|
167
|
-
v = v.replace(
|
|
168
|
-
/\\(?:u\{?([0-9a-f]{4,6})\}?|x([0-9a-f]{2}))/gi,
|
|
169
|
-
(m, u, x) => {
|
|
170
|
-
const c = String.fromCodePoint(parseInt(u ?? x, 16));
|
|
171
|
-
return /^[\p{L}\p{N} _-]$/u.test(c) ? c : m;
|
|
172
|
-
},
|
|
173
|
-
);
|
|
174
|
-
// (A JSON string holding escaped quotes is unwrapped whole later.)
|
|
175
|
-
if (!/^\s*"(?:[^"\\]|\\.)*"\s*$/.test(v)) v = v.replace(/\\([#"' ])/g, '$1');
|
|
176
|
-
else v = v.replace(/\\([# ])/g, '$1');
|
|
177
|
-
const t = v.trim();
|
|
178
|
-
if (/^[[{]/.test(t)) {
|
|
179
|
-
/** @type {any} */
|
|
180
|
-
let j;
|
|
181
|
-
try {
|
|
182
|
-
j = JSON.parse(t);
|
|
183
|
-
} catch {
|
|
184
|
-
// A list in single quotes, as a JS config writes it (`['Inter',
|
|
185
|
-
// 'serif']`): read like the JSON one.
|
|
186
|
-
const items =
|
|
187
|
-
/^\[\s*('(?:[^'\\]|\\.)*'\s*(?:,\s*'(?:[^'\\]|\\.)*'\s*)*),?\s*\]$/.exec(
|
|
188
|
-
t,
|
|
189
|
-
);
|
|
190
|
-
if (!items) return v;
|
|
191
|
-
j = [...items[1].matchAll(/'((?:[^'\\]|\\.)*)'/g)].map(x =>
|
|
192
|
-
x[1].replace(/\\(.)/g, '$1'),
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
/** @param {any} x */
|
|
196
|
-
const text = x =>
|
|
197
|
-
typeof x === 'string' || typeof x === 'number' ? String(x) : null;
|
|
198
|
-
if (Array.isArray(j) && j.length && j.every(x => text(x) != null)) {
|
|
199
|
-
if (j.length === 1) return String(j[0]);
|
|
200
|
-
if (/font/i.test(thing))
|
|
201
|
-
return j
|
|
202
|
-
.map(x =>
|
|
203
|
-
/\s/.test(String(x)) && !/^["']/.test(String(x))
|
|
204
|
-
? `"${x}"`
|
|
205
|
-
: String(x),
|
|
206
|
-
)
|
|
207
|
-
.join(', ');
|
|
208
|
-
return v;
|
|
209
|
-
}
|
|
210
|
-
if (j && typeof j === 'object' && !Array.isArray(j)) {
|
|
211
|
-
const inner =
|
|
212
|
-
'$value' in j ? j.$value : 'value' in j ? j.value : undefined;
|
|
213
|
-
if (text(inner) != null)
|
|
214
|
-
return `${inner}${typeof j.unit === 'string' ? j.unit : ''}`;
|
|
215
|
-
// One labeled entry (`{"color": "#e11d48"}`) is read as the same
|
|
216
|
-
// label in its CSS form (`color: #e11d48`): one reader for both.
|
|
217
|
-
const keys = Object.keys(j);
|
|
218
|
-
if (
|
|
219
|
-
keys.length === 1 &&
|
|
220
|
-
/^(--)?[a-z][a-z0-9-]*$/i.test(keys[0]) &&
|
|
221
|
-
text(j[keys[0]]) != null
|
|
222
|
-
)
|
|
223
|
-
return `${keys[0]}: ${j[keys[0]]}`;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
return v;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* The thing and value a setting was typed as, put right before any check: a
|
|
231
|
-
* declaration pasted whole or split (`accent: #e11d48`, `--color-accent=red`,
|
|
232
|
-
* `font:inter`, `accent:` with the value next), a token typed with an em or
|
|
233
|
-
* en dash (`—color-accent`) or as `var(--color-accent)`, and the value's
|
|
234
|
-
* Unicode spaces. A thing it cannot read is left as typed.
|
|
235
|
-
* @param {string} thing
|
|
236
|
-
* @param {string | undefined} value
|
|
237
|
-
* @returns {{thing: string, value: string | undefined, pasted?: boolean, dropped?: string, scope?: EntryScope}} `pasted`: the setting was read from a pasted form (a declaration, a printed or change line, a quoted key); `dropped`: a second declaration pasted after the first, which is not set; `scope`: the scope the editor's own printed line names
|
|
238
|
-
*/
|
|
239
|
-
export function normalizeEntry(thing, value) {
|
|
240
|
-
let t = normalValueText(String(thing ?? '')).trim();
|
|
241
|
-
let v = value == null ? value : normalValueText(String(value));
|
|
242
|
-
// A pasted declaration (the whole of it in the thing, or the thing with
|
|
243
|
-
// its colon and the value next): its `;` and `!important` go with it.
|
|
244
|
-
let pasted = false;
|
|
245
|
-
// A JSON object around one entry (`{"accent": "#e11d48"}`), whole or
|
|
246
|
-
// split at its colon.
|
|
247
|
-
if ((v == null || v.trim() === '') && /^\{[\s\S]*\}$/.test(t))
|
|
248
|
-
t = t.slice(1, -1).trim().replace(/,\s*$/, '');
|
|
249
|
-
else if (t.startsWith('{') && typeof v === 'string' && /\}\s*$/.test(v)) {
|
|
250
|
-
t = t.slice(1).trim();
|
|
251
|
-
v = v.replace(/\s*\}\s*$/, '');
|
|
252
|
-
}
|
|
253
|
-
// The editor's own result line pasted with its head (`[ok] t: corners =
|
|
254
|
-
// pill (whole theme)`, `t: corners pill -> round`): the head (the status
|
|
255
|
-
// and the theme's name) is no part of the setting.
|
|
256
|
-
// (Typed as one argument, the status may come apart from the rest.)
|
|
257
|
-
const whole =
|
|
258
|
-
v == null || v.trim() === ''
|
|
259
|
-
? t
|
|
260
|
-
: /^\[ok\]$/i.test(t)
|
|
261
|
-
? `${t} ${v.trim()}`
|
|
262
|
-
: null;
|
|
263
|
-
if (whole != null) {
|
|
264
|
-
const head =
|
|
265
|
-
/^(?:\[ok\]\s*)?[a-z][a-z0-9-]*:\s+(\S+\s*(?:=|\S.*?(?:->|\u2192)).*)$/i.exec(
|
|
266
|
-
whole,
|
|
267
|
-
);
|
|
268
|
-
const status = /^\[ok\]\s+(\S.*)$/i.exec(whole);
|
|
269
|
-
if (head || status) v = undefined;
|
|
270
|
-
if (head) {
|
|
271
|
-
t = head[1].trim();
|
|
272
|
-
pasted = true;
|
|
273
|
-
} else if (status) {
|
|
274
|
-
t = status[1].trim();
|
|
275
|
-
pasted = true;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
/** The scope the editor's own printed line names after its value. @type {EntryScope | undefined} */
|
|
279
|
-
let scope;
|
|
280
|
-
// The editor's own change line pasted back (`corners pill -> round`,
|
|
281
|
-
// `--radius-element: 0.5rem -> 0px`, `--color-accent (light): #1b1b1b ->
|
|
282
|
-
// #ff0000`), whole or with its thing apart: the setting, and the value it
|
|
283
|
-
// changed to, with the scope the line names. No CSS value holds `->`
|
|
284
|
-
// (or the `→` arrow, read as it).
|
|
285
|
-
// A mode label after the thing (`(light)`, `(Dark)`, `(dark mode)`,
|
|
286
|
-
// `[dark only]`) goes with the scope, read later by the one --mode reader
|
|
287
|
-
// as a typed --mode is (a typo refused with its line), never into the
|
|
288
|
-
// old value. A line with a label may name no old value (`--color-accent
|
|
289
|
-
// [light only] -> #7c3aed`): the label is still the scope, never taken
|
|
290
|
-
// as the old value.
|
|
291
|
-
const changed =
|
|
292
|
-
/^(\S+?)(?:\s*[([]([A-Za-z][^)\]]*)[)\]]\s*:?(?:\s+(\S.*?))?|\s*:?\s+(\S.*?))\s*(?:->|\u2192)\s*(\S.*?)\s*$/.exec(
|
|
293
|
-
v == null || v.trim() === '' ? t : `${t} ${v}`,
|
|
294
|
-
);
|
|
295
|
-
if (changed) {
|
|
296
|
-
t = changed[1];
|
|
297
|
-
v = changed[5];
|
|
298
|
-
pasted = true;
|
|
299
|
-
const printed = printedScope(v);
|
|
300
|
-
if (printed) {
|
|
301
|
-
v = printed.value;
|
|
302
|
-
scope = printed.scope;
|
|
303
|
-
}
|
|
304
|
-
if (changed[2] != null && changed[2].trim())
|
|
305
|
-
scope = {...scope, ...labelScope(changed[2])};
|
|
306
|
-
} else if (
|
|
307
|
-
v == null ||
|
|
308
|
-
v.trim() === '' ||
|
|
309
|
-
/^\s*[([][A-Za-z][^)\]]*[)\]]\s*[:=]/.test(v) ||
|
|
310
|
-
// A declaration whose value went on past a space (`font=Inter,` then
|
|
311
|
-
// `Arial`, an unquoted list split by the shell): the rest of its value.
|
|
312
|
-
/^[^\s:=]+\s*[:=]\s*\S/.test(t)
|
|
313
|
-
) {
|
|
314
|
-
// A mode label after the thing (`--color-accent (dark mode): #7c3aed`)
|
|
315
|
-
// goes with the scope, as on a change line, also when the words after
|
|
316
|
-
// the thing came as the value.
|
|
317
|
-
const m =
|
|
318
|
-
/^(\S+?)(?:\s*[([]([A-Za-z][^)\]]*)[)\]])?\s*([:=])\s*(.+?)\s*;?\s*$/.exec(
|
|
319
|
-
v == null || v.trim() === '' ? t : `${t} ${v}`,
|
|
320
|
-
);
|
|
321
|
-
if (m && !/^https?$/i.test(m[1])) {
|
|
322
|
-
t = m[1];
|
|
323
|
-
v = m[4];
|
|
324
|
-
pasted = true;
|
|
325
|
-
if (m[2] != null && m[2].trim()) scope = labelScope(m[2]);
|
|
326
|
-
// The editor's own printed line (`corners = pill (whole theme)`,
|
|
327
|
-
// `opacity = 0.5 on card [dark only]`) pasted back: its scope goes
|
|
328
|
-
// with the setting, not into the value.
|
|
329
|
-
if (m[3] === '=') {
|
|
330
|
-
const printed = printedScope(v);
|
|
331
|
-
if (printed) {
|
|
332
|
-
v = printed.value;
|
|
333
|
-
scope = {...printed.scope, ...scope};
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
// A thing ending in its colon or equals sign, the value given next.
|
|
339
|
-
if (/\s*[:=]+$/.test(t)) {
|
|
340
|
-
t = t.replace(/\s*[:=]+$/, '');
|
|
341
|
-
pasted = true;
|
|
342
|
-
}
|
|
343
|
-
// A thing in quotes, as a JSON or design-tokens file writes it
|
|
344
|
-
// (`"accent": "#e11d48"`).
|
|
345
|
-
const quotedThing = /^(["'])(.+)\1$/.exec(t);
|
|
346
|
-
if (quotedThing) {
|
|
347
|
-
t = quotedThing[2].trim();
|
|
348
|
-
pasted = true;
|
|
349
|
-
// Its value in JSON's double quotes is the string they hold
|
|
350
|
-
// (`"#e11d48"`, `"Inter, serif"`): the quotes are JSON's, for a font
|
|
351
|
-
// too; a family that needs its own quotes is written inside the string
|
|
352
|
-
// (`"\"Open Sans\", serif"`).
|
|
353
|
-
if (typeof v === 'string') {
|
|
354
|
-
const jv = /^\s*("(?:[^"\\]|\\.)*")\s*,?\s*$/.exec(v);
|
|
355
|
-
if (jv)
|
|
356
|
-
try {
|
|
357
|
-
v = String(JSON.parse(jv[1]));
|
|
358
|
-
} catch {
|
|
359
|
-
// Not a JSON string: read as typed.
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
// Dash-like characters where a token's two dashes go (an em or en dash,
|
|
364
|
-
// a non-breaking hyphen, a minus sign, a full-width hyphen, alone or
|
|
365
|
-
// mixed with hyphens): `--`.
|
|
366
|
-
t = t.replace(/^[-\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]+/, run =>
|
|
367
|
-
/[^-]/.test(run) ? '--' : run,
|
|
368
|
-
);
|
|
369
|
-
// A dash-like character inside a token's name is its hyphen.
|
|
370
|
-
if (t.startsWith('--'))
|
|
371
|
-
t = `--${t.slice(2).replace(/[\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]/g, '-')}`;
|
|
372
|
-
// A token typed as a var() reference.
|
|
373
|
-
const ref = /^var\(\s*(--[a-z0-9-]+)\s*\)$/i.exec(t);
|
|
374
|
-
if (ref) t = ref[1];
|
|
375
|
-
// A dial name written in another style or letters (textSize,
|
|
376
|
-
// text_size, "text size", Corners, MODE, a dash-like character for its
|
|
377
|
-
// hyphen) is the dial: one reading for every command.
|
|
378
|
-
if (!t.startsWith('-')) {
|
|
379
|
-
const dial = t
|
|
380
|
-
.replace(/[_\s\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]+/g, '-')
|
|
381
|
-
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
|
|
382
|
-
.toLowerCase();
|
|
383
|
-
if (DIALS[dial]) t = dial;
|
|
384
|
-
}
|
|
385
|
-
if (typeof v === 'string') v = escapesMeant(t, v);
|
|
386
|
-
// A second declaration pasted after the first (`accent: red; text-size:
|
|
387
|
-
// 18`): the first is this setting.
|
|
388
|
-
/** A second declaration pasted after the first, which this does not set. @type {string | null} */
|
|
389
|
-
let dropped = null;
|
|
390
|
-
if (typeof v === 'string') {
|
|
391
|
-
const cut = withoutSecondDeclaration(v);
|
|
392
|
-
if (cut !== v)
|
|
393
|
-
dropped = v
|
|
394
|
-
.slice(cut.length)
|
|
395
|
-
.replace(/^\s*;\s*/, '')
|
|
396
|
-
.trim();
|
|
397
|
-
v = cut;
|
|
398
|
-
}
|
|
399
|
-
if (pasted && typeof v === 'string') {
|
|
400
|
-
v = v.replace(/\s*!\s*important\s*;?\s*$/i, '').replace(/;+\s*$/, '');
|
|
401
|
-
}
|
|
402
|
-
if (typeof v === 'string') v = withoutDebris(v);
|
|
403
|
-
// A font stack written as a JSON string (`"\\"Open Sans\\", serif"`): the
|
|
404
|
-
// stack it holds.
|
|
405
|
-
if (
|
|
406
|
-
typeof v === 'string' &&
|
|
407
|
-
/font/i.test(t) &&
|
|
408
|
-
/^"(?:[^"\\]|\\.)*"$/.test(v.trim()) &&
|
|
409
|
-
v.includes('\\"')
|
|
410
|
-
) {
|
|
411
|
-
try {
|
|
412
|
-
const inner = JSON.parse(v.trim());
|
|
413
|
-
if (typeof inner === 'string') v = inner;
|
|
414
|
-
} catch {
|
|
415
|
-
// Not a JSON string.
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
// A font stack with its quotes escaped, as it sits inside a JSON or JS
|
|
419
|
-
// string (`\"Inter\", sans-serif`): the stack.
|
|
420
|
-
if (typeof v === 'string' && /font/i.test(t) && /\\["']/.test(v))
|
|
421
|
-
v = v.replace(/\\(["'])/g, '$1');
|
|
422
|
-
// A font family's name never starts or ends with a space: French quotes
|
|
423
|
-
// (« Inter », with a no-break or narrow no-break space) hold the name.
|
|
424
|
-
if (typeof v === 'string' && /font/i.test(t)) v = trimInsideQuotes(v);
|
|
425
|
-
return {
|
|
426
|
-
thing: t,
|
|
427
|
-
value: v,
|
|
428
|
-
...(pasted ? {pasted} : {}),
|
|
429
|
-
...(dropped ? {dropped} : {}),
|
|
430
|
-
...(scope ? {scope} : {}),
|
|
431
|
-
};
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
/**
|
|
435
|
-
* The mode a printed label names, as typed (`Dark`, `dark mode`, `dark
|
|
436
|
-
* only`, `dark-only` are dark): the word before `only` or `mode`, for the
|
|
437
|
-
* one --mode reader.
|
|
438
|
-
* @param {string} label
|
|
439
|
-
*/
|
|
440
|
-
function modeLabel(label) {
|
|
441
|
-
return label
|
|
442
|
-
.trim()
|
|
443
|
-
.replace(/[\s-]*\b(only|mode)$/i, '')
|
|
444
|
-
.trim();
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
/**
|
|
448
|
-
* The scope a pasted line's label names: a mode the one mode reader reads
|
|
449
|
-
* (`(Dark)`, `[dark only]`, `(drak mode)`), else the word as typed
|
|
450
|
-
* (`label`), which the change reads as a part when it is one exact part
|
|
451
|
-
* name and as a mode (its typo rule) otherwise.
|
|
452
|
-
* @param {string} label
|
|
453
|
-
* @returns {EntryScope}
|
|
454
|
-
*/
|
|
455
|
-
function labelScope(label) {
|
|
456
|
-
const word = modeLabel(label);
|
|
457
|
-
return modeWord(word) || /\b(only|mode)\s*$/i.test(label.trim())
|
|
458
|
-
? {mode: word}
|
|
459
|
-
: {label: word};
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
/**
|
|
463
|
-
* @typedef {{on?: string, only?: boolean, when?: string, state?: string, mode?: string, label?: string}} EntryScope
|
|
464
|
-
* `label`: a pasted line's label that names no mode word (see labelScope)
|
|
465
|
-
*/
|
|
466
|
-
|
|
467
|
-
/**
|
|
468
|
-
* The value and scope of the editor's own printed setting line after its
|
|
469
|
-
* `=` (`pill (whole theme)`, `0.5 on card only when sm :hover [dark only]`),
|
|
470
|
-
* as `describe` writes them; null when the text does not end in that form.
|
|
471
|
-
* @param {string} text
|
|
472
|
-
* @returns {{value: string, scope: EntryScope} | null}
|
|
473
|
-
*/
|
|
474
|
-
function printedScope(text) {
|
|
475
|
-
const m =
|
|
476
|
-
/^(.+?)\s+(?:\(whole theme\)|on ([a-z][a-z0-9-]*)( only)?(?: when (\S+))?(?: :([a-z-]+))?)(?:\s+\[([^\]]+)\])?\s*$/i.exec(
|
|
477
|
-
text,
|
|
478
|
-
);
|
|
479
|
-
if (!m) return null;
|
|
480
|
-
/** @type {EntryScope} */
|
|
481
|
-
const scope = {};
|
|
482
|
-
if (m[2]) scope.on = m[2];
|
|
483
|
-
if (m[3]) scope.only = true;
|
|
484
|
-
if (m[4]) scope.when = m[4];
|
|
485
|
-
if (m[5]) scope.state = m[5];
|
|
486
|
-
if (m[6]) scope.mode = modeLabel(m[6]);
|
|
487
|
-
return {value: m[1], scope};
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
/**
|
|
491
|
-
* A font stack's pieces, split on the commas outside quotes, with each
|
|
492
|
-
* piece's name and whether it was quoted; null when a quote is left open.
|
|
493
|
-
* @param {string} v
|
|
494
|
-
* @returns {Array<{name: string, quoted: boolean}> | null}
|
|
495
|
-
*/
|
|
496
|
-
export function familyPieces(v) {
|
|
497
|
-
/** @type {Array<{name: string, quoted: boolean}>} */
|
|
498
|
-
const out = [];
|
|
499
|
-
let cur = '';
|
|
500
|
-
let quoted = false;
|
|
501
|
-
/** @type {string | null} */
|
|
502
|
-
let q = null;
|
|
503
|
-
let closed = false;
|
|
504
|
-
for (const c of String(v)) {
|
|
505
|
-
if (q) {
|
|
506
|
-
if (c === q) {
|
|
507
|
-
q = null;
|
|
508
|
-
closed = true;
|
|
509
|
-
} else cur += c;
|
|
510
|
-
continue;
|
|
511
|
-
}
|
|
512
|
-
if (c === '"' || c === "'") {
|
|
513
|
-
// A quote opening after text, or a second quoted name before a
|
|
514
|
-
// comma: not a stack of whole pieces.
|
|
515
|
-
if (cur.trim() || closed) return null;
|
|
516
|
-
q = c;
|
|
517
|
-
quoted = true;
|
|
518
|
-
continue;
|
|
519
|
-
}
|
|
520
|
-
// Text after a quoted name before its comma (`"Open Sans" serif`).
|
|
521
|
-
if (closed && c !== ',' && c.trim()) return null;
|
|
522
|
-
if (c === ',') {
|
|
523
|
-
out.push({name: cur.trim(), quoted});
|
|
524
|
-
cur = '';
|
|
525
|
-
quoted = false;
|
|
526
|
-
closed = false;
|
|
527
|
-
continue;
|
|
528
|
-
}
|
|
529
|
-
cur += c;
|
|
530
|
-
}
|
|
531
|
-
if (q) return null;
|
|
532
|
-
out.push({name: cur.trim(), quoted});
|
|
533
|
-
return out;
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
/**
|
|
537
|
-
* The families a typed font stack names: a trailing comma and empty pieces
|
|
538
|
-
* dropped, and a stack wrapped in one more pair of the quotes it uses
|
|
539
|
-
* inside (`""SF Mono", Monaco, monospace"`, as a JSON string) unwrapped.
|
|
540
|
-
* Null when the stack does not read.
|
|
541
|
-
* @param {string} v
|
|
542
|
-
* @returns {string[] | null}
|
|
543
|
-
*/
|
|
544
|
-
export function familiesOf(v) {
|
|
545
|
-
let s = String(v)
|
|
546
|
-
.trim()
|
|
547
|
-
.replace(/,+\s*$/, '');
|
|
548
|
-
const w = /^(["'])([\s\S]*)\1$/.exec(s);
|
|
549
|
-
if (w && w[2].includes(w[1])) s = w[2].trim().replace(/,+\s*$/, '');
|
|
550
|
-
const pieces = familyPieces(s);
|
|
551
|
-
if (!pieces) return null;
|
|
552
|
-
return pieces.map(f => f.name).filter(Boolean);
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
/**
|
|
556
|
-
* A value with the debris of a bad copy taken off: a leading `=` or `:`, a
|
|
557
|
-
* trailing comma (a JSON or CSS list), and a quote or a closing bracket left
|
|
558
|
-
* over at either end. A value without any is returned as it was.
|
|
559
|
-
* @param {string} v
|
|
560
|
-
* @returns {string}
|
|
561
|
-
*/
|
|
562
|
-
export function withoutDebris(v) {
|
|
563
|
-
let s = v.replace(/^\s*[=:]\s*/, '').replace(/\s*,+\s*$/, '');
|
|
564
|
-
// Quotes and brackets counted as CSS reads them: an escaped quote (\")
|
|
565
|
-
// is no quote, and a bracket inside a string is the string's.
|
|
566
|
-
const scan = (/** @type {string} */ t) => {
|
|
567
|
-
/** @type {Record<string, number>} */
|
|
568
|
-
const quotes = {'"': 0, "'": 0};
|
|
569
|
-
let open = 0;
|
|
570
|
-
let close = 0;
|
|
571
|
-
/** @type {string | null} */
|
|
572
|
-
let q = null;
|
|
573
|
-
for (let i = 0; i < t.length; i++) {
|
|
574
|
-
const c = t[i];
|
|
575
|
-
if (c === '\\') {
|
|
576
|
-
i++;
|
|
577
|
-
continue;
|
|
578
|
-
}
|
|
579
|
-
if (q) {
|
|
580
|
-
if (c === q) {
|
|
581
|
-
q = null;
|
|
582
|
-
quotes[c]++;
|
|
583
|
-
}
|
|
584
|
-
continue;
|
|
585
|
-
}
|
|
586
|
-
if (c === '"' || c === "'") {
|
|
587
|
-
q = c;
|
|
588
|
-
quotes[c]++;
|
|
589
|
-
} else if (c === '(') open++;
|
|
590
|
-
else if (c === ')') close++;
|
|
591
|
-
}
|
|
592
|
-
return {quotes, open, close};
|
|
593
|
-
};
|
|
594
|
-
for (const mark of /** @type {const} */ (['"', "'"])) {
|
|
595
|
-
if (scan(s).quotes[mark] % 2 === 1) {
|
|
596
|
-
if (s.endsWith(mark) && !s.endsWith(`\\${mark}`)) s = s.slice(0, -1);
|
|
597
|
-
else if (s.startsWith(mark)) s = s.slice(1);
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
const {open, close} = scan(s);
|
|
601
|
-
if (close > open && s.endsWith(')')) s = s.slice(0, -1);
|
|
602
|
-
return s === v.trim() ? v : s.trim();
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
/**
|
|
606
|
-
* A value without a second declaration pasted after it (`#e11d48;
|
|
607
|
-
* text-size: 18`): cut at the first `;` outside quotes that a `name:`
|
|
608
|
-
* follows. A `;` inside a quoted string (`"Tip; note: x"`) is the string's.
|
|
609
|
-
* @param {string} v
|
|
610
|
-
* @returns {string}
|
|
611
|
-
*/
|
|
612
|
-
export function withoutSecondDeclaration(v) {
|
|
613
|
-
/** @type {string | null} */
|
|
614
|
-
let q = null;
|
|
615
|
-
for (let i = 0; i < v.length; i++) {
|
|
616
|
-
const c = v[i];
|
|
617
|
-
if (q) {
|
|
618
|
-
if (c === '\\') i++;
|
|
619
|
-
else if (c === q) q = null;
|
|
620
|
-
continue;
|
|
621
|
-
}
|
|
622
|
-
if (c === '"' || c === "'") q = c;
|
|
623
|
-
else if (
|
|
624
|
-
c === ';' &&
|
|
625
|
-
/^\s*[a-z-]+\s*:/i.test(v.slice(i + 1)) &&
|
|
626
|
-
// `light: red; dark: blue` is one value for two modes.
|
|
627
|
-
!/^\s*(?:light|dark)\s*:/i.test(v.slice(i + 1))
|
|
628
|
-
)
|
|
629
|
-
return v.slice(0, i);
|
|
630
|
-
}
|
|
631
|
-
return v;
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
/**
|
|
635
|
-
* A saved dial value read as typed input is (invisible or stray spaces,
|
|
636
|
-
* quotes around it, a pasted `corners: pill`, a trailing `;`, other
|
|
637
|
-
* letters): the value the dial may have meant.
|
|
638
|
-
* @param {string} dial
|
|
639
|
-
* @param {string} value
|
|
640
|
-
* @returns {string}
|
|
641
|
-
*/
|
|
642
|
-
export function cleanDialValue(dial, value) {
|
|
643
|
-
const read = normalizeEntry(dial, value).value ?? '';
|
|
644
|
-
return read
|
|
645
|
-
.trim()
|
|
646
|
-
.replace(new RegExp(`^${dial.replace(/-/g, '\\-')}\\s*[:=]\\s*`, 'i'), '')
|
|
647
|
-
.replace(/\s*!\s*important\s*$/i, '')
|
|
648
|
-
.replace(/;+\s*$/, '')
|
|
649
|
-
.replace(/^(["'])(.*)\1$/, '$2')
|
|
650
|
-
.trim()
|
|
651
|
-
.toLowerCase();
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
/**
|
|
655
|
-
* A value with the spaces just inside each straight-quoted string taken off
|
|
656
|
-
* (`" Inter "` is "Inter"), for a font family, whose name holds none there.
|
|
657
|
-
* @param {string} v
|
|
658
|
-
* @returns {string}
|
|
659
|
-
*/
|
|
660
|
-
export function trimInsideQuotes(v) {
|
|
661
|
-
const SP = '[\\s\\u00A0\\u202F\\u2000-\\u200A\\u3000]*';
|
|
662
|
-
return v.replace(
|
|
663
|
-
new RegExp(`(["'])${SP}((?:(?!\\1)[^\\\\]|\\\\.)*?)${SP}\\1`, 'g'),
|
|
664
|
-
'$1$2$1',
|
|
665
|
-
);
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
/**
|
|
669
|
-
* The other setting a dial's value is labeled with, when pasted with a
|
|
670
|
-
* label that names a different dial or a token (`neutral: #64748b` given
|
|
671
|
-
* to accent): that label and the value after it, else null. A label that
|
|
672
|
-
* names this dial, or a CSS property (`color: #e11d48`), is no other
|
|
673
|
-
* setting.
|
|
674
|
-
* @param {string} dial
|
|
675
|
-
* @param {string} value
|
|
676
|
-
* @returns {{thing: string, value: string} | null}
|
|
677
|
-
*/
|
|
678
|
-
export function otherSettingLabel(dial, value) {
|
|
679
|
-
const m =
|
|
680
|
-
/^\s*["']?([a-z][a-z0-9_-]*|--[a-z0-9-]+)["']?\s*[:=]\s*["']?(.+?)["']?\s*;?\s*$/i.exec(
|
|
681
|
-
String(value),
|
|
682
|
-
);
|
|
683
|
-
if (!m) return null;
|
|
684
|
-
// A label in camelCase or snake_case (textSize, text_size) is the dial.
|
|
685
|
-
const label = m[1]
|
|
686
|
-
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
|
|
687
|
-
.replace(/_/g, '-')
|
|
688
|
-
.toLowerCase();
|
|
689
|
-
if (label === dial) return null;
|
|
690
|
-
if (!DIALS[label] && !label.startsWith('--')) return null;
|
|
691
|
-
return {thing: label, value: m[2]};
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
/**
|
|
695
|
-
* A light and a dark color typed as a pair: labeled (`light: red; dark:
|
|
696
|
-
* blue`), a JSON object (`{"light": "red", "dark": "blue"}`), or two colors
|
|
697
|
-
* with a slash or a comma between (`red / blue`, `red, blue`). Null when the
|
|
698
|
-
* value is no such pair; the caller checks that both are colors.
|
|
699
|
-
* @param {string} value
|
|
700
|
-
* @returns {{light: string, dark: string} | null}
|
|
701
|
-
*/
|
|
702
|
-
export function colorPair(value) {
|
|
703
|
-
const v = String(value).trim();
|
|
704
|
-
const unq = (/** @type {string} */ x) =>
|
|
705
|
-
x
|
|
706
|
-
.trim()
|
|
707
|
-
.replace(/^(["'])(.*)\1$/, '$2')
|
|
708
|
-
.trim();
|
|
709
|
-
const labeled =
|
|
710
|
-
/^\{?\s*["']?(light|dark)["']?\s*[:=]\s*(.+?)\s*(?:[;,]\s*|\s+)["']?(light|dark)["']?\s*[:=]\s*(.+?)\s*;?\s*\}?$/i.exec(
|
|
711
|
-
v,
|
|
712
|
-
);
|
|
713
|
-
if (labeled && labeled[1].toLowerCase() !== labeled[3].toLowerCase()) {
|
|
714
|
-
const a = unq(labeled[2]);
|
|
715
|
-
const b = unq(labeled[4]);
|
|
716
|
-
return labeled[1].toLowerCase() === 'light'
|
|
717
|
-
? {light: a, dark: b}
|
|
718
|
-
: {light: b, dark: a};
|
|
719
|
-
}
|
|
720
|
-
const two = /^([^\s,/()]+)\s*(?:\s\/\s|,)\s*([^\s,/()]+)$/.exec(v);
|
|
721
|
-
if (two) return {light: unq(two[1]), dark: unq(two[2])};
|
|
722
|
-
return null;
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
/**
|
|
726
|
-
* A value without the debris a copy brings along: a trailing `;`, a
|
|
727
|
-
* `!important`, a comment, an old browser hack's escape at its end (`\9`,
|
|
728
|
-
* `\0`, which browsers read as part of the last word, so they drop the
|
|
729
|
-
* value). One of the exact cleanups a repair may offer.
|
|
730
|
-
* @param {string} v
|
|
731
|
-
* @returns {string}
|
|
732
|
-
*/
|
|
733
|
-
export function withoutCopyDebris(v) {
|
|
734
|
-
return String(v)
|
|
735
|
-
.replace(/\/\*[\s\S]*?\*\//g, '')
|
|
736
|
-
.replace(/\s*!\s*important\b/gi, '')
|
|
737
|
-
.replace(/[\s;]+$/, '')
|
|
738
|
-
.replace(/\s*\\[09]$/, '')
|
|
739
|
-
.trim();
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
/** Number words, longest phrase first. */
|
|
743
|
-
export const NUMBER_WORDS = wordTable(
|
|
744
|
-
/** @type {Record<string, number>} */ ({
|
|
745
|
-
zero: 0,
|
|
746
|
-
one: 1,
|
|
747
|
-
two: 2,
|
|
748
|
-
three: 3,
|
|
749
|
-
four: 4,
|
|
750
|
-
five: 5,
|
|
751
|
-
six: 6,
|
|
752
|
-
seven: 7,
|
|
753
|
-
eight: 8,
|
|
754
|
-
nine: 9,
|
|
755
|
-
ten: 10,
|
|
756
|
-
eleven: 11,
|
|
757
|
-
twelve: 12,
|
|
758
|
-
thirteen: 13,
|
|
759
|
-
fourteen: 14,
|
|
760
|
-
fifteen: 15,
|
|
761
|
-
sixteen: 16,
|
|
762
|
-
seventeen: 17,
|
|
763
|
-
eighteen: 18,
|
|
764
|
-
nineteen: 19,
|
|
765
|
-
twenty: 20,
|
|
766
|
-
thirty: 30,
|
|
767
|
-
forty: 40,
|
|
768
|
-
fifty: 50,
|
|
769
|
-
sixty: 60,
|
|
770
|
-
seventy: 70,
|
|
771
|
-
eighty: 80,
|
|
772
|
-
ninety: 90,
|
|
773
|
-
hundred: 100,
|
|
774
|
-
}),
|
|
775
|
-
);
|
|
776
|
-
|
|
777
|
-
/**
|
|
778
|
-
* A whole number as typed: digits with any white space at the ends and a
|
|
779
|
-
* zero fraction (`" 3"`, `"3.0"`), or a number word (`"three"`); else null.
|
|
780
|
-
* @param {unknown} text
|
|
781
|
-
* @returns {number | null}
|
|
782
|
-
*/
|
|
783
|
-
export function wholeNumberOf(text) {
|
|
784
|
-
const t = String(text ?? '')
|
|
785
|
-
.trim()
|
|
786
|
-
.toLowerCase();
|
|
787
|
-
if (/^[+-]?\d+(?:\.0+)?$/.test(t)) return Number(t);
|
|
788
|
-
return Object.hasOwn(NUMBER_WORDS, t) ? NUMBER_WORDS[t] : null;
|
|
789
|
-
}
|
|
790
|
-
|
|
791
|
-
/**
|
|
792
|
-
* Words with a documented reading on a CSS property: the exact value each
|
|
793
|
-
* one means. A repair may offer these for the word typed.
|
|
794
|
-
* @type {Record<string, Record<string, string>>}
|
|
795
|
-
*/
|
|
796
|
-
export const PROPERTY_WORDS = wordTable({
|
|
797
|
-
opacity: {
|
|
798
|
-
half: '0.5',
|
|
799
|
-
'half-transparent': '0.5',
|
|
800
|
-
opaque: '1',
|
|
801
|
-
solid: '1',
|
|
802
|
-
},
|
|
803
|
-
'text-transform': {
|
|
804
|
-
caps: 'uppercase',
|
|
805
|
-
'all-caps': 'uppercase',
|
|
806
|
-
upper: 'uppercase',
|
|
807
|
-
lower: 'lowercase',
|
|
808
|
-
title: 'capitalize',
|
|
809
|
-
'no-caps': 'none',
|
|
810
|
-
},
|
|
811
|
-
'border-radius': {
|
|
812
|
-
round: '9999px',
|
|
813
|
-
rounded: '9999px',
|
|
814
|
-
pill: '9999px',
|
|
815
|
-
circle: '50%',
|
|
816
|
-
square: '0',
|
|
817
|
-
},
|
|
818
|
-
});
|
|
819
|
-
|
|
820
|
-
/** Weight words beyond the headings dial's, as their numbers. */
|
|
821
|
-
export const WEIGHT_WORDS = wordTable(
|
|
822
|
-
/** @type {Record<string, string>} */ ({
|
|
823
|
-
thin: '100',
|
|
824
|
-
extralight: '200',
|
|
825
|
-
light: '300',
|
|
826
|
-
regular: '400',
|
|
827
|
-
normal: '400',
|
|
828
|
-
medium: '500',
|
|
829
|
-
semibold: '600',
|
|
830
|
-
bold: '700',
|
|
831
|
-
heavy: '800',
|
|
832
|
-
extrabold: '800',
|
|
833
|
-
ultrabold: '800',
|
|
834
|
-
black: '900',
|
|
835
|
-
}),
|
|
836
|
-
);
|
|
837
|
-
|
|
838
|
-
/**
|
|
839
|
-
* Tailwind's radius classes, as the corners value each one is.
|
|
840
|
-
* @type {Record<string, string>}
|
|
841
|
-
*/
|
|
842
|
-
export const TAILWIND_RADIUS = wordTable({
|
|
843
|
-
'rounded-none': 'square',
|
|
844
|
-
'rounded-sm': '2px',
|
|
845
|
-
rounded: '4px',
|
|
846
|
-
'rounded-md': '6px',
|
|
847
|
-
'rounded-lg': '8px',
|
|
848
|
-
'rounded-xl': '12px',
|
|
849
|
-
'rounded-2xl': '16px',
|
|
850
|
-
'rounded-3xl': '24px',
|
|
851
|
-
'rounded-full': 'pill',
|
|
852
|
-
});
|
|
853
|
-
|
|
854
|
-
/** Words for more or less room, as the spacing rung each one means. */
|
|
855
|
-
export const DENSITY_WORDS = wordTable(
|
|
856
|
-
/** @type {Record<string, string>} */ ({
|
|
857
|
-
airy: 'roomy',
|
|
858
|
-
loose: 'roomy',
|
|
859
|
-
relaxed: 'roomy',
|
|
860
|
-
comfortable: 'roomy',
|
|
861
|
-
open: 'roomy',
|
|
862
|
-
sparse: 'roomy',
|
|
863
|
-
generous: 'roomy',
|
|
864
|
-
dense: 'compact',
|
|
865
|
-
cramped: 'compact',
|
|
866
|
-
condensed: 'compact',
|
|
867
|
-
packed: 'compact',
|
|
868
|
-
}),
|
|
869
|
-
);
|
|
870
|
-
|
|
871
|
-
/**
|
|
872
|
-
* A typed value as the repair gate reads it, in one reading: escapes and
|
|
873
|
-
* full-width characters as the characters they are, a letter O inside a
|
|
874
|
-
* number as a zero, a comma between digits as a decimal point (or before
|
|
875
|
-
* exactly three digits, after a whole number other than 0, a thousands
|
|
876
|
-
* mark), and without what is not the value: quotes and braces around it, a
|
|
877
|
-
* label before it (`accent: red`), a flag inside it (`--mode dark`) or a
|
|
878
|
-
* flag's label after it (`mode: dark`), copy debris (`;`, `!important`,
|
|
879
|
-
* comments), and the axes of a Google Fonts family spec (`Inter:wght@400`
|
|
880
|
-
* is the family Inter).
|
|
881
|
-
* @param {string} typed
|
|
882
|
-
* @param {{thousands?: boolean, fallback?: boolean}} [opts] `thousands` for
|
|
883
|
-
* a setting whose numbers are weights (see commaNumbers); `fallback`
|
|
884
|
-
* (default true) reads an app's own variable with a fallback as the
|
|
885
|
-
* fallback, false keeps the variable as typed
|
|
886
|
-
* @returns {string}
|
|
887
|
-
*/
|
|
888
|
-
export function typedReading(typed, {thousands = false, fallback = true} = {}) {
|
|
889
|
-
let t = String(typed ?? '')
|
|
890
|
-
.replace(/\\(?:u\{?([0-9a-f]{4,6})\}?|x([0-9a-f]{2}))/gi, (_, u, x) =>
|
|
891
|
-
String.fromCodePoint(parseInt(u ?? x, 16)),
|
|
892
|
-
)
|
|
893
|
-
.replace(/\\(?=[#"' ])/g, '')
|
|
894
|
-
.normalize('NFKC')
|
|
895
|
-
.trim();
|
|
896
|
-
t = t
|
|
897
|
-
// A mode's label in any spelling (`dark mode: red`), before the
|
|
898
|
-
// trailing-label reading can take `mode: red` for one.
|
|
899
|
-
.replace(
|
|
900
|
-
/^\s*\{?\s*["']?(?:(?:light|dark)[\s_-]*mode|mode[\s_-]*(?:light|dark))["']?\s*[:=]\s*/i,
|
|
901
|
-
'',
|
|
902
|
-
)
|
|
903
|
-
.replace(
|
|
904
|
-
/\s+--(mode|state|on|when)(?:\s*=\s*|\s+)("[^"]*"|'[^']*'|\S+)/gi,
|
|
905
|
-
'',
|
|
906
|
-
)
|
|
907
|
-
.replace(
|
|
908
|
-
/\s+(mode|theme|state|part|on|variant|size|when)\s*[:=]\s*[a-z][a-z0-9:-]*\s*;?\s*$/i,
|
|
909
|
-
'',
|
|
910
|
-
);
|
|
911
|
-
t = withoutCopyDebris(t)
|
|
912
|
-
// A flag's label before the value (`state=hover 4px`).
|
|
913
|
-
.replace(
|
|
914
|
-
/^(mode|theme|state|part|on|variant|size|when)\s*[:=]\s*[a-z][a-z0-9:-]*\s+(?=\S)/i,
|
|
915
|
-
'',
|
|
916
|
-
);
|
|
917
|
-
const spec = /^([a-z][a-z +]*):(?:ital,)?(?:wght|ital)@/i.exec(t);
|
|
918
|
-
if (spec) t = spec[1].replace(/\+/g, ' ');
|
|
919
|
-
const labeledPair = colorPair(t);
|
|
920
|
-
t =
|
|
921
|
-
labeledPair &&
|
|
922
|
-
!colorProblem(labeledPair.light) &&
|
|
923
|
-
!colorProblem(labeledPair.dark)
|
|
924
|
-
? t
|
|
925
|
-
: t.replace(
|
|
926
|
-
/^\s*\{?\s*["']?(?:--)?[a-z][a-z0-9 _-]*["']?\s*[:=](?!\/\/)\s*/i,
|
|
927
|
-
'',
|
|
928
|
-
);
|
|
929
|
-
t = t
|
|
930
|
-
.replace(/["']?\s*\}\s*$/, '')
|
|
931
|
-
.replace(
|
|
932
|
-
/^(["'`\u201C\u2018\u300C\u300E\u00AB])(.*?)(["'`\u201D\u2019\u300D\u300F\u00BB])$/,
|
|
933
|
-
'$2',
|
|
934
|
-
)
|
|
935
|
-
.replace(/,\s*$/, '')
|
|
936
|
-
.trim();
|
|
937
|
-
t = commaNumbers(
|
|
938
|
-
// A dot after a number's digits (`18.`, `16.px`) is no part of it, and
|
|
939
|
-
// a unit split off by a space (`20 px`, `200 ms`) is its number's.
|
|
940
|
-
unitsJoined(
|
|
941
|
-
t
|
|
942
|
-
.replace(
|
|
943
|
-
/(\d)([oO]+)(?![a-z])/g,
|
|
944
|
-
(_, d, os) => d + '0'.repeat(os.length),
|
|
945
|
-
)
|
|
946
|
-
.replace(/(\d)\.(?=[a-z%]|\s|$)/gi, '$1'),
|
|
947
|
-
),
|
|
948
|
-
thousands,
|
|
949
|
-
)
|
|
950
|
-
.replace(/\b0x([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})\b/gi, '#$1')
|
|
951
|
-
.toLowerCase();
|
|
952
|
-
// An app's own variable with a fallback (`var(--brand, #e11d48)`) is the
|
|
953
|
-
// fallback; Tailwind's arbitrary value (`rounded-[12px]`) is the value.
|
|
954
|
-
if (fallback) t = t.replace(/^var\(\s*--[a-z0-9-]+\s*,\s*(.+)\)$/, '$1');
|
|
955
|
-
t = t.replace(/^[a-z]+(?:-[a-z]+)?-\[(.+)\]$/, '$1');
|
|
956
|
-
// A light and a dark color typed as a pair (`light: red; dark: blue`,
|
|
957
|
-
// `red / blue`) is that light-dark() pair.
|
|
958
|
-
// Both halves colors: `Inter, 600` is a family list, no pair.
|
|
959
|
-
const pair = colorPair(t);
|
|
960
|
-
return pair && !colorProblem(pair.light) && !colorProblem(pair.dark)
|
|
961
|
-
? `light-dark(${pair.light}, ${pair.dark})`
|
|
962
|
-
: t;
|
|
963
|
-
}
|
|
964
|
-
|
|
965
|
-
/**
|
|
966
|
-
* The units no CSS value is written to a thousandth of (a thousandth of a
|
|
967
|
-
* pixel or of a millisecond is below anything a screen or a person tells
|
|
968
|
-
* apart), so three digits after a comma there group thousands. A
|
|
969
|
-
* thousandth of a rem, an em or a second is a real value (1.125rem is
|
|
970
|
-
* 18px), so a comma there stays a decimal point.
|
|
971
|
-
*/
|
|
972
|
-
const WHOLE_UNITS = new Set(['px', 'ms']);
|
|
973
|
-
|
|
974
|
-
/**
|
|
975
|
-
* A text with each comma between digits, outside parentheses, read as a
|
|
976
|
-
* number mark: a decimal point (`1,5rem`, `1,125rem`, `0,500`), or a
|
|
977
|
-
* thousands mark before exactly three digits after a whole number other
|
|
978
|
-
* than 0 (`1,000`) where the setting's numbers are whole thousands (a
|
|
979
|
-
* weight, 1 to 1000) or the number is in a unit no value is written to a
|
|
980
|
-
* thousandth of (`1,200px`, `1,000ms`; see WHOLE_UNITS). Inside a function
|
|
981
|
-
* (`rgb(255,0,0)`) a comma separates arguments.
|
|
982
|
-
* @param {string} text
|
|
983
|
-
* @param {boolean} [thousands] whether the setting's numbers are weights
|
|
984
|
-
* @returns {string}
|
|
985
|
-
*/
|
|
986
|
-
export function commaNumbers(text, thousands = false) {
|
|
987
|
-
let depth = 0;
|
|
988
|
-
let out = '';
|
|
989
|
-
let plain = '';
|
|
990
|
-
const flush = () => {
|
|
991
|
-
// (A number starts a word: the digits of a hex color or of a word
|
|
992
|
-
// before a comma are no number.)
|
|
993
|
-
out += plain.replace(
|
|
994
|
-
/(?<![\w#.])(\d+),(\d+)(?![\d.,])(?=([a-z]+)?)/gi,
|
|
995
|
-
(_, a, b, unit) =>
|
|
996
|
-
(thousands || WHOLE_UNITS.has(String(unit).toLowerCase())) &&
|
|
997
|
-
b.length === 3 &&
|
|
998
|
-
!/^0+$/.test(a)
|
|
999
|
-
? `${a}${b}`
|
|
1000
|
-
: `${a}.${b}`,
|
|
1001
|
-
);
|
|
1002
|
-
plain = '';
|
|
1003
|
-
};
|
|
1004
|
-
for (const c of String(text)) {
|
|
1005
|
-
if (c === '(') {
|
|
1006
|
-
if (depth === 0) flush();
|
|
1007
|
-
depth += 1;
|
|
1008
|
-
}
|
|
1009
|
-
if (depth > 0) out += c;
|
|
1010
|
-
else plain += c;
|
|
1011
|
-
if (c === ')' && depth > 0) depth -= 1;
|
|
1012
|
-
}
|
|
1013
|
-
flush();
|
|
1014
|
-
return out;
|
|
1015
|
-
}
|
|
1016
|
-
|
|
1017
|
-
/**
|
|
1018
|
-
* Whether a comma between a setting's digits groups thousands: its numbers
|
|
1019
|
-
* are weights (whole hundreds up to 1000) or the property takes only whole
|
|
1020
|
-
* numbers (z-index, order), so no comma there can be a decimal one.
|
|
1021
|
-
* @param {string} thing
|
|
1022
|
-
*/
|
|
1023
|
-
export const thousandsSetting = thing => {
|
|
1024
|
-
const t = String(thing).toLowerCase();
|
|
1025
|
-
return (
|
|
1026
|
-
/^(font-weight|headings)$|^--font-weight/.test(t) ||
|
|
1027
|
-
(/^[a-z][a-z-]*$/.test(t) && takesWholeNumbersOnly(t))
|
|
1028
|
-
);
|
|
1029
|
-
};
|
|
1030
|
-
|
|
1031
|
-
/**
|
|
1032
|
-
* A state as CSS writes it, from any case or spelling of its words
|
|
1033
|
-
* (`Hover`, `:hover`, `focusVisible`, `focus_visible`, `focus visible`): the
|
|
1034
|
-
* one state reader, for a typed `--state` and a saved entry's state alike.
|
|
1035
|
-
* @param {string} text
|
|
1036
|
-
* @returns {string}
|
|
1037
|
-
*/
|
|
1038
|
-
export function stateSpelling(text) {
|
|
1039
|
-
// Several states at once are joined by +, with or without spaces around
|
|
1040
|
-
// it (`selected + hover`): each is read on its own.
|
|
1041
|
-
return String(text)
|
|
1042
|
-
.split(/\s*\+\s*/)
|
|
1043
|
-
.map(piece =>
|
|
1044
|
-
piece
|
|
1045
|
-
.trim()
|
|
1046
|
-
.replace(/^:+/, '')
|
|
1047
|
-
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
|
|
1048
|
-
.replace(/[\s_]+/g, '-')
|
|
1049
|
-
.toLowerCase(),
|
|
1050
|
-
)
|
|
1051
|
-
.join('+');
|
|
1052
|
-
}
|
|
1053
|
-
|
|
1054
|
-
/**
|
|
1055
|
-
* The one mode a word names, or null: light or dark, written as a mode
|
|
1056
|
-
* value names one (`dark-only`), with the word mode (`darkmode`, `dark
|
|
1057
|
-
* mode`, `mode dark`), or as a word for it (MODE_WORDS: night, day). The
|
|
1058
|
-
* one mode reader: the `--mode` flag, a mode label in a value, a saved
|
|
1059
|
-
* entry's mode and the settings file's mode all read it; a typo of a mode
|
|
1060
|
-
* is read by {@link modeNear}.
|
|
1061
|
-
* @param {unknown} text
|
|
1062
|
-
* @returns {'light' | 'dark' | null}
|
|
1063
|
-
*/
|
|
1064
|
-
export function modeWord(text) {
|
|
1065
|
-
const w = String(text ?? '')
|
|
1066
|
-
.trim()
|
|
1067
|
-
.toLowerCase()
|
|
1068
|
-
.replace(/[\s_]+/g, '-');
|
|
1069
|
-
const m = /^(?:(light|dark)(?:-?only|-?mode)?|mode-?(light|dark))$/.exec(w);
|
|
1070
|
-
if (m) return /** @type {'light' | 'dark'} */ (m[1] ?? m[2]);
|
|
1071
|
-
return MODE_WORDS[w] ?? null;
|
|
1072
|
-
}
|
|
1073
|
-
|
|
1074
|
-
/**
|
|
1075
|
-
* The one mode a word names as {@link modeWord} reads it, or as a typo of
|
|
1076
|
-
* light or dark that keeps its letters (`drak`, `darkk`, `ligth`, `lght`,
|
|
1077
|
-
* `drak mode`, `kdar`): its letters in any order, or one doubled, dropped or
|
|
1078
|
-
* added, never one
|
|
1079
|
-
* letter for another, so a word of its own (right, tight, bark, park) is
|
|
1080
|
-
* never a mode. The reading of a mode a person types with a typo, wherever
|
|
1081
|
-
* it is typed: the `--mode` flag, a mode label in a `set` value or a `how`
|
|
1082
|
-
* question; each offers or writes the line with the mode it names.
|
|
1083
|
-
* @param {unknown} text
|
|
1084
|
-
* @returns {'light' | 'dark' | null}
|
|
1085
|
-
*/
|
|
1086
|
-
export function modeNear(text) {
|
|
1087
|
-
const exact = modeWord(text);
|
|
1088
|
-
if (exact) return exact;
|
|
1089
|
-
const stem = String(text ?? '')
|
|
1090
|
-
.trim()
|
|
1091
|
-
.toLowerCase()
|
|
1092
|
-
.replace(/[\s_]+/g, '-')
|
|
1093
|
-
.replace(/-?(?:only|mode)$/, '')
|
|
1094
|
-
.replace(/^mode-?/, '');
|
|
1095
|
-
// Same letters in any order (`kdar`), or one letter more or fewer
|
|
1096
|
-
// within the typo limit.
|
|
1097
|
-
const near = /** @type {const} */ (['light', 'dark']).filter(mode =>
|
|
1098
|
-
stem.length === mode.length
|
|
1099
|
-
? lettersBeyond(stem, mode) === 0
|
|
1100
|
-
: typoNear(stem, mode) &&
|
|
1101
|
-
lettersBeyond(mode, stem) <= (stem.length < mode.length ? 1 : 0) &&
|
|
1102
|
-
lettersBeyond(stem, mode) <= (stem.length > mode.length ? 1 : 0),
|
|
1103
|
-
);
|
|
1104
|
-
return near.length === 1 ? near[0] : null;
|
|
1105
|
-
}
|
|
1106
|
-
|
|
1107
|
-
/**
|
|
1108
|
-
* How many letters one word has beyond the other's, counted with repeats
|
|
1109
|
-
* (`darkk` has one beyond `dark`; `bark` one beyond it too).
|
|
1110
|
-
* @param {string} a
|
|
1111
|
-
* @param {string} b
|
|
1112
|
-
*/
|
|
1113
|
-
function lettersBeyond(a, b) {
|
|
1114
|
-
/** @type {Map<string, number>} */
|
|
1115
|
-
const left = new Map();
|
|
1116
|
-
for (const c of b) left.set(c, (left.get(c) ?? 0) + 1);
|
|
1117
|
-
let more = 0;
|
|
1118
|
-
for (const c of a) {
|
|
1119
|
-
const k = left.get(c) ?? 0;
|
|
1120
|
-
if (k) left.set(c, k - 1);
|
|
1121
|
-
else more++;
|
|
1122
|
-
}
|
|
1123
|
-
return more;
|
|
1124
|
-
}
|
|
1125
|
-
|
|
1126
|
-
/**
|
|
1127
|
-
* A base as written in any of its names: its package (`@astryxdesign/
|
|
1128
|
-
* theme-neutral`, `theme-neutral`), its export (`neutralTheme`), or with
|
|
1129
|
-
* the word theme (`neutral-theme`, `neutral theme`), in any case. The one
|
|
1130
|
-
* reading `set`, `new` and the settings file share; the word as read (a
|
|
1131
|
-
* base name or not).
|
|
1132
|
-
* @param {unknown} v
|
|
1133
|
-
* @returns {string}
|
|
1134
|
-
*/
|
|
1135
|
-
export function baseWord(v) {
|
|
1136
|
-
return (
|
|
1137
|
-
String(v ?? '')
|
|
1138
|
-
.trim()
|
|
1139
|
-
// A path names the package its last part does
|
|
1140
|
-
// (`./node_modules/@astryxdesign/theme-neutral` is theme-neutral).
|
|
1141
|
-
.replace(/[\\/]+$/, '')
|
|
1142
|
-
.replace(/^.*[\\/]/, '')
|
|
1143
|
-
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
|
|
1144
|
-
.toLowerCase()
|
|
1145
|
-
.replace(/[\s_]+/g, '-')
|
|
1146
|
-
.replace(/^(@astryxdesign\/)?theme-|-theme$/g, '')
|
|
1147
|
-
);
|
|
1148
|
-
}
|
|
1149
|
-
|
|
1150
|
-
/**
|
|
1151
|
-
* The one reading of a yes/no word, for a flag given a value (`--only=on`)
|
|
1152
|
-
* and a saved `only`: the documented spellings of yes and no, or a typo of
|
|
1153
|
-
* one of them (`ture`); null for any other word, which is neither.
|
|
1154
|
-
* @param {unknown} word
|
|
1155
|
-
* @returns {boolean | null}
|
|
1156
|
-
*/
|
|
1157
|
-
export function booleanWord(word) {
|
|
1158
|
-
if (typeof word === 'boolean') return word;
|
|
1159
|
-
const w = String(word ?? '')
|
|
1160
|
-
.trim()
|
|
1161
|
-
.toLowerCase();
|
|
1162
|
-
const YES = ['true', 'yes', 'y', 'on', '1'];
|
|
1163
|
-
const NO = ['false', 'no', 'n', 'off', '0'];
|
|
1164
|
-
if (YES.includes(w)) return true;
|
|
1165
|
-
if (NO.includes(w)) return false;
|
|
1166
|
-
const near = [...YES, ...NO].filter(x => x.length > 3 && typoNear(w, x));
|
|
1167
|
-
const read = [...new Set(near.map(x => YES.includes(x)))];
|
|
1168
|
-
return read.length === 1 ? read[0] : null;
|
|
1169
|
-
}
|
|
1170
|
-
|
|
1171
|
-
/** The CSS functions a value is written in, whose names carry no value. */
|
|
1172
|
-
const FORM =
|
|
1173
|
-
/\b(?:light-dark|var|calc|(?:repeating-)?(?:linear|radial|conic)-gradient)\(/g;
|
|
1174
|
-
|
|
1175
|
-
/**
|
|
1176
|
-
* The tokens of a value: its words and numbers in order, the names of the
|
|
1177
|
-
* functions it is written in out.
|
|
1178
|
-
* @param {string} text already read (typedReading) or as offered
|
|
1179
|
-
* @returns {string[]}
|
|
1180
|
-
*/
|
|
1181
|
-
function tokensOf(text) {
|
|
1182
|
-
return (
|
|
1183
|
-
String(text).toLowerCase().replace(FORM, '(').match(SIGNED_OR_WORD) ?? []
|
|
1184
|
-
);
|
|
1185
|
-
}
|
|
1186
|
-
|
|
1187
|
-
/**
|
|
1188
|
-
* A token: a number with its minus sign (a sign is one at the start, or
|
|
1189
|
-
* after a space, a parenthesis or a comma: `-8px`, `calc(-1 * 4px)`, never
|
|
1190
|
-
* the minus of `1rem-2px`), or a word or number.
|
|
1191
|
-
*/
|
|
1192
|
-
const SIGNED_OR_WORD =
|
|
1193
|
-
/(?<![a-z0-9.%)])-(?=\.?\d)\d*\.?\d+(?:e[+-]?\d+)?[a-z%]*|[a-z0-9.%]+/g;
|
|
1194
|
-
|
|
1195
|
-
/** @param {string} w */
|
|
1196
|
-
const numberOf = w => {
|
|
1197
|
-
const m = /^(-?\d*\.?\d+(?:e[+-]?\d+)?)(px|rem|em|pt|%)?$/.exec(w);
|
|
1198
|
-
return m ? {n: Number(m[1]), unit: m[2] ?? ''} : null;
|
|
1199
|
-
};
|
|
1200
|
-
|
|
1201
|
-
/**
|
|
1202
|
-
* Whether two tokens are one: the same word, or the same number with a
|
|
1203
|
-
* unit added or taken off (`1.5` and `1.5px`; the vet checks the setting
|
|
1204
|
-
* takes it).
|
|
1205
|
-
* @param {string} a @param {string} b
|
|
1206
|
-
*/
|
|
1207
|
-
const sameToken = (a, b) => {
|
|
1208
|
-
if (a === b) return true;
|
|
1209
|
-
const x = numberOf(a);
|
|
1210
|
-
const y = numberOf(b);
|
|
1211
|
-
return Boolean(x && y && x.n === y.n && (!x.unit || !y.unit));
|
|
1212
|
-
};
|
|
1213
|
-
|
|
1214
|
-
/**
|
|
1215
|
-
* The numbers one typed token may be read as, each exact: as it is, rem or
|
|
1216
|
-
* em in px (x16), pt in px (x4/3), a percent as a share of one or of the
|
|
1217
|
-
* 16px base size, a share of one as a percent.
|
|
1218
|
-
* @param {string} w
|
|
1219
|
-
* @returns {Set<string>}
|
|
1220
|
-
*/
|
|
1221
|
-
function numberReadings(w) {
|
|
1222
|
-
const r2 = (/** @type {number} */ x) => String(Math.round(x * 100) / 100);
|
|
1223
|
-
const m = numberOf(w);
|
|
1224
|
-
/** @type {Set<string>} */
|
|
1225
|
-
const out = new Set();
|
|
1226
|
-
if (!m) return out;
|
|
1227
|
-
out.add(r2(m.n));
|
|
1228
|
-
if (m.unit === 'rem' || m.unit === 'em') out.add(r2(m.n * 16));
|
|
1229
|
-
if (m.unit === 'pt') out.add(r2((m.n * 4) / 3));
|
|
1230
|
-
if (m.unit === '%') {
|
|
1231
|
-
out.add(r2(m.n / 100));
|
|
1232
|
-
out.add(r2((m.n * 16) / 100));
|
|
1233
|
-
}
|
|
1234
|
-
if (!m.unit && m.n > 0 && m.n < 1) out.add(r2(m.n * 100));
|
|
1235
|
-
return out;
|
|
1236
|
-
}
|
|
1237
|
-
|
|
1238
|
-
/**
|
|
1239
|
-
* Whether the numbers of `offered` are the numbers of the whole typed
|
|
1240
|
-
* value written another exact way: position by position (numberReadings),
|
|
1241
|
-
* or one number that is a calc() of the typed lengths worked out. The rest
|
|
1242
|
-
* of `offered` is units and form words only.
|
|
1243
|
-
* @param {string} offered
|
|
1244
|
-
* @param {string} t the typed value, read
|
|
1245
|
-
*/
|
|
1246
|
-
function numbersEqual(offered, t) {
|
|
1247
|
-
// A number with its sign (see SIGNED_OR_WORD): -8px is never 8px.
|
|
1248
|
-
const NUM =
|
|
1249
|
-
/(?<![a-z0-9.%)])[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?|(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?/gi;
|
|
1250
|
-
const rest = offered
|
|
1251
|
-
.replace(NUM, '')
|
|
1252
|
-
.replace(/px|rem|em|%|[\s(),/*+-]|calc|var/g, '');
|
|
1253
|
-
const want = offered.match(NUM)?.map(Number) ?? [];
|
|
1254
|
-
if (!want.length || rest) return false;
|
|
1255
|
-
const r2 = (/** @type {number} */ x) => String(Math.round(x * 100) / 100);
|
|
1256
|
-
const typedNumbers = (
|
|
1257
|
-
t.match(
|
|
1258
|
-
/(?<![a-z0-9.%)])-?\d*\.?\d+(?:e[+-]?\d+)?(?:px|rem|em|pt|%)?|\d*\.?\d+(?:e[+-]?\d+)?(?:px|rem|em|pt|%)?/gi,
|
|
1259
|
-
) ?? []
|
|
1260
|
-
).map(numberReadings);
|
|
1261
|
-
// The typed value is numbers only (any words in it are read elsewhere).
|
|
1262
|
-
if (
|
|
1263
|
-
tokensOf(t).every(w => numberOf(w)) &&
|
|
1264
|
-
typedNumbers.length === want.length &&
|
|
1265
|
-
want.every((w, i) => typedNumbers[i].has(r2(w)))
|
|
1266
|
-
)
|
|
1267
|
-
return true;
|
|
1268
|
-
if (want.length !== 1) return false;
|
|
1269
|
-
const inner = /^\s*calc\(([^()]*)\)\s*$/i.exec(t)?.[1];
|
|
1270
|
-
if (!inner) return false;
|
|
1271
|
-
const scale =
|
|
1272
|
-
/^\s*(\d*\.?\d+)\s*(px|rem|em)?\s*([*/])\s*(\d*\.?\d+)\s*$/i.exec(inner);
|
|
1273
|
-
if (scale) {
|
|
1274
|
-
const a = Number(scale[1]) * (/r?em/i.test(scale[2] ?? '') ? 16 : 1);
|
|
1275
|
-
return (
|
|
1276
|
-
r2(scale[3] === '*' ? a * Number(scale[4]) : a / Number(scale[4])) ===
|
|
1277
|
-
r2(want[0])
|
|
1278
|
-
);
|
|
1279
|
-
}
|
|
1280
|
-
if (/[*/]/.test(inner)) return false;
|
|
1281
|
-
let total = 0;
|
|
1282
|
-
for (const m of inner.matchAll(/([+-]?)\s*(\d*\.?\d+)\s*(px|rem|em)?/gi)) {
|
|
1283
|
-
const x = Number(m[2]) * (/r?em/i.test(m[3] ?? '') ? 16 : 1);
|
|
1284
|
-
total += m[1] === '-' ? -x : x;
|
|
1285
|
-
}
|
|
1286
|
-
return r2(total) === r2(want[0]);
|
|
1287
|
-
}
|
|
1288
|
-
|
|
1289
|
-
/**
|
|
1290
|
-
* Documented readings: each typed word and the exact value it means,
|
|
1291
|
-
* registered by the table that documents it (a dial's words, a Tailwind
|
|
1292
|
-
* class, a property word). A repair may offer only these for a word that
|
|
1293
|
-
* was not typed as the value.
|
|
1294
|
-
* @type {Map<string, Set<string>>}
|
|
1295
|
-
*/
|
|
1296
|
-
const READINGS = new Map();
|
|
1297
|
-
|
|
1298
|
-
/**
|
|
1299
|
-
* Register a documented table's readings with the repair allowlist.
|
|
1300
|
-
* @param {Iterable<[string, string]>} pairs typed word, value it means
|
|
1301
|
-
*/
|
|
1302
|
-
export function documentReadings(pairs) {
|
|
1303
|
-
for (const [w, v] of pairs) {
|
|
1304
|
-
const k = String(w).trim().toLowerCase();
|
|
1305
|
-
if (!READINGS.has(k)) READINGS.set(k, new Set());
|
|
1306
|
-
/** @type {Set<string>} */ (READINGS.get(k)).add(String(v).toLowerCase());
|
|
1307
|
-
}
|
|
1308
|
-
}
|
|
1309
|
-
|
|
1310
|
-
/**
|
|
1311
|
-
* Documented numeric tables (a rung and its number), each for the settings
|
|
1312
|
-
* that document it, registered where it is defined: a number typed for one
|
|
1313
|
-
* of those settings may be offered as its nearest rung.
|
|
1314
|
-
* @type {Array<{things: string[], table: Array<[string, number]>, domain: [number, number]}>}
|
|
1315
|
-
*/
|
|
1316
|
-
const NUMERIC_TABLES = [];
|
|
1317
|
-
|
|
1318
|
-
/**
|
|
1319
|
-
* Register a documented numeric table with the repair allowlist.
|
|
1320
|
-
* @param {string[]} things the settings whose values the table reads (a
|
|
1321
|
-
* name ending in `*` names every setting it starts)
|
|
1322
|
-
* @param {Array<[string, number]>} table
|
|
1323
|
-
* @param {[number, number]} [domain] the numbers the table reads (by
|
|
1324
|
-
* default every number)
|
|
1325
|
-
*/
|
|
1326
|
-
export function documentNumericTable(
|
|
1327
|
-
things,
|
|
1328
|
-
table,
|
|
1329
|
-
domain = [-Infinity, Infinity],
|
|
1330
|
-
) {
|
|
1331
|
-
if (table.length) NUMERIC_TABLES.push({things, table, domain});
|
|
1332
|
-
}
|
|
1333
|
-
|
|
1334
|
-
/**
|
|
1335
|
-
* Documented ranges: a number typed past either end of a setting's range
|
|
1336
|
-
* may be offered as that end.
|
|
1337
|
-
* @type {Array<{things: string[], min: number, max: number}>}
|
|
1338
|
-
*/
|
|
1339
|
-
const RANGES = [];
|
|
1340
|
-
|
|
1341
|
-
/**
|
|
1342
|
-
* The documented words a typed word may be a typo of, item by item in a
|
|
1343
|
-
* list (the CSS generic families, the font presets), registered where each
|
|
1344
|
-
* list is defined.
|
|
1345
|
-
* @type {Set<string>}
|
|
1346
|
-
*/
|
|
1347
|
-
const TYPO_WORDS = new Set();
|
|
1348
|
-
|
|
1349
|
-
/**
|
|
1350
|
-
* Register documented words a typed list item may be a typo of.
|
|
1351
|
-
* @param {Iterable<string>} words
|
|
1352
|
-
*/
|
|
1353
|
-
export function documentTypoWords(words) {
|
|
1354
|
-
for (const w of words) TYPO_WORDS.add(String(w).toLowerCase());
|
|
1355
|
-
}
|
|
1356
|
-
|
|
1357
|
-
/**
|
|
1358
|
-
* Register a setting's documented range with the repair allowlist.
|
|
1359
|
-
* @param {string[]} things as for documentNumericTable
|
|
1360
|
-
* @param {number} min
|
|
1361
|
-
* @param {number} max
|
|
1362
|
-
*/
|
|
1363
|
-
export function documentRange(things, min, max) {
|
|
1364
|
-
RANGES.push({things, min, max});
|
|
1365
|
-
}
|
|
1366
|
-
|
|
1367
|
-
/**
|
|
1368
|
-
* Whether a registered setting list names a setting.
|
|
1369
|
-
* @param {string[]} things
|
|
1370
|
-
* @param {string} key
|
|
1371
|
-
*/
|
|
1372
|
-
const names = (things, key) =>
|
|
1373
|
-
things.some(t =>
|
|
1374
|
-
t.endsWith('*') ? key.startsWith(t.slice(0, -1)) : t === key,
|
|
1375
|
-
);
|
|
1376
|
-
|
|
1377
|
-
/**
|
|
1378
|
-
* Values a setting documents as its own fallback, which a refusal may
|
|
1379
|
-
* offer whatever was typed: no base, the modes.
|
|
1380
|
-
* @type {Record<string, string[]>}
|
|
1381
|
-
*/
|
|
1382
|
-
const DECLARED_VALUES = wordTable({
|
|
1383
|
-
base: ['none'],
|
|
1384
|
-
mode: ['both', 'light-only', 'dark-only'],
|
|
1385
|
-
});
|
|
1386
|
-
|
|
1387
|
-
/** The dials with a scale, which `more` and `less` step. */
|
|
1388
|
-
const SCALED_DIALS = Object.keys(DIALS).filter(d =>
|
|
1389
|
-
String(DIALS[d].values ?? '')
|
|
1390
|
-
.split('|')
|
|
1391
|
-
.includes('more'),
|
|
1392
|
-
);
|
|
1393
|
-
|
|
1394
|
-
/**
|
|
1395
|
-
* What a suggested value reads of the typed value: `true` when it is the
|
|
1396
|
-
* whole value under one of the gate's declared sources, an array of the
|
|
1397
|
-
* typed tokens it carries when it carries only part of them (one setting
|
|
1398
|
-
* of a value that holds several, as `Inter 600` holds a family and a
|
|
1399
|
-
* weight), or `false` when it reads nothing typed.
|
|
1400
|
-
*
|
|
1401
|
-
* The declared sources: (1) the value typed, under the exact readings of
|
|
1402
|
-
* typedReading, sameToken and numbersEqual, the words joined into a CSS
|
|
1403
|
-
* color name (`dark red` is darkred), or the color the typed color is
|
|
1404
|
-
* (rgb() as its hex, a see-through color opaque, Tailwind's `black/5` as
|
|
1405
|
-
* rgb(0 0 0 / 0.05)); (2) a documented table
|
|
1406
|
-
* registered with documentReadings, documentNumericTable or documentRange;
|
|
1407
|
-
* (3) the theme's own saved value of the line's setting, for a setting
|
|
1408
|
-
* other than the one typed (a keep or unlock line); (4) the setting's
|
|
1409
|
-
* documented fallback, or a step (`more`, `less`) on a dial with a scale,
|
|
1410
|
-
* offered for that dial or one of its tokens; (5) the correction of a
|
|
1411
|
-
* typo (typoOf); (6) the typed value with each color written as one mode's
|
|
1412
|
-
* half of `light-dark()` (a shadow's color for one mode, which no color
|
|
1413
|
-
* longhand holds), the other half anything. Several values of one kind typed for a setting that takes
|
|
1414
|
-
* one (`1.5 2`) may be read as the first. (8) The words of the typed value
|
|
1415
|
-
* that name the line's own part or state (`scope`: `card 8px`, `red on
|
|
1416
|
-
* button`, `button:hover: red`) are carried by its --on and --state, so the
|
|
1417
|
-
* rest is the value read.
|
|
1418
|
-
* @param {{offered: string, typed: string, thing?: string, typedThing?: string, saved?: Map<string, string[]>, mode?: string, scope?: {on?: string, state?: string}}} line
|
|
1419
|
-
* @returns {true | number[] | {oneOf: number[]} | false}
|
|
1420
|
-
*/
|
|
1421
|
-
export function readsTyped(line) {
|
|
1422
|
-
const read = readsTypedValue(line);
|
|
1423
|
-
if (read === true || !line.scope?.on) return read;
|
|
1424
|
-
const typed = String(line.typed).trim();
|
|
1425
|
-
const on = String(line.scope.on).toLowerCase();
|
|
1426
|
-
const typedThing = String(line.typedThing ?? '').toLowerCase();
|
|
1427
|
-
const esc = (/** @type {string} */ x) =>
|
|
1428
|
-
x.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
1429
|
-
// The part typed in the setting's place (`card padding 20px`): the
|
|
1430
|
-
// line's --on carries the part, and its setting the name typed first in
|
|
1431
|
-
// the value, so the rest is the value read.
|
|
1432
|
-
const partFirst =
|
|
1433
|
-
typedThing &&
|
|
1434
|
-
[
|
|
1435
|
-
typedThing,
|
|
1436
|
-
typedThing.replace(/e?s$/, ''),
|
|
1437
|
-
typedThing.replace(/s$/, ''),
|
|
1438
|
-
].includes(on) &&
|
|
1439
|
-
line.thing
|
|
1440
|
-
? typed.replace(
|
|
1441
|
-
new RegExp(`^${esc(String(line.thing))}\\s*(?:[:=]\\s*|\\s+)`, 'i'),
|
|
1442
|
-
'',
|
|
1443
|
-
)
|
|
1444
|
-
: null;
|
|
1445
|
-
for (const rest of [withoutScopeWords(typed, line.scope), partFirst])
|
|
1446
|
-
if (
|
|
1447
|
-
rest != null &&
|
|
1448
|
-
rest !== typed &&
|
|
1449
|
-
rest.trim() &&
|
|
1450
|
-
readsTypedValue({
|
|
1451
|
-
...line,
|
|
1452
|
-
typed: rest.replace(/\s*;\s*$/, ''),
|
|
1453
|
-
typedThing: line.thing,
|
|
1454
|
-
}) === true
|
|
1455
|
-
)
|
|
1456
|
-
return true;
|
|
1457
|
-
return read;
|
|
1458
|
-
}
|
|
1459
|
-
|
|
1460
|
-
/**
|
|
1461
|
-
* The typed value without the words that name a part (or one of its
|
|
1462
|
-
* plurals) and its state, as a label (`card: 8px`, `button = red`,
|
|
1463
|
-
* `button:hover: red`), after `on` (`red on button`), or as the first or
|
|
1464
|
-
* last word (`card 8px`, `8px card`); null when it names neither.
|
|
1465
|
-
* @param {string} typed
|
|
1466
|
-
* @param {{on?: string, state?: string}} scope
|
|
1467
|
-
* @returns {string | null}
|
|
1468
|
-
*/
|
|
1469
|
-
function withoutScopeWords(typed, scope) {
|
|
1470
|
-
const on = String(scope.on ?? '').toLowerCase();
|
|
1471
|
-
if (!on) return null;
|
|
1472
|
-
const esc = (/** @type {string} */ x) =>
|
|
1473
|
-
x.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
1474
|
-
const words = on.split('-').map(esc).join('[\\s-]+');
|
|
1475
|
-
const part = `(?:${words})(?:e?s)?`;
|
|
1476
|
-
const state = scope.state ? `(?::+\\s*${esc(scope.state)})?` : '';
|
|
1477
|
-
const t = typed.trim();
|
|
1478
|
-
for (const re of [
|
|
1479
|
-
new RegExp(`^${part}${state}\\s*[:=]\\s*(\\S.*)$`, 'i'),
|
|
1480
|
-
new RegExp(`^(\\S.*?)\\s+on\\s+(?:the\\s+)?${part}$`, 'i'),
|
|
1481
|
-
new RegExp(`^${part}\\s+(\\S.*)$`, 'i'),
|
|
1482
|
-
new RegExp(`^(\\S.*?)\\s+${part}$`, 'i'),
|
|
1483
|
-
]) {
|
|
1484
|
-
const m = re.exec(t);
|
|
1485
|
-
if (m) return m[1].trim();
|
|
1486
|
-
}
|
|
1487
|
-
return null;
|
|
1488
|
-
}
|
|
1489
|
-
|
|
1490
|
-
/** @param {{offered: string, typed: string, thing?: string, typedThing?: string, saved?: Map<string, string[]>, mode?: string}} line */
|
|
1491
|
-
function readsTypedValue({
|
|
1492
|
-
offered,
|
|
1493
|
-
typed,
|
|
1494
|
-
thing = '',
|
|
1495
|
-
typedThing = '',
|
|
1496
|
-
saved,
|
|
1497
|
-
mode = '',
|
|
1498
|
-
}) {
|
|
1499
|
-
const o = String(offered)
|
|
1500
|
-
.trim()
|
|
1501
|
-
.replace(/^(["'])(.*)\1$/, '$2')
|
|
1502
|
-
.toLowerCase();
|
|
1503
|
-
const key = String(thing).toLowerCase();
|
|
1504
|
-
const typedKey = String(typedThing || thing).toLowerCase();
|
|
1505
|
-
const t = typedReading(listWords(typedKey, typed), {
|
|
1506
|
-
thousands: thousandsSetting(typedKey),
|
|
1507
|
-
});
|
|
1508
|
-
const T = tokensOf(t);
|
|
1509
|
-
const O = tokensOf(o);
|
|
1510
|
-
// (1) The value typed: a variable with a fallback as typed too, since
|
|
1511
|
-
// the fallback reading is for an app's own variable, never in place of a
|
|
1512
|
-
// design token typed with one.
|
|
1513
|
-
const asTyped = tokensOf(
|
|
1514
|
-
typedReading(listWords(typedKey, typed), {
|
|
1515
|
-
thousands: thousandsSetting(typedKey),
|
|
1516
|
-
fallback: false,
|
|
1517
|
-
}),
|
|
1518
|
-
);
|
|
1519
|
-
if (
|
|
1520
|
-
O.length &&
|
|
1521
|
-
[T, asTyped].some(
|
|
1522
|
-
R => O.length === R.length && O.every((w, i) => sameToken(w, R[i])),
|
|
1523
|
-
)
|
|
1524
|
-
)
|
|
1525
|
-
return true;
|
|
1526
|
-
// (6) The typed color as one mode's half of light-dark(), every pair
|
|
1527
|
-
// read as the same mode's half.
|
|
1528
|
-
for (const side of [0, 1]) {
|
|
1529
|
-
const halves = o.replace(
|
|
1530
|
-
/light-dark\(\s*([^,()]+(?:\([^()]*\))?)\s*,\s*([^,()]+(?:\([^()]*\))?)\s*\)/g,
|
|
1531
|
-
(_, light, dark) => String(side === 0 ? light : dark).trim(),
|
|
1532
|
-
);
|
|
1533
|
-
const H = halves === o ? [] : tokensOf(halves);
|
|
1534
|
-
if (
|
|
1535
|
-
H.length &&
|
|
1536
|
-
H.length === T.length &&
|
|
1537
|
-
H.every((w, i) => sameToken(w, T[i]))
|
|
1538
|
-
)
|
|
1539
|
-
return true;
|
|
1540
|
-
}
|
|
1541
|
-
// Words joined into one name (`dark red` is darkred); numbers are never
|
|
1542
|
-
// joined (`12 16px` is two sizes, not 1216px).
|
|
1543
|
-
if (
|
|
1544
|
-
O.length === 1 &&
|
|
1545
|
-
T.length > 1 &&
|
|
1546
|
-
T.every(w => /^[a-z]+$/.test(w)) &&
|
|
1547
|
-
O[0] === T.join('')
|
|
1548
|
-
)
|
|
1549
|
-
return true;
|
|
1550
|
-
if (numbersEqual(o, t)) return true;
|
|
1551
|
-
// Two bare numbers typed with a comma between, offered as the slash pair
|
|
1552
|
-
// a grammar takes (`16,9` is `16 / 9`): the comma separated them.
|
|
1553
|
-
{
|
|
1554
|
-
const pair = /^\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*$/.exec(
|
|
1555
|
-
String(typed).replace(/^(["'])(.*)\1$/, '$2'),
|
|
1556
|
-
);
|
|
1557
|
-
if (pair && o.replace(/\s+/g, '') === `${pair[1]}/${pair[2]}`) return true;
|
|
1558
|
-
}
|
|
1559
|
-
// (5) A typo of a documented word in a list, item by item (`Inter,
|
|
1560
|
-
// san-serif` is `Inter, sans-serif`; `sytem` is the system font).
|
|
1561
|
-
{
|
|
1562
|
-
const ti = t.split(',').map(x => x.trim());
|
|
1563
|
-
const oi = o.split(',').map(x => x.trim());
|
|
1564
|
-
const bare = (/** @type {string} */ x) => x.replace(/[\s_-]+/g, '');
|
|
1565
|
-
const same = (/** @type {string} */ a, /** @type {string} */ b) => {
|
|
1566
|
-
const A = tokensOf(a);
|
|
1567
|
-
const B = tokensOf(b);
|
|
1568
|
-
return A.length === B.length && A.every((w, i) => sameToken(w, B[i]));
|
|
1569
|
-
};
|
|
1570
|
-
if (
|
|
1571
|
-
ti.length === oi.length &&
|
|
1572
|
-
oi.some((x, i) => !same(x, ti[i])) &&
|
|
1573
|
-
oi.every(
|
|
1574
|
-
(x, i) =>
|
|
1575
|
-
same(x, ti[i]) ||
|
|
1576
|
-
(TYPO_WORDS.has(x) &&
|
|
1577
|
-
(bare(x) === bare(ti[i]) ||
|
|
1578
|
-
typoNear(ti[i], x) ||
|
|
1579
|
-
typoNear(bare(ti[i]), bare(x)))),
|
|
1580
|
-
)
|
|
1581
|
-
)
|
|
1582
|
-
return true;
|
|
1583
|
-
}
|
|
1584
|
-
// (7) A var() reference whose name is a typo of the one offered
|
|
1585
|
-
// (`var(--color-eror)` is var(--color-error)), the rest as typed.
|
|
1586
|
-
{
|
|
1587
|
-
const VAR = /var\(\s*(--[a-z0-9-]+)/g;
|
|
1588
|
-
const tNames = [...t.matchAll(VAR)].map(m => m[1]);
|
|
1589
|
-
const oNames = [...o.matchAll(VAR)].map(m => m[1]);
|
|
1590
|
-
if (
|
|
1591
|
-
tNames.length &&
|
|
1592
|
-
tNames.length === oNames.length &&
|
|
1593
|
-
t.replace(VAR, 'var(') === o.replace(VAR, 'var(') &&
|
|
1594
|
-
tNames.some((x, i) => x !== oNames[i]) &&
|
|
1595
|
-
tNames.every((x, i) => x === oNames[i] || typoNear(x, oNames[i]))
|
|
1596
|
-
)
|
|
1597
|
-
return true;
|
|
1598
|
-
}
|
|
1599
|
-
const color = parseColor(t);
|
|
1600
|
-
if (
|
|
1601
|
-
/^#[0-9a-f]{3,8}$/.test(o) &&
|
|
1602
|
-
(color ? hex(color) === o : colorProblem(t)?.opaque === o)
|
|
1603
|
-
)
|
|
1604
|
-
return true;
|
|
1605
|
-
// A color with an opacity after its slash, as Tailwind writes it
|
|
1606
|
-
// (`black/5`), is that color at that opacity, as CSS writes it.
|
|
1607
|
-
const clear = colorOpacitiesAsCss(t);
|
|
1608
|
-
if (clear && !clear.unread.length && clear.css.trim().toLowerCase() === o)
|
|
1609
|
-
return true;
|
|
1610
|
-
// One half of a light and a dark pair: for the line that sets that mode,
|
|
1611
|
-
// or, as the light one, for a color dial, which makes both modes from
|
|
1612
|
-
// one color.
|
|
1613
|
-
const half = /^light-dark\(\s*([^,()]+?)\s*,\s*([^,()]+?)\s*\)$/.exec(t);
|
|
1614
|
-
if (half) {
|
|
1615
|
-
const [light, dark] = [half[1], half[2]];
|
|
1616
|
-
const only = /^(light|dark)-only$/.exec(saved?.get('mode')?.[0] ?? '')?.[1];
|
|
1617
|
-
const shown = mode || only || '';
|
|
1618
|
-
if (shown === 'dark' ? o === dark : shown === 'light' ? o === light : false)
|
|
1619
|
-
return true;
|
|
1620
|
-
if (o === light && DIALS[key] && !key.startsWith('--')) return true;
|
|
1621
|
-
}
|
|
1622
|
-
// A base named by its package (@astryxdesign/theme-butter is butter).
|
|
1623
|
-
if (
|
|
1624
|
-
key === 'base' &&
|
|
1625
|
-
[
|
|
1626
|
-
t,
|
|
1627
|
-
String(typed)
|
|
1628
|
-
.trim()
|
|
1629
|
-
.replace(/^(["'])(.*)\1$/, '$2'),
|
|
1630
|
-
].some(x => baseWord(x) === o)
|
|
1631
|
-
)
|
|
1632
|
-
return true;
|
|
1633
|
-
// (2) A documented reading of the whole value.
|
|
1634
|
-
const whole = [t, t.replace(/\s+/g, '-'), t.replace(/[\s-]+/g, '')];
|
|
1635
|
-
if (whole.some(k => READINGS.get(k)?.has(o))) return true;
|
|
1636
|
-
// A scale factor written as the dial's help writes it (x0.75) is that
|
|
1637
|
-
// number.
|
|
1638
|
-
const n = T.length === 1 ? numberOf(T[0].replace(/^x(?=\.?\d)/, '')) : null;
|
|
1639
|
-
if (n) {
|
|
1640
|
-
for (const {things, table, domain} of NUMERIC_TABLES)
|
|
1641
|
-
if (names(things, key) && n.n >= domain[0] && n.n <= domain[1]) {
|
|
1642
|
-
const [rung] = table.reduce((a, b) =>
|
|
1643
|
-
Math.abs(b[1] - n.n) < Math.abs(a[1] - n.n) ? b : a,
|
|
1644
|
-
);
|
|
1645
|
-
if (rung === o) return true;
|
|
1646
|
-
}
|
|
1647
|
-
for (const {things, min, max} of RANGES)
|
|
1648
|
-
if (names(things, key))
|
|
1649
|
-
// Past an end is that end; a minus sign is no amount past the low
|
|
1650
|
-
// end (-700 is no weight 1).
|
|
1651
|
-
if (
|
|
1652
|
-
(n.n > max && o === String(max)) ||
|
|
1653
|
-
(n.n < min && n.n >= 0 && o === String(min))
|
|
1654
|
-
)
|
|
1655
|
-
return true;
|
|
1656
|
-
// A radius at or below zero is square; at or past half, or 1000px, pill.
|
|
1657
|
-
const signed = Number(/^-?\d*\.?\d+/.exec(t)?.[0] ?? NaN);
|
|
1658
|
-
if (key === 'corners' && o === 'square' && signed <= 0) return true;
|
|
1659
|
-
if (
|
|
1660
|
-
key === 'corners' &&
|
|
1661
|
-
o === 'pill' &&
|
|
1662
|
-
((n.unit === '%' && n.n >= 50) || n.n >= 1000)
|
|
1663
|
-
)
|
|
1664
|
-
return true;
|
|
1665
|
-
}
|
|
1666
|
-
// Rounding said with a negation (not round, unrounded, rounded-none).
|
|
1667
|
-
if (
|
|
1668
|
-
o === 'square' &&
|
|
1669
|
-
/round/.test(t) &&
|
|
1670
|
-
/(^|[\s-])(not|no|non|without|never|less)([\s-]|$)|^un|-none$/.test(t)
|
|
1671
|
-
)
|
|
1672
|
-
return true;
|
|
1673
|
-
// (3) A saved value of the line's own setting, never the one typed: as
|
|
1674
|
-
// saved, or under the readings a typed value has (`blod` saved for
|
|
1675
|
-
// headings is bold, as set reads it).
|
|
1676
|
-
if (
|
|
1677
|
-
key &&
|
|
1678
|
-
key !== typedKey &&
|
|
1679
|
-
saved
|
|
1680
|
-
?.get(key)
|
|
1681
|
-
?.some(
|
|
1682
|
-
v =>
|
|
1683
|
-
typedReading(v) === o ||
|
|
1684
|
-
readsTyped({offered: o, typed: v, thing: key, typedThing: key}) ===
|
|
1685
|
-
true,
|
|
1686
|
-
)
|
|
1687
|
-
)
|
|
1688
|
-
return true;
|
|
1689
|
-
// (4) A documented fallback; a step on a dial with a scale.
|
|
1690
|
-
if (Object.hasOwn(DECLARED_VALUES, key) && DECLARED_VALUES[key].includes(o))
|
|
1691
|
-
return true;
|
|
1692
|
-
if (
|
|
1693
|
-
/^(more|less)$/.test(t) &&
|
|
1694
|
-
SCALED_DIALS.includes(typedKey) &&
|
|
1695
|
-
(key === typedKey || key.startsWith('--'))
|
|
1696
|
-
)
|
|
1697
|
-
return true;
|
|
1698
|
-
// A CSS font shorthand typed for a font family setting is its family:
|
|
1699
|
-
// what follows the size (`italic 700 16px/1.4 Georgia, serif`).
|
|
1700
|
-
// For the setting typed (the font dial, a family token) that is the
|
|
1701
|
-
// whole reading; for one longhand of a part's font it is the family's
|
|
1702
|
-
// part, beside the lines for the other longhands.
|
|
1703
|
-
if (/^font(-family)?$|^--font-family/.test(key)) {
|
|
1704
|
-
const family = /(?:^|\s)\d*\.?\d+(?:px|rem|em|pt|%)(?:\/\S+)?\s+(.+)$/.exec(
|
|
1705
|
-
t,
|
|
1706
|
-
)?.[1];
|
|
1707
|
-
const F = family ? tokensOf(family) : [];
|
|
1708
|
-
if (F.length && F.length === O.length && O.every((w, i) => w === F[i]))
|
|
1709
|
-
return key === typedKey
|
|
1710
|
-
? true
|
|
1711
|
-
: Array.from({length: F.length}, (_, i) => T.length - F.length + i);
|
|
1712
|
-
}
|
|
1713
|
-
// (5) A typo, of a word or of a number's unit (16pxx is 16px).
|
|
1714
|
-
// The typed value is one word, dashes and all (`not-alowed` is one
|
|
1715
|
-
// keyword typed, not two words).
|
|
1716
|
-
if (/^[a-z][a-z-]*$/.test(t) && typoOf(t, o)) return true;
|
|
1717
|
-
const typedUnit = /^(\d*\.?\d+)([a-z]+)$/.exec(t);
|
|
1718
|
-
const offeredUnit = /^(\d*\.?\d+)([a-z]+)$/.exec(o);
|
|
1719
|
-
if (
|
|
1720
|
-
typedUnit &&
|
|
1721
|
-
offeredUnit &&
|
|
1722
|
-
typedUnit[1] === offeredUnit[1] &&
|
|
1723
|
-
// A unit is a short word: one edit, two letters swapped as one.
|
|
1724
|
-
levenshteinDistance(typedUnit[2], offeredUnit[2], 1, {swaps: true}) <= 1
|
|
1725
|
-
)
|
|
1726
|
-
return true;
|
|
1727
|
-
// Several values of one kind for a setting that takes one: the first
|
|
1728
|
-
// (the one reading the repair that offers it uses, sameKindValues).
|
|
1729
|
-
const same = sameKindValues(t);
|
|
1730
|
-
if (same) {
|
|
1731
|
-
const first = tokensOf(same.items[0]);
|
|
1732
|
-
if (first.length === O.length && O.every((w, i) => sameToken(w, first[i])))
|
|
1733
|
-
return true;
|
|
1734
|
-
}
|
|
1735
|
-
// Part of the value: a run of its tokens, or a documented reading of one.
|
|
1736
|
-
// (currentcolor stands for the color another line of the refusal sets:
|
|
1737
|
-
// the shape of a border in both modes, its color in one.)
|
|
1738
|
-
// Part of the value: its tokens in the order typed, the others carried
|
|
1739
|
-
// by the refusal's other lines (`Inter, sans-serif` and `semibold` for
|
|
1740
|
-
// `Inter 600, sans-serif`).
|
|
1741
|
-
const part = O.filter(w => w !== 'currentcolor');
|
|
1742
|
-
if (part.length && part.length < T.length)
|
|
1743
|
-
for (const same of [
|
|
1744
|
-
(/** @type {string} */ a, /** @type {string} */ b) => a === b,
|
|
1745
|
-
sameToken,
|
|
1746
|
-
]) {
|
|
1747
|
-
/** @type {number[]} */
|
|
1748
|
-
const at = [];
|
|
1749
|
-
for (const w of part) {
|
|
1750
|
-
const from = at.length ? at[at.length - 1] + 1 : 0;
|
|
1751
|
-
const i = T.findIndex((x, k) => k >= from && same(w, x));
|
|
1752
|
-
if (i < 0) break;
|
|
1753
|
-
at.push(i);
|
|
1754
|
-
}
|
|
1755
|
-
if (at.length === part.length) return at;
|
|
1756
|
-
}
|
|
1757
|
-
// One item of a list value, read through the one typo limit, offered as
|
|
1758
|
-
// its own line: the setting's dial takes that word alone (`blod tight`
|
|
1759
|
-
// for headings is the lines `bold` and `tight`).
|
|
1760
|
-
if (O.length === 1 && T.length > 1 && dialWords(key).has(O[0])) {
|
|
1761
|
-
const hits = T.flatMap((w, i) => (typoOf(w, O[0]) ? [i] : []));
|
|
1762
|
-
if (hits.length === 1) return hits;
|
|
1763
|
-
}
|
|
1764
|
-
// A family name the editor knows, typed with a typo, at the start of a
|
|
1765
|
-
// family with words after it (`georgia` for `Georiga Bold`): its tokens,
|
|
1766
|
-
// the words after carried by the refusal's other lines.
|
|
1767
|
-
if (/^font(-family)?$|^--font-family/.test(key) && TYPO_WORDS.has(o))
|
|
1768
|
-
for (let k = T.length - 1; k >= 1; k -= 1) {
|
|
1769
|
-
const head = T.slice(0, k).join(' ');
|
|
1770
|
-
const bare = (/** @type {string} */ x) => x.replace(/[\s_-]+/g, '');
|
|
1771
|
-
if (typoNear(head, o) || typoNear(bare(head), bare(o)))
|
|
1772
|
-
return Array.from({length: k}, (_, j) => j);
|
|
1773
|
-
}
|
|
1774
|
-
// A documented reading of a run of its tokens (`extra bold` in `Inter
|
|
1775
|
-
// extra bold` is heavy).
|
|
1776
|
-
if (T.length > 1)
|
|
1777
|
-
for (let k = T.length - 1; k >= 2; k -= 1)
|
|
1778
|
-
for (let i = 0; i + k <= T.length; i += 1) {
|
|
1779
|
-
const run = T.slice(i, i + k).join(' ');
|
|
1780
|
-
if (
|
|
1781
|
-
[run, run.replace(/\s+/g, '-'), run.replace(/\s+/g, '')].some(r =>
|
|
1782
|
-
READINGS.get(r)?.has(o),
|
|
1783
|
-
)
|
|
1784
|
-
)
|
|
1785
|
-
return Array.from({length: k}, (_, j) => i + j);
|
|
1786
|
-
}
|
|
1787
|
-
// A documented reading of one token (`900` or `black` is heavy): every
|
|
1788
|
-
// token that reads as it is a candidate, and coversTyped gives the line
|
|
1789
|
-
// the one the other lines leave.
|
|
1790
|
-
/** @type {number[]} */
|
|
1791
|
-
const oneOf = [];
|
|
1792
|
-
if (T.length > 1)
|
|
1793
|
-
for (const [i, w] of T.entries()) {
|
|
1794
|
-
if (READINGS.get(w)?.has(o)) {
|
|
1795
|
-
oneOf.push(i);
|
|
1796
|
-
continue;
|
|
1797
|
-
}
|
|
1798
|
-
// A weight word typed with a typo after a family (`Georgia Blod`) is
|
|
1799
|
-
// that weight, as the font reader reads it.
|
|
1800
|
-
if (
|
|
1801
|
-
i > 0 &&
|
|
1802
|
-
/^font(-family)?$|^--font-family/.test(typedKey) &&
|
|
1803
|
-
!READINGS.has(w) &&
|
|
1804
|
-
Object.keys(WEIGHT_WORDS).some(
|
|
1805
|
-
x => typoNear(w, x) && (x === o || READINGS.get(x)?.has(o)),
|
|
1806
|
-
)
|
|
1807
|
-
) {
|
|
1808
|
-
oneOf.push(i);
|
|
1809
|
-
continue;
|
|
1810
|
-
}
|
|
1811
|
-
if (numberOf(w) && numbersEqual(o, w)) {
|
|
1812
|
-
oneOf.push(i);
|
|
1813
|
-
continue;
|
|
1814
|
-
}
|
|
1815
|
-
const m = numberOf(w);
|
|
1816
|
-
if (m)
|
|
1817
|
-
for (const {things, table, domain} of NUMERIC_TABLES)
|
|
1818
|
-
if (names(things, key) && m.n >= domain[0] && m.n <= domain[1]) {
|
|
1819
|
-
const [rung] = table.reduce((a, b) =>
|
|
1820
|
-
Math.abs(b[1] - m.n) < Math.abs(a[1] - m.n) ? b : a,
|
|
1821
|
-
);
|
|
1822
|
-
if (rung === o) {
|
|
1823
|
-
oneOf.push(i);
|
|
1824
|
-
break;
|
|
1825
|
-
}
|
|
1826
|
-
}
|
|
1827
|
-
}
|
|
1828
|
-
if (oneOf.length === 1) return oneOf;
|
|
1829
|
-
if (oneOf.length > 1) return {oneOf};
|
|
1830
|
-
return false;
|
|
1831
|
-
}
|
|
1832
|
-
|
|
1833
|
-
/**
|
|
1834
|
-
* The words a dial takes alone, as its `values` lists them (placeholders
|
|
1835
|
-
* such as `<color>` aside); none for anything that is not a dial.
|
|
1836
|
-
* @param {string} key the setting, lowercased
|
|
1837
|
-
* @returns {Set<string>}
|
|
1838
|
-
*/
|
|
1839
|
-
function dialWords(key) {
|
|
1840
|
-
const values = Object.hasOwn(DIALS, key) ? String(DIALS[key].values) : '';
|
|
1841
|
-
return new Set(
|
|
1842
|
-
values
|
|
1843
|
-
.split('|')
|
|
1844
|
-
.map(w => w.trim().toLowerCase())
|
|
1845
|
-
.filter(w => /^[a-z][a-z-]*$/.test(w)),
|
|
1846
|
-
);
|
|
1847
|
-
}
|
|
1848
|
-
|
|
1849
|
-
/**
|
|
1850
|
-
* Whether the lines of one refusal, taken together, carry the whole typed
|
|
1851
|
-
* value: each reads it whole, or the parts they carry cover every typed
|
|
1852
|
-
* token. A part alone (`all` for `all 200`, `bold` for `extra bold`) is not
|
|
1853
|
-
* offered.
|
|
1854
|
-
* A line read whole that offers one of several typed values (`padding-top
|
|
1855
|
-
* 2px` for `2px 15px 3px`) carries the tokens it offers; a line that reads
|
|
1856
|
-
* the whole value carries none, so a part alone is still not offered.
|
|
1857
|
-
* @param {Array<true | number[] | {oneOf: number[]} | false>} reads
|
|
1858
|
-
* @param {string} typed
|
|
1859
|
-
* @param {string} [thing] the setting typed (its numbers' reading)
|
|
1860
|
-
* @param {Array<string | undefined>} [offered] each line's value, beside its read
|
|
1861
|
-
*/
|
|
1862
|
-
export function coversTyped(reads, typed, thing = '', offered = []) {
|
|
1863
|
-
const T = tokensOf(
|
|
1864
|
-
typedReading(listWords(thing, typed), {
|
|
1865
|
-
thousands: thousandsSetting(thing),
|
|
1866
|
-
}),
|
|
1867
|
-
);
|
|
1868
|
-
// By token, counted: two lines may each carry one of two equal words
|
|
1869
|
-
// (`normal normal 16px serif`).
|
|
1870
|
-
/** @type {Map<string, number>} */
|
|
1871
|
-
const carried = new Map();
|
|
1872
|
-
/** @param {number} i */
|
|
1873
|
-
const carry = i => carried.set(T[i], (carried.get(T[i]) ?? 0) + 1);
|
|
1874
|
-
/** @type {Map<string, number>} */
|
|
1875
|
-
const typedCount = new Map();
|
|
1876
|
-
for (const w of T) typedCount.set(w, (typedCount.get(w) ?? 0) + 1);
|
|
1877
|
-
for (const r of reads) if (Array.isArray(r)) r.forEach(carry);
|
|
1878
|
-
// (Only a line that offers fewer tokens than were typed: a line that
|
|
1879
|
-
// reads the whole value never makes a part alone offerable beside it.)
|
|
1880
|
-
for (const [i, r] of reads.entries()) {
|
|
1881
|
-
if (r !== true || offered[i] == null) continue;
|
|
1882
|
-
const own = tokensOf(String(offered[i]).replace(/^(["'])(.*)\1$/, '$2'));
|
|
1883
|
-
if (own.length >= T.length) continue;
|
|
1884
|
-
for (const w of own)
|
|
1885
|
-
if ((carried.get(w) ?? 0) < (typedCount.get(w) ?? 0))
|
|
1886
|
-
carried.set(w, (carried.get(w) ?? 0) + 1);
|
|
1887
|
-
}
|
|
1888
|
-
// A line whose one token may be any of several takes one the others
|
|
1889
|
-
// left.
|
|
1890
|
-
for (const r of reads)
|
|
1891
|
-
if (r && typeof r === 'object' && !Array.isArray(r)) {
|
|
1892
|
-
const left = r.oneOf.find(
|
|
1893
|
-
i => (carried.get(T[i]) ?? 0) < (typedCount.get(T[i]) ?? 0),
|
|
1894
|
-
);
|
|
1895
|
-
carry(left ?? r.oneOf[0]);
|
|
1896
|
-
}
|
|
1897
|
-
return [...typedCount].every(([w, n]) => (carried.get(w) ?? 0) >= n);
|
|
1898
|
-
}
|
|
1899
|
-
|
|
1900
|
-
/**
|
|
1901
|
-
* A headings value with `and` or `&` between its words (`bold and wide`):
|
|
1902
|
-
* the list it names, as the headings reader splits it.
|
|
1903
|
-
* @param {string} thing
|
|
1904
|
-
* @param {string} typed
|
|
1905
|
-
*/
|
|
1906
|
-
function listWords(thing, typed) {
|
|
1907
|
-
return thing === 'headings'
|
|
1908
|
-
? String(typed).replace(/\s+(?:and|&)\s+/gi, ', ')
|
|
1909
|
-
: typed;
|
|
1910
|
-
}
|
|
1911
|
-
|
|
1912
|
-
documentReadings([
|
|
1913
|
-
['more', 'less'],
|
|
1914
|
-
['less', 'more'],
|
|
1915
|
-
]);
|
|
1916
|
-
documentReadings(Object.entries(TAILWIND_RADIUS));
|
|
1917
|
-
documentReadings(Object.values(PROPERTY_WORDS).flatMap(m => Object.entries(m)));
|
|
1918
|
-
documentReadings(Object.entries(WEIGHT_WORDS));
|
|
1919
|
-
documentReadings(Object.entries(WEIGHT_WORDS).map(([w, n]) => [n, w]));
|
|
1920
|
-
documentReadings(Object.entries(DENSITY_WORDS));
|
|
1921
|
-
// A font dial word as a part's font-family is the stack it stands for.
|
|
1922
|
-
documentReadings(
|
|
1923
|
-
[...Object.keys(FONTS), ...Object.keys(OPEN_FONTS)].map(w => [
|
|
1924
|
-
w,
|
|
1925
|
-
/** @type {string} */ (fontStack(w)),
|
|
1926
|
-
]),
|
|
1927
|
-
);
|
|
1928
|
-
|
|
1929
|
-
/**
|
|
1930
|
-
* Whether a repair line may offer `offered` as the whole typed value (see
|
|
1931
|
-
* readsTyped).
|
|
1932
|
-
* @param {{offered: string, typed: string, thing?: string, typedThing?: string, saved?: Map<string, string[]>, mode?: string}} line
|
|
1933
|
-
*/
|
|
1934
|
-
export const allowedLine = line => readsTyped(line) === true;
|
|
1935
|
-
|
|
1936
|
-
/**
|
|
1937
|
-
* Whether a typed word is a typo of a word: both words of four letters or
|
|
1938
|
-
* more, within one edit for a typed word of five letters or fewer and two
|
|
1939
|
-
* for a longer one (`nnone` is none, `normall` normal; `line` is no none).
|
|
1940
|
-
* @param {string} typed
|
|
1941
|
-
* @param {string} word
|
|
1942
|
-
*/
|
|
1943
|
-
export function typoOf(typed, word) {
|
|
1944
|
-
const t = String(typed).trim().toLowerCase();
|
|
1945
|
-
const w = String(word).trim().toLowerCase();
|
|
1946
|
-
return (
|
|
1947
|
-
/^[a-z][a-z-]{3,}$/.test(t) && /^[a-z][a-z-]{3,}$/.test(w) && typoNear(t, w)
|
|
1948
|
-
);
|
|
1949
|
-
}
|
|
1950
|
-
|
|
1951
|
-
/**
|
|
1952
|
-
* The edits a typo of a typed word may hold: none for a word under four
|
|
1953
|
-
* letters (leading dashes aside), so only that word in other letters is
|
|
1954
|
-
* near it; one for a word of five letters or fewer; two for a longer one.
|
|
1955
|
-
* Every typo reading of the editor (a value, a setting, a part, a state, a
|
|
1956
|
-
* theme or token name) uses this limit.
|
|
1957
|
-
* @param {string} typed
|
|
1958
|
-
*/
|
|
1959
|
-
export const typoLimit = typed => {
|
|
1960
|
-
const n = String(typed).trim().replace(/^-+/, '').length;
|
|
1961
|
-
return n < 4 ? 0 : n <= 5 ? 1 : 2;
|
|
1962
|
-
};
|
|
1963
|
-
|
|
1964
|
-
/**
|
|
1965
|
-
* Whether `word` is within the typo limit of `typed` (two letters swapped
|
|
1966
|
-
* count as one edit, as typed: flxe is flex).
|
|
1967
|
-
* @param {string} typed
|
|
1968
|
-
* @param {string} word
|
|
1969
|
-
*/
|
|
1970
|
-
export function typoNear(typed, word) {
|
|
1971
|
-
const t = String(typed).trim().toLowerCase();
|
|
1972
|
-
const limit = typoLimit(t);
|
|
1973
|
-
return (
|
|
1974
|
-
levenshteinDistance(t, String(word).trim().toLowerCase(), limit, {
|
|
1975
|
-
swaps: true,
|
|
1976
|
-
}) <= limit
|
|
1977
|
-
);
|
|
1978
|
-
}
|