@tbela99/css-parser 0.9.0 → 0.9.1
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 +8 -5
- package/dist/index-umd-web.js +469 -1946
- package/dist/index.cjs +459 -1936
- package/dist/index.d.ts +25 -12
- package/dist/lib/ast/expand.js +15 -2
- package/dist/lib/ast/minify.js +1 -1
- package/dist/lib/ast/types.js +1 -0
- package/dist/lib/parser/parse.js +66 -40
- package/dist/lib/renderer/color/color.js +2 -2
- package/dist/lib/renderer/color/lab.js +2 -1
- package/dist/lib/renderer/color/prophotoRgb.js +2 -2
- package/dist/lib/renderer/color/prophotorgb.js +2 -2
- package/dist/lib/renderer/color/xyz.js +2 -18
- package/dist/lib/renderer/color/xyzd50.js +20 -2
- package/dist/lib/renderer/render.js +3 -2
- package/dist/lib/renderer/sourcemap/sourcemap.js +1 -1
- package/dist/lib/syntax/syntax.js +2 -1
- package/dist/lib/validation/at-rules/document.js +40 -60
- package/dist/lib/validation/at-rules/import.js +61 -59
- package/dist/lib/validation/at-rules/media.js +2 -1
- package/dist/lib/validation/at-rules/supports.js +40 -9
- package/dist/lib/validation/config.json.js +83 -35
- package/dist/lib/validation/declaration.js +2 -10
- package/dist/lib/validation/parser/parse.js +1 -95
- package/dist/lib/validation/parser/types.js +1 -2
- package/dist/lib/validation/syntax.js +39 -5
- package/dist/lib/validation/syntaxes/compound-selector.js +2 -2
- package/dist/lib/validation/syntaxes/layer-name.js +5 -16
- package/package.json +4 -3
|
@@ -6,6 +6,7 @@ import '../../renderer/color/utils/constants.js';
|
|
|
6
6
|
import '../../renderer/sourcemap/lib/encode.js';
|
|
7
7
|
import '../../parser/utils/config.js';
|
|
8
8
|
import { consumeWhitespace } from '../utils/whitespace.js';
|
|
9
|
+
import { splitTokenList } from '../utils/list.js';
|
|
9
10
|
import { validateURL } from '../syntaxes/url.js';
|
|
10
11
|
|
|
11
12
|
function validateAtRuleDocument(atRule, options, root) {
|
|
@@ -34,71 +35,50 @@ function validateAtRuleDocument(atRule, options, root) {
|
|
|
34
35
|
tokens
|
|
35
36
|
};
|
|
36
37
|
}
|
|
37
|
-
|
|
38
|
+
for (const t of splitTokenList(tokens)) {
|
|
39
|
+
if (t.length != 1) {
|
|
40
|
+
return {
|
|
41
|
+
valid: ValidationLevel.Drop,
|
|
42
|
+
matches: [],
|
|
43
|
+
node: t[0] ?? atRule,
|
|
44
|
+
syntax: '@document',
|
|
45
|
+
error: 'unexpected token',
|
|
46
|
+
tokens
|
|
47
|
+
};
|
|
48
|
+
}
|
|
38
49
|
// @ts-ignore
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
while (tokens.length > 0) {
|
|
49
|
-
if (tokens[0].typ == EnumToken.CommentTokenType) {
|
|
50
|
-
tokens.shift();
|
|
51
|
-
consumeWhitespace(tokens);
|
|
50
|
+
if ((t[0].typ != EnumToken.FunctionTokenType && t[0].typ != EnumToken.UrlFunctionTokenType) || !['url', 'url-prefix', 'domain', 'media-document', 'regexp'].some((f) => f.localeCompare(t[0].val, undefined, { sensitivity: 'base' }) == 0)) {
|
|
51
|
+
return {
|
|
52
|
+
valid: ValidationLevel.Drop,
|
|
53
|
+
matches: [],
|
|
54
|
+
node: t[0] ?? atRule,
|
|
55
|
+
syntax: '@document',
|
|
56
|
+
error: 'expecting any of url-prefix(), domain(), media-document(), regexp() but found ' + t[0].val,
|
|
57
|
+
tokens
|
|
58
|
+
};
|
|
52
59
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
60
|
+
if (t[0].typ == EnumToken.UrlFunctionTokenType) {
|
|
61
|
+
result = validateURL(t[0]);
|
|
62
|
+
if (result.valid == ValidationLevel.Drop) {
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
57
65
|
continue;
|
|
58
66
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
const children = tokens[0].chi.slice();
|
|
72
|
-
consumeWhitespace(children);
|
|
73
|
-
if (children.length == 0) {
|
|
74
|
-
// @ts-ignore
|
|
75
|
-
return {
|
|
76
|
-
valid: ValidationLevel.Drop,
|
|
77
|
-
matches: [],
|
|
78
|
-
node: tokens[0],
|
|
79
|
-
syntax: '@document',
|
|
80
|
-
error: 'expecting string argument',
|
|
81
|
-
tokens
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
if (children[0].typ == EnumToken.StringTokenType) {
|
|
85
|
-
children.shift();
|
|
86
|
-
consumeWhitespace(children);
|
|
87
|
-
}
|
|
88
|
-
if (children.length > 0) {
|
|
89
|
-
// @ts-ignore
|
|
90
|
-
return {
|
|
91
|
-
valid: ValidationLevel.Drop,
|
|
92
|
-
matches: [],
|
|
93
|
-
node: children[0],
|
|
94
|
-
syntax: '@document',
|
|
95
|
-
error: 'unexpected token',
|
|
96
|
-
tokens
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
tokens.shift();
|
|
100
|
-
consumeWhitespace(tokens);
|
|
67
|
+
const children = t[0].chi.slice();
|
|
68
|
+
consumeWhitespace(children);
|
|
69
|
+
if (children.length != 1 || (children[0].typ != EnumToken.StringTokenType && children[0].typ != EnumToken.UrlTokenTokenType)) {
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
return {
|
|
72
|
+
valid: ValidationLevel.Drop,
|
|
73
|
+
matches: [],
|
|
74
|
+
node: tokens[0],
|
|
75
|
+
syntax: '@document',
|
|
76
|
+
error: 'expecting string argument',
|
|
77
|
+
tokens
|
|
78
|
+
};
|
|
101
79
|
}
|
|
80
|
+
tokens.shift();
|
|
81
|
+
consumeWhitespace(tokens);
|
|
102
82
|
}
|
|
103
83
|
// @ts-ignore
|
|
104
84
|
return {
|
|
@@ -5,7 +5,6 @@ import '../../parser/parse.js';
|
|
|
5
5
|
import '../../renderer/color/utils/constants.js';
|
|
6
6
|
import '../../renderer/sourcemap/lib/encode.js';
|
|
7
7
|
import '../../parser/utils/config.js';
|
|
8
|
-
import { validateAtRuleSupports } from './supports.js';
|
|
9
8
|
import { validateAtRuleMediaQueryList } from './media.js';
|
|
10
9
|
import { consumeWhitespace } from '../utils/whitespace.js';
|
|
11
10
|
import { validateLayerName } from '../syntaxes/layer-name.js';
|
|
@@ -13,6 +12,7 @@ import '../syntaxes/complex-selector.js';
|
|
|
13
12
|
import '../parser/types.js';
|
|
14
13
|
import '../parser/parse.js';
|
|
15
14
|
import '../config.js';
|
|
15
|
+
import { validateAtRuleSupportsConditions } from './supports.js';
|
|
16
16
|
|
|
17
17
|
function validateAtRuleImport(atRule, options, root) {
|
|
18
18
|
if (!Array.isArray(atRule.tokens) || atRule.tokens.length == 0) {
|
|
@@ -82,6 +82,9 @@ function validateAtRuleImport(atRule, options, root) {
|
|
|
82
82
|
};
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
|
+
tokens.shift();
|
|
86
|
+
// @ts-ignore
|
|
87
|
+
consumeWhitespace(tokens);
|
|
85
88
|
}
|
|
86
89
|
else {
|
|
87
90
|
// @ts-ignore
|
|
@@ -117,71 +120,70 @@ function validateAtRuleImport(atRule, options, root) {
|
|
|
117
120
|
// @ts-ignore
|
|
118
121
|
else if (tokens[0].typ == EnumToken.FunctionTokenType) {
|
|
119
122
|
// @ts-ignore
|
|
120
|
-
if ('layer'.localeCompare(tokens[0].val, undefined, { sensitivity: 'base' })
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
syntax: '@' + atRule.nam,
|
|
127
|
-
error: 'expecting layer()',
|
|
128
|
-
tokens
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
// @ts-ignore
|
|
132
|
-
const result = validateLayerName(tokens[0].chi);
|
|
133
|
-
if (result.valid == ValidationLevel.Drop) {
|
|
134
|
-
return result;
|
|
135
|
-
}
|
|
136
|
-
tokens.shift();
|
|
137
|
-
// @ts-ignore
|
|
138
|
-
if (!consumeWhitespace(tokens)) {
|
|
139
|
-
// @ts-ignore
|
|
140
|
-
return {
|
|
141
|
-
valid: ValidationLevel.Drop,
|
|
142
|
-
matches: [],
|
|
143
|
-
node: tokens[0],
|
|
144
|
-
syntax: '@' + atRule.nam,
|
|
145
|
-
error: 'expecting whitespace',
|
|
146
|
-
tokens
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
if (tokens.length > 0) {
|
|
152
|
-
// @ts-ignore
|
|
153
|
-
if (tokens[0].typ == EnumToken.AtRuleTokenType) {
|
|
154
|
-
if (tokens[0].nam != 'supports') {
|
|
123
|
+
if ('layer'.localeCompare(tokens[0].val, undefined, { sensitivity: 'base' }) == 0) {
|
|
124
|
+
const result = validateLayerName(tokens[0].chi);
|
|
125
|
+
if (result.valid == ValidationLevel.Drop) {
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
tokens.shift();
|
|
155
129
|
// @ts-ignore
|
|
156
|
-
|
|
157
|
-
valid: ValidationLevel.Drop,
|
|
158
|
-
matches: [],
|
|
159
|
-
node: tokens[0],
|
|
160
|
-
syntax: '@' + atRule.nam,
|
|
161
|
-
error: 'expecting @supports or media query list',
|
|
162
|
-
tokens
|
|
163
|
-
};
|
|
130
|
+
consumeWhitespace(tokens);
|
|
164
131
|
}
|
|
165
132
|
// @ts-ignore
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
if (!consumeWhitespace(tokens)) {
|
|
133
|
+
if ('supports'.localeCompare(tokens[0]?.val, undefined, { sensitivity: 'base' }) == 0) {
|
|
134
|
+
const result = validateAtRuleSupportsConditions(atRule, tokens[0].chi);
|
|
135
|
+
if (result.valid == ValidationLevel.Drop) {
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
tokens.shift();
|
|
173
139
|
// @ts-ignore
|
|
174
|
-
|
|
175
|
-
valid: ValidationLevel.Drop,
|
|
176
|
-
matches: [],
|
|
177
|
-
node: tokens[0],
|
|
178
|
-
syntax: '@' + atRule.nam,
|
|
179
|
-
error: 'expecting whitespace',
|
|
180
|
-
tokens
|
|
181
|
-
};
|
|
140
|
+
consumeWhitespace(tokens);
|
|
182
141
|
}
|
|
183
142
|
}
|
|
184
143
|
}
|
|
144
|
+
// if (tokens.length > 0) {
|
|
145
|
+
//
|
|
146
|
+
// // @ts-ignore
|
|
147
|
+
// if (tokens[0].typ == EnumToken.AtRuleTokenType) {
|
|
148
|
+
//
|
|
149
|
+
// if ((tokens[0] as AstAtRule).nam != 'supports') {
|
|
150
|
+
//
|
|
151
|
+
// // @ts-ignore
|
|
152
|
+
// return {
|
|
153
|
+
// valid: ValidationLevel.Drop,
|
|
154
|
+
// matches: [],
|
|
155
|
+
// node: tokens[0],
|
|
156
|
+
// syntax: '@' + atRule.nam,
|
|
157
|
+
// error: 'expecting @supports or media query list',
|
|
158
|
+
// tokens
|
|
159
|
+
// }
|
|
160
|
+
// }
|
|
161
|
+
//
|
|
162
|
+
// // @ts-ignore
|
|
163
|
+
// const result: ValidationSyntaxResult = validateAtRuleSupports(tokens[0] as AstAtRule, options, atRule);
|
|
164
|
+
//
|
|
165
|
+
// if (result.valid == ValidationLevel.Drop) {
|
|
166
|
+
//
|
|
167
|
+
// return result;
|
|
168
|
+
// }
|
|
169
|
+
//
|
|
170
|
+
// tokens.shift();
|
|
171
|
+
//
|
|
172
|
+
// // @ts-ignore
|
|
173
|
+
// if (!consumeWhitespace(tokens)) {
|
|
174
|
+
//
|
|
175
|
+
// // @ts-ignore
|
|
176
|
+
// return {
|
|
177
|
+
// valid: ValidationLevel.Drop,
|
|
178
|
+
// matches: [],
|
|
179
|
+
// node: tokens[0],
|
|
180
|
+
// syntax: '@' + atRule.nam,
|
|
181
|
+
// error: 'expecting whitespace',
|
|
182
|
+
// tokens
|
|
183
|
+
// }
|
|
184
|
+
// }
|
|
185
|
+
// }
|
|
186
|
+
// }
|
|
185
187
|
if (tokens.length > 0) {
|
|
186
188
|
return validateAtRuleMediaQueryList(tokens, atRule);
|
|
187
189
|
}
|
|
@@ -225,7 +225,7 @@ function validateMediaCondition(token, atRule) {
|
|
|
225
225
|
if (token.typ == EnumToken.MediaFeatureNotTokenType) {
|
|
226
226
|
return validateMediaCondition(token.val, atRule);
|
|
227
227
|
}
|
|
228
|
-
if (token.typ != EnumToken.ParensTokenType && !(['when', 'else'].includes(atRule.nam) && token.typ == EnumToken.FunctionTokenType && ['media', 'supports'].includes(token.val))) {
|
|
228
|
+
if (token.typ != EnumToken.ParensTokenType && !(['when', 'else', 'import'].includes(atRule.nam) && token.typ == EnumToken.FunctionTokenType && ['media', 'supports', 'selector'].includes(token.val))) {
|
|
229
229
|
return false;
|
|
230
230
|
}
|
|
231
231
|
const chi = token.chi.filter((t) => t.typ != EnumToken.CommentTokenType && t.typ != EnumToken.WhitespaceTokenType);
|
|
@@ -241,6 +241,7 @@ function validateMediaCondition(token, atRule) {
|
|
|
241
241
|
if (chi[0].typ == EnumToken.MediaQueryConditionTokenType) {
|
|
242
242
|
return chi[0].l.typ == EnumToken.IdenTokenType;
|
|
243
243
|
}
|
|
244
|
+
console.error(chi[0].parent);
|
|
244
245
|
return false;
|
|
245
246
|
}
|
|
246
247
|
function validateMediaFeature(token) {
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { ValidationLevel, EnumToken } from '../../ast/types.js';
|
|
2
2
|
import '../../ast/minify.js';
|
|
3
3
|
import '../../ast/walk.js';
|
|
4
|
-
import '../../parser/parse.js';
|
|
4
|
+
import { parseSelector } from '../../parser/parse.js';
|
|
5
5
|
import { colorFontTech, fontFeaturesTech, fontFormat } from '../../syntax/syntax.js';
|
|
6
6
|
import '../../parser/utils/config.js';
|
|
7
7
|
import '../../renderer/color/utils/constants.js';
|
|
8
8
|
import '../../renderer/sourcemap/lib/encode.js';
|
|
9
9
|
import { consumeWhitespace } from '../utils/whitespace.js';
|
|
10
10
|
import { splitTokenList } from '../utils/list.js';
|
|
11
|
-
import {
|
|
12
|
-
import { getParsedSyntax } from '../config.js';
|
|
11
|
+
import { validateComplexSelector } from '../syntaxes/complex-selector.js';
|
|
13
12
|
|
|
14
13
|
function validateAtRuleSupports(atRule, options, root) {
|
|
15
14
|
// media-query-list
|
|
@@ -53,6 +52,7 @@ function validateAtRuleSupports(atRule, options, root) {
|
|
|
53
52
|
};
|
|
54
53
|
}
|
|
55
54
|
function validateAtRuleSupportsConditions(atRule, tokenList) {
|
|
55
|
+
let result = null;
|
|
56
56
|
for (const tokens of splitTokenList(tokenList)) {
|
|
57
57
|
if (tokens.length == 0) {
|
|
58
58
|
// @ts-ignore
|
|
@@ -66,22 +66,46 @@ function validateAtRuleSupportsConditions(atRule, tokenList) {
|
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
68
|
let previousToken = null;
|
|
69
|
-
let result = null;
|
|
70
69
|
while (tokens.length > 0) {
|
|
71
70
|
result = validateSupportCondition(atRule, tokens[0]);
|
|
72
71
|
// supports-condition
|
|
73
|
-
if (result
|
|
72
|
+
if (result.valid == ValidationLevel.Valid) {
|
|
74
73
|
previousToken = tokens[0];
|
|
75
74
|
tokens.shift();
|
|
76
75
|
}
|
|
77
76
|
else {
|
|
78
77
|
result = validateSupportFeature(tokens[0]);
|
|
79
|
-
if (result == null || result.valid == ValidationLevel.Valid) {
|
|
78
|
+
if ( /*result == null || */result.valid == ValidationLevel.Valid) {
|
|
80
79
|
previousToken = tokens[0];
|
|
81
80
|
tokens.shift();
|
|
82
81
|
}
|
|
83
82
|
else {
|
|
84
|
-
|
|
83
|
+
if (tokens[0].typ == EnumToken.ParensTokenType) {
|
|
84
|
+
result = validateAtRuleSupportsConditions(atRule, tokens[0].chi);
|
|
85
|
+
if ( /* result == null || */result.valid == ValidationLevel.Valid) {
|
|
86
|
+
previousToken = tokens[0];
|
|
87
|
+
tokens.shift();
|
|
88
|
+
// continue;
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
// if (result!= null && result.valid == ValidationLevel.Drop) {
|
|
98
|
+
//
|
|
99
|
+
// return {
|
|
100
|
+
// valid: ValidationLevel.Drop,
|
|
101
|
+
// matches: [],
|
|
102
|
+
// node: tokens[0] ?? atRule,
|
|
103
|
+
// syntax: '@' + atRule.nam,
|
|
104
|
+
// // @ts-ignore
|
|
105
|
+
// error: result.error as string ?? 'unexpected token',
|
|
106
|
+
// tokens: []
|
|
107
|
+
// };
|
|
108
|
+
// }
|
|
85
109
|
}
|
|
86
110
|
}
|
|
87
111
|
if (tokens.length == 0) {
|
|
@@ -136,7 +160,14 @@ function validateAtRuleSupportsConditions(atRule, tokenList) {
|
|
|
136
160
|
}
|
|
137
161
|
}
|
|
138
162
|
}
|
|
139
|
-
return
|
|
163
|
+
return {
|
|
164
|
+
valid: ValidationLevel.Valid,
|
|
165
|
+
matches: [],
|
|
166
|
+
node: atRule,
|
|
167
|
+
syntax: '@' + atRule.nam,
|
|
168
|
+
error: '',
|
|
169
|
+
tokens: []
|
|
170
|
+
};
|
|
140
171
|
}
|
|
141
172
|
function validateSupportCondition(atRule, token) {
|
|
142
173
|
if (token.typ == EnumToken.MediaFeatureNotTokenType) {
|
|
@@ -203,7 +234,7 @@ function validateSupportCondition(atRule, token) {
|
|
|
203
234
|
function validateSupportFeature(token) {
|
|
204
235
|
if (token.typ == EnumToken.FunctionTokenType) {
|
|
205
236
|
if (token.val.localeCompare('selector', undefined, { sensitivity: 'base' }) == 0) {
|
|
206
|
-
return
|
|
237
|
+
return validateComplexSelector(parseSelector(token.chi));
|
|
207
238
|
}
|
|
208
239
|
if (token.val.localeCompare('font-tech', undefined, { sensitivity: 'base' }) == 0) {
|
|
209
240
|
const chi = token.chi.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ));
|