@tbela99/css-parser 1.4.2 → 1.4.4
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 +354 -4
- package/README.md +69 -1077
- package/dist/config.json.js +76 -3
- package/dist/index-umd-web.js +26838 -18743
- package/dist/index.cjs +26819 -18731
- package/dist/index.d.ts +2471 -1884
- package/dist/lib/ast/clone.js +46 -0
- package/dist/lib/ast/expand.js +90 -65
- package/dist/lib/ast/features/calc.js +86 -43
- package/dist/lib/ast/features/if.js +221 -0
- package/dist/lib/ast/features/index.js +1 -0
- package/dist/lib/ast/features/inlinecssvariables.js +33 -28
- package/dist/lib/ast/features/prefix.js +406 -44
- package/dist/lib/ast/features/shorthand.js +6 -9
- package/dist/lib/ast/features/transform.js +14 -15
- package/dist/lib/ast/find.js +175 -0
- package/dist/lib/ast/math/expression.js +145 -99
- package/dist/lib/ast/math/math.js +30 -41
- package/dist/lib/ast/minify.js +551 -232
- package/dist/lib/ast/transform/compute.js +82 -69
- package/dist/lib/ast/transform/matrix.js +12 -26
- package/dist/lib/ast/transform/minify.js +154 -150
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.js +1 -1
- package/dist/lib/ast/transform/scale.js +1 -1
- package/dist/lib/ast/transform/skew.js +1 -1
- package/dist/lib/ast/transform/translate.js +1 -1
- package/dist/lib/ast/transform/utils.js +3 -2
- package/dist/lib/ast/types.js +345 -30
- package/dist/lib/ast/walk.js +119 -64
- package/dist/lib/fs/resolve.js +94 -88
- package/dist/lib/parser/declaration/list.js +117 -26
- package/dist/lib/parser/declaration/map.js +141 -96
- package/dist/lib/parser/declaration/set.js +30 -25
- package/dist/lib/parser/parse.js +1810 -1739
- package/dist/lib/parser/tokenize.js +662 -403
- package/dist/lib/parser/utils/angle.js +29 -0
- package/dist/lib/parser/utils/at-rule-container.js +486 -0
- package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
- package/dist/lib/parser/utils/at-rule-generic.js +118 -0
- package/dist/lib/parser/utils/at-rule-import.js +393 -0
- package/dist/lib/parser/utils/at-rule-media.js +603 -0
- package/dist/lib/parser/utils/at-rule-support.js +431 -0
- package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
- package/dist/lib/parser/utils/at-rule.js +31 -0
- package/dist/lib/parser/utils/cache.js +19 -0
- package/dist/lib/parser/utils/config.js +1 -0
- package/dist/lib/parser/utils/declaration.js +672 -91
- package/dist/lib/parser/utils/hash.js +1 -1
- package/dist/lib/parser/utils/selector.js +518 -0
- package/dist/lib/parser/utils/text.js +17 -1
- package/dist/lib/parser/utils/token.js +104 -0
- package/dist/lib/parser/utils/type.js +29 -18
- package/dist/lib/renderer/render.js +870 -269
- package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
- package/dist/lib/syntax/color/a98rgb.js +8 -12
- package/dist/lib/syntax/color/alpha.js +64 -0
- package/dist/lib/syntax/color/cmyk.js +23 -21
- package/dist/lib/syntax/color/color-mix.js +311 -237
- package/dist/lib/syntax/color/color.js +98 -92
- package/dist/lib/syntax/color/hex.js +29 -33
- package/dist/lib/syntax/color/hsl.js +7 -14
- package/dist/lib/syntax/color/hwb.js +14 -27
- package/dist/lib/syntax/color/lab.js +23 -24
- package/dist/lib/syntax/color/lch.js +16 -18
- package/dist/lib/syntax/color/oklab.js +21 -40
- package/dist/lib/syntax/color/oklch.js +16 -18
- package/dist/lib/syntax/color/p3.js +0 -8
- package/dist/lib/syntax/color/rec2020.js +9 -13
- package/dist/lib/syntax/color/relativecolor.js +160 -48
- package/dist/lib/syntax/color/rgb.js +14 -18
- package/dist/lib/syntax/color/srgb.js +30 -29
- package/dist/lib/syntax/color/utils/color-type.js +18 -0
- package/dist/lib/syntax/color/utils/colorspace.js +46 -0
- package/dist/lib/syntax/color/utils/components.js +36 -16
- package/dist/lib/syntax/color/utils/distance.js +21 -11
- package/dist/lib/syntax/color/utils/make-color.js +56 -0
- package/dist/lib/syntax/color/xyz.js +10 -22
- package/dist/lib/syntax/color/xyzd50.js +6 -20
- package/dist/lib/syntax/constants.js +464 -0
- package/dist/lib/syntax/syntax.js +825 -592
- package/dist/lib/validation/config.js +72 -33
- package/dist/lib/validation/config.json.js +1185 -85
- package/dist/lib/validation/match.js +3120 -0
- package/dist/lib/validation/parser/parse.js +683 -935
- package/dist/lib/validation/parser/typedef.js +106 -0
- package/dist/lib/validation/utils/list.js +4 -11
- package/dist/lib/validation/utils/whitespace.js +2 -8
- package/dist/node.js +102 -57
- package/dist/web.js +100 -48
- package/package.json +31 -17
- package/dist/lib/syntax/color/utils/constants.js +0 -214
- package/dist/lib/syntax/utils.js +0 -70
- package/dist/lib/validation/at-rules/container.js +0 -342
- package/dist/lib/validation/at-rules/counter-style.js +0 -90
- package/dist/lib/validation/at-rules/custom-media.js +0 -50
- package/dist/lib/validation/at-rules/document.js +0 -89
- package/dist/lib/validation/at-rules/else.js +0 -5
- package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
- package/dist/lib/validation/at-rules/import.js +0 -150
- package/dist/lib/validation/at-rules/keyframes.js +0 -67
- package/dist/lib/validation/at-rules/layer.js +0 -41
- package/dist/lib/validation/at-rules/media.js +0 -255
- package/dist/lib/validation/at-rules/namespace.js +0 -81
- package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
- package/dist/lib/validation/at-rules/page.js +0 -100
- package/dist/lib/validation/at-rules/supports.js +0 -295
- package/dist/lib/validation/at-rules/when.js +0 -185
- package/dist/lib/validation/atrule.js +0 -184
- package/dist/lib/validation/selector.js +0 -36
- package/dist/lib/validation/syntax.js +0 -1073
- package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
- package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
- package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
- package/dist/lib/validation/syntaxes/family-name.js +0 -57
- package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
- package/dist/lib/validation/syntaxes/layer-name.js +0 -57
- package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
- package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
- package/dist/lib/validation/syntaxes/selector-list.js +0 -5
- package/dist/lib/validation/syntaxes/selector.js +0 -5
- package/dist/lib/validation/syntaxes/url.js +0 -40
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
import { EnumToken, EnumAstNodeStatus } from '../../ast/types.js';
|
|
2
|
+
import { renderValue } from '../../renderer/render.js';
|
|
3
|
+
import { definedPropertySettings, tokensfuncDefMap, combinators } from '../../syntax/constants.js';
|
|
4
|
+
import { pseudoElements, isIdent, isHash } from '../../syntax/syntax.js';
|
|
5
|
+
import { getParsedSyntax, getSyntaxRule, getSyntaxConfig } from '../../validation/config.js';
|
|
6
|
+
import { matchAllSyntaxes, createValidationContext, trimArray, matchSelectorSyntax } from '../../validation/match.js';
|
|
7
|
+
import { ValidationSyntaxGroupEnum, ValidationTokenEnum } from '../../validation/parser/typedef.js';
|
|
8
|
+
import { splitTokenList } from '../../validation/utils/list.js';
|
|
9
|
+
import { trimWhiteSpace } from '../parse.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* parse selector
|
|
13
|
+
*/
|
|
14
|
+
function parseSelector(tokens, context, options, errors) {
|
|
15
|
+
if (context?.typ === EnumToken.KeyframesAtRuleNodeType) {
|
|
16
|
+
const result = matchAllSyntaxes(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "keyframe-selectors"), createValidationContext(tokens), options);
|
|
17
|
+
const parts = splitTokenList(tokens);
|
|
18
|
+
for (const part of parts) {
|
|
19
|
+
trimArray(part);
|
|
20
|
+
if (options.minify) {
|
|
21
|
+
const filtered = part.filter((token) => token.typ !== EnumToken.WhitespaceTokenType && token.typ !== EnumToken.CommentTokenType);
|
|
22
|
+
if (filtered.length === 1) {
|
|
23
|
+
if (filtered[0].typ === EnumToken.IdenTokenType &&
|
|
24
|
+
"from" === filtered[0].val.toLowerCase()) {
|
|
25
|
+
filtered[0] = Object.defineProperty({
|
|
26
|
+
typ: EnumToken.PercentageTokenType,
|
|
27
|
+
val: 0,
|
|
28
|
+
}, "loc", {
|
|
29
|
+
...definedPropertySettings,
|
|
30
|
+
value: filtered[0].loc,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
else if (filtered[0].typ === EnumToken.PercentageTokenType &&
|
|
34
|
+
100 === filtered[0].val) {
|
|
35
|
+
filtered[0] = Object.defineProperty({
|
|
36
|
+
typ: EnumToken.IdenTokenType,
|
|
37
|
+
val: "to",
|
|
38
|
+
}, "loc", {
|
|
39
|
+
...definedPropertySettings,
|
|
40
|
+
value: filtered[0].loc,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
part.splice(0, part.length, ...filtered);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
tokens.splice(0, tokens.length, ...parts.reduce((acc, curr) => {
|
|
48
|
+
if (acc.length > 0) {
|
|
49
|
+
acc.push({ typ: EnumToken.CommaTokenType });
|
|
50
|
+
}
|
|
51
|
+
acc.push(...curr);
|
|
52
|
+
return acc;
|
|
53
|
+
}, []));
|
|
54
|
+
return Object.defineProperties({
|
|
55
|
+
typ: EnumToken.KeyFramesRuleNodeType,
|
|
56
|
+
sel: [
|
|
57
|
+
...splitTokenList(trimArray(tokens)).reduce((acc, curr) => {
|
|
58
|
+
acc.add(curr.reduce((acc, curr) => acc + renderValue(curr, { minify: false }), ""));
|
|
59
|
+
return acc;
|
|
60
|
+
}, new Set()),
|
|
61
|
+
].join(),
|
|
62
|
+
chi: [],
|
|
63
|
+
}, {
|
|
64
|
+
state: {
|
|
65
|
+
...definedPropertySettings,
|
|
66
|
+
value: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
|
|
67
|
+
},
|
|
68
|
+
errors: {
|
|
69
|
+
...definedPropertySettings,
|
|
70
|
+
value: result.errors,
|
|
71
|
+
},
|
|
72
|
+
tokens: { ...definedPropertySettings, value: tokens.length === 0 ? null : tokens },
|
|
73
|
+
loc: {
|
|
74
|
+
...definedPropertySettings,
|
|
75
|
+
value: {
|
|
76
|
+
...tokens[0].loc,
|
|
77
|
+
end: { ...(tokens[tokens.length - 1]?.loc?.end ?? tokens[0]?.loc?.end) },
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
const stack = [];
|
|
83
|
+
const uniq = new Map();
|
|
84
|
+
let allowed = true;
|
|
85
|
+
let i = 0;
|
|
86
|
+
let index;
|
|
87
|
+
let parent = context;
|
|
88
|
+
let nested = false;
|
|
89
|
+
let val;
|
|
90
|
+
if (context?.typ !== EnumToken.StyleSheetNodeType && context?.typ !== EnumToken.RuleNodeType) {
|
|
91
|
+
allowed = false;
|
|
92
|
+
if (context?.typ === EnumToken.AtRuleNodeType) {
|
|
93
|
+
const syntaxRule = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + context.nam);
|
|
94
|
+
allowed = syntaxRule?.acceptAnyRule ?? false;
|
|
95
|
+
if (!allowed) {
|
|
96
|
+
const rules = syntaxRule?.getBlockRules?.();
|
|
97
|
+
if (rules != null) {
|
|
98
|
+
for (const rule of rules) {
|
|
99
|
+
if (ValidationTokenEnum.PropertyType === rule.typ) {
|
|
100
|
+
if ("block-contents" === rule.val ||
|
|
101
|
+
"rule-list" === rule.val) {
|
|
102
|
+
allowed = true;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
do {
|
|
112
|
+
if (parent?.typ === EnumToken.AtRuleNodeType && "media" === parent.nam) {
|
|
113
|
+
parent = parent.parent;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
nested = parent?.typ == EnumToken.RuleNodeType;
|
|
117
|
+
parent = parent?.parent;
|
|
118
|
+
} while (!nested && parent != null);
|
|
119
|
+
for (; i < tokens.length; i++) {
|
|
120
|
+
if (tokens[i].typ == EnumToken.ColonTokenType) {
|
|
121
|
+
if (tokens[i + 1]?.typ == EnumToken.IdenTokenType) {
|
|
122
|
+
Object.assign(tokens[i], {
|
|
123
|
+
typ: EnumToken.PseudoElementTokenType,
|
|
124
|
+
val: ":" + tokens[i + 1].val,
|
|
125
|
+
});
|
|
126
|
+
tokens[i].loc.end = tokens[i + 1].loc.end;
|
|
127
|
+
tokens.splice(i + 1, 1);
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (tokens[i + 1]?.typ == EnumToken.FunctionTokenDefType) {
|
|
131
|
+
val = ":" + tokens[i + 1].val;
|
|
132
|
+
Object.assign(tokens[i], {
|
|
133
|
+
typ: val + "()" in getSyntaxConfig().selectors
|
|
134
|
+
? EnumToken.PseudoClassFunctionTokenDefType
|
|
135
|
+
: tokens[i + 1].typ,
|
|
136
|
+
val,
|
|
137
|
+
});
|
|
138
|
+
tokens[i].loc.end = tokens[i + 1].loc.end;
|
|
139
|
+
tokens.splice(i + 1, 1);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (tokens[i].typ == EnumToken.DoubleColonTokenType) {
|
|
144
|
+
val = ":" + tokens[i + 1].val;
|
|
145
|
+
if (tokens[i + 1]?.typ == EnumToken.IdenTokenType) {
|
|
146
|
+
Object.assign(tokens[i], {
|
|
147
|
+
typ: EnumToken.PseudoClassTokenType,
|
|
148
|
+
val: (pseudoElements.includes(val) ? "" : ":") + val,
|
|
149
|
+
});
|
|
150
|
+
tokens[i].loc.end = tokens[i + 1].loc.end;
|
|
151
|
+
tokens.splice(i + 1, 1);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (tokens[i + 1]?.typ == EnumToken.FunctionTokenDefType) {
|
|
155
|
+
val = "::" + tokens[i + 1].val;
|
|
156
|
+
Object.assign(tokens[i], {
|
|
157
|
+
typ: val + "()" in getSyntaxConfig().selectors
|
|
158
|
+
? EnumToken.PseudoClassFunctionTokenDefType
|
|
159
|
+
: EnumToken.FunctionTokenDefType,
|
|
160
|
+
val,
|
|
161
|
+
});
|
|
162
|
+
tokens[i].loc.end = tokens[i + 1].loc.end;
|
|
163
|
+
tokens.splice(i + 1, 1);
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (tokens[i].typ == EnumToken.ColorTokenType) {
|
|
168
|
+
if (isIdent(tokens[i].val)) {
|
|
169
|
+
Object.assign(tokens[i], {
|
|
170
|
+
typ: EnumToken.IdenTokenType,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
else if (isHash(tokens[i].val)) {
|
|
174
|
+
Object.assign(tokens[i], {
|
|
175
|
+
typ: EnumToken.HashTokenType,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
const result = matchSelectorSyntax(tokens, errors, options, nested === true);
|
|
181
|
+
trimArray(tokens);
|
|
182
|
+
if (result.success) {
|
|
183
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
184
|
+
const token = tokens[i];
|
|
185
|
+
switch (token.typ) {
|
|
186
|
+
case EnumToken.AttrStartTokenType:
|
|
187
|
+
stack.push(token);
|
|
188
|
+
break;
|
|
189
|
+
case EnumToken.AttrEndTokenType:
|
|
190
|
+
if (stack.at(-1)?.typ == EnumToken.AttrStartTokenType) {
|
|
191
|
+
index = tokens.indexOf(stack.at(-1));
|
|
192
|
+
// @ts-expect-error
|
|
193
|
+
const { val, ...attr } = stack.at(-1);
|
|
194
|
+
Object.defineProperty(attr, "loc", {
|
|
195
|
+
...definedPropertySettings,
|
|
196
|
+
value: {
|
|
197
|
+
...stack.at(-1).loc,
|
|
198
|
+
end: token.loc.end,
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
tokens.splice(i, 1);
|
|
202
|
+
Object.assign(attr, {
|
|
203
|
+
typ: EnumToken.AttrTokenType,
|
|
204
|
+
chi: tokens.splice(index + 1, i - index - 1),
|
|
205
|
+
});
|
|
206
|
+
tokens[index] = attr;
|
|
207
|
+
i = index;
|
|
208
|
+
stack.pop();
|
|
209
|
+
}
|
|
210
|
+
break;
|
|
211
|
+
case EnumToken.PseudoClassFunctionTokenDefType:
|
|
212
|
+
stack.push(token);
|
|
213
|
+
break;
|
|
214
|
+
case EnumToken.EndParensTokenType:
|
|
215
|
+
if (stack.at(-1)?.typ == EnumToken.PseudoClassFunctionTokenDefType) {
|
|
216
|
+
const func = stack.at(-1);
|
|
217
|
+
index = tokens.indexOf(func);
|
|
218
|
+
stack.at(-1).loc.end = token.loc.end;
|
|
219
|
+
tokens.splice(i, 1);
|
|
220
|
+
if (tokensfuncDefMap.has(func.typ)) {
|
|
221
|
+
// @ts-expect-error
|
|
222
|
+
func.typ = tokensfuncDefMap.get(func.typ);
|
|
223
|
+
func.chi = trimArray(tokens.splice(index + 1, i - index - 1));
|
|
224
|
+
}
|
|
225
|
+
if (result.success && options.minify) {
|
|
226
|
+
// parse an+b
|
|
227
|
+
// an+b produces an ident such as 'n-0', a literal such as '+2n-0' or a list of tokens such as[2n\s?[+-]\s?3]
|
|
228
|
+
if (func.val == ":nth-child" ||
|
|
229
|
+
func.val == ":nth-last-child" ||
|
|
230
|
+
func.val == ":nth-of-type" ||
|
|
231
|
+
func.val == ":nth-last-of-type") {
|
|
232
|
+
const token = func.chi.find((t) => t.typ != EnumToken.WhitespaceTokenType && t.typ != EnumToken.CommentTokenType);
|
|
233
|
+
if (token?.typ == EnumToken.IdenTokenType || token?.typ == EnumToken.LiteralTokenType) {
|
|
234
|
+
if (token.typ == EnumToken.IdenTokenType &&
|
|
235
|
+
(token.val == "odd" || token.val == "even")) {
|
|
236
|
+
if (token.val == "even") {
|
|
237
|
+
Object.assign(token, {
|
|
238
|
+
typ: EnumToken.DimensionTokenType,
|
|
239
|
+
val: 2,
|
|
240
|
+
unit: "n",
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
if (!/\d+$/.test(token.val)) {
|
|
246
|
+
let index = func.chi.indexOf(token);
|
|
247
|
+
let i = index + 1;
|
|
248
|
+
let sign = null;
|
|
249
|
+
let num = null;
|
|
250
|
+
for (; i < func.chi.length; i++) {
|
|
251
|
+
if (func.chi[i].typ == EnumToken.WhitespaceTokenType ||
|
|
252
|
+
func.chi[i].typ == EnumToken.CommentTokenType) {
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
if (func.chi[i].typ == EnumToken.NumberTokenType) {
|
|
256
|
+
num = func.chi[i];
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
sign = func.chi[i];
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (num != null) {
|
|
264
|
+
if (num.val === 0) {
|
|
265
|
+
func.chi.splice(index + 1, i - index);
|
|
266
|
+
if (token.val == "-n") {
|
|
267
|
+
token.val = "n";
|
|
268
|
+
}
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
if (sign == null) {
|
|
272
|
+
func.chi.splice(index + 1, i - index - 1);
|
|
273
|
+
if (Math.sign(num.val) === 1) {
|
|
274
|
+
func.chi.splice(index + 1, 0, {
|
|
275
|
+
typ: EnumToken.LiteralTokenType,
|
|
276
|
+
val: "+",
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
else if (token.val == "-n") {
|
|
282
|
+
token.val = "n";
|
|
283
|
+
}
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
const matches = /^(([+-]?[0-9]*)?n)?([+-]?[0-9]+)?$/.exec(token.val);
|
|
287
|
+
if (matches != null) {
|
|
288
|
+
const a1 = matches[2] === "" ? 1 : matches[2] === "-" ? -1 : +matches[2];
|
|
289
|
+
const b1 = +matches[3];
|
|
290
|
+
if (a1 === 0) {
|
|
291
|
+
if (b1 === 1) {
|
|
292
|
+
let hasSelector = false;
|
|
293
|
+
let i = func.chi.indexOf(token);
|
|
294
|
+
let j = i + 1;
|
|
295
|
+
for (; j < func.chi.length; j++) {
|
|
296
|
+
if (func.chi[j].typ == EnumToken.IdenTokenType &&
|
|
297
|
+
func.chi[j].val == "of") {
|
|
298
|
+
hasSelector = true;
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (hasSelector) {
|
|
303
|
+
Object.assign(token, {
|
|
304
|
+
typ: EnumToken.NumberTokenType,
|
|
305
|
+
val: b1,
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
// :first-child
|
|
310
|
+
tokens[tokens.indexOf(func)] = Object.defineProperties({
|
|
311
|
+
typ: EnumToken.PseudoClassTokenType,
|
|
312
|
+
val: ":first-child",
|
|
313
|
+
}, {
|
|
314
|
+
loc: {
|
|
315
|
+
...definedPropertySettings,
|
|
316
|
+
value: func.loc,
|
|
317
|
+
},
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
break;
|
|
321
|
+
}
|
|
322
|
+
else {
|
|
323
|
+
Object.assign(token, { typ: EnumToken.NumberTokenType, val: b1 });
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
else if (b1 === 0) {
|
|
327
|
+
Object.assign(token, Math.abs(a1) === 1
|
|
328
|
+
? {
|
|
329
|
+
typ: EnumToken.IdenTokenType,
|
|
330
|
+
val: "n",
|
|
331
|
+
}
|
|
332
|
+
: {
|
|
333
|
+
typ: EnumToken.DimensionTokenType,
|
|
334
|
+
val: a1 * Math.sign(a1),
|
|
335
|
+
unit: "n",
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
else if (Math.abs(a1) === 2) {
|
|
339
|
+
if (b1 === 0) {
|
|
340
|
+
Object.assign(token, {
|
|
341
|
+
typ: EnumToken.DimensionTokenType,
|
|
342
|
+
val: a1,
|
|
343
|
+
unit: "n",
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
else if (Math.abs(b1) === 1) {
|
|
347
|
+
Object.assign(token, { typ: EnumToken.IdenTokenType, val: "odd" });
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
else if (token?.typ === EnumToken.DimensionTokenType) {
|
|
354
|
+
const index = func.chi.indexOf(token);
|
|
355
|
+
let sign = null;
|
|
356
|
+
let num = null;
|
|
357
|
+
let i = index + 1;
|
|
358
|
+
for (; i < func.chi.length; i++) {
|
|
359
|
+
if (func.chi[i].typ == EnumToken.WhitespaceTokenType ||
|
|
360
|
+
func.chi[i].typ == EnumToken.CommentTokenType) {
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
if (func.chi[i].typ == EnumToken.NumberTokenType) {
|
|
364
|
+
num = func.chi[i];
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
sign = func.chi[i];
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
if (num != null) {
|
|
372
|
+
if (token.val === 0) {
|
|
373
|
+
if (num.val === 0) {
|
|
374
|
+
func.chi.splice(0, i);
|
|
375
|
+
}
|
|
376
|
+
else if (num.val === 1) {
|
|
377
|
+
let hasSelector = false;
|
|
378
|
+
let j = i + 1;
|
|
379
|
+
for (; j < func.chi.length; j++) {
|
|
380
|
+
if (func.chi[j].typ == EnumToken.IdenTokenType &&
|
|
381
|
+
func.chi[j].val == "of") {
|
|
382
|
+
hasSelector = true;
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
if (hasSelector) {
|
|
387
|
+
func.chi.splice(0, i);
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
tokens[tokens.indexOf(func)] = Object.defineProperties({
|
|
391
|
+
typ: EnumToken.PseudoClassTokenType,
|
|
392
|
+
val: ":first-child",
|
|
393
|
+
}, {
|
|
394
|
+
loc: {
|
|
395
|
+
...definedPropertySettings,
|
|
396
|
+
value: func.loc,
|
|
397
|
+
},
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
else {
|
|
403
|
+
func.chi.splice(0, i);
|
|
404
|
+
}
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
else if (num.val === 0) {
|
|
408
|
+
func.chi.splice(index + 1, i - index);
|
|
409
|
+
if (token.val < 0) {
|
|
410
|
+
token.val *= -1;
|
|
411
|
+
}
|
|
412
|
+
break;
|
|
413
|
+
}
|
|
414
|
+
else if (token.val === 2 &&
|
|
415
|
+
Math.abs(num.val) === 1) {
|
|
416
|
+
Object.assign(token, {
|
|
417
|
+
typ: EnumToken.IdenTokenType,
|
|
418
|
+
val: "odd",
|
|
419
|
+
});
|
|
420
|
+
func.chi.splice(index + 1, i - index);
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
if (token.val === 1) {
|
|
424
|
+
Object.assign(token, {
|
|
425
|
+
typ: EnumToken.IdenTokenType,
|
|
426
|
+
val: Math.sign(token.val) === 1
|
|
427
|
+
? "n"
|
|
428
|
+
: "-n",
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
if (sign == null) {
|
|
432
|
+
func.chi.splice(index + 1, i - index - 1);
|
|
433
|
+
if (Math.sign(num.val) === 1) {
|
|
434
|
+
func.chi.splice(index + 1, 0, {
|
|
435
|
+
typ: EnumToken.LiteralTokenType,
|
|
436
|
+
val: "+",
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
i = index;
|
|
445
|
+
stack.pop();
|
|
446
|
+
}
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
return Object.defineProperties({
|
|
452
|
+
typ: EnumToken.RuleNodeType,
|
|
453
|
+
sel: [
|
|
454
|
+
...tokens
|
|
455
|
+
.reduce((acc, curr, index, array) => {
|
|
456
|
+
if (curr.typ == EnumToken.CommentTokenType) {
|
|
457
|
+
return acc;
|
|
458
|
+
}
|
|
459
|
+
if (curr.typ == EnumToken.WhitespaceTokenType) {
|
|
460
|
+
if (trimWhiteSpace.includes(array[index - 1]?.typ) ||
|
|
461
|
+
trimWhiteSpace.includes(array[index + 1]?.typ) ||
|
|
462
|
+
combinators.includes(array[index - 1]?.val) ||
|
|
463
|
+
combinators.includes(array[index + 1]?.val)) {
|
|
464
|
+
return acc;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
let t = renderValue(curr, { minify: false });
|
|
468
|
+
if (t == ",") {
|
|
469
|
+
acc.push([]);
|
|
470
|
+
}
|
|
471
|
+
else {
|
|
472
|
+
acc[acc.length - 1].push(t);
|
|
473
|
+
}
|
|
474
|
+
return acc;
|
|
475
|
+
}, [[]])
|
|
476
|
+
.reduce((acc, curr) => {
|
|
477
|
+
let i = 0;
|
|
478
|
+
for (; i < curr.length; i++) {
|
|
479
|
+
if (i + 1 < curr.length && curr[i] == "*") {
|
|
480
|
+
if (curr[i] == "*") {
|
|
481
|
+
let index = curr[i + 1] == " " ? 2 : 1;
|
|
482
|
+
if (![">", "~", "+"].includes(curr[index])) {
|
|
483
|
+
curr.splice(i, index);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
acc.set(curr.join(""), curr);
|
|
489
|
+
return acc;
|
|
490
|
+
}, uniq)
|
|
491
|
+
.keys(),
|
|
492
|
+
].join(","),
|
|
493
|
+
chi: [],
|
|
494
|
+
}, {
|
|
495
|
+
state: {
|
|
496
|
+
...definedPropertySettings,
|
|
497
|
+
value: result.success && allowed
|
|
498
|
+
? EnumAstNodeStatus.Validated
|
|
499
|
+
: allowed
|
|
500
|
+
? EnumAstNodeStatus.Invalid
|
|
501
|
+
: EnumAstNodeStatus.Disallowed,
|
|
502
|
+
},
|
|
503
|
+
errors: {
|
|
504
|
+
...definedPropertySettings,
|
|
505
|
+
value: result.success ? [] : result.errors,
|
|
506
|
+
},
|
|
507
|
+
tokens: { ...definedPropertySettings, value: tokens },
|
|
508
|
+
loc: {
|
|
509
|
+
...definedPropertySettings,
|
|
510
|
+
value: {
|
|
511
|
+
...tokens[0].loc,
|
|
512
|
+
end: tokens[tokens.length - 1].loc.end,
|
|
513
|
+
},
|
|
514
|
+
},
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
export { parseSelector };
|
|
@@ -4,5 +4,21 @@ function dasherize(value) {
|
|
|
4
4
|
function camelize(value) {
|
|
5
5
|
return value.replace(/-([a-z])/g, (all, one) => one.toUpperCase());
|
|
6
6
|
}
|
|
7
|
+
function equalsIgnoreCase(a, b) {
|
|
8
|
+
if (a.length !== b.length)
|
|
9
|
+
return false;
|
|
10
|
+
for (let i = 0; i < a.length; i++) {
|
|
11
|
+
let ca = a.charCodeAt(i);
|
|
12
|
+
let cb = b.charCodeAt(i);
|
|
13
|
+
// Normalize A-Z to a-z
|
|
14
|
+
if (ca >= 65 && ca <= 90)
|
|
15
|
+
ca += 32;
|
|
16
|
+
if (cb >= 65 && cb <= 90)
|
|
17
|
+
cb += 32;
|
|
18
|
+
if (ca !== cb)
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
7
23
|
|
|
8
|
-
export { camelize, dasherize };
|
|
24
|
+
export { camelize, dasherize, equalsIgnoreCase };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { EnumToken } from '../../ast/types.js';
|
|
2
|
+
import { definedPropertySettings, tokensfuncSet } from '../../syntax/constants.js';
|
|
3
|
+
|
|
4
|
+
const trimTokenSpace = new Set([
|
|
5
|
+
EnumToken.CommaTokenType,
|
|
6
|
+
EnumToken.DelimTokenType,
|
|
7
|
+
EnumToken.GtTokenType,
|
|
8
|
+
EnumToken.GteTokenType,
|
|
9
|
+
EnumToken.LtTokenType,
|
|
10
|
+
EnumToken.LteTokenType,
|
|
11
|
+
EnumToken.ChildCombinatorTokenType,
|
|
12
|
+
]);
|
|
13
|
+
const trimSpaceAfter = new Set([
|
|
14
|
+
...tokensfuncSet.values(),
|
|
15
|
+
EnumToken.ChildCombinatorTokenType,
|
|
16
|
+
EnumToken.SupportsQueryConditionTokenType,
|
|
17
|
+
EnumToken.SupportsQueryUnaryConditionTokenType,
|
|
18
|
+
]);
|
|
19
|
+
/**
|
|
20
|
+
* Replace token in its parent node
|
|
21
|
+
* @param parent
|
|
22
|
+
* @param node
|
|
23
|
+
* @param replacement
|
|
24
|
+
* @throws TypeError replacement is null
|
|
25
|
+
* @throws ReferenceError node not found in parent
|
|
26
|
+
*/
|
|
27
|
+
function replaceNodeOrValue(parent, node, replacement) {
|
|
28
|
+
if (replacement == null || (Array.isArray(replacement) && replacement.length === 0)) {
|
|
29
|
+
throw new TypeError(`replacement is null`);
|
|
30
|
+
}
|
|
31
|
+
for (const node of Array.isArray(replacement) ? replacement : [replacement]) {
|
|
32
|
+
if ("parent" in node && node.parent != node.parent) {
|
|
33
|
+
Object.defineProperty(node, "parent", {
|
|
34
|
+
...definedPropertySettings,
|
|
35
|
+
value: node.parent,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (parent.typ == EnumToken.BinaryExpressionTokenType) {
|
|
40
|
+
if (parent.l == node) {
|
|
41
|
+
parent.l = replacement;
|
|
42
|
+
}
|
|
43
|
+
else if (parent.r == node) {
|
|
44
|
+
parent.r = replacement;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
throw new ReferenceError("Node not found");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
const target = "val" in parent && Array.isArray(parent.val)
|
|
52
|
+
? parent.val
|
|
53
|
+
: (parent.chi ?? parent);
|
|
54
|
+
if (Array.isArray(target)) {
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
const index = target.indexOf(node);
|
|
57
|
+
if (index == -1) {
|
|
58
|
+
throw new ReferenceError("Node not found");
|
|
59
|
+
}
|
|
60
|
+
target.splice(index, 1, ...(Array.isArray(replacement) ? replacement : [replacement]));
|
|
61
|
+
}
|
|
62
|
+
else if ("l" in target && target.l == node) {
|
|
63
|
+
target.l = replacement;
|
|
64
|
+
}
|
|
65
|
+
else if ("r" in target && target.r == node) {
|
|
66
|
+
target.r = replacement;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
throw new ReferenceError("Node not found");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
function trimWhiteSpaceTokens(tokens) {
|
|
75
|
+
let i = 0;
|
|
76
|
+
if (tokens[0]?.typ === EnumToken.WhitespaceTokenType) {
|
|
77
|
+
tokens.shift();
|
|
78
|
+
}
|
|
79
|
+
if (tokens[tokens.length - 1]?.typ === EnumToken.WhitespaceTokenType) {
|
|
80
|
+
tokens.pop();
|
|
81
|
+
}
|
|
82
|
+
if (tokens.length === 1) {
|
|
83
|
+
if (tokens[0].typ === EnumToken.WhitespaceTokenType) {
|
|
84
|
+
tokens.length = 0;
|
|
85
|
+
}
|
|
86
|
+
return tokens;
|
|
87
|
+
}
|
|
88
|
+
for (; i < tokens.length; i++) {
|
|
89
|
+
if (trimSpaceAfter.has(tokens[i].typ) && tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
|
|
90
|
+
tokens.splice(i + 1, 1);
|
|
91
|
+
}
|
|
92
|
+
else if (trimTokenSpace.has(tokens[i].typ)) {
|
|
93
|
+
if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
|
|
94
|
+
tokens.splice(i + 1, 1);
|
|
95
|
+
}
|
|
96
|
+
if (i > 0 && tokens[i - 1].typ === EnumToken.Whitespace) {
|
|
97
|
+
tokens.splice(--i, 1);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return tokens;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export { replaceNodeOrValue, trimWhiteSpaceTokens };
|