@astryxdesign/cli 0.7.18-canary.gfacee17 → 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 +5 -5
- 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 +2 -2
- package/api/icon/_adapter.mjs +6 -2
- package/api/icon/_authoring.mjs +22 -13
- package/api/icon/_default-look.mjs +15 -15
- package/api/icon/add/add.mjs +14 -9
- package/api/icon/icon.doc.mjs +7 -7
- package/api/icon/icon.type.d.mts +33 -33
- package/api/icon/icon.type.mjs +45 -45
- package/api/icon/iconAdd.doc.mjs +9 -10
- package/api/icon/iconMap.doc.mjs +11 -12
- package/api/icon/iconNewDefinition.doc.mjs +8 -8
- package/api/icon/iconNewSet.doc.mjs +5 -5
- package/api/icon/map/map.mjs +7 -7
- 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/integration/validate-integration.type.d.mts +6 -6
- package/api/integration/validate-integration.type.mjs +6 -6
- package/api/render/_adapter.d.mts +5 -3
- package/api/render/_adapter.mjs +64 -4
- package/api/render/render.doc.mjs +9 -2
- package/api/render/render.mjs +5 -2
- package/api/render/render.type.d.mts +17 -0
- package/api/render/render.type.mjs +10 -0
- 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/data-colors-in-renderers.doc.mjs +400 -0
- package/assets/docs/tree/font-setup.doc.mjs +30 -0
- package/assets/docs/tree/icon-sets.doc.mjs +2 -2
- package/assets/docs/tree/share-a-page.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/tree/visualization.doc.mjs +30 -0
- 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/doctor.mjs +2 -2
- package/clients/cli/commands/experimental/export.mjs +2 -1
- package/clients/cli/commands/experimental/icon.mjs +17 -17
- package/clients/cli/commands/experimental/index.mjs +0 -5
- package/clients/cli/commands/experimental/render-output.mjs +9 -0
- package/clients/cli/commands/experimental-export.doc.mjs +6 -0
- package/clients/cli/commands/experimental-icon-add.doc.mjs +8 -8
- package/clients/cli/commands/experimental-icon-map.doc.mjs +6 -6
- package/clients/cli/commands/experimental-icon-new-definition.doc.mjs +6 -6
- package/clients/cli/commands/experimental-icon-new-set.doc.mjs +3 -3
- package/clients/cli/commands/experimental-icon-use.doc.mjs +1 -1
- package/clients/cli/commands/experimental-icon.doc.mjs +6 -6
- 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/cli-self-docs.mjs +4 -4
- package/foundation/discovery/fixture-refs.d.mts +51 -0
- package/foundation/discovery/fixture-refs.mjs +351 -0
- package/foundation/discovery/icon-definition-discovery.mjs +7 -7
- package/foundation/discovery/icon-ranking.mjs +2 -2
- package/foundation/discovery/icon-set-checks.mjs +6 -6
- package/foundation/discovery/icon-set-discovery.mjs +2 -2
- package/foundation/discovery/icon-set-writer.d.mts +24 -7
- package/foundation/discovery/icon-set-writer.mjs +75 -31
- 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/bundle.mjs +2 -2
- 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 +152 -0
- package/foundation/pictures/pictures.mjs +565 -0
- 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 +18 -15
- package/foundation/render/picture-playwright.mjs +10 -7
- package/foundation/render/picture-window.d.mts +13 -0
- package/foundation/render/picture-window.mjs +31 -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 +8 -69
- 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 -33291
- 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 -2294
- 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 -549
- package/api/theme-edit/_preview.mjs +0 -3264
- 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 -18435
- 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
package/api/theme-edit/_css.mjs
DELETED
|
@@ -1,1167 +0,0 @@
|
|
|
1
|
-
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @file CSS knowledge for `astryx experimental theme`, read from css-tree
|
|
5
|
-
* and the MDN data it ships: the property names a theme may set on a part,
|
|
6
|
-
* and whether a value matches a property's grammar. css-tree loads on the
|
|
7
|
-
* first check, so commands that never check CSS do not pay for it.
|
|
8
|
-
* @position api/theme-edit — pure helpers
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import {createRequire} from 'node:module';
|
|
12
|
-
import {wordTable} from './_tables.mjs';
|
|
13
|
-
import {
|
|
14
|
-
coversProperty,
|
|
15
|
-
cssTree,
|
|
16
|
-
longhandsOf,
|
|
17
|
-
mdnCssProperties,
|
|
18
|
-
} from '../../foundation/runtime/css-tree.mjs';
|
|
19
|
-
|
|
20
|
-
export {coversProperty, longhandsOf};
|
|
21
|
-
export {cssNumber, splitTop} from '../../foundation/color/css-color.mjs';
|
|
22
|
-
import {splitTop} from '../../foundation/color/css-color.mjs';
|
|
23
|
-
|
|
24
|
-
const require = createRequire(import.meta.url);
|
|
25
|
-
|
|
26
|
-
// Resolved when this module loads, before a command stats any file, as
|
|
27
|
-
// css-tree is (foundation/runtime/css-tree.mjs).
|
|
28
|
-
const SYNTAX_PATCHES =
|
|
29
|
-
require.resolve('@csstools/css-syntax-patches-for-csstree');
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Properties browsers ship that MDN does not list yet (Chrome experiments).
|
|
33
|
-
* A name here is a property a theme may set; it has no grammar to match.
|
|
34
|
-
*/
|
|
35
|
-
const NOT_IN_MDN_YET = [
|
|
36
|
-
'border-shape',
|
|
37
|
-
'flex-line-count',
|
|
38
|
-
'frame-sizing',
|
|
39
|
-
'link-parameters',
|
|
40
|
-
'path-length',
|
|
41
|
-
'scroll-axis-lock',
|
|
42
|
-
'text-fit',
|
|
43
|
-
'timeline-trigger-activation-range',
|
|
44
|
-
'timeline-trigger-activation-range-end',
|
|
45
|
-
'timeline-trigger-activation-range-start',
|
|
46
|
-
'timeline-trigger-active-range',
|
|
47
|
-
'timeline-trigger-active-range-end',
|
|
48
|
-
'timeline-trigger-active-range-start',
|
|
49
|
-
'view-transition-scope',
|
|
50
|
-
'window-drag',
|
|
51
|
-
];
|
|
52
|
-
|
|
53
|
-
/** css-tree, loaded once. */
|
|
54
|
-
const csstree = cssTree;
|
|
55
|
-
|
|
56
|
-
/** The MDN property data css-tree reads, at the version css-tree pins. */
|
|
57
|
-
const mdnProperties = mdnCssProperties;
|
|
58
|
-
|
|
59
|
-
/** @type {ReadonlySet<string> | null} */
|
|
60
|
-
let names = null;
|
|
61
|
-
/**
|
|
62
|
-
* The standard CSS property names: every property css-tree's MDN data
|
|
63
|
-
* lists that is not vendor-prefixed or obsolete, plus NOT_IN_MDN_YET. A
|
|
64
|
-
* name outside it is not a CSS property, so the command refuses it.
|
|
65
|
-
* @returns {ReadonlySet<string>}
|
|
66
|
-
*/
|
|
67
|
-
export function cssProperties() {
|
|
68
|
-
if (names) return names;
|
|
69
|
-
const mdn = mdnProperties();
|
|
70
|
-
const current = syntaxPatches().properties;
|
|
71
|
-
names = new Set([
|
|
72
|
-
...Object.keys(csstree().lexer.properties).filter(
|
|
73
|
-
k =>
|
|
74
|
-
!k.startsWith('-') &&
|
|
75
|
-
mdn[k] &&
|
|
76
|
-
// MDN marks a renamed property obsolete (font-stretch is font-width
|
|
77
|
-
// now); one the current specifications still define, as browsers
|
|
78
|
-
// ship it, stays.
|
|
79
|
-
(mdn[k].status !== 'obsolete' || k in current),
|
|
80
|
-
),
|
|
81
|
-
...NOT_IN_MDN_YET,
|
|
82
|
-
]);
|
|
83
|
-
return names;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** @type {ReadonlySet<string> | null} */
|
|
87
|
-
let vendorNames = null;
|
|
88
|
-
/**
|
|
89
|
-
* The vendor-prefixed property names css-tree's data lists
|
|
90
|
-
* (`-webkit-line-clamp`, `-moz-appearance`).
|
|
91
|
-
* @returns {ReadonlySet<string>}
|
|
92
|
-
*/
|
|
93
|
-
export function vendorProperties() {
|
|
94
|
-
vendorNames ??= new Set(
|
|
95
|
-
Object.keys(csstree().lexer.properties).filter(k => /^-[a-z]+-/.test(k)),
|
|
96
|
-
);
|
|
97
|
-
return vendorNames;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* The current specifications' grammar, as patches to css-tree's: the
|
|
102
|
-
* ranges MDN's data leaves out (`line-height` takes `<number [0,∞]>`).
|
|
103
|
-
* @returns {{properties: Record<string, string>, types: Record<string, string>, atrules: Record<string, unknown>}}
|
|
104
|
-
*/
|
|
105
|
-
const syntaxPatches = () => require(SYNTAX_PATCHES).next;
|
|
106
|
-
|
|
107
|
-
/** @type {any} */
|
|
108
|
-
let patched = null;
|
|
109
|
-
/** css-tree's lexer with the current specifications' grammar. */
|
|
110
|
-
const currentLexer = () => (patched ??= csstree().fork(syntaxPatches()).lexer);
|
|
111
|
-
|
|
112
|
-
/** What a var() or env() may stand for while the rest of a value is matched. */
|
|
113
|
-
const VAR_STANDS_FOR = ['0', '1px', '1ms', 'red', 'ease', 'serif', '0 0 red'];
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* The value as it reads when each variable is unset: every `var(--x, f)`
|
|
117
|
-
* replaced by its fallback `f`; null when no var() has one.
|
|
118
|
-
* @param {string} value
|
|
119
|
-
* @returns {string | null}
|
|
120
|
-
*/
|
|
121
|
-
export function withFallbacks(value) {
|
|
122
|
-
let any = false;
|
|
123
|
-
/** @param {string} v @returns {string} */
|
|
124
|
-
const pass = v => {
|
|
125
|
-
const at = v.search(/var\(/i);
|
|
126
|
-
if (at < 0) return v;
|
|
127
|
-
let depth = 0;
|
|
128
|
-
let comma = -1;
|
|
129
|
-
let end = -1;
|
|
130
|
-
for (let i = at + 4; i < v.length; i++) {
|
|
131
|
-
const c = v[i];
|
|
132
|
-
if (c === '(') depth++;
|
|
133
|
-
else if (c === ')') {
|
|
134
|
-
if (depth === 0) {
|
|
135
|
-
end = i;
|
|
136
|
-
break;
|
|
137
|
-
}
|
|
138
|
-
depth--;
|
|
139
|
-
} else if (c === ',' && depth === 0 && comma < 0) comma = i;
|
|
140
|
-
}
|
|
141
|
-
if (end < 0) return v;
|
|
142
|
-
if (comma < 0) return v.slice(0, end + 1) + pass(v.slice(end + 1));
|
|
143
|
-
any = true;
|
|
144
|
-
return (
|
|
145
|
-
v.slice(0, at) +
|
|
146
|
-
pass(v.slice(comma + 1, end).trim()) +
|
|
147
|
-
pass(v.slice(end + 1))
|
|
148
|
-
);
|
|
149
|
-
};
|
|
150
|
-
const out = pass(value);
|
|
151
|
-
return any ? out : null;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Whether a value matches a property's grammar, as css-tree reads it. A
|
|
156
|
-
* var() or env() can hold anything, so the value matches when some
|
|
157
|
-
* ordinary value in its place does. Returns the parsed value on a match, for range checks the
|
|
158
|
-
* grammar does not make.
|
|
159
|
-
* @param {string} property
|
|
160
|
-
* @param {string} value
|
|
161
|
-
* @returns {any | null}
|
|
162
|
-
*/
|
|
163
|
-
export function matchValue(property, value) {
|
|
164
|
-
const standard = standardSyntax(property);
|
|
165
|
-
return matchWith(value, (lexer, ast) =>
|
|
166
|
-
standard ? lexer.match(standard, ast) : lexer.matchProperty(property, ast),
|
|
167
|
-
);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/** @type {Map<string, string | null>} */
|
|
171
|
-
const standardCache = new Map();
|
|
172
|
-
/**
|
|
173
|
-
* A property's grammar without its legacy Internet Explorer alternatives
|
|
174
|
-
* (`filter`'s `<-ms-filter-function-list>` takes any word), or null when it
|
|
175
|
-
* has none.
|
|
176
|
-
* @param {string} property
|
|
177
|
-
* @returns {string | null}
|
|
178
|
-
*/
|
|
179
|
-
function standardSyntax(property) {
|
|
180
|
-
if (standardCache.has(property))
|
|
181
|
-
return /** @type {string | null} */ (standardCache.get(property));
|
|
182
|
-
const {definitionSyntax} = csstree();
|
|
183
|
-
const syntax = propertyGrammar(property);
|
|
184
|
-
/** @type {string | null} */
|
|
185
|
-
let out = null;
|
|
186
|
-
if (syntax?.type === 'Group' && syntax.combinator === '|') {
|
|
187
|
-
const kept = syntax.terms.filter(
|
|
188
|
-
(/** @type {any} */ t) => !(t.type === 'Type' && /^-ms-/.test(t.name)),
|
|
189
|
-
);
|
|
190
|
-
if (kept.length && kept.length < syntax.terms.length)
|
|
191
|
-
out = kept
|
|
192
|
-
.map((/** @type {any} */ t) => definitionSyntax.generate(t))
|
|
193
|
-
.join(' | ');
|
|
194
|
-
}
|
|
195
|
-
standardCache.set(property, out);
|
|
196
|
-
return out;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Whether a value is one value of a CSS type (`time`, `easing-function`,
|
|
201
|
-
* `length-percentage`), never a list. Returns the parsed value on a match.
|
|
202
|
-
* @param {string} type
|
|
203
|
-
* @param {string} value
|
|
204
|
-
* @returns {any | null}
|
|
205
|
-
*/
|
|
206
|
-
export function matchType(type, value) {
|
|
207
|
-
return matchWith(value, (lexer, ast) => lexer.matchType(type, ast));
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* A value matched by `match`, with each var() tried as an ordinary value.
|
|
212
|
-
* @param {string} value
|
|
213
|
-
* @param {(lexer: any, ast: any) => {error: unknown}} match
|
|
214
|
-
* @returns {any | null}
|
|
215
|
-
*/
|
|
216
|
-
function matchWith(value, match) {
|
|
217
|
-
const {lexer, parse} = csstree();
|
|
218
|
-
// css-tree's lexer forgives a last word ending in an old browser hack's
|
|
219
|
-
// escape (`8px\9`, `none\0`); a browser reads the escape as part of the
|
|
220
|
-
// word (a unit `px` and a tab), so it drops the value.
|
|
221
|
-
if (/\\[09]\s*$/.test(value)) return null;
|
|
222
|
-
// A color function holding a variable (color-mix(…, var(--x))) is a
|
|
223
|
-
// color whatever the variable holds: it is matched as one, so a value
|
|
224
|
-
// with many of them is read like the same value with plain colors.
|
|
225
|
-
for (let i = 0; i < 4; i++) {
|
|
226
|
-
const next = value.replace(
|
|
227
|
-
/\b(?:color-mix|light-dark|rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\((?:[^()]|\((?:[^()]|\([^()]*\))*\))*\)/gi,
|
|
228
|
-
m => (/\bvar\(/i.test(m) ? 'red' : m),
|
|
229
|
-
);
|
|
230
|
-
if (next === value) break;
|
|
231
|
-
value = next;
|
|
232
|
-
}
|
|
233
|
-
const refs = varReferences(value);
|
|
234
|
-
const vars = refs.map(r => r.text);
|
|
235
|
-
/**
|
|
236
|
-
* The value with each reference replaced by its stand-in, each set apart
|
|
237
|
-
* by spaces: a reference is its tokens, so the text glued to it
|
|
238
|
-
* (`var(--x)px`) stays a token of its own, as the browser reads it.
|
|
239
|
-
* @param {string[]} stands
|
|
240
|
-
*/
|
|
241
|
-
const standIn = stands => {
|
|
242
|
-
let out = '';
|
|
243
|
-
let last = 0;
|
|
244
|
-
refs.forEach((r, i) => {
|
|
245
|
-
out += `${value.slice(last, r.at)} ${stands[i]} `;
|
|
246
|
-
last = r.at + r.text.length;
|
|
247
|
-
});
|
|
248
|
-
return out + value.slice(last);
|
|
249
|
-
};
|
|
250
|
-
/** @param {string} v */
|
|
251
|
-
const tryOne = v => {
|
|
252
|
-
try {
|
|
253
|
-
const ast = parse(v, {context: 'value'});
|
|
254
|
-
// Each math function is matched as a plain value of its type, so
|
|
255
|
-
// math css-tree's grammar does not know (pow(), sqrt()) is read too.
|
|
256
|
-
const plain = mathAsValues(ast);
|
|
257
|
-
return plain?.every(
|
|
258
|
-
x => !match(lexer, parse(x, {context: 'value'})).error,
|
|
259
|
-
)
|
|
260
|
-
? ast
|
|
261
|
-
: null;
|
|
262
|
-
} catch {
|
|
263
|
-
return null;
|
|
264
|
-
}
|
|
265
|
-
};
|
|
266
|
-
if (!vars.length) return tryOne(value);
|
|
267
|
-
// A value that is only a variable can stand for any value, also on a
|
|
268
|
-
// property that takes only keywords (display, font-weight).
|
|
269
|
-
if (vars.length === 1 && value.trim() === vars[0]) {
|
|
270
|
-
try {
|
|
271
|
-
return parse(value, {context: 'value'});
|
|
272
|
-
} catch {
|
|
273
|
-
return null;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
// Many variables: each stands for the same kind of value at once.
|
|
277
|
-
if (vars.length > 3) {
|
|
278
|
-
for (const stand of VAR_STANDS_FOR) {
|
|
279
|
-
const ast = tryOne(standIn(vars.map(() => stand)));
|
|
280
|
-
if (ast) return ast;
|
|
281
|
-
}
|
|
282
|
-
return null;
|
|
283
|
-
}
|
|
284
|
-
/** @param {string[]} stands @returns {any | null} */
|
|
285
|
-
const fill = stands => {
|
|
286
|
-
if (stands.length === vars.length) return tryOne(standIn(stands));
|
|
287
|
-
for (const stand of VAR_STANDS_FOR) {
|
|
288
|
-
const ast = fill([...stands, stand]);
|
|
289
|
-
if (ast) return ast;
|
|
290
|
-
}
|
|
291
|
-
return null;
|
|
292
|
-
};
|
|
293
|
-
return fill([]);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Each var() or env() reference in a value, with where it starts, as CSS
|
|
298
|
-
* reads one: a function token, so `var(` glued after a letter, a digit, a
|
|
299
|
-
* dot or a dash (`2var(--x)`, `8pxvar(--x)`) is no reference (CSS reads a
|
|
300
|
-
* dimension or a name there).
|
|
301
|
-
* @param {string} value
|
|
302
|
-
* @returns {Array<{at: number, text: string}>}
|
|
303
|
-
*/
|
|
304
|
-
export function varReferences(value) {
|
|
305
|
-
return [
|
|
306
|
-
...String(value).matchAll(
|
|
307
|
-
/(?<![\w.-])(?:var|env)\([^()]*(?:\([^()]*\)[^()]*)*\)/gi,
|
|
308
|
-
),
|
|
309
|
-
].map(m => ({at: /** @type {number} */ (m.index), text: m[0]}));
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* Whether a value that matches a property's grammar is still below a range
|
|
314
|
-
* the specs set and css-tree's data does not carry: a `<line-width>` (a
|
|
315
|
-
* border or outline width) and a duration are never negative, nor is any
|
|
316
|
-
* number the current specifications' grammar ranges at 0 or more.
|
|
317
|
-
* @param {string} property
|
|
318
|
-
* @param {string} value
|
|
319
|
-
*/
|
|
320
|
-
export function belowRange(property, value) {
|
|
321
|
-
const {lexer, parse} = csstree();
|
|
322
|
-
/** @type {any} */
|
|
323
|
-
let ast;
|
|
324
|
-
try {
|
|
325
|
-
ast = parse(value, {context: 'value'});
|
|
326
|
-
} catch {
|
|
327
|
-
return false;
|
|
328
|
-
}
|
|
329
|
-
const r = lexer.matchProperty(property, ast);
|
|
330
|
-
if (r.error || !r.matched) return false;
|
|
331
|
-
let below = false;
|
|
332
|
-
/** @param {any} m @param {boolean} inWidth */
|
|
333
|
-
const visit = (m, inWidth) => {
|
|
334
|
-
const here =
|
|
335
|
-
inWidth || (m.syntax?.type === 'Type' && m.syntax.name === 'line-width');
|
|
336
|
-
if (here && typeof m.token === 'string' && m.token.startsWith('-'))
|
|
337
|
-
below = true;
|
|
338
|
-
for (const c of m.match ?? []) visit(c, here);
|
|
339
|
-
};
|
|
340
|
-
visit(r.matched, false);
|
|
341
|
-
if (
|
|
342
|
-
/-duration$/.test(property) &&
|
|
343
|
-
numbersIn(ast).some(n => n.value < 0 && ['s', 'ms'].includes(n.unit))
|
|
344
|
-
)
|
|
345
|
-
below = true;
|
|
346
|
-
// A negative number the current grammar's range refuses (`line-height
|
|
347
|
-
// -1`, `gap -2px`).
|
|
348
|
-
// Only the sign decides: the same value with every number made positive
|
|
349
|
-
// must match the current grammar.
|
|
350
|
-
if (!below && numbersIn(ast).some(n => n.value < 0)) {
|
|
351
|
-
const lexer = currentLexer();
|
|
352
|
-
const positive = parse(value.replace(/(^|[\s(,/])-(?=\.?\d)/g, '$1'), {
|
|
353
|
-
context: 'value',
|
|
354
|
-
});
|
|
355
|
-
below =
|
|
356
|
-
Boolean(lexer.matchProperty(property, ast).error) &&
|
|
357
|
-
!lexer.matchProperty(property, positive).error;
|
|
358
|
-
}
|
|
359
|
-
return below;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
/** CSS math functions, whose insides css-tree's grammar does not check. */
|
|
363
|
-
const MATH = new Set([
|
|
364
|
-
'calc',
|
|
365
|
-
'min',
|
|
366
|
-
'max',
|
|
367
|
-
'clamp',
|
|
368
|
-
'round',
|
|
369
|
-
'mod',
|
|
370
|
-
'rem',
|
|
371
|
-
'abs',
|
|
372
|
-
'sign',
|
|
373
|
-
'pow',
|
|
374
|
-
'sqrt',
|
|
375
|
-
'hypot',
|
|
376
|
-
'log',
|
|
377
|
-
'exp',
|
|
378
|
-
'sin',
|
|
379
|
-
'cos',
|
|
380
|
-
'tan',
|
|
381
|
-
'asin',
|
|
382
|
-
'acos',
|
|
383
|
-
'atan',
|
|
384
|
-
'atan2',
|
|
385
|
-
]);
|
|
386
|
-
|
|
387
|
-
/** The CSS type of each unit a math function may hold. */
|
|
388
|
-
const UNIT_TYPES = /** @type {Record<string, string>} */ (
|
|
389
|
-
Object.fromEntries(
|
|
390
|
-
Object.entries({
|
|
391
|
-
length: [
|
|
392
|
-
'px',
|
|
393
|
-
'em',
|
|
394
|
-
'rem',
|
|
395
|
-
'ex',
|
|
396
|
-
'rex',
|
|
397
|
-
'ch',
|
|
398
|
-
'rch',
|
|
399
|
-
'cap',
|
|
400
|
-
'rcap',
|
|
401
|
-
'ic',
|
|
402
|
-
'ric',
|
|
403
|
-
'lh',
|
|
404
|
-
'rlh',
|
|
405
|
-
'vw',
|
|
406
|
-
'vh',
|
|
407
|
-
'vi',
|
|
408
|
-
'vb',
|
|
409
|
-
'vmin',
|
|
410
|
-
'vmax',
|
|
411
|
-
'svw',
|
|
412
|
-
'svh',
|
|
413
|
-
'svi',
|
|
414
|
-
'svb',
|
|
415
|
-
'svmin',
|
|
416
|
-
'svmax',
|
|
417
|
-
'lvw',
|
|
418
|
-
'lvh',
|
|
419
|
-
'lvi',
|
|
420
|
-
'lvb',
|
|
421
|
-
'lvmin',
|
|
422
|
-
'lvmax',
|
|
423
|
-
'dvw',
|
|
424
|
-
'dvh',
|
|
425
|
-
'dvi',
|
|
426
|
-
'dvb',
|
|
427
|
-
'dvmin',
|
|
428
|
-
'dvmax',
|
|
429
|
-
'cqw',
|
|
430
|
-
'cqh',
|
|
431
|
-
'cqi',
|
|
432
|
-
'cqb',
|
|
433
|
-
'cqmin',
|
|
434
|
-
'cqmax',
|
|
435
|
-
'cm',
|
|
436
|
-
'mm',
|
|
437
|
-
'q',
|
|
438
|
-
'in',
|
|
439
|
-
'pt',
|
|
440
|
-
'pc',
|
|
441
|
-
],
|
|
442
|
-
time: ['s', 'ms'],
|
|
443
|
-
angle: ['deg', 'grad', 'rad', 'turn'],
|
|
444
|
-
frequency: ['hz', 'khz'],
|
|
445
|
-
resolution: ['dpi', 'dpcm', 'dppx', 'x'],
|
|
446
|
-
}).flatMap(([type, units]) => units.map(u => [u, type])),
|
|
447
|
-
)
|
|
448
|
-
);
|
|
449
|
-
/**
|
|
450
|
-
* A value with each unit split off its number by a space joined to it
|
|
451
|
-
* (`50 %`, `200 ms`, `8 PX`), for every unit CSS reads (the table above, and
|
|
452
|
-
* `%`); two numbers (`12 16px`) and a word that is no unit stay apart.
|
|
453
|
-
* @param {string} value
|
|
454
|
-
*/
|
|
455
|
-
export function unitsJoined(value) {
|
|
456
|
-
return String(value).replace(
|
|
457
|
-
/(\d)\s+(%|[a-z]+)(?=$|[\s,/)])/gi,
|
|
458
|
-
(all, digit, unit) =>
|
|
459
|
-
unit === '%' || UNIT_TYPES[unit.toLowerCase()] ? `${digit}${unit}` : all,
|
|
460
|
-
);
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
/** A value of each type, to match where a math function stands. */
|
|
464
|
-
const TYPE_SAMPLES = wordTable(
|
|
465
|
-
/** @type {Record<string, string>} */ ({
|
|
466
|
-
number: '1',
|
|
467
|
-
percent: '1%',
|
|
468
|
-
length: '1px',
|
|
469
|
-
time: '1s',
|
|
470
|
-
angle: '1deg',
|
|
471
|
-
frequency: '1hz',
|
|
472
|
-
resolution: '1dppx',
|
|
473
|
-
}),
|
|
474
|
-
);
|
|
475
|
-
|
|
476
|
-
/**
|
|
477
|
-
* The type of a sum (or of the arguments of min(), max(), clamp()):
|
|
478
|
-
* both sides of one type, a percentage beside a length or another type
|
|
479
|
-
* (`length%`: it resolves against that type), or null when they do not mix.
|
|
480
|
-
* `any` is a var() or env(), which can be anything.
|
|
481
|
-
* @param {string | null} a
|
|
482
|
-
* @param {string | null} b
|
|
483
|
-
*/
|
|
484
|
-
function addTypes(a, b) {
|
|
485
|
-
if (a == null || b == null) return null;
|
|
486
|
-
if (a === 'any') return b;
|
|
487
|
-
if (b === 'any') return a;
|
|
488
|
-
if (a === b) return a;
|
|
489
|
-
const bare = (/** @type {string} */ t) => t.replace(/%$/, '');
|
|
490
|
-
if (a === 'number' || b === 'number') return null;
|
|
491
|
-
if (a === 'percent') return `${bare(b)}%`;
|
|
492
|
-
if (b === 'percent') return `${bare(a)}%`;
|
|
493
|
-
return bare(a) === bare(b) ? `${bare(a)}%` : null;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
/**
|
|
497
|
-
* The CSS type of a math function (`calc(4px + 2)` has none): a sum needs
|
|
498
|
-
* one type on both sides; a product needs a number on one side; a quotient
|
|
499
|
-
* needs a number on the right. null when the math is unsound.
|
|
500
|
-
* @param {any} fn a Function node
|
|
501
|
-
* @returns {string | null}
|
|
502
|
-
*/
|
|
503
|
-
function mathType(fn) {
|
|
504
|
-
/** @param {any} node @returns {string | null} */
|
|
505
|
-
const leaf = node => {
|
|
506
|
-
if (node.type === 'Number') return 'number';
|
|
507
|
-
if (node.type === 'Percentage') return 'percent';
|
|
508
|
-
if (node.type === 'Dimension')
|
|
509
|
-
return UNIT_TYPES[node.unit.toLowerCase()] ?? null;
|
|
510
|
-
if (node.type === 'Identifier') {
|
|
511
|
-
const w = node.name.toLowerCase();
|
|
512
|
-
if (['e', 'pi', 'infinity', '-infinity', 'nan'].includes(w))
|
|
513
|
-
return 'number';
|
|
514
|
-
// clamp() takes none for a bound it does not set.
|
|
515
|
-
if (w === 'none' && fn.name.toLowerCase() === 'clamp') return 'any';
|
|
516
|
-
return null;
|
|
517
|
-
}
|
|
518
|
-
if (node.type === 'Function') {
|
|
519
|
-
const name = node.name.toLowerCase();
|
|
520
|
-
if (name === 'var' || name === 'env') return 'any';
|
|
521
|
-
return MATH.has(name) ? mathType(node) : null;
|
|
522
|
-
}
|
|
523
|
-
if (node.type === 'Parentheses') return expression(node.children.toArray());
|
|
524
|
-
return null;
|
|
525
|
-
};
|
|
526
|
-
/** @param {any[]} nodes @returns {string | null} */
|
|
527
|
-
const expression = nodes => {
|
|
528
|
-
const items = nodes.filter(n => n.type !== 'WhiteSpace');
|
|
529
|
-
if (!items.length) return null;
|
|
530
|
-
// Products first, then sums.
|
|
531
|
-
/** @type {Array<string | null>} */
|
|
532
|
-
const terms = [];
|
|
533
|
-
let current = /** @type {string | null | undefined} */ (undefined);
|
|
534
|
-
let op = '';
|
|
535
|
-
for (const node of items) {
|
|
536
|
-
if (node.type === 'Operator') {
|
|
537
|
-
const o = String(node.value).trim();
|
|
538
|
-
if (current === undefined) return null;
|
|
539
|
-
if (o === '+' || o === '-') {
|
|
540
|
-
terms.push(current);
|
|
541
|
-
current = undefined;
|
|
542
|
-
} else op = o;
|
|
543
|
-
continue;
|
|
544
|
-
}
|
|
545
|
-
const t = leaf(node);
|
|
546
|
-
if (current === undefined) current = t;
|
|
547
|
-
else if (op === '*')
|
|
548
|
-
current =
|
|
549
|
-
current === 'number'
|
|
550
|
-
? t
|
|
551
|
-
: t === 'number'
|
|
552
|
-
? current
|
|
553
|
-
: current === 'any' || t === 'any'
|
|
554
|
-
? 'any'
|
|
555
|
-
: null;
|
|
556
|
-
else if (op === '/')
|
|
557
|
-
current = t === 'number' || t === 'any' ? current : null;
|
|
558
|
-
else return null;
|
|
559
|
-
op = '';
|
|
560
|
-
if (current == null) return null;
|
|
561
|
-
}
|
|
562
|
-
if (current === undefined || op) return null;
|
|
563
|
-
terms.push(current);
|
|
564
|
-
return terms.reduce((a, b) => addTypes(a, b));
|
|
565
|
-
};
|
|
566
|
-
const children = fn.children.toArray();
|
|
567
|
-
const name = fn.name.toLowerCase();
|
|
568
|
-
// Arguments split at commas; round() may open with a strategy word.
|
|
569
|
-
/** @type {any[][]} */
|
|
570
|
-
const args = [[]];
|
|
571
|
-
for (const n of children)
|
|
572
|
-
if (n.type === 'Operator' && String(n.value).trim() === ',') args.push([]);
|
|
573
|
-
else args[args.length - 1].push(n);
|
|
574
|
-
if (
|
|
575
|
-
name === 'round' &&
|
|
576
|
-
args.length > 1 &&
|
|
577
|
-
args[0].filter(n => n.type !== 'WhiteSpace').length === 1 &&
|
|
578
|
-
['nearest', 'up', 'down', 'to-zero'].includes(
|
|
579
|
-
String(args[0].find(n => n.type === 'Identifier')?.name).toLowerCase(),
|
|
580
|
-
)
|
|
581
|
-
)
|
|
582
|
-
args.shift();
|
|
583
|
-
const types = args.map(expression);
|
|
584
|
-
if (types.some(t => t == null)) return null;
|
|
585
|
-
/** @param {string | null} t */
|
|
586
|
-
const isNumber = t => t === 'number' || t === 'any';
|
|
587
|
-
// What each function takes and gives, as CSS Values 4 types them.
|
|
588
|
-
switch (name) {
|
|
589
|
-
case 'pow':
|
|
590
|
-
return types.length === 2 && types.every(isNumber) ? 'number' : null;
|
|
591
|
-
case 'sqrt':
|
|
592
|
-
case 'exp':
|
|
593
|
-
return types.length === 1 && isNumber(types[0]) ? 'number' : null;
|
|
594
|
-
case 'log':
|
|
595
|
-
return types.length <= 2 && types.every(isNumber) ? 'number' : null;
|
|
596
|
-
case 'sin':
|
|
597
|
-
case 'cos':
|
|
598
|
-
case 'tan':
|
|
599
|
-
return types.length === 1 && (isNumber(types[0]) || types[0] === 'angle')
|
|
600
|
-
? 'number'
|
|
601
|
-
: null;
|
|
602
|
-
case 'asin':
|
|
603
|
-
case 'acos':
|
|
604
|
-
case 'atan':
|
|
605
|
-
return types.length === 1 && isNumber(types[0]) ? 'angle' : null;
|
|
606
|
-
case 'atan2':
|
|
607
|
-
return types.length === 2 && addTypes(types[0], types[1])
|
|
608
|
-
? 'angle'
|
|
609
|
-
: null;
|
|
610
|
-
case 'sign':
|
|
611
|
-
return types.length === 1 ? 'number' : null;
|
|
612
|
-
case 'abs':
|
|
613
|
-
case 'calc':
|
|
614
|
-
return types.length === 1 ? types[0] : null;
|
|
615
|
-
case 'mod':
|
|
616
|
-
case 'rem':
|
|
617
|
-
return types.length === 2 ? addTypes(types[0], types[1]) : null;
|
|
618
|
-
case 'round':
|
|
619
|
-
return types.length <= 2 ? types.reduce((a, b) => addTypes(a, b)) : null;
|
|
620
|
-
default:
|
|
621
|
-
// min(), max(), clamp() and hypot(): arguments of one type.
|
|
622
|
-
if (name === 'clamp' && types.length !== 3) return null;
|
|
623
|
-
return types.reduce((a, b) => addTypes(a, b));
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
/**
|
|
628
|
-
* The value with each outermost math function put as plain values of its
|
|
629
|
-
* type (`calc(1px + 2px)` as `1px`), so the property's grammar decides
|
|
630
|
-
* whether that type may stand there; a percentage beside a length is tried
|
|
631
|
-
* as both. Null when a math function is unsound. A value with no math
|
|
632
|
-
* function gives itself.
|
|
633
|
-
* @param {any} ast
|
|
634
|
-
* @returns {string[] | null}
|
|
635
|
-
*/
|
|
636
|
-
function mathAsValues(ast) {
|
|
637
|
-
const t = csstree();
|
|
638
|
-
/** @type {Array<{text: string, samples: string[]}>} */
|
|
639
|
-
const found = [];
|
|
640
|
-
let sound = true;
|
|
641
|
-
t.walk(ast, {
|
|
642
|
-
enter(/** @type {any} */ node) {
|
|
643
|
-
if (node.type !== 'Function' || !MATH.has(node.name.toLowerCase()))
|
|
644
|
-
return undefined;
|
|
645
|
-
const type = mathType(node);
|
|
646
|
-
if (type == null) sound = false;
|
|
647
|
-
else if (type !== 'any')
|
|
648
|
-
found.push({
|
|
649
|
-
text: t.generate(node),
|
|
650
|
-
samples: type.endsWith('%')
|
|
651
|
-
? [TYPE_SAMPLES[type.slice(0, -1)], '1%']
|
|
652
|
-
: [TYPE_SAMPLES[type] ?? ''],
|
|
653
|
-
});
|
|
654
|
-
return t.walk.skip;
|
|
655
|
-
},
|
|
656
|
-
});
|
|
657
|
-
if (!sound || found.some(f => f.samples.some(x => !x))) return null;
|
|
658
|
-
/** @type {string[]} */
|
|
659
|
-
let out = [t.generate(ast)];
|
|
660
|
-
// Spaced, so a value that follows with no space stays its own token.
|
|
661
|
-
for (const f of found)
|
|
662
|
-
out = out.flatMap(v => f.samples.map(x => v.replace(f.text, ` ${x} `)));
|
|
663
|
-
return out.slice(0, 16);
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
/**
|
|
667
|
-
* Each number a matched value holds, with its unit (`''` for a plain
|
|
668
|
-
* number) and the function it sits in, in order.
|
|
669
|
-
* @param {any} ast
|
|
670
|
-
* @returns {Array<{value: number, unit: string, fn: string | null}>}
|
|
671
|
-
*/
|
|
672
|
-
export function numbersIn(ast) {
|
|
673
|
-
/** @type {Array<{value: number, unit: string, fn: string | null}>} */
|
|
674
|
-
const out = [];
|
|
675
|
-
/** @type {string[]} */
|
|
676
|
-
const fns = [];
|
|
677
|
-
csstree().walk(ast, {
|
|
678
|
-
enter(/** @type {any} */ node) {
|
|
679
|
-
if (node.type === 'Function') fns.push(node.name.toLowerCase());
|
|
680
|
-
if (
|
|
681
|
-
node.type === 'Dimension' ||
|
|
682
|
-
node.type === 'Number' ||
|
|
683
|
-
node.type === 'Percentage'
|
|
684
|
-
)
|
|
685
|
-
out.push({
|
|
686
|
-
value: Number(node.value),
|
|
687
|
-
unit:
|
|
688
|
-
node.type === 'Percentage' ? '%' : (node.unit ?? '').toLowerCase(),
|
|
689
|
-
fn: fns.at(-1) ?? null,
|
|
690
|
-
});
|
|
691
|
-
},
|
|
692
|
-
leave(/** @type {any} */ node) {
|
|
693
|
-
if (node.type === 'Function') fns.pop();
|
|
694
|
-
},
|
|
695
|
-
});
|
|
696
|
-
return out;
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
/**
|
|
700
|
-
* A property's grammar as css-tree holds it, or null when it has none.
|
|
701
|
-
* @param {string} prop
|
|
702
|
-
* @returns {any | null}
|
|
703
|
-
*/
|
|
704
|
-
export const propertyGrammar = prop =>
|
|
705
|
-
csstree().lexer.properties[prop]?.syntax ?? null;
|
|
706
|
-
|
|
707
|
-
/**
|
|
708
|
-
* The keywords a property's grammar takes, through the types it names.
|
|
709
|
-
* @param {string} prop
|
|
710
|
-
* @returns {string[]}
|
|
711
|
-
*/
|
|
712
|
-
export function keywordsOf(prop) {
|
|
713
|
-
const t = csstree();
|
|
714
|
-
const seen = new Set();
|
|
715
|
-
/** @type {Set<string>} */
|
|
716
|
-
const words = new Set();
|
|
717
|
-
/** @param {any} syntax */
|
|
718
|
-
const walk = syntax => {
|
|
719
|
-
if (!syntax) return;
|
|
720
|
-
t.definitionSyntax.walk(syntax, (/** @type {any} */ n) => {
|
|
721
|
-
if (n.type === 'Keyword') {
|
|
722
|
-
if (!n.name.startsWith('-')) words.add(n.name);
|
|
723
|
-
} else if (n.type === 'Type' && !seen.has(n.name)) {
|
|
724
|
-
seen.add(n.name);
|
|
725
|
-
walk(t.lexer.types[n.name]?.syntax);
|
|
726
|
-
} else if (n.type === 'Property' && !seen.has(`'${n.name}'`)) {
|
|
727
|
-
// A shorthand's longhands (<'text-decoration-line'>) hold its words.
|
|
728
|
-
seen.add(`'${n.name}'`);
|
|
729
|
-
walk(t.lexer.getProperty(n.name)?.syntax);
|
|
730
|
-
}
|
|
731
|
-
});
|
|
732
|
-
};
|
|
733
|
-
walk(propertyGrammar(prop));
|
|
734
|
-
return [...words];
|
|
735
|
-
}
|
|
736
|
-
|
|
737
|
-
/** @type {string[] | null} */
|
|
738
|
-
let generics = null;
|
|
739
|
-
|
|
740
|
-
/**
|
|
741
|
-
* The CSS generic font families (serif, math, ui-rounded …), as the
|
|
742
|
-
* grammar's `<generic-family>` type lists them; non-standard ones left out.
|
|
743
|
-
* @returns {string[]}
|
|
744
|
-
*/
|
|
745
|
-
export function genericFamilies() {
|
|
746
|
-
if (generics) return generics;
|
|
747
|
-
const t = csstree();
|
|
748
|
-
const seen = new Set();
|
|
749
|
-
/** @type {Set<string>} */
|
|
750
|
-
const words = new Set();
|
|
751
|
-
/** @param {any} syntax */
|
|
752
|
-
const walk = syntax => {
|
|
753
|
-
if (!syntax) return;
|
|
754
|
-
t.definitionSyntax.walk(syntax, (/** @type {any} */ n) => {
|
|
755
|
-
if (n.type === 'Keyword') {
|
|
756
|
-
if (!n.name.startsWith('-')) words.add(n.name);
|
|
757
|
-
} else if (
|
|
758
|
-
n.type === 'Type' &&
|
|
759
|
-
!n.name.startsWith('-') &&
|
|
760
|
-
!seen.has(n.name)
|
|
761
|
-
) {
|
|
762
|
-
seen.add(n.name);
|
|
763
|
-
walk(currentLexer().types[n.name]?.syntax);
|
|
764
|
-
}
|
|
765
|
-
});
|
|
766
|
-
};
|
|
767
|
-
walk(currentLexer().types['generic-family']?.syntax);
|
|
768
|
-
return (generics = [...words]);
|
|
769
|
-
}
|
|
770
|
-
|
|
771
|
-
/**
|
|
772
|
-
* A value the property takes, to put in a suggestion: its initial value
|
|
773
|
-
* from MDN's data (a shorthand's first longhand's).
|
|
774
|
-
* @param {string} prop
|
|
775
|
-
* @returns {string | null}
|
|
776
|
-
*/
|
|
777
|
-
export function initialOf(prop) {
|
|
778
|
-
const initial = mdnProperties()[prop]?.initial;
|
|
779
|
-
if (Array.isArray(initial)) return initial[0] ? initialOf(initial[0]) : null;
|
|
780
|
-
// MDN writes a note, not a value, where the initial value varies
|
|
781
|
-
// (dependsOnUserAgent): one camelCase word, never a CSS value.
|
|
782
|
-
return typeof initial === 'string' &&
|
|
783
|
-
!/^[a-z]+[A-Z]\w*$/.test(initial) &&
|
|
784
|
-
matchValue(prop, initial)
|
|
785
|
-
? initial
|
|
786
|
-
: null;
|
|
787
|
-
}
|
|
788
|
-
|
|
789
|
-
/**
|
|
790
|
-
* Several values of one kind (`-4px -8px`, `1.5 2`, `var(--a) var(--b)`):
|
|
791
|
-
* the items, split as CSS splits them, and their kind (a number's unit, ''
|
|
792
|
-
* for a bare number, `var` for a var()), when every item is of the first
|
|
793
|
-
* one's kind; null otherwise (`all 200`). The one reading of "several
|
|
794
|
-
* values of one kind" that a repair offers the first of and the repair
|
|
795
|
-
* gate allows.
|
|
796
|
-
* @param {string} v
|
|
797
|
-
* @returns {{items: string[], kind: string} | null}
|
|
798
|
-
*/
|
|
799
|
-
export function sameKindValues(v) {
|
|
800
|
-
const items = splitTop(String(v).trim(), ' ');
|
|
801
|
-
/** @param {string} x */
|
|
802
|
-
const kind = x =>
|
|
803
|
-
/^var\(/i.test(x)
|
|
804
|
-
? 'var'
|
|
805
|
-
: (/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?([a-z%]*)$/i
|
|
806
|
-
.exec(x)?.[1]
|
|
807
|
-
?.toLowerCase() ?? null);
|
|
808
|
-
const first = kind(items[0] ?? '');
|
|
809
|
-
if (items.length < 2 || first === null || items.some(x => kind(x) !== first))
|
|
810
|
-
return null;
|
|
811
|
-
return {items, kind: first};
|
|
812
|
-
}
|
|
813
|
-
|
|
814
|
-
/** The longhand each type of the `font` grammar sets, where css-tree names a type, not a property. */
|
|
815
|
-
const FONT_TYPE_LONGHAND = wordTable(
|
|
816
|
-
/** @type {Record<string, string>} */ ({
|
|
817
|
-
'font-variant-css2': 'font-variant-caps',
|
|
818
|
-
'font-width-css3': 'font-stretch',
|
|
819
|
-
'font-stretch-absolute': 'font-stretch',
|
|
820
|
-
'system-family-name': 'system',
|
|
821
|
-
}),
|
|
822
|
-
);
|
|
823
|
-
|
|
824
|
-
/**
|
|
825
|
-
* The parts of a CSS `font` value, by the longhand each sets, as written:
|
|
826
|
-
* `italic small-caps 700 1rem/1 Arial` gives font-style, font-variant-caps,
|
|
827
|
-
* font-weight, font-size, line-height and font-family. A system font
|
|
828
|
-
* keyword (`caption`) is `system`. A single keyword of one longhand
|
|
829
|
-
* (`bold`) is that longhand. Null for anything else.
|
|
830
|
-
* @param {string} value
|
|
831
|
-
* @returns {Record<string, string> | null}
|
|
832
|
-
*/
|
|
833
|
-
export function fontShorthandParts(value) {
|
|
834
|
-
const {lexer, parse, generate} = csstree();
|
|
835
|
-
// Words that are each one longhand's keyword, size or weight (`bold`,
|
|
836
|
-
// `italic bold`, `bold 1rem`) are those longhands, in any order before the
|
|
837
|
-
// size; after the size the rest is the family (`1rem bold` names a family
|
|
838
|
-
// called bold), as CSS reads it.
|
|
839
|
-
const LONGHANDS = [
|
|
840
|
-
'font-weight',
|
|
841
|
-
'font-style',
|
|
842
|
-
'font-variant-caps',
|
|
843
|
-
'font-stretch',
|
|
844
|
-
'font-size',
|
|
845
|
-
];
|
|
846
|
-
// A lone generic family (math, fangsong) is a family, though a longhand
|
|
847
|
-
// may share the word (font-size: math).
|
|
848
|
-
if (genericFamilies().includes(value.trim().toLowerCase()))
|
|
849
|
-
return {'font-family': value.trim()};
|
|
850
|
-
if (!value.includes(',')) {
|
|
851
|
-
/** @type {Record<string, string>} */
|
|
852
|
-
const each = {};
|
|
853
|
-
const words = splitTop(value.trim(), ' ');
|
|
854
|
-
let all = true;
|
|
855
|
-
// `normal` fits several longhands: every other word is placed first,
|
|
856
|
-
// then each `normal` takes the first longhand still free.
|
|
857
|
-
const normals = words.filter(w => w.toLowerCase() === 'normal').length;
|
|
858
|
-
// A normal after the size is part of the family, read below.
|
|
859
|
-
const sizeAt = words.findIndex(
|
|
860
|
-
w => matchValue('font-size', w.split('/')[0]) && !/^[a-z-]+$/i.test(w),
|
|
861
|
-
);
|
|
862
|
-
if (
|
|
863
|
-
sizeAt >= 0 &&
|
|
864
|
-
words.slice(sizeAt + 1).some(w => w.toLowerCase() === 'normal')
|
|
865
|
-
)
|
|
866
|
-
all = false;
|
|
867
|
-
for (const w of words.filter(x => x.toLowerCase() !== 'normal')) {
|
|
868
|
-
if (!all) break;
|
|
869
|
-
const lower = w.toLowerCase();
|
|
870
|
-
// After the size, the rest is the family (16px bold names a family
|
|
871
|
-
// called bold): the grammar below reads it.
|
|
872
|
-
if ('font-size' in each) {
|
|
873
|
-
all = false;
|
|
874
|
-
break;
|
|
875
|
-
}
|
|
876
|
-
// A size with its line height (1rem/1.5).
|
|
877
|
-
const slash = /^([^/]+)\/([^/]+)$/.exec(w);
|
|
878
|
-
if (
|
|
879
|
-
slash &&
|
|
880
|
-
!('font-size' in each) &&
|
|
881
|
-
matchValue('font-size', slash[1]) &&
|
|
882
|
-
matchValue('line-height', slash[2])
|
|
883
|
-
) {
|
|
884
|
-
each['font-size'] = slash[1];
|
|
885
|
-
each['line-height'] = slash[2];
|
|
886
|
-
continue;
|
|
887
|
-
}
|
|
888
|
-
const takers = LONGHANDS.filter(p => keywordsOf(p).includes(lower));
|
|
889
|
-
const keyword = takers.find(p => !(p in each));
|
|
890
|
-
// A part given twice (bold bold, bold lighter, italic oblique) is no
|
|
891
|
-
// font value.
|
|
892
|
-
if (takers.length && !keyword) return {repeat: lower, part: takers[0]};
|
|
893
|
-
const prop =
|
|
894
|
-
keyword ??
|
|
895
|
-
['font-size', 'font-weight'].find(
|
|
896
|
-
p => !(p in each) && !/^[a-z-]+$/i.test(w) && matchValue(p, w),
|
|
897
|
-
);
|
|
898
|
-
if (!prop) {
|
|
899
|
-
all = false;
|
|
900
|
-
break;
|
|
901
|
-
}
|
|
902
|
-
each[prop] = /^[a-z-]+$/i.test(w) ? lower : w;
|
|
903
|
-
}
|
|
904
|
-
for (let n = 0; all && n < normals; n += 1) {
|
|
905
|
-
const free = LONGHANDS.find(
|
|
906
|
-
p => !(p in each) && keywordsOf(p).includes('normal'),
|
|
907
|
-
);
|
|
908
|
-
if (!free)
|
|
909
|
-
return {
|
|
910
|
-
repeat: 'normal',
|
|
911
|
-
part: LONGHANDS.filter(p => keywordsOf(p).includes('normal')).join(
|
|
912
|
-
', ',
|
|
913
|
-
),
|
|
914
|
-
};
|
|
915
|
-
each[free] = 'normal';
|
|
916
|
-
}
|
|
917
|
-
if (all && words.length) return each;
|
|
918
|
-
}
|
|
919
|
-
/** @type {any} */
|
|
920
|
-
let ast;
|
|
921
|
-
try {
|
|
922
|
-
ast = parse(value, {context: 'value', positions: true});
|
|
923
|
-
} catch {
|
|
924
|
-
return null;
|
|
925
|
-
}
|
|
926
|
-
const r = lexer.matchProperty('font', ast);
|
|
927
|
-
if (r.error || !r.matched) return null;
|
|
928
|
-
/** @type {Record<string, any[]>} */
|
|
929
|
-
const parts = {};
|
|
930
|
-
/** @param {any} m @param {string | null} under */
|
|
931
|
-
const visit = (m, under) => {
|
|
932
|
-
const syntax = m.syntax;
|
|
933
|
-
const name =
|
|
934
|
-
under === 'font-family'
|
|
935
|
-
? under
|
|
936
|
-
: syntax?.type === 'Property' && syntax.name !== 'font'
|
|
937
|
-
? String(syntax.name)
|
|
938
|
-
: syntax?.type === 'Type' && FONT_TYPE_LONGHAND[syntax.name]
|
|
939
|
-
? FONT_TYPE_LONGHAND[syntax.name]
|
|
940
|
-
: syntax?.type === 'Type' && /family/.test(syntax.name)
|
|
941
|
-
? 'font-family'
|
|
942
|
-
: under;
|
|
943
|
-
if (m.node && name) (parts[name] ??= []).push(m.node);
|
|
944
|
-
for (const c of m.match ?? []) visit(c, name);
|
|
945
|
-
};
|
|
946
|
-
visit(r.matched, null);
|
|
947
|
-
/** @type {Record<string, string>} */
|
|
948
|
-
const out = {};
|
|
949
|
-
for (const [k, nodes] of Object.entries(parts)) {
|
|
950
|
-
// The family list is the rest of the value, as written.
|
|
951
|
-
out[k] =
|
|
952
|
-
k === 'font-family'
|
|
953
|
-
? value.slice(nodes[0].loc?.start.offset ?? 0).trim()
|
|
954
|
-
: nodes.map(n => generate(n)).join(' ');
|
|
955
|
-
}
|
|
956
|
-
return Object.keys(out).length ? out : null;
|
|
957
|
-
}
|
|
958
|
-
|
|
959
|
-
/**
|
|
960
|
-
* A grammar as text, cut at a whole term when it is long.
|
|
961
|
-
* @param {any} syntax
|
|
962
|
-
*/
|
|
963
|
-
export function grammarText(syntax) {
|
|
964
|
-
const text = csstree().definitionSyntax.generate(syntax);
|
|
965
|
-
if (text.length <= 120) return text;
|
|
966
|
-
return `${text.slice(0, 120).replace(/\s*\|[^|]*$/, '')} | …`;
|
|
967
|
-
}
|
|
968
|
-
|
|
969
|
-
/** The math functions, by name, for a check of whether a value holds one. */
|
|
970
|
-
export const MATH_FUNCTION = new RegExp(`\\b(${[...MATH].join('|')})\\(`, 'i');
|
|
971
|
-
|
|
972
|
-
/** @type {Map<string, Set<string>>} */
|
|
973
|
-
const typesCache = new Map();
|
|
974
|
-
/**
|
|
975
|
-
* Every type a property's grammar names, through the types and properties
|
|
976
|
-
* it refers to (padding: length-percentage, length, percentage, …).
|
|
977
|
-
* @param {string} prop
|
|
978
|
-
* @returns {Set<string>}
|
|
979
|
-
*/
|
|
980
|
-
function grammarTypes(prop) {
|
|
981
|
-
const known = typesCache.get(prop);
|
|
982
|
-
if (known) return known;
|
|
983
|
-
const t = csstree();
|
|
984
|
-
/** @type {Set<string>} */
|
|
985
|
-
const found = new Set();
|
|
986
|
-
const seen = new Set();
|
|
987
|
-
/** @param {any} syntax */
|
|
988
|
-
const walk = syntax => {
|
|
989
|
-
if (!syntax) return;
|
|
990
|
-
t.definitionSyntax.walk(syntax, (/** @type {any} */ n) => {
|
|
991
|
-
if (n.type === 'Type') {
|
|
992
|
-
found.add(n.name);
|
|
993
|
-
if (!seen.has(n.name)) {
|
|
994
|
-
seen.add(n.name);
|
|
995
|
-
walk(t.lexer.types[n.name]?.syntax);
|
|
996
|
-
}
|
|
997
|
-
} else if (n.type === 'Property' && !seen.has(`'${n.name}'`)) {
|
|
998
|
-
seen.add(`'${n.name}'`);
|
|
999
|
-
walk(t.lexer.properties[n.name]?.syntax);
|
|
1000
|
-
}
|
|
1001
|
-
});
|
|
1002
|
-
};
|
|
1003
|
-
walk(propertyGrammar(prop));
|
|
1004
|
-
typesCache.set(prop, found);
|
|
1005
|
-
return found;
|
|
1006
|
-
}
|
|
1007
|
-
|
|
1008
|
-
/**
|
|
1009
|
-
* Whether a property's grammar takes a percentage anywhere, through the
|
|
1010
|
-
* types it names (padding does; border-width and outline-offset do not).
|
|
1011
|
-
* @param {string} prop
|
|
1012
|
-
*/
|
|
1013
|
-
export function takesPercentage(prop) {
|
|
1014
|
-
return [...grammarTypes(prop)].some(n => /percentage/.test(n));
|
|
1015
|
-
}
|
|
1016
|
-
|
|
1017
|
-
/**
|
|
1018
|
-
* Whether every number a property takes is whole: its grammar names
|
|
1019
|
-
* `<integer>` and no other numeric type (z-index, order; never opacity or
|
|
1020
|
-
* a length). A comma between such digits can only group thousands.
|
|
1021
|
-
* @param {string} prop
|
|
1022
|
-
*/
|
|
1023
|
-
export function takesWholeNumbersOnly(prop) {
|
|
1024
|
-
const types = grammarTypes(prop);
|
|
1025
|
-
return (
|
|
1026
|
-
types.has('integer') &&
|
|
1027
|
-
![...types].some(n =>
|
|
1028
|
-
/^(number|length|percentage|length-percentage|dimension|angle|time|frequency|resolution|flex|ratio)$/.test(
|
|
1029
|
-
n,
|
|
1030
|
-
),
|
|
1031
|
-
)
|
|
1032
|
-
);
|
|
1033
|
-
}
|
|
1034
|
-
|
|
1035
|
-
/** The generic font families CSS names, which are never quoted. */
|
|
1036
|
-
const GENERIC_FAMILIES = new Set([
|
|
1037
|
-
'serif',
|
|
1038
|
-
'sans-serif',
|
|
1039
|
-
'monospace',
|
|
1040
|
-
'cursive',
|
|
1041
|
-
'fantasy',
|
|
1042
|
-
'system-ui',
|
|
1043
|
-
'math',
|
|
1044
|
-
'emoji',
|
|
1045
|
-
'fangsong',
|
|
1046
|
-
'ui-serif',
|
|
1047
|
-
'ui-sans-serif',
|
|
1048
|
-
'ui-monospace',
|
|
1049
|
-
'ui-rounded',
|
|
1050
|
-
]);
|
|
1051
|
-
|
|
1052
|
-
/**
|
|
1053
|
-
* A font family list with its missing commas put back, read with css-tree:
|
|
1054
|
-
* within one comma-separated entry, a quoted family next to another quoted
|
|
1055
|
-
* family or a generic one (`"Open Sans" serif`) is two families. Returns the
|
|
1056
|
-
* list as CSS writes it (`"Open Sans", serif`), or null when nothing is
|
|
1057
|
-
* missing, the value does not parse, or a family would hold a quote.
|
|
1058
|
-
* @param {string} value
|
|
1059
|
-
* @returns {string | null}
|
|
1060
|
-
*/
|
|
1061
|
-
export function familyListMeant(value) {
|
|
1062
|
-
const {parse} = csstree();
|
|
1063
|
-
/** @type {any} */
|
|
1064
|
-
let ast;
|
|
1065
|
-
try {
|
|
1066
|
-
ast = parse(value, {context: 'value'});
|
|
1067
|
-
} catch {
|
|
1068
|
-
return null;
|
|
1069
|
-
}
|
|
1070
|
-
/** @type {Array<Array<{type: string, value: string}>>} */
|
|
1071
|
-
const entries = [[]];
|
|
1072
|
-
let ok = true;
|
|
1073
|
-
ast.children.forEach((/** @type {any} */ n) => {
|
|
1074
|
-
if (n.type === 'Operator' && n.value === ',') entries.push([]);
|
|
1075
|
-
else if (n.type === 'String' || n.type === 'Identifier')
|
|
1076
|
-
entries[entries.length - 1].push({
|
|
1077
|
-
type: n.type,
|
|
1078
|
-
value: n.type === 'String' ? n.value : n.name,
|
|
1079
|
-
});
|
|
1080
|
-
else ok = false;
|
|
1081
|
-
});
|
|
1082
|
-
if (!ok) return null;
|
|
1083
|
-
/** @type {string[]} */
|
|
1084
|
-
const families = [];
|
|
1085
|
-
let changed = false;
|
|
1086
|
-
for (const entry of entries) {
|
|
1087
|
-
if (!entry.length) return null;
|
|
1088
|
-
// One family: a quoted name alone, or a run of bare words.
|
|
1089
|
-
if (
|
|
1090
|
-
entry.length === 1 ||
|
|
1091
|
-
entry.every(
|
|
1092
|
-
x =>
|
|
1093
|
-
x.type === 'Identifier' &&
|
|
1094
|
-
!GENERIC_FAMILIES.has(x.value.toLowerCase()),
|
|
1095
|
-
)
|
|
1096
|
-
) {
|
|
1097
|
-
families.push(
|
|
1098
|
-
entry.length === 1 && entry[0].type === 'String'
|
|
1099
|
-
? JSON.stringify(entry[0].value)
|
|
1100
|
-
: entry.map(x => x.value).join(' '),
|
|
1101
|
-
);
|
|
1102
|
-
continue;
|
|
1103
|
-
}
|
|
1104
|
-
// A quoted name or a generic family ends a family: the comma between
|
|
1105
|
-
// them was left out.
|
|
1106
|
-
changed = true;
|
|
1107
|
-
/** @type {string[]} */
|
|
1108
|
-
let words = [];
|
|
1109
|
-
const flush = () => {
|
|
1110
|
-
if (words.length) families.push(words.join(' '));
|
|
1111
|
-
words = [];
|
|
1112
|
-
};
|
|
1113
|
-
for (const x of entry) {
|
|
1114
|
-
if (x.type === 'String') {
|
|
1115
|
-
flush();
|
|
1116
|
-
families.push(JSON.stringify(x.value));
|
|
1117
|
-
} else if (GENERIC_FAMILIES.has(x.value.toLowerCase())) {
|
|
1118
|
-
flush();
|
|
1119
|
-
families.push(x.value);
|
|
1120
|
-
} else words.push(x.value);
|
|
1121
|
-
}
|
|
1122
|
-
flush();
|
|
1123
|
-
}
|
|
1124
|
-
// A family that would hold a quote (escaped) is never a suggestion.
|
|
1125
|
-
if (!changed || families.some(f => f.includes('\\'))) return null;
|
|
1126
|
-
return families.join(', ');
|
|
1127
|
-
}
|
|
1128
|
-
|
|
1129
|
-
/** A side word in a longhand's name, in the order a box value lists sides. */
|
|
1130
|
-
export const BOX_SIDES = /** @type {const} */ ([
|
|
1131
|
-
'top',
|
|
1132
|
-
'right',
|
|
1133
|
-
'bottom',
|
|
1134
|
-
'left',
|
|
1135
|
-
]);
|
|
1136
|
-
|
|
1137
|
-
/**
|
|
1138
|
-
* A two-sided (start/end) or four-sided (top/right/bottom/left) longhand's
|
|
1139
|
-
* value, as each side's property and value by the shorthand rules; null for
|
|
1140
|
-
* any other property.
|
|
1141
|
-
* @param {string} prop kebab-case
|
|
1142
|
-
* @param {string} v
|
|
1143
|
-
* @returns {Record<string, string> | null}
|
|
1144
|
-
*/
|
|
1145
|
-
export function sidesOf(prop, v) {
|
|
1146
|
-
// (A logical pair the property data lists no longhands for,
|
|
1147
|
-
// `border-block-width`: its start and its end.)
|
|
1148
|
-
const pair = /^(border-(?:block|inline))-(width|style|color)$/.exec(prop);
|
|
1149
|
-
const direct = pair
|
|
1150
|
-
? [`${pair[1]}-start-${pair[2]}`, `${pair[1]}-end-${pair[2]}`]
|
|
1151
|
-
: [...longhandsOf(prop)].filter(p => longhandsOf(p).size === 0);
|
|
1152
|
-
const w = splitTop(v.trim(), ' ').map(x => x.trim());
|
|
1153
|
-
if (!w.length || /!important/i.test(v)) return null;
|
|
1154
|
-
const start = direct.find(p => /-start(-|$)/.test(p));
|
|
1155
|
-
const end = direct.find(p => /-end(-|$)/.test(p));
|
|
1156
|
-
if (direct.length === 2 && start && end && w.length <= 2)
|
|
1157
|
-
return {[start]: w[0], [end]: w[1] ?? w[0]};
|
|
1158
|
-
const named = BOX_SIDES.map(side =>
|
|
1159
|
-
direct.find(p => new RegExp(`-${side}(-|$)`).test(p)),
|
|
1160
|
-
);
|
|
1161
|
-
if (direct.length !== 4 || named.some(x => !x) || w.length > 4) return null;
|
|
1162
|
-
const [t, r = t, bo = t, l = r] = w;
|
|
1163
|
-
const vals = [t, r, bo, l];
|
|
1164
|
-
return Object.fromEntries(
|
|
1165
|
-
named.map((p, i) => [/** @type {string} */ (p), vals[i]]),
|
|
1166
|
-
);
|
|
1167
|
-
}
|