@astryxdesign/cli 0.7.15-canary.gea16e8d → 0.7.15
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 +353 -3
- package/README.md +63 -63
- package/api/build/_adapter.d.mts +4 -0
- package/api/build/_adapter.mjs +28 -2
- package/api/build/blocks/blocks.d.mts +10 -1
- package/api/build/blocks/blocks.mjs +84 -7
- package/api/build/build.mjs +6 -1
- package/api/build/tree.d.mts +4 -0
- package/api/build/tree.mjs +9 -2
- package/api/doctor/_adapter.d.mts +2 -1
- package/api/doctor/_adapter.mjs +6 -1
- package/api/doctor/doctor.mjs +2 -2
- package/api/doctor/theme-checks.mjs +56 -21
- package/api/integration/add-helpers.mjs +5 -0
- package/api/integration/add-theme.mjs +15 -110
- package/api/integration/validate-integration.mjs +17 -3
- package/api/integration/validateIntegration.doc.mjs +1 -1
- package/api/theme/_adapter.d.mts +37 -5
- package/api/theme/_adapter.mjs +363 -50
- package/api/theme/_fork.d.mts +90 -0
- package/api/theme/_fork.mjs +246 -0
- package/api/theme/build/build.d.mts +25 -1
- package/api/theme/build/build.mjs +352 -41
- package/api/theme/eject/eject.mjs +36 -15
- package/api/theme/list/list.mjs +3 -6
- package/api/theme/palette/generate/generator.d.mts +13 -0
- package/api/theme/palette/generate/generator.mjs +32 -2
- package/api/theme/targets/targets.mjs +10 -3
- package/api/theme/template/template.mjs +48 -2
- package/api/theme/theme.type.d.mts +8 -0
- package/api/theme/theme.type.mjs +5 -1
- package/api/theme/themeBuild.doc.mjs +2 -2
- package/api/theme/themeRemove.doc.mjs +5 -2
- package/api/theme/themeTemplate.doc.mjs +2 -2
- package/api/theme/themeUse.doc.mjs +8 -2
- package/api/theme-edit/_adapter.mjs +157 -36
- package/api/theme-edit/_catalog.d.mts +1 -6
- package/api/theme-edit/_catalog.mjs +5 -268
- package/api/theme-edit/_check.d.mts +3 -1
- package/api/theme-edit/_check.mjs +733 -308
- package/api/theme-edit/_color.d.mts +19 -0
- package/api/theme-edit/_color.mjs +277 -33
- package/api/theme-edit/_compiler.d.mts +1 -0
- package/api/theme-edit/_compiler.mjs +25 -0
- package/api/theme-edit/_context.d.mts +2 -0
- package/api/theme-edit/_context.mjs +83 -9
- package/api/theme-edit/_css.d.mts +84 -0
- package/api/theme-edit/_css.mjs +700 -0
- package/api/theme-edit/_edit.mjs +370 -85
- package/api/theme-edit/_how-commands.d.mts +5 -0
- package/api/theme-edit/_how-commands.mjs +20 -13
- package/api/theme-edit/_how-question.d.mts +29 -0
- package/api/theme-edit/_how-question.mjs +1573 -182
- package/api/theme-edit/_how.mjs +306 -22
- package/api/theme-edit/_overview.mjs +2 -7
- package/api/theme-edit/_palette.d.mts +27 -5
- package/api/theme-edit/_palette.mjs +231 -97
- package/api/theme-edit/_parts.d.mts +42 -10
- package/api/theme-edit/_parts.mjs +224 -69
- package/api/theme-edit/_preview-app.d.mts +8 -0
- package/api/theme-edit/_preview-app.mjs +549 -96
- package/api/theme-edit/_preview.d.mts +7 -16
- package/api/theme-edit/_preview.mjs +77 -84
- package/api/theme-edit/_project.d.mts +20 -1
- package/api/theme-edit/_project.mjs +43 -13
- package/api/theme-edit/_settings.d.mts +96 -20
- package/api/theme-edit/_settings.mjs +646 -141
- package/api/theme-edit/_tables.d.mts +18 -12
- package/api/theme-edit/_tables.mjs +144 -147
- package/api/theme-edit/theme-edit.type.d.mts +10 -1
- package/api/theme-edit/theme-edit.type.mjs +3 -1
- package/api/theme-edit/themeEditNew.doc.mjs +1 -1
- package/api/theme-edit/themeEditPreviewAdd.doc.mjs +1 -1
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +1 -1
- package/api/theme-edit/themeEditPreviewList.doc.mjs +2 -2
- package/api/theme-edit/themeEditSet.doc.mjs +1 -1
- package/api/theme-edit/themeEditUndo.doc.mjs +1 -1
- package/assets/docs/tree/checks.doc.mjs +22 -1
- package/assets/docs/tree/publishing.doc.mjs +1 -0
- package/assets/docs.generated.json +2 -2
- package/assets/self-audit.generated.json +1 -1
- package/authoring/integration/integration.doc.mjs +8 -0
- package/authoring/integration/schema.d.mts +8 -0
- package/authoring/integration/schema.mjs +1 -0
- package/authoring/integration/type.ts +9 -0
- package/clients/cli/build-id.generated.json +1 -1
- package/clients/cli/commands/experimental/index.mjs +10 -10
- package/clients/cli/commands/experimental/theme.mjs +22 -3
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +3 -3
- package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
- package/clients/cli/commands/integration.mjs +8 -0
- package/clients/cli/commands/theme-build.doc.mjs +2 -2
- package/clients/cli/commands/theme-list.doc.mjs +1 -1
- package/clients/cli/commands/theme-template.doc.mjs +1 -1
- package/clients/cli/commands/theme.mjs +325 -48
- package/clients/cli/index.mjs +18 -3
- package/clients/cli/lib/cli-error.mjs +118 -3
- package/clients/cli/manifest.generated.json +1 -1
- package/foundation/config/project.mjs +34 -8
- package/foundation/config/theme-state.d.mts +16 -0
- package/foundation/config/theme-state.mjs +84 -13
- package/foundation/discovery/template-needs.d.mts +13 -0
- package/foundation/discovery/template-needs.mjs +30 -15
- package/foundation/discovery/theme-discovery.mjs +14 -2
- package/foundation/discovery/theme-imports.d.mts +16 -0
- package/foundation/discovery/theme-imports.mjs +30 -2
- package/foundation/doc-compiler/import.mjs +10 -4
- package/foundation/fs/gather-cache.mjs +82 -23
- package/foundation/fs/jiti-cache.d.mts +21 -0
- package/foundation/fs/jiti-cache.mjs +66 -0
- package/foundation/fs/module-loader.mjs +7 -2
- package/foundation/integrations/autolink.d.mts +1 -24
- package/foundation/integrations/autolink.mjs +7 -60
- package/foundation/integrations/declared-dependencies.d.mts +27 -0
- package/foundation/integrations/declared-dependencies.mjs +73 -0
- package/foundation/integrations/integrations.d.mts +8 -0
- package/foundation/integrations/integrations.mjs +4 -0
- package/foundation/integrations/may-have-integrations.d.mts +8 -0
- package/foundation/integrations/may-have-integrations.mjs +47 -0
- package/foundation/integrations/validate-contributions.d.mts +62 -1
- package/foundation/integrations/validate-contributions.mjs +452 -29
- package/foundation/release/cache.d.mts +21 -2
- package/foundation/release/cache.mjs +48 -8
- package/foundation/release/folder-lock.d.mts +7 -0
- package/foundation/release/folder-lock.mjs +60 -26
- package/foundation/release/lock-lease.d.mts +2 -1
- package/foundation/release/lock-lease.mjs +7 -2
- package/foundation/response/error-codes.doc.mjs +1 -1
- package/foundation/response/response-types.doc.mjs +5 -5
- package/package.json +6 -6
- package/api/theme-edit/_css-properties.d.mts +0 -10
- package/api/theme-edit/_css-properties.mjs +0 -556
|
@@ -0,0 +1,700 @@
|
|
|
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
|
+
|
|
13
|
+
const require = createRequire(import.meta.url);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Properties browsers ship that MDN does not list yet (Chrome experiments).
|
|
17
|
+
* A name here is a property a theme may set; it has no grammar to match.
|
|
18
|
+
*/
|
|
19
|
+
const NOT_IN_MDN_YET = [
|
|
20
|
+
'border-shape',
|
|
21
|
+
'flex-line-count',
|
|
22
|
+
'frame-sizing',
|
|
23
|
+
'link-parameters',
|
|
24
|
+
'path-length',
|
|
25
|
+
'scroll-axis-lock',
|
|
26
|
+
'text-fit',
|
|
27
|
+
'timeline-trigger-activation-range',
|
|
28
|
+
'timeline-trigger-activation-range-end',
|
|
29
|
+
'timeline-trigger-activation-range-start',
|
|
30
|
+
'timeline-trigger-active-range',
|
|
31
|
+
'timeline-trigger-active-range-end',
|
|
32
|
+
'timeline-trigger-active-range-start',
|
|
33
|
+
'view-transition-scope',
|
|
34
|
+
'window-drag',
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
/** @type {any} */
|
|
38
|
+
let tree = null;
|
|
39
|
+
/** css-tree, loaded once. */
|
|
40
|
+
const csstree = () => (tree ??= require('css-tree'));
|
|
41
|
+
|
|
42
|
+
/** The MDN property data css-tree reads, at the version css-tree pins. */
|
|
43
|
+
const mdnProperties = () =>
|
|
44
|
+
/** @type {Record<string, {status?: string, initial?: string | string[]}>} */ (
|
|
45
|
+
createRequire(require.resolve('css-tree/package.json'))(
|
|
46
|
+
'mdn-data/css/properties.json',
|
|
47
|
+
)
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
/** @type {ReadonlySet<string> | null} */
|
|
51
|
+
let names = null;
|
|
52
|
+
/**
|
|
53
|
+
* The standard CSS property names: every property css-tree's MDN data
|
|
54
|
+
* lists that is not vendor-prefixed or obsolete, plus NOT_IN_MDN_YET. A
|
|
55
|
+
* name outside it is not a CSS property, so the command refuses it.
|
|
56
|
+
* @returns {ReadonlySet<string>}
|
|
57
|
+
*/
|
|
58
|
+
export function cssProperties() {
|
|
59
|
+
if (names) return names;
|
|
60
|
+
const mdn = mdnProperties();
|
|
61
|
+
names = new Set([
|
|
62
|
+
...Object.keys(csstree().lexer.properties).filter(
|
|
63
|
+
k => !k.startsWith('-') && mdn[k] && mdn[k].status !== 'obsolete',
|
|
64
|
+
),
|
|
65
|
+
...NOT_IN_MDN_YET,
|
|
66
|
+
]);
|
|
67
|
+
return names;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** What a var() or env() may stand for while the rest of a value is matched. */
|
|
71
|
+
const VAR_STANDS_FOR = ['0', '1px', '1ms', 'red', 'ease', 'serif', '0 0 red'];
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Whether a value matches a property's grammar, as css-tree reads it. A
|
|
75
|
+
* var() or env() can hold anything, so the value matches when some
|
|
76
|
+
* ordinary value in its place does. Returns the parsed value on a match, for range checks the
|
|
77
|
+
* grammar does not make.
|
|
78
|
+
* @param {string} property
|
|
79
|
+
* @param {string} value
|
|
80
|
+
* @returns {any | null}
|
|
81
|
+
*/
|
|
82
|
+
export function matchValue(property, value) {
|
|
83
|
+
const standard = standardSyntax(property);
|
|
84
|
+
return matchWith(value, (lexer, ast) =>
|
|
85
|
+
standard ? lexer.match(standard, ast) : lexer.matchProperty(property, ast),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** @type {Map<string, string | null>} */
|
|
90
|
+
const standardCache = new Map();
|
|
91
|
+
/**
|
|
92
|
+
* A property's grammar without its legacy Internet Explorer alternatives
|
|
93
|
+
* (`filter`'s `<-ms-filter-function-list>` takes any word), or null when it
|
|
94
|
+
* has none.
|
|
95
|
+
* @param {string} property
|
|
96
|
+
* @returns {string | null}
|
|
97
|
+
*/
|
|
98
|
+
function standardSyntax(property) {
|
|
99
|
+
if (standardCache.has(property))
|
|
100
|
+
return /** @type {string | null} */ (standardCache.get(property));
|
|
101
|
+
const {definitionSyntax} = csstree();
|
|
102
|
+
const syntax = propertyGrammar(property);
|
|
103
|
+
/** @type {string | null} */
|
|
104
|
+
let out = null;
|
|
105
|
+
if (syntax?.type === 'Group' && syntax.combinator === '|') {
|
|
106
|
+
const kept = syntax.terms.filter(
|
|
107
|
+
(/** @type {any} */ t) => !(t.type === 'Type' && /^-ms-/.test(t.name)),
|
|
108
|
+
);
|
|
109
|
+
if (kept.length && kept.length < syntax.terms.length)
|
|
110
|
+
out = kept
|
|
111
|
+
.map((/** @type {any} */ t) => definitionSyntax.generate(t))
|
|
112
|
+
.join(' | ');
|
|
113
|
+
}
|
|
114
|
+
standardCache.set(property, out);
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Whether a value is one value of a CSS type (`time`, `easing-function`,
|
|
120
|
+
* `length-percentage`), never a list. Returns the parsed value on a match.
|
|
121
|
+
* @param {string} type
|
|
122
|
+
* @param {string} value
|
|
123
|
+
* @returns {any | null}
|
|
124
|
+
*/
|
|
125
|
+
export function matchType(type, value) {
|
|
126
|
+
return matchWith(value, (lexer, ast) => lexer.matchType(type, ast));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* A value matched by `match`, with each var() tried as an ordinary value.
|
|
131
|
+
* @param {string} value
|
|
132
|
+
* @param {(lexer: any, ast: any) => {error: unknown}} match
|
|
133
|
+
* @returns {any | null}
|
|
134
|
+
*/
|
|
135
|
+
function matchWith(value, match) {
|
|
136
|
+
const {lexer, parse} = csstree();
|
|
137
|
+
const vars =
|
|
138
|
+
value.match(/(?:var|env)\([^()]*(?:\([^()]*\)[^()]*)*\)/gi) ?? [];
|
|
139
|
+
/** @param {string} v */
|
|
140
|
+
const tryOne = v => {
|
|
141
|
+
try {
|
|
142
|
+
const ast = parse(v, {context: 'value'});
|
|
143
|
+
// Each math function is matched as a plain value of its type, so
|
|
144
|
+
// math css-tree's grammar does not know (pow(), sqrt()) is read too.
|
|
145
|
+
const plain = mathAsValues(ast);
|
|
146
|
+
return plain?.every(
|
|
147
|
+
x => !match(lexer, parse(x, {context: 'value'})).error,
|
|
148
|
+
)
|
|
149
|
+
? ast
|
|
150
|
+
: null;
|
|
151
|
+
} catch {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
if (!vars.length) return tryOne(value);
|
|
156
|
+
// A value that is only a variable can stand for any value, also on a
|
|
157
|
+
// property that takes only keywords (display, font-weight).
|
|
158
|
+
if (vars.length === 1 && value.trim() === vars[0]) {
|
|
159
|
+
try {
|
|
160
|
+
return parse(value, {context: 'value'});
|
|
161
|
+
} catch {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (vars.length > 3) return null;
|
|
166
|
+
/** @param {string} v @param {number} i @returns {any | null} */
|
|
167
|
+
const fill = (v, i) => {
|
|
168
|
+
if (i === vars.length) return tryOne(v);
|
|
169
|
+
for (const stand of VAR_STANDS_FOR) {
|
|
170
|
+
const ast = fill(v.replace(vars[i], stand), i + 1);
|
|
171
|
+
if (ast) return ast;
|
|
172
|
+
}
|
|
173
|
+
return null;
|
|
174
|
+
};
|
|
175
|
+
return fill(value, 0);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Whether a value that matches a property's grammar is still below a range
|
|
180
|
+
* the specs set and css-tree's data does not carry: a `<line-width>` (a
|
|
181
|
+
* border or outline width) and a duration are never negative.
|
|
182
|
+
* @param {string} property
|
|
183
|
+
* @param {string} value
|
|
184
|
+
*/
|
|
185
|
+
export function belowRange(property, value) {
|
|
186
|
+
const {lexer, parse} = csstree();
|
|
187
|
+
/** @type {any} */
|
|
188
|
+
let ast;
|
|
189
|
+
try {
|
|
190
|
+
ast = parse(value, {context: 'value'});
|
|
191
|
+
} catch {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
const r = lexer.matchProperty(property, ast);
|
|
195
|
+
if (r.error || !r.matched) return false;
|
|
196
|
+
let below = false;
|
|
197
|
+
/** @param {any} m @param {boolean} inWidth */
|
|
198
|
+
const visit = (m, inWidth) => {
|
|
199
|
+
const here =
|
|
200
|
+
inWidth || (m.syntax?.type === 'Type' && m.syntax.name === 'line-width');
|
|
201
|
+
if (here && typeof m.token === 'string' && m.token.startsWith('-'))
|
|
202
|
+
below = true;
|
|
203
|
+
for (const c of m.match ?? []) visit(c, here);
|
|
204
|
+
};
|
|
205
|
+
visit(r.matched, false);
|
|
206
|
+
if (
|
|
207
|
+
/-duration$/.test(property) &&
|
|
208
|
+
numbersIn(ast).some(n => n.value < 0 && ['s', 'ms'].includes(n.unit))
|
|
209
|
+
)
|
|
210
|
+
below = true;
|
|
211
|
+
return below;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** CSS math functions, whose insides css-tree's grammar does not check. */
|
|
215
|
+
const MATH = new Set([
|
|
216
|
+
'calc',
|
|
217
|
+
'min',
|
|
218
|
+
'max',
|
|
219
|
+
'clamp',
|
|
220
|
+
'round',
|
|
221
|
+
'mod',
|
|
222
|
+
'rem',
|
|
223
|
+
'abs',
|
|
224
|
+
'sign',
|
|
225
|
+
'pow',
|
|
226
|
+
'sqrt',
|
|
227
|
+
'hypot',
|
|
228
|
+
'log',
|
|
229
|
+
'exp',
|
|
230
|
+
'sin',
|
|
231
|
+
'cos',
|
|
232
|
+
'tan',
|
|
233
|
+
'asin',
|
|
234
|
+
'acos',
|
|
235
|
+
'atan',
|
|
236
|
+
'atan2',
|
|
237
|
+
]);
|
|
238
|
+
|
|
239
|
+
/** The CSS type of each unit a math function may hold. */
|
|
240
|
+
const UNIT_TYPES = /** @type {Record<string, string>} */ (
|
|
241
|
+
Object.fromEntries(
|
|
242
|
+
Object.entries({
|
|
243
|
+
length: [
|
|
244
|
+
'px',
|
|
245
|
+
'em',
|
|
246
|
+
'rem',
|
|
247
|
+
'ex',
|
|
248
|
+
'rex',
|
|
249
|
+
'ch',
|
|
250
|
+
'rch',
|
|
251
|
+
'cap',
|
|
252
|
+
'rcap',
|
|
253
|
+
'ic',
|
|
254
|
+
'ric',
|
|
255
|
+
'lh',
|
|
256
|
+
'rlh',
|
|
257
|
+
'vw',
|
|
258
|
+
'vh',
|
|
259
|
+
'vi',
|
|
260
|
+
'vb',
|
|
261
|
+
'vmin',
|
|
262
|
+
'vmax',
|
|
263
|
+
'svw',
|
|
264
|
+
'svh',
|
|
265
|
+
'svi',
|
|
266
|
+
'svb',
|
|
267
|
+
'svmin',
|
|
268
|
+
'svmax',
|
|
269
|
+
'lvw',
|
|
270
|
+
'lvh',
|
|
271
|
+
'lvi',
|
|
272
|
+
'lvb',
|
|
273
|
+
'lvmin',
|
|
274
|
+
'lvmax',
|
|
275
|
+
'dvw',
|
|
276
|
+
'dvh',
|
|
277
|
+
'dvi',
|
|
278
|
+
'dvb',
|
|
279
|
+
'dvmin',
|
|
280
|
+
'dvmax',
|
|
281
|
+
'cqw',
|
|
282
|
+
'cqh',
|
|
283
|
+
'cqi',
|
|
284
|
+
'cqb',
|
|
285
|
+
'cqmin',
|
|
286
|
+
'cqmax',
|
|
287
|
+
'cm',
|
|
288
|
+
'mm',
|
|
289
|
+
'q',
|
|
290
|
+
'in',
|
|
291
|
+
'pt',
|
|
292
|
+
'pc',
|
|
293
|
+
],
|
|
294
|
+
time: ['s', 'ms'],
|
|
295
|
+
angle: ['deg', 'grad', 'rad', 'turn'],
|
|
296
|
+
frequency: ['hz', 'khz'],
|
|
297
|
+
resolution: ['dpi', 'dpcm', 'dppx', 'x'],
|
|
298
|
+
}).flatMap(([type, units]) => units.map(u => [u, type])),
|
|
299
|
+
)
|
|
300
|
+
);
|
|
301
|
+
/** A value of each type, to match where a math function stands. */
|
|
302
|
+
const TYPE_SAMPLES = /** @type {Record<string, string>} */ ({
|
|
303
|
+
number: '1',
|
|
304
|
+
percent: '1%',
|
|
305
|
+
length: '1px',
|
|
306
|
+
time: '1s',
|
|
307
|
+
angle: '1deg',
|
|
308
|
+
frequency: '1hz',
|
|
309
|
+
resolution: '1dppx',
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* The type of a sum (or of the arguments of min(), max(), clamp()):
|
|
314
|
+
* both sides of one type, a percentage beside a length or another type
|
|
315
|
+
* (`length%`: it resolves against that type), or null when they do not mix.
|
|
316
|
+
* `any` is a var() or env(), which can be anything.
|
|
317
|
+
* @param {string | null} a
|
|
318
|
+
* @param {string | null} b
|
|
319
|
+
*/
|
|
320
|
+
function addTypes(a, b) {
|
|
321
|
+
if (a == null || b == null) return null;
|
|
322
|
+
if (a === 'any') return b;
|
|
323
|
+
if (b === 'any') return a;
|
|
324
|
+
if (a === b) return a;
|
|
325
|
+
const bare = (/** @type {string} */ t) => t.replace(/%$/, '');
|
|
326
|
+
if (a === 'number' || b === 'number') return null;
|
|
327
|
+
if (a === 'percent') return `${bare(b)}%`;
|
|
328
|
+
if (b === 'percent') return `${bare(a)}%`;
|
|
329
|
+
return bare(a) === bare(b) ? `${bare(a)}%` : null;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* The CSS type of a math function (`calc(4px + 2)` has none): a sum needs
|
|
334
|
+
* one type on both sides; a product needs a number on one side; a quotient
|
|
335
|
+
* needs a number on the right. null when the math is unsound.
|
|
336
|
+
* @param {any} fn a Function node
|
|
337
|
+
* @returns {string | null}
|
|
338
|
+
*/
|
|
339
|
+
function mathType(fn) {
|
|
340
|
+
/** @param {any} node @returns {string | null} */
|
|
341
|
+
const leaf = node => {
|
|
342
|
+
if (node.type === 'Number') return 'number';
|
|
343
|
+
if (node.type === 'Percentage') return 'percent';
|
|
344
|
+
if (node.type === 'Dimension')
|
|
345
|
+
return UNIT_TYPES[node.unit.toLowerCase()] ?? null;
|
|
346
|
+
if (node.type === 'Identifier') {
|
|
347
|
+
const w = node.name.toLowerCase();
|
|
348
|
+
if (['e', 'pi', 'infinity', '-infinity', 'nan'].includes(w))
|
|
349
|
+
return 'number';
|
|
350
|
+
// clamp() takes none for a bound it does not set.
|
|
351
|
+
if (w === 'none' && fn.name.toLowerCase() === 'clamp') return 'any';
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
if (node.type === 'Function') {
|
|
355
|
+
const name = node.name.toLowerCase();
|
|
356
|
+
if (name === 'var' || name === 'env') return 'any';
|
|
357
|
+
return MATH.has(name) ? mathType(node) : null;
|
|
358
|
+
}
|
|
359
|
+
if (node.type === 'Parentheses') return expression(node.children.toArray());
|
|
360
|
+
return null;
|
|
361
|
+
};
|
|
362
|
+
/** @param {any[]} nodes @returns {string | null} */
|
|
363
|
+
const expression = nodes => {
|
|
364
|
+
const items = nodes.filter(n => n.type !== 'WhiteSpace');
|
|
365
|
+
if (!items.length) return null;
|
|
366
|
+
// Products first, then sums.
|
|
367
|
+
/** @type {Array<string | null>} */
|
|
368
|
+
const terms = [];
|
|
369
|
+
let current = /** @type {string | null | undefined} */ (undefined);
|
|
370
|
+
let op = '';
|
|
371
|
+
for (const node of items) {
|
|
372
|
+
if (node.type === 'Operator') {
|
|
373
|
+
const o = String(node.value).trim();
|
|
374
|
+
if (current === undefined) return null;
|
|
375
|
+
if (o === '+' || o === '-') {
|
|
376
|
+
terms.push(current);
|
|
377
|
+
current = undefined;
|
|
378
|
+
} else op = o;
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
const t = leaf(node);
|
|
382
|
+
if (current === undefined) current = t;
|
|
383
|
+
else if (op === '*')
|
|
384
|
+
current =
|
|
385
|
+
current === 'number'
|
|
386
|
+
? t
|
|
387
|
+
: t === 'number'
|
|
388
|
+
? current
|
|
389
|
+
: current === 'any' || t === 'any'
|
|
390
|
+
? 'any'
|
|
391
|
+
: null;
|
|
392
|
+
else if (op === '/')
|
|
393
|
+
current = t === 'number' || t === 'any' ? current : null;
|
|
394
|
+
else return null;
|
|
395
|
+
op = '';
|
|
396
|
+
if (current == null) return null;
|
|
397
|
+
}
|
|
398
|
+
if (current === undefined || op) return null;
|
|
399
|
+
terms.push(current);
|
|
400
|
+
return terms.reduce((a, b) => addTypes(a, b));
|
|
401
|
+
};
|
|
402
|
+
const children = fn.children.toArray();
|
|
403
|
+
const name = fn.name.toLowerCase();
|
|
404
|
+
// Arguments split at commas; round() may open with a strategy word.
|
|
405
|
+
/** @type {any[][]} */
|
|
406
|
+
const args = [[]];
|
|
407
|
+
for (const n of children)
|
|
408
|
+
if (n.type === 'Operator' && String(n.value).trim() === ',') args.push([]);
|
|
409
|
+
else args[args.length - 1].push(n);
|
|
410
|
+
if (
|
|
411
|
+
name === 'round' &&
|
|
412
|
+
args.length > 1 &&
|
|
413
|
+
args[0].filter(n => n.type !== 'WhiteSpace').length === 1 &&
|
|
414
|
+
['nearest', 'up', 'down', 'to-zero'].includes(
|
|
415
|
+
String(args[0].find(n => n.type === 'Identifier')?.name).toLowerCase(),
|
|
416
|
+
)
|
|
417
|
+
)
|
|
418
|
+
args.shift();
|
|
419
|
+
const types = args.map(expression);
|
|
420
|
+
if (types.some(t => t == null)) return null;
|
|
421
|
+
/** @param {string | null} t */
|
|
422
|
+
const isNumber = t => t === 'number' || t === 'any';
|
|
423
|
+
// What each function takes and gives, as CSS Values 4 types them.
|
|
424
|
+
switch (name) {
|
|
425
|
+
case 'pow':
|
|
426
|
+
return types.length === 2 && types.every(isNumber) ? 'number' : null;
|
|
427
|
+
case 'sqrt':
|
|
428
|
+
case 'exp':
|
|
429
|
+
return types.length === 1 && isNumber(types[0]) ? 'number' : null;
|
|
430
|
+
case 'log':
|
|
431
|
+
return types.length <= 2 && types.every(isNumber) ? 'number' : null;
|
|
432
|
+
case 'sin':
|
|
433
|
+
case 'cos':
|
|
434
|
+
case 'tan':
|
|
435
|
+
return types.length === 1 && (isNumber(types[0]) || types[0] === 'angle')
|
|
436
|
+
? 'number'
|
|
437
|
+
: null;
|
|
438
|
+
case 'asin':
|
|
439
|
+
case 'acos':
|
|
440
|
+
case 'atan':
|
|
441
|
+
return types.length === 1 && isNumber(types[0]) ? 'angle' : null;
|
|
442
|
+
case 'atan2':
|
|
443
|
+
return types.length === 2 && addTypes(types[0], types[1])
|
|
444
|
+
? 'angle'
|
|
445
|
+
: null;
|
|
446
|
+
case 'sign':
|
|
447
|
+
return types.length === 1 ? 'number' : null;
|
|
448
|
+
case 'abs':
|
|
449
|
+
case 'calc':
|
|
450
|
+
return types.length === 1 ? types[0] : null;
|
|
451
|
+
case 'mod':
|
|
452
|
+
case 'rem':
|
|
453
|
+
return types.length === 2 ? addTypes(types[0], types[1]) : null;
|
|
454
|
+
case 'round':
|
|
455
|
+
return types.length <= 2 ? types.reduce((a, b) => addTypes(a, b)) : null;
|
|
456
|
+
default:
|
|
457
|
+
// min(), max(), clamp() and hypot(): arguments of one type.
|
|
458
|
+
if (name === 'clamp' && types.length !== 3) return null;
|
|
459
|
+
return types.reduce((a, b) => addTypes(a, b));
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* The value with each outermost math function put as plain values of its
|
|
465
|
+
* type (`calc(1px + 2px)` as `1px`), so the property's grammar decides
|
|
466
|
+
* whether that type may stand there; a percentage beside a length is tried
|
|
467
|
+
* as both. Null when a math function is unsound. A value with no math
|
|
468
|
+
* function gives itself.
|
|
469
|
+
* @param {any} ast
|
|
470
|
+
* @returns {string[] | null}
|
|
471
|
+
*/
|
|
472
|
+
function mathAsValues(ast) {
|
|
473
|
+
const t = csstree();
|
|
474
|
+
/** @type {Array<{text: string, samples: string[]}>} */
|
|
475
|
+
const found = [];
|
|
476
|
+
let sound = true;
|
|
477
|
+
t.walk(ast, {
|
|
478
|
+
enter(/** @type {any} */ node) {
|
|
479
|
+
if (node.type !== 'Function' || !MATH.has(node.name.toLowerCase()))
|
|
480
|
+
return undefined;
|
|
481
|
+
const type = mathType(node);
|
|
482
|
+
if (type == null) sound = false;
|
|
483
|
+
else if (type !== 'any')
|
|
484
|
+
found.push({
|
|
485
|
+
text: t.generate(node),
|
|
486
|
+
samples: type.endsWith('%')
|
|
487
|
+
? [TYPE_SAMPLES[type.slice(0, -1)], '1%']
|
|
488
|
+
: [TYPE_SAMPLES[type] ?? ''],
|
|
489
|
+
});
|
|
490
|
+
return t.walk.skip;
|
|
491
|
+
},
|
|
492
|
+
});
|
|
493
|
+
if (!sound || found.some(f => f.samples.some(x => !x))) return null;
|
|
494
|
+
/** @type {string[]} */
|
|
495
|
+
let out = [t.generate(ast)];
|
|
496
|
+
// Spaced, so a value that follows with no space stays its own token.
|
|
497
|
+
for (const f of found)
|
|
498
|
+
out = out.flatMap(v => f.samples.map(x => v.replace(f.text, ` ${x} `)));
|
|
499
|
+
return out.slice(0, 16);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Each number a matched value holds, with its unit (`''` for a plain
|
|
504
|
+
* number) and the function it sits in, in order.
|
|
505
|
+
* @param {any} ast
|
|
506
|
+
* @returns {Array<{value: number, unit: string, fn: string | null}>}
|
|
507
|
+
*/
|
|
508
|
+
export function numbersIn(ast) {
|
|
509
|
+
/** @type {Array<{value: number, unit: string, fn: string | null}>} */
|
|
510
|
+
const out = [];
|
|
511
|
+
/** @type {string[]} */
|
|
512
|
+
const fns = [];
|
|
513
|
+
csstree().walk(ast, {
|
|
514
|
+
enter(/** @type {any} */ node) {
|
|
515
|
+
if (node.type === 'Function') fns.push(node.name.toLowerCase());
|
|
516
|
+
if (node.type === 'Dimension' || node.type === 'Number')
|
|
517
|
+
out.push({
|
|
518
|
+
value: Number(node.value),
|
|
519
|
+
unit: (node.unit ?? '').toLowerCase(),
|
|
520
|
+
fn: fns.at(-1) ?? null,
|
|
521
|
+
});
|
|
522
|
+
},
|
|
523
|
+
leave(/** @type {any} */ node) {
|
|
524
|
+
if (node.type === 'Function') fns.pop();
|
|
525
|
+
},
|
|
526
|
+
});
|
|
527
|
+
return out;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* A property's grammar as css-tree holds it, or null when it has none.
|
|
532
|
+
* @param {string} prop
|
|
533
|
+
* @returns {any | null}
|
|
534
|
+
*/
|
|
535
|
+
export const propertyGrammar = prop =>
|
|
536
|
+
csstree().lexer.properties[prop]?.syntax ?? null;
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* The keywords a property's grammar takes, through the types it names.
|
|
540
|
+
* @param {string} prop
|
|
541
|
+
* @returns {string[]}
|
|
542
|
+
*/
|
|
543
|
+
export function keywordsOf(prop) {
|
|
544
|
+
const t = csstree();
|
|
545
|
+
const seen = new Set();
|
|
546
|
+
/** @type {Set<string>} */
|
|
547
|
+
const words = new Set();
|
|
548
|
+
/** @param {any} syntax */
|
|
549
|
+
const walk = syntax => {
|
|
550
|
+
if (!syntax) return;
|
|
551
|
+
t.definitionSyntax.walk(syntax, (/** @type {any} */ n) => {
|
|
552
|
+
if (n.type === 'Keyword') {
|
|
553
|
+
if (!n.name.startsWith('-')) words.add(n.name);
|
|
554
|
+
} else if (n.type === 'Type' && !seen.has(n.name)) {
|
|
555
|
+
seen.add(n.name);
|
|
556
|
+
walk(t.lexer.types[n.name]?.syntax);
|
|
557
|
+
} else if (n.type === 'Property' && !seen.has(`'${n.name}'`)) {
|
|
558
|
+
// A shorthand's longhands (<'text-decoration-line'>) hold its words.
|
|
559
|
+
seen.add(`'${n.name}'`);
|
|
560
|
+
walk(t.lexer.getProperty(n.name)?.syntax);
|
|
561
|
+
}
|
|
562
|
+
});
|
|
563
|
+
};
|
|
564
|
+
walk(propertyGrammar(prop));
|
|
565
|
+
return [...words];
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* A value the property takes, to put in a suggestion: its initial value
|
|
570
|
+
* from MDN's data (a shorthand's first longhand's).
|
|
571
|
+
* @param {string} prop
|
|
572
|
+
* @returns {string | null}
|
|
573
|
+
*/
|
|
574
|
+
export function initialOf(prop) {
|
|
575
|
+
const initial = mdnProperties()[prop]?.initial;
|
|
576
|
+
if (Array.isArray(initial)) return initial[0] ? initialOf(initial[0]) : null;
|
|
577
|
+
// MDN writes a note, not a value, where the initial value varies
|
|
578
|
+
// (dependsOnUserAgent): one camelCase word, never a CSS value.
|
|
579
|
+
return typeof initial === 'string' &&
|
|
580
|
+
!/^[a-z]+[A-Z]\w*$/.test(initial) &&
|
|
581
|
+
matchValue(prop, initial)
|
|
582
|
+
? initial
|
|
583
|
+
: null;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/** The longhand each type of the `font` grammar sets, where css-tree names a type, not a property. */
|
|
587
|
+
const FONT_TYPE_LONGHAND = /** @type {Record<string, string>} */ ({
|
|
588
|
+
'font-variant-css2': 'font-variant-caps',
|
|
589
|
+
'font-width-css3': 'font-stretch',
|
|
590
|
+
'font-stretch-absolute': 'font-stretch',
|
|
591
|
+
'system-family-name': 'system',
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* The parts of a CSS `font` value, by the longhand each sets, as written:
|
|
596
|
+
* `italic small-caps 700 1rem/1 Arial` gives font-style, font-variant-caps,
|
|
597
|
+
* font-weight, font-size, line-height and font-family. A system font
|
|
598
|
+
* keyword (`caption`) is `system`. A single keyword of one longhand
|
|
599
|
+
* (`bold`) is that longhand. Null for anything else.
|
|
600
|
+
* @param {string} value
|
|
601
|
+
* @returns {Record<string, string> | null}
|
|
602
|
+
*/
|
|
603
|
+
export function fontShorthandParts(value) {
|
|
604
|
+
const {lexer, parse, generate} = csstree();
|
|
605
|
+
const word = value.trim().toLowerCase();
|
|
606
|
+
if (/^[a-z-]+$/.test(word))
|
|
607
|
+
for (const prop of [
|
|
608
|
+
'font-weight',
|
|
609
|
+
'font-style',
|
|
610
|
+
'font-variant-caps',
|
|
611
|
+
'font-stretch',
|
|
612
|
+
])
|
|
613
|
+
if (keywordsOf(prop).includes(word)) return {[prop]: word};
|
|
614
|
+
/** @type {any} */
|
|
615
|
+
let ast;
|
|
616
|
+
try {
|
|
617
|
+
ast = parse(value, {context: 'value', positions: true});
|
|
618
|
+
} catch {
|
|
619
|
+
return null;
|
|
620
|
+
}
|
|
621
|
+
const r = lexer.matchProperty('font', ast);
|
|
622
|
+
if (r.error || !r.matched) return null;
|
|
623
|
+
/** @type {Record<string, any[]>} */
|
|
624
|
+
const parts = {};
|
|
625
|
+
/** @param {any} m @param {string | null} under */
|
|
626
|
+
const visit = (m, under) => {
|
|
627
|
+
const syntax = m.syntax;
|
|
628
|
+
const name =
|
|
629
|
+
under === 'font-family'
|
|
630
|
+
? under
|
|
631
|
+
: syntax?.type === 'Property' && syntax.name !== 'font'
|
|
632
|
+
? String(syntax.name)
|
|
633
|
+
: syntax?.type === 'Type' && FONT_TYPE_LONGHAND[syntax.name]
|
|
634
|
+
? FONT_TYPE_LONGHAND[syntax.name]
|
|
635
|
+
: syntax?.type === 'Type' && /family/.test(syntax.name)
|
|
636
|
+
? 'font-family'
|
|
637
|
+
: under;
|
|
638
|
+
if (m.node && name) (parts[name] ??= []).push(m.node);
|
|
639
|
+
for (const c of m.match ?? []) visit(c, name);
|
|
640
|
+
};
|
|
641
|
+
visit(r.matched, null);
|
|
642
|
+
/** @type {Record<string, string>} */
|
|
643
|
+
const out = {};
|
|
644
|
+
for (const [k, nodes] of Object.entries(parts)) {
|
|
645
|
+
// The family list is the rest of the value, as written.
|
|
646
|
+
out[k] =
|
|
647
|
+
k === 'font-family'
|
|
648
|
+
? value.slice(nodes[0].loc?.start.offset ?? 0).trim()
|
|
649
|
+
: nodes.map(n => generate(n)).join(' ');
|
|
650
|
+
}
|
|
651
|
+
return Object.keys(out).length ? out : null;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* A grammar as text, cut at a whole term when it is long.
|
|
656
|
+
* @param {any} syntax
|
|
657
|
+
*/
|
|
658
|
+
export function grammarText(syntax) {
|
|
659
|
+
const text = csstree().definitionSyntax.generate(syntax);
|
|
660
|
+
if (text.length <= 120) return text;
|
|
661
|
+
return `${text.slice(0, 120).replace(/\s*\|[^|]*$/, '')} | …`;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
/** The math functions, by name, for a check of whether a value holds one. */
|
|
665
|
+
export const MATH_FUNCTION = new RegExp(`\\b(${[...MATH].join('|')})\\(`, 'i');
|
|
666
|
+
|
|
667
|
+
/** @type {Map<string, boolean>} */
|
|
668
|
+
const percentCache = new Map();
|
|
669
|
+
/**
|
|
670
|
+
* Whether a property's grammar takes a percentage anywhere, through the
|
|
671
|
+
* types it names (padding does; border-width and outline-offset do not).
|
|
672
|
+
* @param {string} prop
|
|
673
|
+
*/
|
|
674
|
+
export function takesPercentage(prop) {
|
|
675
|
+
const known = percentCache.get(prop);
|
|
676
|
+
if (known != null) return known;
|
|
677
|
+
const t = csstree();
|
|
678
|
+
const seen = new Set();
|
|
679
|
+
let found = false;
|
|
680
|
+
/** @param {any} syntax */
|
|
681
|
+
const walk = syntax => {
|
|
682
|
+
if (!syntax || found) return;
|
|
683
|
+
t.definitionSyntax.walk(syntax, (/** @type {any} */ n) => {
|
|
684
|
+
if (found) return;
|
|
685
|
+
if (n.type === 'Type') {
|
|
686
|
+
if (/percentage/.test(n.name)) found = true;
|
|
687
|
+
else if (!seen.has(n.name)) {
|
|
688
|
+
seen.add(n.name);
|
|
689
|
+
walk(t.lexer.types[n.name]?.syntax);
|
|
690
|
+
}
|
|
691
|
+
} else if (n.type === 'Property' && !seen.has(`'${n.name}'`)) {
|
|
692
|
+
seen.add(`'${n.name}'`);
|
|
693
|
+
walk(t.lexer.properties[n.name]?.syntax);
|
|
694
|
+
}
|
|
695
|
+
});
|
|
696
|
+
};
|
|
697
|
+
walk(propertyGrammar(prop));
|
|
698
|
+
percentCache.set(prop, found);
|
|
699
|
+
return found;
|
|
700
|
+
}
|