@tbela99/css-parser 0.5.4 → 0.7.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/README.md +108 -5
- package/dist/config.json.js +3 -1
- package/dist/index-umd-web.js +53911 -50
- package/dist/index.cjs +53911 -50
- package/dist/index.d.ts +118 -23
- package/dist/lib/ast/expand.js +8 -2
- package/dist/lib/ast/features/index.js +1 -0
- package/dist/lib/ast/features/prefix.js +118 -0
- package/dist/lib/ast/features/shorthand.js +1 -0
- package/dist/lib/ast/minify.js +44 -10
- package/dist/lib/ast/types.js +16 -1
- package/dist/lib/ast/walk.js +3 -1
- package/dist/lib/parser/declaration/list.js +1 -1
- package/dist/lib/parser/declaration/map.js +9 -4
- package/dist/lib/parser/declaration/set.js +2 -1
- package/dist/lib/parser/parse.js +262 -16
- package/dist/lib/parser/tokenize.js +19 -5
- package/dist/lib/parser/utils/config.js +1 -0
- package/dist/lib/parser/utils/declaration.js +2 -1
- package/dist/lib/parser/utils/syntax.js +10 -0
- package/dist/lib/parser/utils/type.js +1 -0
- package/dist/lib/renderer/color/a98rgb.js +1 -0
- package/dist/lib/renderer/color/color.js +1 -0
- package/dist/lib/renderer/color/colormix.js +3 -2
- package/dist/lib/renderer/color/hex.js +1 -0
- package/dist/lib/renderer/color/hsl.js +1 -0
- package/dist/lib/renderer/color/hwb.js +1 -0
- package/dist/lib/renderer/color/lab.js +1 -0
- package/dist/lib/renderer/color/lch.js +1 -0
- package/dist/lib/renderer/color/oklab.js +1 -0
- package/dist/lib/renderer/color/oklch.js +1 -0
- package/dist/lib/renderer/color/p3.js +1 -0
- package/dist/lib/renderer/color/rec2020.js +1 -0
- package/dist/lib/renderer/color/relativecolor.js +1 -0
- package/dist/lib/renderer/color/rgb.js +1 -0
- package/dist/lib/renderer/color/srgb.js +1 -0
- package/dist/lib/renderer/color/utils/components.js +1 -0
- package/dist/lib/renderer/color/utils/constants.js +6 -1
- package/dist/lib/renderer/color/xyz.js +1 -0
- package/dist/lib/renderer/color/xyzd50.js +1 -0
- package/dist/lib/renderer/render.js +30 -7
- package/dist/lib/syntax/syntax.js +456 -0
- package/dist/lib/validation/config.js +9 -0
- package/dist/lib/validation/config.json.js +52883 -0
- package/dist/lib/validation/parser/parse.js +16 -0
- package/dist/lib/validation/parser/types.js +39 -0
- package/dist/lib/validation/selector.js +459 -0
- package/dist/node/index.js +2 -0
- package/dist/web/index.js +1 -0
- package/dist/web/load.js +1 -0
- package/package.json +4 -3
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ValidationTokenEnum } from './types.js';
|
|
2
|
+
import '../../ast/types.js';
|
|
3
|
+
import '../../ast/minify.js';
|
|
4
|
+
import '../../parser/parse.js';
|
|
5
|
+
import '../../parser/utils/config.js';
|
|
6
|
+
import '../../renderer/color/utils/constants.js';
|
|
7
|
+
import '../../renderer/sourcemap/lib/encode.js';
|
|
8
|
+
|
|
9
|
+
[
|
|
10
|
+
ValidationTokenEnum.Star,
|
|
11
|
+
ValidationTokenEnum.HashMark,
|
|
12
|
+
ValidationTokenEnum.AtLeastOnce,
|
|
13
|
+
ValidationTokenEnum.Exclamation,
|
|
14
|
+
ValidationTokenEnum.QuestionMark,
|
|
15
|
+
ValidationTokenEnum.OpenCurlyBrace
|
|
16
|
+
];
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
var ValidationTokenEnum;
|
|
2
|
+
(function (ValidationTokenEnum) {
|
|
3
|
+
ValidationTokenEnum[ValidationTokenEnum["Root"] = 0] = "Root";
|
|
4
|
+
ValidationTokenEnum[ValidationTokenEnum["Keyword"] = 1] = "Keyword";
|
|
5
|
+
ValidationTokenEnum[ValidationTokenEnum["PropertyType"] = 2] = "PropertyType";
|
|
6
|
+
ValidationTokenEnum[ValidationTokenEnum["DeclarationType"] = 3] = "DeclarationType";
|
|
7
|
+
ValidationTokenEnum[ValidationTokenEnum["AtRule"] = 4] = "AtRule";
|
|
8
|
+
ValidationTokenEnum[ValidationTokenEnum["ValidationFunctionDefinition"] = 5] = "ValidationFunctionDefinition";
|
|
9
|
+
ValidationTokenEnum[ValidationTokenEnum["OpenBracket"] = 6] = "OpenBracket";
|
|
10
|
+
ValidationTokenEnum[ValidationTokenEnum["CloseBracket"] = 7] = "CloseBracket";
|
|
11
|
+
ValidationTokenEnum[ValidationTokenEnum["OpenParenthesis"] = 8] = "OpenParenthesis";
|
|
12
|
+
ValidationTokenEnum[ValidationTokenEnum["CloseParenthesis"] = 9] = "CloseParenthesis";
|
|
13
|
+
ValidationTokenEnum[ValidationTokenEnum["Comma"] = 10] = "Comma";
|
|
14
|
+
ValidationTokenEnum[ValidationTokenEnum["Pipe"] = 11] = "Pipe";
|
|
15
|
+
ValidationTokenEnum[ValidationTokenEnum["Column"] = 12] = "Column";
|
|
16
|
+
ValidationTokenEnum[ValidationTokenEnum["Star"] = 13] = "Star";
|
|
17
|
+
ValidationTokenEnum[ValidationTokenEnum["OpenCurlyBrace"] = 14] = "OpenCurlyBrace";
|
|
18
|
+
ValidationTokenEnum[ValidationTokenEnum["CloseCurlyBrace"] = 15] = "CloseCurlyBrace";
|
|
19
|
+
ValidationTokenEnum[ValidationTokenEnum["HashMark"] = 16] = "HashMark";
|
|
20
|
+
ValidationTokenEnum[ValidationTokenEnum["QuestionMark"] = 17] = "QuestionMark";
|
|
21
|
+
ValidationTokenEnum[ValidationTokenEnum["Function"] = 18] = "Function";
|
|
22
|
+
ValidationTokenEnum[ValidationTokenEnum["Number"] = 19] = "Number";
|
|
23
|
+
ValidationTokenEnum[ValidationTokenEnum["Whitespace"] = 20] = "Whitespace";
|
|
24
|
+
ValidationTokenEnum[ValidationTokenEnum["Parenthesis"] = 21] = "Parenthesis";
|
|
25
|
+
ValidationTokenEnum[ValidationTokenEnum["Bracket"] = 22] = "Bracket";
|
|
26
|
+
ValidationTokenEnum[ValidationTokenEnum["AtLeastOnce"] = 23] = "AtLeastOnce";
|
|
27
|
+
ValidationTokenEnum[ValidationTokenEnum["Separator"] = 24] = "Separator";
|
|
28
|
+
ValidationTokenEnum[ValidationTokenEnum["Exclamation"] = 25] = "Exclamation";
|
|
29
|
+
ValidationTokenEnum[ValidationTokenEnum["Ampersand"] = 26] = "Ampersand";
|
|
30
|
+
ValidationTokenEnum[ValidationTokenEnum["PipeToken"] = 27] = "PipeToken";
|
|
31
|
+
ValidationTokenEnum[ValidationTokenEnum["ColumnToken"] = 28] = "ColumnToken";
|
|
32
|
+
ValidationTokenEnum[ValidationTokenEnum["AmpersandToken"] = 29] = "AmpersandToken";
|
|
33
|
+
ValidationTokenEnum[ValidationTokenEnum["Parens"] = 30] = "Parens";
|
|
34
|
+
ValidationTokenEnum[ValidationTokenEnum["PseudoClassToken"] = 31] = "PseudoClassToken";
|
|
35
|
+
ValidationTokenEnum[ValidationTokenEnum["PseudoClassFunctionToken"] = 32] = "PseudoClassFunctionToken";
|
|
36
|
+
ValidationTokenEnum[ValidationTokenEnum["StringToken"] = 33] = "StringToken";
|
|
37
|
+
})(ValidationTokenEnum || (ValidationTokenEnum = {}));
|
|
38
|
+
|
|
39
|
+
export { ValidationTokenEnum };
|
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
import { EnumToken, ValidationLevel } from '../ast/types.js';
|
|
2
|
+
import '../ast/minify.js';
|
|
3
|
+
import '../parser/parse.js';
|
|
4
|
+
import '../renderer/color/utils/constants.js';
|
|
5
|
+
import '../renderer/sourcemap/lib/encode.js';
|
|
6
|
+
import '../parser/utils/config.js';
|
|
7
|
+
import { getConfig } from './config.js';
|
|
8
|
+
|
|
9
|
+
const expressions = [
|
|
10
|
+
EnumToken.DelimTokenType, EnumToken.IncludeMatchTokenType, EnumToken.DashMatchTokenType,
|
|
11
|
+
EnumToken.StartMatchTokenType, EnumToken.EndMatchTokenType, EnumToken.ContainMatchTokenType
|
|
12
|
+
];
|
|
13
|
+
const selectorTokens = [
|
|
14
|
+
EnumToken.IdenTokenType, EnumToken.ClassSelectorTokenType, EnumToken.AttrTokenType,
|
|
15
|
+
EnumToken.PseudoClassTokenType, EnumToken.PseudoClassFuncTokenType, EnumToken.HashTokenType,
|
|
16
|
+
EnumToken.UniversalSelectorTokenType
|
|
17
|
+
];
|
|
18
|
+
const combinatorTokens = [
|
|
19
|
+
EnumToken.ChildCombinatorTokenType, EnumToken.NextSiblingCombinatorTokenType,
|
|
20
|
+
EnumToken.SubsequentSiblingCombinatorTokenType
|
|
21
|
+
];
|
|
22
|
+
function validateSelector(selector, options, root) {
|
|
23
|
+
if (selector.length == 0) {
|
|
24
|
+
return {
|
|
25
|
+
valid: ValidationLevel.Drop,
|
|
26
|
+
node: null,
|
|
27
|
+
error: 'The selector is empty.'
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
return doValidateSelector(selector, options, root);
|
|
31
|
+
}
|
|
32
|
+
function doValidateSelector(selector, options, root) {
|
|
33
|
+
let result;
|
|
34
|
+
if (combinatorTokens.includes(selector[0].typ)) {
|
|
35
|
+
if (root == null || root.typ == EnumToken.StyleSheetNodeType) {
|
|
36
|
+
return {
|
|
37
|
+
valid: ValidationLevel.Drop,
|
|
38
|
+
node: null,
|
|
39
|
+
error: 'The selector cannot start with a combinator.'
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
selector = selector.slice(1);
|
|
43
|
+
}
|
|
44
|
+
while ((selector?.length ?? 0) > 0) {
|
|
45
|
+
result = validateSimpleSelector(selector, options, root);
|
|
46
|
+
if (result.valid == ValidationLevel.Drop) {
|
|
47
|
+
return {
|
|
48
|
+
valid: ValidationLevel.Drop,
|
|
49
|
+
node: result.nodes[0] ?? null,
|
|
50
|
+
error: result.error
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (result.nodes.length == 0) {
|
|
54
|
+
return {
|
|
55
|
+
valid: result.valid,
|
|
56
|
+
node: result.nodes[0] ?? null,
|
|
57
|
+
error: result.error
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
selector = result.nodes;
|
|
61
|
+
result = validateCombinator(selector);
|
|
62
|
+
if (result.valid == ValidationLevel.Drop) {
|
|
63
|
+
while (result.valid == ValidationLevel.Drop && selector.length > 0) {
|
|
64
|
+
if (selector[0].typ == EnumToken.CommaTokenType) {
|
|
65
|
+
selector = consumeWhitespace(selector.slice(1));
|
|
66
|
+
if (selector.length == 0) {
|
|
67
|
+
return {
|
|
68
|
+
valid: ValidationLevel.Drop,
|
|
69
|
+
node: selector[0],
|
|
70
|
+
error: 'unexpected token'
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
if (selector.length > 0 && combinatorTokens.includes(selector[0].typ)) {
|
|
74
|
+
if (root == null || root.typ == EnumToken.StyleSheetNodeType) {
|
|
75
|
+
return {
|
|
76
|
+
valid: ValidationLevel.Drop,
|
|
77
|
+
node: selector[0],
|
|
78
|
+
error: 'Unexpected token'
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
selector = selector.slice(1);
|
|
82
|
+
}
|
|
83
|
+
result = validateSimpleSelector(selector, options, root);
|
|
84
|
+
if (result.valid == ValidationLevel.Drop) {
|
|
85
|
+
return {
|
|
86
|
+
valid: ValidationLevel.Drop,
|
|
87
|
+
node: null,
|
|
88
|
+
error: 'Invalid selector'
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
if (result.nodes.length == 0) {
|
|
92
|
+
return {
|
|
93
|
+
valid: result.valid,
|
|
94
|
+
node: null,
|
|
95
|
+
error: result.error
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
selector = result.nodes;
|
|
99
|
+
result = validateCombinator(selector);
|
|
100
|
+
if (result.nodes.length == 0) {
|
|
101
|
+
return {
|
|
102
|
+
valid: result.valid,
|
|
103
|
+
node: null,
|
|
104
|
+
error: result.error
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
selector = result.nodes;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
selector = result.nodes;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
valid: ValidationLevel.Valid,
|
|
120
|
+
node: null,
|
|
121
|
+
error: ''
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function consumeWhitespace(selector) {
|
|
125
|
+
let i = 0;
|
|
126
|
+
while (i < selector.length &&
|
|
127
|
+
(selector[i].typ == EnumToken.WhitespaceTokenType ||
|
|
128
|
+
selector[i].typ == EnumToken.CommentTokenType)) {
|
|
129
|
+
i++;
|
|
130
|
+
}
|
|
131
|
+
return selector.slice(i);
|
|
132
|
+
}
|
|
133
|
+
function validateSimpleSelector(selector, options, root) {
|
|
134
|
+
let i = 0;
|
|
135
|
+
if (i >= selector.length) {
|
|
136
|
+
return {
|
|
137
|
+
valid: ValidationLevel.Valid,
|
|
138
|
+
nodes: selector.slice(i),
|
|
139
|
+
error: 'empty selector'
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
if (root?.typ == EnumToken.AtRuleNodeType && null != root.nam.match(/(-[a-zA-Z]+-)?keyframes/)) {
|
|
143
|
+
while (i < selector.length) {
|
|
144
|
+
if ([EnumToken.PercentageTokenType, EnumToken.CommentTokenType].includes(selector[i].typ)) {
|
|
145
|
+
i++;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if ((selector[i].typ == EnumToken.IdenTokenType && ['from', 'to'].includes(selector[i].val))) {
|
|
149
|
+
i++;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (selector[i].typ == EnumToken.CommaTokenType) {
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
valid: ValidationLevel.Drop,
|
|
157
|
+
nodes: selector.slice(i),
|
|
158
|
+
error: 'invalid selector'
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
valid: ValidationLevel.Valid,
|
|
163
|
+
nodes: selector.slice(i),
|
|
164
|
+
error: 'empty selector'
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
while (i < selector.length && [EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(selector[i].typ)) {
|
|
168
|
+
i++;
|
|
169
|
+
}
|
|
170
|
+
if (i >= selector.length) {
|
|
171
|
+
return {
|
|
172
|
+
valid: ValidationLevel.Valid,
|
|
173
|
+
nodes: selector.slice(i),
|
|
174
|
+
error: 'empty selector'
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
if (selectorTokens.includes(selector[i].typ)) {
|
|
178
|
+
let result;
|
|
179
|
+
if (selector[i].typ == EnumToken.PseudoClassTokenType) {
|
|
180
|
+
result = validatePseudoClass(selector[i], options);
|
|
181
|
+
if (result.valid != ValidationLevel.Valid) {
|
|
182
|
+
return result;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (selector[i].typ == EnumToken.PseudoClassFuncTokenType) {
|
|
186
|
+
result = validatePseudoClassFunction(selector[i], options);
|
|
187
|
+
if (result.valid != ValidationLevel.Valid) {
|
|
188
|
+
return result;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (selector[i].typ == EnumToken.AttrTokenType) {
|
|
192
|
+
result = validateAttributeSelector(selector[i], options);
|
|
193
|
+
if (result.valid != ValidationLevel.Valid) {
|
|
194
|
+
return result;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
valid: ValidationLevel.Valid,
|
|
199
|
+
nodes: selector.slice(i + 1),
|
|
200
|
+
error: ''
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
if (selector[i].typ == EnumToken.LiteralTokenType && selector[i].val.startsWith('\\')) {
|
|
204
|
+
return {
|
|
205
|
+
valid: ValidationLevel.Valid,
|
|
206
|
+
nodes: selector.slice(i + 1),
|
|
207
|
+
error: ''
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
if (selector[i].typ == EnumToken.NestingSelectorTokenType && root != null && root.typ != EnumToken.StyleSheetNodeType) {
|
|
211
|
+
return {
|
|
212
|
+
valid: ValidationLevel.Valid,
|
|
213
|
+
nodes: selector.slice(i + 1),
|
|
214
|
+
error: 'Compound selector not allowed here'
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
valid: ValidationLevel.Drop,
|
|
219
|
+
nodes: selector.slice(i + 1, 1),
|
|
220
|
+
error: 'unexpected token'
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
function validatePseudoClass(selector, options) {
|
|
224
|
+
const name = selector.val.slice(selector.val[1] == ':' ? 2 : 1);
|
|
225
|
+
if (name.match(/^-[a-zA-Z]+-/) || selector.val == ':before' || selector.val == ':after') {
|
|
226
|
+
return {
|
|
227
|
+
valid: ValidationLevel.Valid,
|
|
228
|
+
nodes: [selector],
|
|
229
|
+
error: ''
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
const config = getConfig();
|
|
233
|
+
const isValid = selector.val in config.selectors;
|
|
234
|
+
return {
|
|
235
|
+
valid: isValid || !options.validation ? ValidationLevel.Valid : ValidationLevel.Drop,
|
|
236
|
+
nodes: [selector],
|
|
237
|
+
error: isValid ? '' : 'invalid selector'
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function validatePseudoClassFunction(selector, options) {
|
|
241
|
+
const name = selector.val.slice(1);
|
|
242
|
+
if (name.match(/^-[a-zA-Z]+-/)) {
|
|
243
|
+
const isValid = name == '-webkit-any' || name == '-moz-any';
|
|
244
|
+
return {
|
|
245
|
+
valid: isValid ? ValidationLevel.Valid : ValidationLevel.Drop,
|
|
246
|
+
nodes: [selector],
|
|
247
|
+
error: isValid ? '' : 'invalid pseudo class'
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
const config = getConfig();
|
|
251
|
+
if (selector.val in config.selectors) {
|
|
252
|
+
if (!('chi' in config.selectors[selector.val].ast[0])) {
|
|
253
|
+
return {
|
|
254
|
+
valid: !options.validation ? ValidationLevel.Valid : ValidationLevel.Drop,
|
|
255
|
+
nodes: [selector],
|
|
256
|
+
error: ''
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
else if (!(selector.val + '()' in config.selectors)) {
|
|
261
|
+
return {
|
|
262
|
+
valid: !options.validation ? ValidationLevel.Valid : ValidationLevel.Drop,
|
|
263
|
+
nodes: [selector],
|
|
264
|
+
error: ''
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
// TODO: validate params
|
|
268
|
+
return {
|
|
269
|
+
valid: ValidationLevel.Valid,
|
|
270
|
+
nodes: [selector],
|
|
271
|
+
error: ''
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
function validateAttributeSelector(selector, options) {
|
|
275
|
+
let i = 0;
|
|
276
|
+
let isMatchExpression = false;
|
|
277
|
+
let expression;
|
|
278
|
+
while (i < selector.chi.length) {
|
|
279
|
+
if ([EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(selector.chi[i].typ)) {
|
|
280
|
+
i++;
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
if (EnumToken.IdenTokenType == selector.chi[i].typ ||
|
|
284
|
+
EnumToken.MatchExpressionTokenType == selector.chi[i].typ ||
|
|
285
|
+
EnumToken.NameSpaceAttributeTokenType == selector.chi[i].typ) {
|
|
286
|
+
isMatchExpression = EnumToken.MatchExpressionTokenType == selector.chi[i].typ;
|
|
287
|
+
if (isMatchExpression) {
|
|
288
|
+
expression = selector.chi[i];
|
|
289
|
+
}
|
|
290
|
+
if (EnumToken.NameSpaceAttributeTokenType == selector.chi[i].typ) {
|
|
291
|
+
if (selector.chi[i].r.typ != EnumToken.IdenTokenType) {
|
|
292
|
+
return {
|
|
293
|
+
valid: ValidationLevel.Drop,
|
|
294
|
+
nodes: [selector],
|
|
295
|
+
error: 'identifier expected'
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
if (selector.chi[i].l != null) {
|
|
299
|
+
// @ts-ignore
|
|
300
|
+
if (selector.chi[i].l.typ != EnumToken.IdenTokenType &&
|
|
301
|
+
// @ts-ignore
|
|
302
|
+
selector.chi[i].l.typ != EnumToken.LiteralTokenType) {
|
|
303
|
+
return {
|
|
304
|
+
valid: ValidationLevel.Drop,
|
|
305
|
+
nodes: [selector],
|
|
306
|
+
error: 'invalid namespace prefix'
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
if (
|
|
310
|
+
// @ts-ignore
|
|
311
|
+
selector.chi[i].l.typ == EnumToken.LiteralTokenType &&
|
|
312
|
+
selector.chi[i].l.val != '*') {
|
|
313
|
+
return {
|
|
314
|
+
valid: ValidationLevel.Drop,
|
|
315
|
+
nodes: [selector],
|
|
316
|
+
error: 'exoected universal selector or namespace prefix'
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
i++;
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
return {
|
|
325
|
+
valid: ValidationLevel.Drop,
|
|
326
|
+
nodes: [selector],
|
|
327
|
+
error: 'invalid selector'
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
if (!isMatchExpression) {
|
|
331
|
+
while (i < selector.chi.length) {
|
|
332
|
+
if ([EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(selector.chi[i].typ)) {
|
|
333
|
+
i++;
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
if (i < selector.chi.length) {
|
|
339
|
+
if (!expressions.includes(selector.chi[i].typ)) {
|
|
340
|
+
return {
|
|
341
|
+
valid: ValidationLevel.Drop,
|
|
342
|
+
nodes: [selector],
|
|
343
|
+
error: 'invalid selector'
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
i++;
|
|
347
|
+
while (i < selector.chi.length) {
|
|
348
|
+
if ([EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(selector.chi[i].typ)) {
|
|
349
|
+
i++;
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
if (i >= selector.chi.length || ![EnumToken.IdenTokenType, EnumToken.StringTokenType].includes(selector.chi[i].typ)) {
|
|
355
|
+
return {
|
|
356
|
+
valid: ValidationLevel.Drop,
|
|
357
|
+
nodes: [selector],
|
|
358
|
+
error: 'expected identifier or string'
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
i++;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
if (expression.l.typ != EnumToken.IdenTokenType ||
|
|
366
|
+
![EnumToken.IdenTokenType, EnumToken.StringTokenType].includes(expression.r.typ) ||
|
|
367
|
+
!expressions.includes(expression.op)) {
|
|
368
|
+
return {
|
|
369
|
+
valid: ValidationLevel.Drop,
|
|
370
|
+
nodes: [selector],
|
|
371
|
+
error: 'expected identifier or string'
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (i < selector.chi.length) {
|
|
376
|
+
let hasWhitespace = false;
|
|
377
|
+
while (i < selector.chi.length) {
|
|
378
|
+
if ([EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(selector.chi[i].typ)) {
|
|
379
|
+
if (!hasWhitespace) {
|
|
380
|
+
hasWhitespace = selector.chi[i].typ == EnumToken.WhitespaceTokenType;
|
|
381
|
+
}
|
|
382
|
+
i++;
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
// @ts-ignore
|
|
388
|
+
if (!hasWhitespace || i >= selector.chi.length || selector.chi[i].typ != EnumToken.IdenTokenType || !['i', 's'].includes(selector.chi[i].val)) {
|
|
389
|
+
const valid = !options.validation && selector.chi[i].typ == EnumToken.IdenTokenType && selector.chi[i].val.match(/^[a-z]$/);
|
|
390
|
+
return {
|
|
391
|
+
valid: valid ? ValidationLevel.Valid : ValidationLevel.Drop,
|
|
392
|
+
nodes: [selector],
|
|
393
|
+
error: valid ? '' : 'invalid attribute selector'
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
i++;
|
|
397
|
+
while (i < selector.chi.length) {
|
|
398
|
+
if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(selector.chi[i].typ)) {
|
|
399
|
+
return {
|
|
400
|
+
valid: ValidationLevel.Drop,
|
|
401
|
+
nodes: [selector],
|
|
402
|
+
error: 'invalid attribute selector'
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
i++;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
return {
|
|
409
|
+
valid: ValidationLevel.Valid,
|
|
410
|
+
nodes: [selector],
|
|
411
|
+
error: ''
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
function validateCombinator(selector, options, root) {
|
|
415
|
+
selector = consumeWhitespace(selector);
|
|
416
|
+
if (selector.length == 0) {
|
|
417
|
+
return {
|
|
418
|
+
valid: ValidationLevel.Drop,
|
|
419
|
+
nodes: selector,
|
|
420
|
+
error: 'expecting combinator'
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
const combinatorTypes = [
|
|
424
|
+
EnumToken.ChildCombinatorTokenType,
|
|
425
|
+
EnumToken.DescendantCombinatorTokenType,
|
|
426
|
+
EnumToken.NextSiblingCombinatorTokenType,
|
|
427
|
+
EnumToken.SubsequentSiblingCombinatorTokenType,
|
|
428
|
+
EnumToken.ColumnCombinatorTokenType
|
|
429
|
+
];
|
|
430
|
+
if (!combinatorTypes.includes(selector[0].typ)) {
|
|
431
|
+
return {
|
|
432
|
+
valid: ValidationLevel.Drop,
|
|
433
|
+
nodes: selector,
|
|
434
|
+
error: 'Expecting combinator'
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
selector = consumeWhitespace(selector.slice(1));
|
|
438
|
+
if (selector.length == 0) {
|
|
439
|
+
return {
|
|
440
|
+
valid: ValidationLevel.Drop,
|
|
441
|
+
nodes: selector,
|
|
442
|
+
error: 'Unexpected combinator'
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
if (combinatorTypes.includes(selector[0].typ)) {
|
|
446
|
+
return {
|
|
447
|
+
valid: ValidationLevel.Drop,
|
|
448
|
+
nodes: selector,
|
|
449
|
+
error: 'Unexpected combinator'
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
return {
|
|
453
|
+
valid: ValidationLevel.Valid,
|
|
454
|
+
nodes: selector,
|
|
455
|
+
error: ''
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
export { validateSelector };
|
package/dist/node/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import process from 'node:process';
|
|
1
2
|
export { EnumToken } from '../lib/ast/types.js';
|
|
2
3
|
export { minify } from '../lib/ast/minify.js';
|
|
3
4
|
export { walk, walkValues } from '../lib/ast/walk.js';
|
|
@@ -7,6 +8,7 @@ export { renderToken } from '../lib/renderer/render.js';
|
|
|
7
8
|
import { doParse } from '../lib/parser/parse.js';
|
|
8
9
|
export { parseString, parseTokens } from '../lib/parser/parse.js';
|
|
9
10
|
import '../lib/renderer/color/utils/constants.js';
|
|
11
|
+
import '../lib/parser/utils/config.js';
|
|
10
12
|
import { resolve, dirname } from '../lib/fs/resolve.js';
|
|
11
13
|
import { load } from './load.js';
|
|
12
14
|
|
package/dist/web/index.js
CHANGED
|
@@ -7,6 +7,7 @@ export { renderToken } from '../lib/renderer/render.js';
|
|
|
7
7
|
import { doParse } from '../lib/parser/parse.js';
|
|
8
8
|
export { parseString, parseTokens } from '../lib/parser/parse.js';
|
|
9
9
|
import '../lib/renderer/color/utils/constants.js';
|
|
10
|
+
import '../lib/parser/utils/config.js';
|
|
10
11
|
import { resolve, dirname } from '../lib/fs/resolve.js';
|
|
11
12
|
import { load } from './load.js';
|
|
12
13
|
|
package/dist/web/load.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbela99/css-parser",
|
|
3
3
|
"description": "CSS parser for node and the browser",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dist/node/index.js",
|
|
7
7
|
"./umd": "./dist/index-umd-web.js",
|
|
@@ -55,12 +55,13 @@
|
|
|
55
55
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
56
56
|
"@types/chai": "^4.3.12",
|
|
57
57
|
"@types/mocha": "^10.0.6",
|
|
58
|
-
"@types/node": "^20.11
|
|
58
|
+
"@types/node": "^20.14.11",
|
|
59
|
+
"@types/web": "^0.0.151",
|
|
59
60
|
"@web/test-runner": "^0.18.1",
|
|
60
61
|
"@web/test-runner-playwright": "^0.11.0",
|
|
61
62
|
"c8": "^9.1.0",
|
|
62
63
|
"mocha": "^10.4.0",
|
|
63
|
-
"playwright": "^1.
|
|
64
|
+
"playwright": "^1.45.2",
|
|
64
65
|
"rollup": "^4.13.0",
|
|
65
66
|
"rollup-plugin-dts": "^6.1.0",
|
|
66
67
|
"tslib": "^2.6.2"
|