@tbela99/css-parser 1.6.0 → 1.6.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/CHANGELOG.md +26 -0
- package/README.md +3 -3
- package/dist/index-umd-web.js +425 -230
- package/dist/index.cjs +425 -230
- package/dist/index.d.ts +1 -1
- package/dist/lib/ast/features/transform.js +1 -1
- package/dist/lib/ast/math/expression.js +19 -4
- package/dist/lib/ast/minify.js +84 -10
- package/dist/lib/ast/transform/compute.js +28 -55
- package/dist/lib/ast/transform/minify.js +1 -1
- package/dist/{config.json.js → lib/data/properties.json.js} +106 -0
- package/dist/lib/{validation/config.json.js → data/validation.json.js} +11 -8
- package/dist/lib/parser/parse.js +6 -5
- package/dist/lib/parser/tokenize.js +40 -41
- package/dist/lib/parser/utils/config.js +1 -1
- package/dist/lib/parser/utils/declaration.js +11 -3
- package/dist/lib/parser/utils/selector.js +98 -88
- package/dist/lib/renderer/render.js +2 -2
- package/dist/lib/renderer/sourcemap/lib/codec.js +3 -3
- package/dist/lib/syntax/color/cmyk.js +4 -3
- package/dist/lib/syntax/color/srgb.js +4 -3
- package/dist/lib/syntax/constants.js +1 -1
- package/dist/lib/validation/config.js +1 -1
- package/dist/lib/validation/parser/parse.js +7 -3
- package/package.json +4 -4
|
@@ -50,7 +50,6 @@ function parseGridTemplate(template) {
|
|
|
50
50
|
* @param errors
|
|
51
51
|
*/
|
|
52
52
|
function parseDeclaration(tokens, parent, options, errors) {
|
|
53
|
-
// console.error(tokens);
|
|
54
53
|
const name = tokens.shift();
|
|
55
54
|
let i;
|
|
56
55
|
let rules = null;
|
|
@@ -384,10 +383,19 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
384
383
|
}
|
|
385
384
|
if (trimTokenSpace.has(token.typ)) {
|
|
386
385
|
if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
|
|
387
|
-
|
|
386
|
+
for (let index = i + 1; index < tokens.length - 1; index++) {
|
|
387
|
+
tokens[index] = tokens[index + 1];
|
|
388
|
+
}
|
|
389
|
+
tokens.length--;
|
|
390
|
+
i--;
|
|
391
|
+
continue;
|
|
388
392
|
}
|
|
389
393
|
if (tokens[i - 1]?.typ == EnumToken.WhitespaceTokenType) {
|
|
390
|
-
|
|
394
|
+
for (let index = i - 1; index < tokens.length - 1; index++) {
|
|
395
|
+
tokens[index] = tokens[index + 1];
|
|
396
|
+
}
|
|
397
|
+
tokens.length--;
|
|
398
|
+
i--;
|
|
391
399
|
}
|
|
392
400
|
}
|
|
393
401
|
}
|
|
@@ -74,7 +74,6 @@ function parseSelector(tokens, context, options, errors) {
|
|
|
74
74
|
const stack = [];
|
|
75
75
|
const uniq = new Map();
|
|
76
76
|
let allowed = true;
|
|
77
|
-
let i = 0;
|
|
78
77
|
let index;
|
|
79
78
|
let parent = context;
|
|
80
79
|
let nested = false;
|
|
@@ -108,119 +107,130 @@ function parseSelector(tokens, context, options, errors) {
|
|
|
108
107
|
nested = parent?.typ == EnumToken.RuleNodeType;
|
|
109
108
|
parent = parent?.[PARENT];
|
|
110
109
|
} while (!nested && parent != null);
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
110
|
+
const all = new Array(tokens.length);
|
|
111
|
+
let write = 0;
|
|
112
|
+
for (let read = 0; read < tokens.length; read++) {
|
|
113
|
+
let token = tokens[read];
|
|
114
|
+
if (token.typ == EnumToken.ColonTokenType) {
|
|
115
|
+
const next = tokens[read + 1];
|
|
116
|
+
if (next?.typ == EnumToken.IdenTokenType) {
|
|
117
|
+
all[write++] = {
|
|
118
|
+
...token,
|
|
115
119
|
typ: EnumToken.PseudoElementTokenType,
|
|
116
|
-
val: ":" +
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
+
val: ":" + next.val,
|
|
121
|
+
[LOCEND]: next[LOCEND],
|
|
122
|
+
};
|
|
123
|
+
read++;
|
|
120
124
|
continue;
|
|
121
125
|
}
|
|
122
|
-
if (
|
|
123
|
-
val = ":" +
|
|
124
|
-
|
|
126
|
+
if (next?.typ == EnumToken.FunctionTokenDefType) {
|
|
127
|
+
val = ":" + next.val;
|
|
128
|
+
all[write++] = {
|
|
129
|
+
...token,
|
|
125
130
|
typ: val + "()" in getSyntaxConfig().selectors
|
|
126
131
|
? EnumToken.PseudoClassFunctionTokenDefType
|
|
127
|
-
:
|
|
132
|
+
: next.typ,
|
|
128
133
|
val,
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
134
|
+
[LOCEND]: next[LOCEND],
|
|
135
|
+
};
|
|
136
|
+
read++;
|
|
132
137
|
continue;
|
|
133
138
|
}
|
|
134
139
|
}
|
|
135
|
-
if (
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
140
|
+
if (token.typ == EnumToken.DoubleColonTokenType) {
|
|
141
|
+
const next = tokens[read + 1];
|
|
142
|
+
val = ":" + next.val;
|
|
143
|
+
if (next?.typ == EnumToken.IdenTokenType) {
|
|
144
|
+
all[write++] = {
|
|
145
|
+
...token,
|
|
139
146
|
typ: EnumToken.PseudoClassTokenType,
|
|
140
147
|
val: (pseudoElements.includes(val) ? "" : ":") + val,
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
148
|
+
[LOCEND]: next[LOCEND],
|
|
149
|
+
};
|
|
150
|
+
read++;
|
|
144
151
|
continue;
|
|
145
152
|
}
|
|
146
|
-
if (
|
|
147
|
-
val = "::" +
|
|
148
|
-
|
|
153
|
+
if (next?.typ == EnumToken.FunctionTokenDefType) {
|
|
154
|
+
val = "::" + next.val;
|
|
155
|
+
all[write++] = {
|
|
156
|
+
...token,
|
|
149
157
|
typ: val + "()" in getSyntaxConfig().selectors
|
|
150
158
|
? EnumToken.PseudoClassFunctionTokenDefType
|
|
151
159
|
: EnumToken.FunctionTokenDefType,
|
|
152
160
|
val,
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
161
|
+
[LOCEND]: next[LOCEND],
|
|
162
|
+
};
|
|
163
|
+
read++;
|
|
156
164
|
continue;
|
|
157
165
|
}
|
|
158
166
|
}
|
|
159
|
-
if (
|
|
160
|
-
if (isHash(
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
167
|
+
if (token.typ == EnumToken.ColorTokenType) {
|
|
168
|
+
if (isHash(token.val)) {
|
|
169
|
+
token.typ = EnumToken.HashTokenType;
|
|
170
|
+
all[write++] = token;
|
|
171
|
+
continue;
|
|
164
172
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
trimWhiteSpace.includes(array[index + 1]?.typ) ||
|
|
177
|
-
combinators.includes(array[index - 1]?.val) ||
|
|
178
|
-
combinators.includes(array[index + 1]?.val)) {
|
|
179
|
-
return acc;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
let t = renderValue(curr, { minify: false });
|
|
183
|
-
if (t == ",") {
|
|
184
|
-
acc.push([]);
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
acc[acc.length - 1].push(t);
|
|
173
|
+
return {
|
|
174
|
+
typ: EnumToken.RuleNodeType,
|
|
175
|
+
sel: [
|
|
176
|
+
...tokens
|
|
177
|
+
.reduce((acc, curr, index, array) => {
|
|
178
|
+
if (curr.typ == EnumToken.WhitespaceTokenType) {
|
|
179
|
+
if (trimWhiteSpace.includes(array[index - 1]?.typ) ||
|
|
180
|
+
trimWhiteSpace.includes(array[index + 1]?.typ) ||
|
|
181
|
+
combinators.includes(array[index - 1]?.val) ||
|
|
182
|
+
combinators.includes(array[index + 1]?.val)) {
|
|
183
|
+
return acc;
|
|
188
184
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
185
|
+
}
|
|
186
|
+
let t = renderValue(curr, { minify: false });
|
|
187
|
+
if (t == ",") {
|
|
188
|
+
acc.push([]);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
acc[acc.length - 1].push(t);
|
|
192
|
+
}
|
|
193
|
+
return acc;
|
|
194
|
+
}, [[]])
|
|
195
|
+
.reduce((acc, curr) => {
|
|
196
|
+
let i = 0;
|
|
197
|
+
for (; i < curr.length; i++) {
|
|
198
|
+
if (i + 1 < curr.length && curr[i] == "*") {
|
|
199
|
+
if (curr[i] == "*") {
|
|
200
|
+
let index = curr[i + 1] == " " ? 2 : 1;
|
|
201
|
+
if (![">", "~", "+"].includes(curr[index])) {
|
|
202
|
+
curr.splice(i, index);
|
|
200
203
|
}
|
|
201
204
|
}
|
|
202
205
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
206
|
+
}
|
|
207
|
+
acc.set(curr.join(""), curr);
|
|
208
|
+
return acc;
|
|
209
|
+
}, uniq)
|
|
210
|
+
.keys(),
|
|
211
|
+
].join(","),
|
|
212
|
+
chi: [],
|
|
213
|
+
[LOCSRCID]: tokens[0][LOCSRCID],
|
|
214
|
+
[LOCSTA]: tokens[0][LOCSTA],
|
|
215
|
+
[LOCEND]: tokens[tokens.length - 1][LOCEND],
|
|
216
|
+
[TOKENS]: tokens,
|
|
217
|
+
[STATE]: EnumAstNodeStatus.Invalid,
|
|
218
|
+
[ERRORS]: [
|
|
219
|
+
{
|
|
220
|
+
action: "drop",
|
|
221
|
+
node: token,
|
|
222
|
+
message: "invalid hash id",
|
|
223
|
+
},
|
|
224
|
+
],
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
all[write++] = token;
|
|
228
|
+
}
|
|
229
|
+
if (write !== tokens.length) {
|
|
230
|
+
for (let index = 0; index < write; index++) {
|
|
231
|
+
tokens[index] = all[index];
|
|
223
232
|
}
|
|
233
|
+
tokens.length = write;
|
|
224
234
|
}
|
|
225
235
|
const result = matchSelectorSyntax(tokens, errors, options, nested === true);
|
|
226
236
|
trimArray(tokens);
|
|
@@ -515,8 +515,8 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
515
515
|
return token.val;
|
|
516
516
|
}
|
|
517
517
|
if (Array.isArray(token.chi)) {
|
|
518
|
-
const fnName = token.val
|
|
519
|
-
const isLegacy = ["rgb", "rgba", "hsl", "hsla"].includes(token.val
|
|
518
|
+
const fnName = token.val;
|
|
519
|
+
const isLegacy = ["rgb", "rgba", "hsl", "hsla"].includes(token.val);
|
|
520
520
|
const hasAlpha = [
|
|
521
521
|
"rgb",
|
|
522
522
|
"rgba",
|
|
@@ -47,9 +47,9 @@ function decode(str) {
|
|
|
47
47
|
* @returns
|
|
48
48
|
*/
|
|
49
49
|
function encode(value) {
|
|
50
|
-
if (typeof value === 'number') {
|
|
51
|
-
|
|
52
|
-
}
|
|
50
|
+
// if (typeof value === 'number') {
|
|
51
|
+
// return encode_integer(value);
|
|
52
|
+
// }
|
|
53
53
|
let result = '';
|
|
54
54
|
for (let i = 0; i < value.length; i += 1) {
|
|
55
55
|
result += encode_integer(value[i]);
|
|
@@ -61,9 +61,10 @@ function color2cmykToken(token) {
|
|
|
61
61
|
}
|
|
62
62
|
function srgb2cmykvalues(r, g, b, a = null) {
|
|
63
63
|
const k = 1 - Math.max(r, g, b);
|
|
64
|
-
const
|
|
65
|
-
const
|
|
66
|
-
const
|
|
64
|
+
const div = 1 - k;
|
|
65
|
+
const c = k == 1 ? 0 : (1 - r - k) / div;
|
|
66
|
+
const m = k == 1 ? 0 : (1 - g - k) / div;
|
|
67
|
+
const y = k == 1 ? 0 : (1 - b - k) / div;
|
|
67
68
|
const result = [c, m, y, k];
|
|
68
69
|
if (a != null && a < 1) {
|
|
69
70
|
result.push(a);
|
|
@@ -116,10 +116,11 @@ function cmyk2srgbvalues(token) {
|
|
|
116
116
|
t = components[3];
|
|
117
117
|
// @ts-ignore
|
|
118
118
|
const k = getNumber(t);
|
|
119
|
+
const mul = 1 - k;
|
|
119
120
|
const rgb = [
|
|
120
|
-
1 - Math.min(1, c *
|
|
121
|
-
1 - Math.min(1, m *
|
|
122
|
-
1 - Math.min(1, y *
|
|
121
|
+
1 - Math.min(1, c * mul + k),
|
|
122
|
+
1 - Math.min(1, m * mul + k),
|
|
123
|
+
1 - Math.min(1, y * mul + k),
|
|
123
124
|
];
|
|
124
125
|
if (components.length == 5) {
|
|
125
126
|
rgb.push(getNumber(components[4]));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import config from '
|
|
1
|
+
import config from '../data/validation.json.js';
|
|
2
2
|
import { trimSyntaxArray, parseSyntax } from './parser/parse.js';
|
|
3
3
|
import { ValidationTokenEnum } from './parser/typedef.js';
|
|
4
4
|
import { memoize } from '../parser/utils/cache.js';
|
|
@@ -677,9 +677,13 @@ function parseSyntax(syntax) {
|
|
|
677
677
|
stack.pop();
|
|
678
678
|
}
|
|
679
679
|
}
|
|
680
|
-
if (stack.length > 0) {
|
|
681
|
-
|
|
682
|
-
}
|
|
680
|
+
// if (stack.length > 0) {
|
|
681
|
+
// throw new SyntaxError(
|
|
682
|
+
// `Unexpected token ${ValidationTokenEnum[stack.at(-1)?.typ as ValidationTokenEnum]} at ${
|
|
683
|
+
// stack.at(-1)?.[LOC]?.lin
|
|
684
|
+
// }:${stack.at(-1)?.[LOC]?.col}`,
|
|
685
|
+
// );
|
|
686
|
+
// }
|
|
683
687
|
return trimSyntaxArray(tokens);
|
|
684
688
|
}
|
|
685
689
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbela99/css-parser",
|
|
3
3
|
"description": "CSS parser, minifier and validator for node and the browser",
|
|
4
|
-
"version": "1.6.
|
|
4
|
+
"version": "1.6.1",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dist/node.js",
|
|
7
7
|
"./node": "./dist/node.js",
|
|
@@ -66,16 +66,16 @@
|
|
|
66
66
|
"@rollup/plugin-typescript": "^12.3.0",
|
|
67
67
|
"@types/chai": "^5.2.3",
|
|
68
68
|
"@types/mocha": "^10.0.10",
|
|
69
|
-
"@types/node": "^26.
|
|
69
|
+
"@types/node": "^26.4.1",
|
|
70
70
|
"@types/web": "^0.0.354",
|
|
71
71
|
"@web/test-runner": "^1.0.0",
|
|
72
72
|
"@web/test-runner-playwright": "^1.0.0",
|
|
73
73
|
"chai": "^6.2.2",
|
|
74
74
|
"esno": "^4.8.0",
|
|
75
75
|
"markdown-it-task-lists": "^2.1.1",
|
|
76
|
-
"mocha": "^
|
|
76
|
+
"mocha": "^12.0.0",
|
|
77
77
|
"playwright": "^1.62.1",
|
|
78
|
-
"rollup": "^4.
|
|
78
|
+
"rollup": "^4.63.1",
|
|
79
79
|
"rollup-plugin-dts": "^6.5.1",
|
|
80
80
|
"superc8": "^12.7.0",
|
|
81
81
|
"tslib": "^2.8.1",
|