@tbela99/css-parser 2.2.0 → 2.3.0
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 +24 -0
- package/dist/{lib/ast → ast}/arena-node.js +1 -1
- package/dist/{lib/ast → ast}/arena.js +1 -1
- package/dist/{lib/ast → ast}/cache.js +39 -23
- package/dist/{lib/ast → ast}/features/calc.js +1 -1
- package/dist/{lib/ast → ast}/features/if.js +1 -1
- package/dist/ast/features/index.d.ts +7 -0
- package/dist/{lib/ast → ast}/features/index.js +1 -1
- package/dist/{lib/ast → ast}/features/inlinecssvariables.js +1 -1
- package/dist/{lib/ast → ast}/features/mixin.js +1 -1
- package/dist/{lib/ast → ast}/features/prefix.js +1 -1
- package/dist/{lib/ast → ast}/features/shorthand.js +1 -1
- package/dist/{lib/ast → ast}/features/transform.js +1 -1
- package/dist/{lib/ast → ast}/features/type.js +1 -1
- package/dist/{lib/ast → ast}/math/expression.js +1 -1
- package/dist/{lib/ast → ast}/math/math.js +1 -1
- package/dist/{lib/ast → ast}/minify.js +1 -1
- package/dist/{lib/ast → ast}/mixins/custom-function.js +1 -1
- package/dist/{lib/ast → ast}/mixins/utils.js +1 -1
- package/dist/{lib/ast → ast}/mixins/variables.js +1 -1
- package/dist/{lib/ast → ast}/strings.js +5 -1
- package/dist/{lib/ast → ast}/transform/compute.js +1 -1
- package/dist/{lib/ast → ast}/transform/matrix.js +1 -1
- package/dist/{lib/ast → ast}/transform/minify.js +1 -1
- package/dist/{lib/ast → ast}/transform/perspective.js +1 -1
- package/dist/{lib/ast → ast}/transform/rotate.js +1 -1
- package/dist/{lib/ast → ast}/transform/scale.js +1 -1
- package/dist/{lib/ast → ast}/transform/skew.js +1 -1
- package/dist/{lib/ast → ast}/transform/translate.js +1 -1
- package/dist/{lib/ast → ast}/transform/utils.js +1 -1
- package/dist/{lib/ast → ast}/types.js +1 -1
- package/dist/{lib/ast → ast}/walk.js +1 -1
- package/dist/data/properties.json.js +1 -1
- package/dist/data/validation.json.js +26 -20
- package/dist/{lib/fs → fs}/resolve.js +1 -1
- package/dist/index-umd-web.js +2755 -126
- package/dist/index.d.ts +6 -2
- package/dist/node.js +21 -21
- package/dist/{lib/parser → parser}/declaration/list.js +1 -1
- package/dist/{lib/parser → parser}/declaration/map.js +1 -1
- package/dist/{lib/parser → parser}/declaration/set.js +1 -1
- package/dist/{lib/parser → parser}/input.js +1 -1
- package/dist/{lib/parser → parser}/linesmap.js +1 -1
- package/dist/{lib/parser → parser}/parse.js +20 -21
- package/dist/{lib/parser → parser}/source.js +2 -2
- package/dist/{lib/parser → parser}/tokenize.js +1 -1
- package/dist/{lib/parser → parser}/utils/angle.js +1 -1
- package/dist/{lib/parser → parser}/utils/at-rule-container.js +1 -1
- package/dist/{lib/parser → parser}/utils/at-rule-font-feature-values.js +1 -1
- package/dist/{lib/parser → parser}/utils/at-rule-generic.js +1 -1
- package/dist/{lib/parser → parser}/utils/at-rule-import.js +1 -1
- package/dist/{lib/parser → parser}/utils/at-rule-media.js +1 -1
- package/dist/{lib/parser → parser}/utils/at-rule-support.js +1 -1
- package/dist/{lib/parser → parser}/utils/at-rule-when-else.js +1 -1
- package/dist/{lib/parser → parser}/utils/at-rule.js +1 -1
- package/dist/{lib/parser → parser}/utils/base64.js +1 -1
- package/dist/{lib/parser → parser}/utils/cache.js +1 -1
- package/dist/{lib/parser → parser}/utils/canonical.js +1 -1
- package/dist/{lib/parser → parser}/utils/codes.js +1 -1
- package/dist/{lib/parser → parser}/utils/config.js +2 -2
- package/dist/{lib/parser → parser}/utils/declaration.js +1 -1
- package/dist/{lib/parser → parser}/utils/eq.js +1 -1
- package/dist/{lib/parser → parser}/utils/hash.js +1 -1
- package/dist/{lib/parser → parser}/utils/selector.js +38 -28
- package/dist/{lib/parser → parser}/utils/text.js +1 -1
- package/dist/{lib/parser → parser}/utils/token.js +1 -1
- package/dist/{lib/parser → parser}/utils/type.js +1 -1
- package/dist/{lib/printer/sourcemap → printer}/lib/codec.js +1 -1
- package/dist/{lib/printer → printer}/render.js +184 -23
- package/dist/{lib/printer → printer}/result.js +1 -1
- package/dist/{lib/printer/sourcemap → printer}/sourcemap.js +3 -2
- package/dist/{lib/printer → printer}/writer.js +1 -1
- package/dist/{lib/syntax → syntax}/color/a98rgb.js +1 -1
- package/dist/{lib/syntax → syntax}/color/alpha.js +1 -1
- package/dist/{lib/syntax → syntax}/color/cmyk.js +1 -1
- package/dist/{lib/syntax → syntax}/color/color-mix.js +1 -1
- package/dist/{lib/syntax → syntax}/color/color.js +1 -1
- package/dist/{lib/syntax → syntax}/color/hex.js +1 -1
- package/dist/{lib/syntax → syntax}/color/hsl.js +1 -1
- package/dist/{lib/syntax → syntax}/color/hwb.js +1 -1
- package/dist/{lib/syntax → syntax}/color/lab.js +1 -1
- package/dist/{lib/syntax → syntax}/color/lch.js +1 -1
- package/dist/{lib/syntax → syntax}/color/oklab.js +1 -1
- package/dist/{lib/syntax → syntax}/color/oklch.js +1 -1
- package/dist/{lib/syntax → syntax}/color/p3.js +1 -1
- package/dist/{lib/syntax → syntax}/color/prophotorgb.js +1 -1
- package/dist/{lib/syntax → syntax}/color/rec2020.js +1 -1
- package/dist/{lib/syntax → syntax}/color/relative-color.js +1 -1
- package/dist/{lib/syntax → syntax}/color/rgb.js +1 -1
- package/dist/{lib/syntax → syntax}/color/srgb.js +1 -1
- package/dist/{lib/syntax → syntax}/color/utils/color-type.js +1 -1
- package/dist/{lib/syntax → syntax}/color/utils/colorspace.js +1 -1
- package/dist/{lib/syntax → syntax}/color/utils/components.js +1 -1
- package/dist/{lib/syntax → syntax}/color/utils/distance.js +1 -1
- package/dist/{lib/syntax → syntax}/color/utils/make-color.js +1 -1
- package/dist/{lib/syntax → syntax}/color/utils/matrix.js +1 -1
- package/dist/{lib/syntax → syntax}/color/xyz.js +1 -1
- package/dist/{lib/syntax → syntax}/color/xyzd50.js +1 -1
- package/dist/{lib/syntax → syntax}/constants.js +2 -2
- package/dist/{lib/syntax → syntax}/syntax.js +1 -1
- package/dist/types.js +1 -1
- package/dist/utils/sync.js +3 -3
- package/dist/validation/compiled/builtins.js +32 -0
- package/dist/validation/compiled/runtime.js +705 -0
- package/dist/validation/compiled/syntaxes.js +1620 -0
- package/dist/{lib/validation → validation}/config.js +41 -7
- package/dist/{lib/validation → validation}/first.js +6 -3
- package/dist/{lib/validation → validation}/match.js +87 -14
- package/dist/{lib/validation → validation}/parser/parse.js +1 -1
- package/dist/{lib/validation → validation}/parser/typedef.js +1 -1
- package/dist/{lib/validation → validation}/utils/list.js +1 -1
- package/dist/version.js +2 -2
- package/dist/web.js +23 -23
- package/package.json +4 -2
- package/dist/types.d.ts +0 -21
- package/dist/utils/sync.d.ts +0 -16
- package/dist/version.d.ts +0 -4
|
@@ -0,0 +1,705 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 2.3.0
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
8
|
+
import { EnumToken } from '../../ast/types.js';
|
|
9
|
+
import { lowercase } from '../../parser/utils/text.js';
|
|
10
|
+
import { isFunctionDefType, funcLike, isFunctionType } from '../../syntax/constants.js';
|
|
11
|
+
import { ValidationTokenEnum } from '../parser/typedef.js';
|
|
12
|
+
import { isCssWideKeyword, matchProperty, funcTypes, ValidationTokenContext } from '../match.js';
|
|
13
|
+
import { keys, builders } from './syntaxes.js';
|
|
14
|
+
import { rawTypes } from './builtins.js';
|
|
15
|
+
|
|
16
|
+
const FAIL = -1;
|
|
17
|
+
/**
|
|
18
|
+
* thrown when the value must be validated by the interpreter
|
|
19
|
+
*/
|
|
20
|
+
const BAIL = Object.freeze({ bail: true });
|
|
21
|
+
/**
|
|
22
|
+
* the maximum depth of the references being matched: deeper, the interpreter validates the value
|
|
23
|
+
*/
|
|
24
|
+
const maxDepth = 256;
|
|
25
|
+
/**
|
|
26
|
+
* the matchers of the definitions, by index (see {@link builders})
|
|
27
|
+
*/
|
|
28
|
+
const matchers = [];
|
|
29
|
+
/**
|
|
30
|
+
* the matcher of a definition, built once
|
|
31
|
+
* @param index
|
|
32
|
+
*/
|
|
33
|
+
function definition(index) {
|
|
34
|
+
return (matchers[index] ??= builders[index]());
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* a reference to a definition: the matcher is built when it is first matched
|
|
38
|
+
* @param index
|
|
39
|
+
*/
|
|
40
|
+
function ref(index) {
|
|
41
|
+
let matcher = null;
|
|
42
|
+
return (c, p) => {
|
|
43
|
+
if (++c.depth > maxDepth) {
|
|
44
|
+
throw BAIL;
|
|
45
|
+
}
|
|
46
|
+
const result = (matcher ??= definition(index))(c, p);
|
|
47
|
+
c.depth--;
|
|
48
|
+
return result;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* a definition the compiler could not compile: the interpreter validates it
|
|
53
|
+
*/
|
|
54
|
+
const interpreted = () => {
|
|
55
|
+
throw BAIL;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* an empty syntax
|
|
59
|
+
*/
|
|
60
|
+
const empty = (_c, p) => p;
|
|
61
|
+
/**
|
|
62
|
+
* a sequence of syntaxes. the remaining syntaxes must be nullable when the tokens are consumed
|
|
63
|
+
* @param items
|
|
64
|
+
* @param nullable nullable[i]: the syntaxes from i are nullable
|
|
65
|
+
*/
|
|
66
|
+
function seq(items, nullable) {
|
|
67
|
+
const length = items.length;
|
|
68
|
+
if (length === 1) {
|
|
69
|
+
const item = items[0];
|
|
70
|
+
const optional = nullable[0] === 1;
|
|
71
|
+
return (c, p) => (p >= c.len ? (optional ? p : FAIL) : item(c, p));
|
|
72
|
+
}
|
|
73
|
+
return (c, p) => {
|
|
74
|
+
for (let i = 0; i < length; i++) {
|
|
75
|
+
if (p >= c.len) {
|
|
76
|
+
return nullable[i] === 1 ? p : FAIL;
|
|
77
|
+
}
|
|
78
|
+
p = items[i](c, p);
|
|
79
|
+
if (p < 0) {
|
|
80
|
+
return FAIL;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return p;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* alternatives: the alternative that matches the most tokens
|
|
88
|
+
* @param alternatives
|
|
89
|
+
*/
|
|
90
|
+
function alt(alternatives) {
|
|
91
|
+
const length = alternatives.length;
|
|
92
|
+
return (c, p) => {
|
|
93
|
+
let best = FAIL;
|
|
94
|
+
for (let i = 0; i < length; i++) {
|
|
95
|
+
const result = alternatives[i](c, p);
|
|
96
|
+
if (result > best) {
|
|
97
|
+
best = result;
|
|
98
|
+
if (result >= c.len) {
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return best;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* the lowercase value of an ident token, null for another token
|
|
108
|
+
* @param c
|
|
109
|
+
* @param token
|
|
110
|
+
*/
|
|
111
|
+
function identValue(c, token) {
|
|
112
|
+
return c.arena.dataTypes[token] === EnumToken.IdenTokenType
|
|
113
|
+
? lowercase(c.arena.resolveString(c.arena.stringValue[token]))
|
|
114
|
+
: null;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* a keyword, ignoring the case
|
|
118
|
+
* @param value the lowercase keyword
|
|
119
|
+
*/
|
|
120
|
+
function kw(value) {
|
|
121
|
+
return (c, p) => (identValue(c, c.toks[p]) === value ? p + 1 : FAIL);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* alternative keywords, ignoring the case
|
|
125
|
+
* @param values the lowercase keywords
|
|
126
|
+
*/
|
|
127
|
+
function kws(values) {
|
|
128
|
+
const set = new Set(values);
|
|
129
|
+
return (c, p) => {
|
|
130
|
+
const value = identValue(c, c.toks[p]);
|
|
131
|
+
return value !== null && set.has(value) ? p + 1 : FAIL;
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* an optional syntax: skipped when it does not match
|
|
136
|
+
* @param matcher
|
|
137
|
+
*/
|
|
138
|
+
function opt(matcher) {
|
|
139
|
+
return (c, p) => {
|
|
140
|
+
const result = matcher(c, p);
|
|
141
|
+
return result < 0 ? p : result;
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* a repeated syntax: `*` (zero or more) or `+` (one or more), see matchRepeatableSyntax()
|
|
146
|
+
* @param item
|
|
147
|
+
* @param zero zero matches are allowed
|
|
148
|
+
*/
|
|
149
|
+
function rep(item, zero) {
|
|
150
|
+
return (c, p) => {
|
|
151
|
+
let matched = false;
|
|
152
|
+
while (true) {
|
|
153
|
+
const result = item(c, p);
|
|
154
|
+
if (result < 0) {
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
matched = true;
|
|
158
|
+
if (result >= c.len) {
|
|
159
|
+
p = c.len;
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
// no token matched
|
|
163
|
+
if (result === p) {
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
p = result;
|
|
167
|
+
}
|
|
168
|
+
return matched || zero ? p : FAIL;
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* a syntax repeated from min to max times: `{2}`, `{1,4}`, see matchOccurenceSyntax()
|
|
173
|
+
* @param item
|
|
174
|
+
* @param min
|
|
175
|
+
* @param max
|
|
176
|
+
*/
|
|
177
|
+
function occ(item, min, max) {
|
|
178
|
+
return (c, p) => {
|
|
179
|
+
let count = 0;
|
|
180
|
+
do {
|
|
181
|
+
const result = item(c, p);
|
|
182
|
+
if (result < 0) {
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
count++;
|
|
186
|
+
if (result >= c.len) {
|
|
187
|
+
p = c.len;
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
if (result === p) {
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
p = result;
|
|
194
|
+
if (count === max) {
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
} while (p < c.len);
|
|
198
|
+
return count === 0 || count < min || count > max ? FAIL : p;
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* the end of a comma separated item: the position of the comma that follows the item (the items may contain commas)
|
|
203
|
+
* @param c
|
|
204
|
+
* @param p
|
|
205
|
+
* @param commas the number of commas of an item
|
|
206
|
+
*/
|
|
207
|
+
function itemEnd(c, p, commas) {
|
|
208
|
+
let depth = 0;
|
|
209
|
+
for (let i = p; i < c.len; i++) {
|
|
210
|
+
const type = c.arena.dataTypes[c.toks[i]];
|
|
211
|
+
if (type === EnumToken.StartParensTokenType || isFunctionDefType(type)) {
|
|
212
|
+
depth++;
|
|
213
|
+
}
|
|
214
|
+
else if (type === EnumToken.EndParensTokenType) {
|
|
215
|
+
depth--;
|
|
216
|
+
}
|
|
217
|
+
else if (depth === 0 && type === EnumToken.CommaTokenType && commas-- === 0) {
|
|
218
|
+
return i;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return c.len;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* a comma separated list: `#`, `#{1,4}`, see matchListSyntax(). an item is matched up to the next comma
|
|
225
|
+
* @param item
|
|
226
|
+
* @param min
|
|
227
|
+
* @param max
|
|
228
|
+
* @param commas the number of commas of an item
|
|
229
|
+
*/
|
|
230
|
+
function list(item, min, max, commas) {
|
|
231
|
+
return (c, p) => {
|
|
232
|
+
const len = c.len;
|
|
233
|
+
let count = 0;
|
|
234
|
+
while (true) {
|
|
235
|
+
const end = itemEnd(c, p, commas);
|
|
236
|
+
// BAIL aborts the whole match: the length is not restored
|
|
237
|
+
c.len = end;
|
|
238
|
+
const result = item(c, p);
|
|
239
|
+
c.len = len;
|
|
240
|
+
if (result < 0) {
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
// the item does not match the tokens before the comma: the list ends with this item
|
|
244
|
+
if (result < end) {
|
|
245
|
+
if (result > p) {
|
|
246
|
+
count++;
|
|
247
|
+
p = result;
|
|
248
|
+
}
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
count++;
|
|
252
|
+
if (count === max) {
|
|
253
|
+
p = end;
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
// the comma
|
|
257
|
+
p = end < len ? end + 1 : end;
|
|
258
|
+
if (p >= len) {
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return count === 0 || count < min ? FAIL : p;
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* the context passed to matchProperty(): the significant tokens, up to the end of the tokens being matched
|
|
267
|
+
* @param c
|
|
268
|
+
* @param p
|
|
269
|
+
*/
|
|
270
|
+
function leafContext(c, p) {
|
|
271
|
+
if (c.leaf === null || c.leafLen !== c.len) {
|
|
272
|
+
c.leaf = new ValidationTokenContext(c.arena, c.len === c.toks.length ? c.toks : c.toks.slice(0, c.len), p - 1);
|
|
273
|
+
c.leafLen = c.len;
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
c.leaf.index = p - 1;
|
|
277
|
+
}
|
|
278
|
+
return c.leaf;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* the token types matched by the numeric types without calc() and the css wide keywords: the matchers of these types
|
|
282
|
+
* do not call matchProperty() for the other tokens (see {@link leaf})
|
|
283
|
+
*/
|
|
284
|
+
const numericTypes = {
|
|
285
|
+
number: (_c, _t, type) => type === EnumToken.NumberTokenType,
|
|
286
|
+
integer: (c, t, type) => type === EnumToken.NumberTokenType && Number.isInteger(Number(c.arena.numericValue[t])),
|
|
287
|
+
zero: (c, t, type) => type === EnumToken.NumberTokenType && c.arena.numericValue[t] === 0,
|
|
288
|
+
length: (c, t, type) => type === EnumToken.LengthTokenType ||
|
|
289
|
+
type === EnumToken.UserDefinedDimensionTokenType ||
|
|
290
|
+
(type === EnumToken.NumberTokenType && c.arena.numericValue[t] === 0),
|
|
291
|
+
percentage: (_c, _t, type) => type === EnumToken.PercentageTokenType,
|
|
292
|
+
"length-percentage": (c, t, type) => type === EnumToken.PercentageTokenType ||
|
|
293
|
+
type === EnumToken.LengthTokenType ||
|
|
294
|
+
type === EnumToken.UserDefinedDimensionTokenType ||
|
|
295
|
+
(type === EnumToken.NumberTokenType && c.arena.numericValue[t] === 0),
|
|
296
|
+
angle: (_c, _t, type) => type === EnumToken.AngleTokenType || type === EnumToken.UserDefinedDimensionTokenType,
|
|
297
|
+
"angle-percentage": (_c, _t, type) => type === EnumToken.AngleTokenType ||
|
|
298
|
+
type === EnumToken.UserDefinedDimensionTokenType ||
|
|
299
|
+
type === EnumToken.PercentageTokenType,
|
|
300
|
+
time: (_c, _t, type) => type === EnumToken.TimeTokenType || type === EnumToken.UserDefinedDimensionTokenType,
|
|
301
|
+
flex: (_c, _t, type) => type === EnumToken.FlexTokenType || type === EnumToken.UserDefinedDimensionTokenType,
|
|
302
|
+
resolution: (_c, _t, type) => type === EnumToken.ResolutionTokenType,
|
|
303
|
+
frequency: (_c, _t, type) => type === EnumToken.FrequencyTokenType,
|
|
304
|
+
string: (_c, _t, type) => type === EnumToken.StringTokenType,
|
|
305
|
+
"dashed-ident": (_c, _t, type) => type === EnumToken.DashedIdenTokenType,
|
|
306
|
+
"custom-property-name": (_c, _t, type) => type === EnumToken.DashedIdenTokenType,
|
|
307
|
+
};
|
|
308
|
+
/**
|
|
309
|
+
* a type validated by matchProperty(): the numeric types are matched here when the token is neither an ident (a css
|
|
310
|
+
* wide keyword is accepted by the types) nor a math function (calc())
|
|
311
|
+
* @param name
|
|
312
|
+
*/
|
|
313
|
+
function leaf(name) {
|
|
314
|
+
if (rawTypes.has(name)) {
|
|
315
|
+
return interpreted;
|
|
316
|
+
}
|
|
317
|
+
const property = {
|
|
318
|
+
typ: ValidationTokenEnum.PropertyType,
|
|
319
|
+
val: name,
|
|
320
|
+
};
|
|
321
|
+
const fast = numericTypes[name] ?? null;
|
|
322
|
+
return (c, p) => {
|
|
323
|
+
const token = c.toks[p];
|
|
324
|
+
const type = c.arena.dataTypes[token];
|
|
325
|
+
if (fast !== null &&
|
|
326
|
+
type !== EnumToken.IdenTokenType &&
|
|
327
|
+
type !== EnumToken.MathFunctionTokenType &&
|
|
328
|
+
type !== EnumToken.MathFunctionTokenDefType) {
|
|
329
|
+
return fast(c, token, type) ? p + 1 : FAIL;
|
|
330
|
+
}
|
|
331
|
+
const context = leafContext(c, p);
|
|
332
|
+
const result = matchProperty(c.arena, property, context, c.options);
|
|
333
|
+
if (!result.success) {
|
|
334
|
+
return FAIL;
|
|
335
|
+
}
|
|
336
|
+
return context.index >= c.len ? c.len : context.index + 1;
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* see isInRange() in src/validation/match.ts
|
|
341
|
+
* @param c
|
|
342
|
+
* @param token
|
|
343
|
+
* @param min
|
|
344
|
+
* @param max
|
|
345
|
+
* @param unit the unit of a dimension range, its token type
|
|
346
|
+
*/
|
|
347
|
+
function inRange(c, token, min, max, unit) {
|
|
348
|
+
const type = c.arena.dataTypes[token];
|
|
349
|
+
if (type === EnumToken.MathFunctionTokenType ||
|
|
350
|
+
type === EnumToken.MathFunctionTokenDefType ||
|
|
351
|
+
type === EnumToken.WildCardFunctionTokenType ||
|
|
352
|
+
type === EnumToken.WildCardFunctionTokenDefType) {
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
let success = !c.arena.hasFraction(token) && c.arena.numericValue[token] >= min;
|
|
356
|
+
if (unit !== null) {
|
|
357
|
+
success = type === EnumToken[unit];
|
|
358
|
+
}
|
|
359
|
+
return success && c.arena.numericValue[token] <= max;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* a type with a range: `<integer [1,∞]>`. the range of the first token is checked after the match, a value out of the
|
|
363
|
+
* range fails even if the type is optional
|
|
364
|
+
* @param matcher
|
|
365
|
+
* @param bounds [min, max, unit, has min]
|
|
366
|
+
* @param optional
|
|
367
|
+
*/
|
|
368
|
+
function rng(matcher, bounds, optional) {
|
|
369
|
+
const [min, max, unit] = bounds;
|
|
370
|
+
return (c, p) => {
|
|
371
|
+
const result = matcher(c, p);
|
|
372
|
+
if (result < 0) {
|
|
373
|
+
return optional ? p : FAIL;
|
|
374
|
+
}
|
|
375
|
+
return inRange(c, c.toks[p], min, max, unit) ? result : FAIL;
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* the significant tokens of a list of tokens: the whitespace and the comments are skipped, a trailing `!important` is
|
|
380
|
+
* removed (see createValidationContext())
|
|
381
|
+
* @param arena
|
|
382
|
+
* @param tokens
|
|
383
|
+
* @param result
|
|
384
|
+
*/
|
|
385
|
+
function significantTokens(arena, tokens, result) {
|
|
386
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
387
|
+
const type = arena.dataTypes[tokens[i]];
|
|
388
|
+
if (type !== EnumToken.WhitespaceTokenType &&
|
|
389
|
+
type !== EnumToken.CommentTokenType &&
|
|
390
|
+
type !== EnumToken.CDOCOMMTokenType) {
|
|
391
|
+
result.push(tokens[i]);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
if (result.length > 0 && arena.dataTypes[result[result.length - 1]] === EnumToken.ImportantTokenType) {
|
|
395
|
+
result.pop();
|
|
396
|
+
}
|
|
397
|
+
return result;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* the position of the parenthesis that closes the function at a position: the arguments of a function are not nested
|
|
401
|
+
* in the tokens being validated, they follow the function token (see peekRange()). -1 if the function is not closed
|
|
402
|
+
* @param c
|
|
403
|
+
* @param p
|
|
404
|
+
*/
|
|
405
|
+
function closingParens(c, p) {
|
|
406
|
+
let depth = 0;
|
|
407
|
+
for (let i = p; i < c.len; i++) {
|
|
408
|
+
const type = c.arena.dataTypes[c.toks[i]];
|
|
409
|
+
if (type === EnumToken.StartParensTokenType || isFunctionDefType(type)) {
|
|
410
|
+
depth++;
|
|
411
|
+
}
|
|
412
|
+
else if (type === EnumToken.EndParensTokenType && --depth === 0) {
|
|
413
|
+
return i;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return -1;
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* match the arguments of a function, from p to end (excluded)
|
|
420
|
+
* @param c
|
|
421
|
+
* @param matcher
|
|
422
|
+
* @param p
|
|
423
|
+
* @param end
|
|
424
|
+
* @returns true if the matcher matches all the tokens
|
|
425
|
+
*/
|
|
426
|
+
function matchRange(c, matcher, p, end) {
|
|
427
|
+
const len = c.len;
|
|
428
|
+
// BAIL aborts the whole match: the length is not restored
|
|
429
|
+
c.len = end;
|
|
430
|
+
const result = matcher(c, p);
|
|
431
|
+
c.len = len;
|
|
432
|
+
return result === end;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* a function and its arguments: `rgb( ... )`
|
|
436
|
+
* @param name the lowercase function name
|
|
437
|
+
* @param args
|
|
438
|
+
*/
|
|
439
|
+
function fn(name, args) {
|
|
440
|
+
return (c, p) => {
|
|
441
|
+
const token = c.toks[p];
|
|
442
|
+
const type = c.arena.dataTypes[token];
|
|
443
|
+
if ((!isFunctionDefType(type) && !funcLike.includes(type) && !funcTypes.includes(type)) ||
|
|
444
|
+
lowercase(c.arena.resolveString(c.arena.stringValue[token])) !== name) {
|
|
445
|
+
return FAIL;
|
|
446
|
+
}
|
|
447
|
+
if (isFunctionDefType(type)) {
|
|
448
|
+
const close = closingParens(c, p);
|
|
449
|
+
return close !== -1 && matchRange(c, args, p + 1, close) ? close + 1 : FAIL;
|
|
450
|
+
}
|
|
451
|
+
const toks = significantTokens(c.arena, c.arena.tokens(token), []);
|
|
452
|
+
const sub = {
|
|
453
|
+
arena: c.arena,
|
|
454
|
+
toks,
|
|
455
|
+
len: toks.length,
|
|
456
|
+
options: c.options,
|
|
457
|
+
depth: c.depth,
|
|
458
|
+
leaf: null,
|
|
459
|
+
leafLen: -1,
|
|
460
|
+
};
|
|
461
|
+
return args(sub, 0) === toks.length ? p + 1 : FAIL;
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* a function definition: `<rgb()>`
|
|
466
|
+
* @param name the lowercase function name
|
|
467
|
+
* @param syntax the matcher of the function syntax (`rgb()`), null when it is not defined
|
|
468
|
+
*/
|
|
469
|
+
function fndef(name, syntax) {
|
|
470
|
+
return (c, p) => {
|
|
471
|
+
const token = c.toks[p];
|
|
472
|
+
const type = c.arena.dataTypes[token];
|
|
473
|
+
if (syntax === null || lowercase(c.arena.resolveString(c.arena.stringValue[token])) !== name) {
|
|
474
|
+
return FAIL;
|
|
475
|
+
}
|
|
476
|
+
if (isFunctionDefType(type)) {
|
|
477
|
+
const close = closingParens(c, p);
|
|
478
|
+
return close !== -1 && matchRange(c, syntax, p, close + 1) ? close + 1 : FAIL;
|
|
479
|
+
}
|
|
480
|
+
if (!isFunctionType(type)) {
|
|
481
|
+
return FAIL;
|
|
482
|
+
}
|
|
483
|
+
const sub = {
|
|
484
|
+
arena: c.arena,
|
|
485
|
+
toks: [token],
|
|
486
|
+
len: 1,
|
|
487
|
+
options: c.options,
|
|
488
|
+
depth: c.depth,
|
|
489
|
+
leaf: null,
|
|
490
|
+
leafLen: -1,
|
|
491
|
+
};
|
|
492
|
+
return syntax(sub, 0) === 1 ? p + 1 : FAIL;
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* a literal number or dimension: `0`, `90deg`, see isLiteralNumber()
|
|
497
|
+
* @param value
|
|
498
|
+
* @param unit the token type of a dimension
|
|
499
|
+
* @param unitText the unit of a dimension
|
|
500
|
+
*/
|
|
501
|
+
function num(value, unit, unitText) {
|
|
502
|
+
const unitType = unit === null ? null : EnumToken[unit];
|
|
503
|
+
const lowerUnit = unitText === null ? null : unitText.toLowerCase();
|
|
504
|
+
return (c, p) => {
|
|
505
|
+
const token = c.toks[p];
|
|
506
|
+
if (c.arena.hasFraction(token) || c.arena.numericValue[token] !== value) {
|
|
507
|
+
return FAIL;
|
|
508
|
+
}
|
|
509
|
+
if (unitType === null) {
|
|
510
|
+
return c.arena.dataTypes[token] === EnumToken.NumberTokenType ? p + 1 : FAIL;
|
|
511
|
+
}
|
|
512
|
+
return c.arena.dataTypes[token] === unitType &&
|
|
513
|
+
c.arena.resolveString(c.arena.unit[token]).toLowerCase() === lowerUnit
|
|
514
|
+
? p + 1
|
|
515
|
+
: FAIL;
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* a quoted literal: `'+'`, `'/'`
|
|
520
|
+
* @param value
|
|
521
|
+
*/
|
|
522
|
+
function str(value) {
|
|
523
|
+
return (c, p) => {
|
|
524
|
+
const token = c.toks[p];
|
|
525
|
+
switch (c.arena.dataTypes[token]) {
|
|
526
|
+
case EnumToken.Add:
|
|
527
|
+
return value === "+" ? p + 1 : FAIL;
|
|
528
|
+
case EnumToken.Sub:
|
|
529
|
+
return value === "-" ? p + 1 : FAIL;
|
|
530
|
+
case EnumToken.LiteralTokenType:
|
|
531
|
+
return c.arena.resolveString(c.arena.stringValue[token]) === value ? p + 1 : FAIL;
|
|
532
|
+
case EnumToken.Plus:
|
|
533
|
+
return p + 1;
|
|
534
|
+
}
|
|
535
|
+
return FAIL;
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* a token of a type
|
|
540
|
+
* @param type
|
|
541
|
+
*/
|
|
542
|
+
function tokenType(type) {
|
|
543
|
+
return (c, p) => (c.arena.dataTypes[c.toks[p]] === type ? p + 1 : FAIL);
|
|
544
|
+
}
|
|
545
|
+
const comma = tokenType(EnumToken.CommaTokenType);
|
|
546
|
+
const colon = tokenType(EnumToken.ColonTokenType);
|
|
547
|
+
const openParens = tokenType(EnumToken.StartParensTokenType);
|
|
548
|
+
const closeParens = tokenType(EnumToken.EndParensTokenType);
|
|
549
|
+
/**
|
|
550
|
+
* the `/` separator
|
|
551
|
+
*/
|
|
552
|
+
const slash = (c, p) => {
|
|
553
|
+
const token = c.toks[p];
|
|
554
|
+
return c.arena.dataTypes[token] === EnumToken.LiteralTokenType && c.arena.resolveString(c.arena.stringValue[token]) === "/"
|
|
555
|
+
? p + 1
|
|
556
|
+
: FAIL;
|
|
557
|
+
};
|
|
558
|
+
/**
|
|
559
|
+
* an optional group: skipped when it does not match
|
|
560
|
+
* @param group
|
|
561
|
+
*/
|
|
562
|
+
function optGroup(group) {
|
|
563
|
+
return opt(group);
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* `<bg-layer>#? , <final-bg-layer>`: the items of the list followed by a comma, `[ <bg-layer> , ]*`. the last item is
|
|
567
|
+
* left to the next syntax
|
|
568
|
+
* @param group the item followed by a comma
|
|
569
|
+
*/
|
|
570
|
+
function optList(group) {
|
|
571
|
+
return (c, p) => {
|
|
572
|
+
while (p < c.len) {
|
|
573
|
+
const result = group(c, p);
|
|
574
|
+
if (result <= p) {
|
|
575
|
+
break;
|
|
576
|
+
}
|
|
577
|
+
p = result;
|
|
578
|
+
}
|
|
579
|
+
return p;
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* syntaxes in any order: one or more (`||`) or all the syntaxes that match a token (`&&`), the order that matches the
|
|
584
|
+
* most tokens, see searchUnorderedSyntaxes()
|
|
585
|
+
* @param operands
|
|
586
|
+
* @param nullable nullable[i]: the operand may match no token
|
|
587
|
+
* @param all
|
|
588
|
+
*/
|
|
589
|
+
function unordered(operands, nullable, all) {
|
|
590
|
+
const count = operands.length;
|
|
591
|
+
const allNullable = nullable.every((value) => value === 1);
|
|
592
|
+
return (c, p) => {
|
|
593
|
+
const size = c.len + 1;
|
|
594
|
+
// the position after an operand, by operand and position. -2: not matched yet
|
|
595
|
+
const memo = new Int32Array(count * size).fill(-2);
|
|
596
|
+
const remaining = [];
|
|
597
|
+
for (let i = 0; i < count; i++) {
|
|
598
|
+
remaining.push(i);
|
|
599
|
+
}
|
|
600
|
+
const best = search(c, operands, nullable, all, remaining, p, memo, size);
|
|
601
|
+
if (best >= 0) {
|
|
602
|
+
return best;
|
|
603
|
+
}
|
|
604
|
+
return all && allNullable ? p : FAIL;
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* the position after the operands that match the most tokens, -1 if no operand matches
|
|
609
|
+
*/
|
|
610
|
+
function search(c, operands, nullable, all, remaining, p, memo, size) {
|
|
611
|
+
let best = -1;
|
|
612
|
+
for (let k = 0; k < remaining.length; k++) {
|
|
613
|
+
const operand = remaining[k];
|
|
614
|
+
const key = operand * size + p;
|
|
615
|
+
let next = memo[key];
|
|
616
|
+
if (next === -2) {
|
|
617
|
+
next = p < c.len ? operands[operand](c, p) : FAIL;
|
|
618
|
+
// the operand must match a token
|
|
619
|
+
if (next <= p) {
|
|
620
|
+
next = -1;
|
|
621
|
+
}
|
|
622
|
+
memo[key] = next;
|
|
623
|
+
}
|
|
624
|
+
if (next < 0) {
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
const rest = remaining.slice(0, k).concat(remaining.slice(k + 1));
|
|
628
|
+
let complete = true;
|
|
629
|
+
if (all) {
|
|
630
|
+
for (let i = 0; i < rest.length; i++) {
|
|
631
|
+
if (nullable[rest[i]] !== 1) {
|
|
632
|
+
complete = false;
|
|
633
|
+
break;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
if (next < c.len && rest.length > 0) {
|
|
638
|
+
const deeper = search(c, operands, nullable, all, rest, next, memo, size);
|
|
639
|
+
if (deeper >= 0) {
|
|
640
|
+
if (deeper >= c.len) {
|
|
641
|
+
return deeper;
|
|
642
|
+
}
|
|
643
|
+
if (deeper > best) {
|
|
644
|
+
best = deeper;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
if (complete) {
|
|
649
|
+
if (next >= c.len) {
|
|
650
|
+
return next;
|
|
651
|
+
}
|
|
652
|
+
if (next > best) {
|
|
653
|
+
best = next;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
return best;
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* the compiled matcher of a definition, by `group/name`
|
|
661
|
+
*/
|
|
662
|
+
let definitions = null;
|
|
663
|
+
/**
|
|
664
|
+
* the compiled matcher of a definition, null if the definition is not compiled
|
|
665
|
+
* @param key `group/name`
|
|
666
|
+
*/
|
|
667
|
+
function compiledMatcher(key) {
|
|
668
|
+
if (definitions === null) {
|
|
669
|
+
definitions = new Map();
|
|
670
|
+
for (let i = 0; i < keys.length; i++) {
|
|
671
|
+
definitions.set(keys[i], i);
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
const index = definitions.get(key);
|
|
675
|
+
return index === undefined ? null : definition(index);
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* match the tokens of a value with a compiled matcher
|
|
679
|
+
* @param arena
|
|
680
|
+
* @param matcher
|
|
681
|
+
* @param tokens the value tokens
|
|
682
|
+
* @param options
|
|
683
|
+
* @returns true if the value is valid, false if the interpreter must validate it
|
|
684
|
+
*/
|
|
685
|
+
function matchCompiled(arena, matcher, tokens, options) {
|
|
686
|
+
const toks = significantTokens(arena, tokens, []);
|
|
687
|
+
// a css wide keyword (see matchSyntax())
|
|
688
|
+
if (toks.length === 1 &&
|
|
689
|
+
arena.dataTypes[toks[0]] === EnumToken.IdenTokenType &&
|
|
690
|
+
isCssWideKeyword(arena.resolveString(arena.stringValue[toks[0]]))) {
|
|
691
|
+
return true;
|
|
692
|
+
}
|
|
693
|
+
const c = { arena, toks, len: toks.length, options, depth: 0, leaf: null, leafLen: -1 };
|
|
694
|
+
try {
|
|
695
|
+
return matcher(c, 0) === toks.length;
|
|
696
|
+
}
|
|
697
|
+
catch (error) {
|
|
698
|
+
if (error === BAIL) {
|
|
699
|
+
return false;
|
|
700
|
+
}
|
|
701
|
+
throw error;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
export { BAIL, FAIL, alt, closeParens, colon, comma, compiledMatcher, empty, fn, fndef, interpreted, kw, kws, leaf, list, matchCompiled, num, occ, openParens, opt, optGroup, optList, ref, rep, rng, seq, slash, str, unordered };
|