@tbela99/css-parser 0.0.1-rc7 → 0.1.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 +157 -41
- package/dist/index-umd-web.js +2058 -1242
- package/dist/index.cjs +2052 -1238
- package/dist/index.d.ts +677 -332
- package/dist/lib/ast/expand.js +14 -20
- package/dist/lib/ast/features/calc.js +256 -0
- package/dist/lib/ast/features/index.js +3 -0
- package/dist/lib/ast/features/inlinecssvariables.js +115 -0
- package/dist/lib/ast/features/shorthand.js +45 -0
- package/dist/lib/ast/minify.js +395 -371
- package/dist/lib/ast/types.js +88 -0
- package/dist/lib/ast/utiles/minifyfeature.js +8 -0
- package/dist/lib/ast/walk.js +24 -9
- package/dist/lib/parser/declaration/list.js +18 -4
- package/dist/lib/parser/declaration/map.js +51 -30
- package/dist/lib/parser/declaration/set.js +18 -12
- package/dist/lib/parser/parse.js +176 -136
- package/dist/lib/parser/tokenize.js +42 -35
- package/dist/lib/parser/utils/eq.js +1 -1
- package/dist/lib/parser/utils/syntax.js +13 -10
- package/dist/lib/parser/utils/type.js +18 -6
- package/dist/lib/renderer/render.js +201 -79
- package/dist/lib/renderer/sourcemap/lib/encode.js +37 -0
- package/dist/lib/renderer/sourcemap/sourcemap.js +58 -0
- package/dist/lib/renderer/utils/color.js +25 -20
- package/dist/node/index.js +29 -10
- package/dist/web/index.js +33 -12
- package/package.json +5 -4
- package/dist/lib/transform.js +0 -24
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { getAngle, COLORS_NAMES, rgb2Hex, hsl2Hex, hwb2hex, cmyk2hex, NAMES_COLORS } from './utils/color.js';
|
|
2
|
+
import { EnumToken } from '../ast/types.js';
|
|
3
|
+
import '../ast/minify.js';
|
|
2
4
|
import { expand } from '../ast/expand.js';
|
|
5
|
+
import { SourceMap } from './sourcemap/sourcemap.js';
|
|
6
|
+
import '../parser/parse.js';
|
|
7
|
+
import { isNewLine } from '../parser/utils/syntax.js';
|
|
3
8
|
|
|
4
9
|
const colorsFunc = ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk'];
|
|
5
10
|
function reduceNumber(val) {
|
|
@@ -19,35 +24,88 @@ function reduceNumber(val) {
|
|
|
19
24
|
}
|
|
20
25
|
return val;
|
|
21
26
|
}
|
|
22
|
-
function
|
|
27
|
+
function update(position, str) {
|
|
28
|
+
let i = 0;
|
|
29
|
+
for (; i < str.length; i++) {
|
|
30
|
+
if (isNewLine(str[i].charCodeAt(0))) {
|
|
31
|
+
position.lin++;
|
|
32
|
+
position.col = 0;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
position.col++;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function doRender(data, options = {}) {
|
|
40
|
+
options = {
|
|
41
|
+
...(options.minify ?? true ? {
|
|
42
|
+
indent: '',
|
|
43
|
+
newLine: '',
|
|
44
|
+
removeComments: true
|
|
45
|
+
} : {
|
|
46
|
+
indent: ' ',
|
|
47
|
+
newLine: '\n',
|
|
48
|
+
compress: false,
|
|
49
|
+
removeComments: false,
|
|
50
|
+
}), sourcemap: false, colorConvert: true, expandNestingRules: false, preserveLicense: false, ...options
|
|
51
|
+
};
|
|
23
52
|
const startTime = performance.now();
|
|
24
53
|
const errors = [];
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}, { colorConvert: true, expandNestingRules: false, preserveLicense: false }, opt);
|
|
35
|
-
return {
|
|
36
|
-
code: doRender(options.expandNestingRules ? expand(data) : data, options, errors, function reducer(acc, curr) {
|
|
37
|
-
if (curr.typ == 'Comment' && options.removeComments) {
|
|
54
|
+
const sourcemap = options.sourcemap ? new SourceMap : null;
|
|
55
|
+
const cache = Object.create(null);
|
|
56
|
+
const result = {
|
|
57
|
+
code: renderAstNode(options.expandNestingRules ? expand(data) : data, options, sourcemap, {
|
|
58
|
+
ind: 0,
|
|
59
|
+
lin: 1,
|
|
60
|
+
col: 1
|
|
61
|
+
}, errors, function reducer(acc, curr) {
|
|
62
|
+
if (curr.typ == EnumToken.CommentTokenType && options.removeComments) {
|
|
38
63
|
if (!options.preserveLicense || !curr.val.startsWith('/*!')) {
|
|
39
64
|
return acc;
|
|
40
65
|
}
|
|
41
66
|
return acc + curr.val;
|
|
42
67
|
}
|
|
43
|
-
return acc + renderToken(curr, options, reducer, errors);
|
|
44
|
-
},
|
|
68
|
+
return acc + renderToken(curr, options, cache, reducer, errors);
|
|
69
|
+
}, cache), errors, stats: {
|
|
45
70
|
total: `${(performance.now() - startTime).toFixed(2)}ms`
|
|
46
71
|
}
|
|
47
72
|
};
|
|
73
|
+
if (options.output != null) {
|
|
74
|
+
// @ts-ignore
|
|
75
|
+
options.output = options.resolve(options.output, options.cwd).absolute;
|
|
76
|
+
}
|
|
77
|
+
if (sourcemap != null) {
|
|
78
|
+
result.map = sourcemap.toJSON();
|
|
79
|
+
// @ts-ignore
|
|
80
|
+
// result.map.sources = result.map.sources?.map(s => <string>options?.resolve(s, <string>options?.cwd)?.relative)
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
function updateSourceMap(node, options, cache, sourcemap, position, str) {
|
|
85
|
+
if ([4 /* NodeType.RuleNodeType */, 3 /* NodeType.AtRuleNodeType */].includes(node.typ)) {
|
|
86
|
+
let src = node.loc?.src ?? '';
|
|
87
|
+
let output = options.output ?? '';
|
|
88
|
+
if (src !== '') {
|
|
89
|
+
if (!(src in cache)) {
|
|
90
|
+
// @ts-ignore
|
|
91
|
+
cache[src] = options.resolve(src, options.cwd ?? '').relative;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (!(output in cache)) {
|
|
95
|
+
// @ts-ignore
|
|
96
|
+
cache[output] = options.resolve(output, options.cwd).relative;
|
|
97
|
+
}
|
|
98
|
+
// @ts-ignore
|
|
99
|
+
sourcemap.add({ src: cache[output], sta: { ...position } }, {
|
|
100
|
+
...node.loc,
|
|
101
|
+
// @ts-ignore
|
|
102
|
+
src: options.resolve(cache[src], options.cwd).relative
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
update(position, str);
|
|
48
106
|
}
|
|
49
107
|
// @ts-ignore
|
|
50
|
-
function
|
|
108
|
+
function renderAstNode(data, options, sourcemap, position, errors, reducer, cache, level = 0, indents = []) {
|
|
51
109
|
if (indents.length < level + 1) {
|
|
52
110
|
indents.push(options.indent.repeat(level));
|
|
53
111
|
}
|
|
@@ -57,46 +115,61 @@ function doRender(data, options, errors, reducer, level = 0, indents = []) {
|
|
|
57
115
|
const indent = indents[level];
|
|
58
116
|
const indentSub = indents[level + 1];
|
|
59
117
|
switch (data.typ) {
|
|
60
|
-
case
|
|
118
|
+
case 5 /* NodeType.DeclarationNodeType */:
|
|
61
119
|
return `${data.nam}:${options.indent}${data.val.reduce(reducer, '')}`;
|
|
62
|
-
case
|
|
63
|
-
case
|
|
120
|
+
case 0 /* NodeType.CommentNodeType */:
|
|
121
|
+
case 1 /* NodeType.CDOCOMMNodeType */:
|
|
122
|
+
if (data.val.startsWith('# sourceMappingURL=')) {
|
|
123
|
+
// ignore sourcemap
|
|
124
|
+
return '';
|
|
125
|
+
}
|
|
64
126
|
return !options.removeComments || (options.preserveLicense && data.val.startsWith('/*!')) ? data.val : '';
|
|
65
|
-
case
|
|
127
|
+
case 2 /* NodeType.StyleSheetNodeType */:
|
|
66
128
|
return data.chi.reduce((css, node) => {
|
|
67
|
-
const str =
|
|
129
|
+
const str = renderAstNode(node, options, sourcemap, { ...position }, errors, reducer, cache, level, indents);
|
|
68
130
|
if (str === '') {
|
|
69
131
|
return css;
|
|
70
132
|
}
|
|
71
133
|
if (css === '') {
|
|
134
|
+
if (sourcemap != null) {
|
|
135
|
+
updateSourceMap(node, options, cache, sourcemap, position, str);
|
|
136
|
+
}
|
|
72
137
|
return str;
|
|
73
138
|
}
|
|
139
|
+
if (sourcemap != null) {
|
|
140
|
+
update(position, options.newLine);
|
|
141
|
+
updateSourceMap(node, options, cache, sourcemap, position, str);
|
|
142
|
+
}
|
|
74
143
|
return `${css}${options.newLine}${str}`;
|
|
75
144
|
}, '');
|
|
76
|
-
case
|
|
77
|
-
case
|
|
78
|
-
if (data.typ ==
|
|
145
|
+
case 3 /* NodeType.AtRuleNodeType */:
|
|
146
|
+
case 4 /* NodeType.RuleNodeType */:
|
|
147
|
+
if (data.typ == 3 /* NodeType.AtRuleNodeType */ && !('chi' in data)) {
|
|
79
148
|
return `${indent}@${data.nam}${data.val === '' ? '' : options.indent || ' '}${data.val};`;
|
|
80
149
|
}
|
|
81
150
|
// @ts-ignore
|
|
82
151
|
let children = data.chi.reduce((css, node) => {
|
|
83
152
|
let str;
|
|
84
|
-
if (node.typ ==
|
|
153
|
+
if (node.typ == 0 /* NodeType.CommentNodeType */) {
|
|
85
154
|
str = options.removeComments && (!options.preserveLicense || !node.val.startsWith('/*!')) ? '' : node.val;
|
|
86
155
|
}
|
|
87
|
-
else if (node.typ ==
|
|
156
|
+
else if (node.typ == 5 /* NodeType.DeclarationNodeType */) {
|
|
88
157
|
if (node.val.length == 0) {
|
|
89
158
|
// @ts-ignore
|
|
90
|
-
errors.push({
|
|
159
|
+
errors.push({
|
|
160
|
+
action: 'ignore',
|
|
161
|
+
message: `render: invalid declaration ${JSON.stringify(node)}`,
|
|
162
|
+
location: node.loc
|
|
163
|
+
});
|
|
91
164
|
return '';
|
|
92
165
|
}
|
|
93
166
|
str = `${node.nam}:${options.indent}${node.val.reduce(reducer, '').trimEnd()};`;
|
|
94
167
|
}
|
|
95
|
-
else if (node.typ ==
|
|
168
|
+
else if (node.typ == 3 /* NodeType.AtRuleNodeType */ && !('chi' in node)) {
|
|
96
169
|
str = `${data.val === '' ? '' : options.indent || ' '}${data.val};`;
|
|
97
170
|
}
|
|
98
171
|
else {
|
|
99
|
-
str =
|
|
172
|
+
str = renderAstNode(node, options, sourcemap, { ...position }, errors, reducer, cache, level + 1, indents);
|
|
100
173
|
}
|
|
101
174
|
if (css === '') {
|
|
102
175
|
return str;
|
|
@@ -109,28 +182,40 @@ function doRender(data, options, errors, reducer, level = 0, indents = []) {
|
|
|
109
182
|
if (children.endsWith(';')) {
|
|
110
183
|
children = children.slice(0, -1);
|
|
111
184
|
}
|
|
112
|
-
if (data.typ ==
|
|
185
|
+
if (data.typ == 3 /* NodeType.AtRuleNodeType */) {
|
|
113
186
|
return `@${data.nam}${data.val === '' ? '' : options.indent || ' '}${data.val}${options.indent}{${options.newLine}` + (children === '' ? '' : indentSub + children + options.newLine) + indent + `}`;
|
|
114
187
|
}
|
|
115
188
|
return data.sel + `${options.indent}{${options.newLine}` + (children === '' ? '' : indentSub + children + options.newLine) + indent + `}`;
|
|
116
189
|
}
|
|
117
190
|
return '';
|
|
118
191
|
}
|
|
119
|
-
function renderToken(token, options = {}, reducer, errors) {
|
|
192
|
+
function renderToken(token, options = {}, cache = Object.create(null), reducer, errors) {
|
|
120
193
|
if (reducer == null) {
|
|
121
194
|
reducer = function (acc, curr) {
|
|
122
|
-
if (curr.typ ==
|
|
195
|
+
if (curr.typ == EnumToken.CommentTokenType && options.removeComments) {
|
|
123
196
|
if (!options.preserveLicense || !curr.val.startsWith('/*!')) {
|
|
124
197
|
return acc;
|
|
125
198
|
}
|
|
126
199
|
return acc + curr.val;
|
|
127
200
|
}
|
|
128
|
-
return acc + renderToken(curr, options, reducer, errors);
|
|
201
|
+
return acc + renderToken(curr, options, cache, reducer, errors);
|
|
129
202
|
};
|
|
130
203
|
}
|
|
131
204
|
switch (token.typ) {
|
|
132
|
-
case
|
|
133
|
-
|
|
205
|
+
case EnumToken.ListToken:
|
|
206
|
+
return token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache), '');
|
|
207
|
+
case EnumToken.BinaryExpressionTokenType:
|
|
208
|
+
return renderToken(token.l, options, cache) + (token.op == EnumToken.Add ? ' + ' : (token.op == EnumToken.Sub ? ' - ' : (token.op == EnumToken.Mul ? '*' : '/'))) + renderToken(token.r, options, cache);
|
|
209
|
+
case EnumToken.Add:
|
|
210
|
+
return ' + ';
|
|
211
|
+
case EnumToken.Sub:
|
|
212
|
+
return ' - ';
|
|
213
|
+
case EnumToken.Mul:
|
|
214
|
+
return '*';
|
|
215
|
+
case EnumToken.Div:
|
|
216
|
+
return '/';
|
|
217
|
+
case EnumToken.ColorTokenType:
|
|
218
|
+
if (options.colorConvert) {
|
|
134
219
|
if (token.kin == 'lit' && token.val.toLowerCase() == 'currentcolor') {
|
|
135
220
|
return 'currentcolor';
|
|
136
221
|
}
|
|
@@ -170,54 +255,69 @@ function renderToken(token, options = {}, reducer, errors) {
|
|
|
170
255
|
if (token.kin == 'hex' || token.kin == 'lit') {
|
|
171
256
|
return token.val;
|
|
172
257
|
}
|
|
173
|
-
case
|
|
174
|
-
|
|
175
|
-
|
|
258
|
+
case EnumToken.ParensTokenType:
|
|
259
|
+
case EnumToken.FunctionTokenType:
|
|
260
|
+
case EnumToken.UrlFunctionTokenType:
|
|
261
|
+
case EnumToken.PseudoClassFuncTokenType:
|
|
262
|
+
if (token.typ == EnumToken.FunctionTokenType &&
|
|
263
|
+
token.val == 'calc' &&
|
|
264
|
+
token.chi.length == 1) {
|
|
265
|
+
// calc(200px) => 200px
|
|
266
|
+
return token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer), '');
|
|
176
267
|
}
|
|
177
|
-
case 'Func':
|
|
178
|
-
case 'UrlFunc':
|
|
179
|
-
case 'Pseudo-class-func':
|
|
180
268
|
// @ts-ignore
|
|
181
269
|
return ( /* options.minify && 'Pseudo-class-func' == token.typ && token.val.slice(0, 2) == '::' ? token.val.slice(1) :*/token.val ?? '') + '(' + token.chi.reduce(reducer, '') + ')';
|
|
182
|
-
case
|
|
270
|
+
case EnumToken.StartParensTokenType:
|
|
271
|
+
return '(';
|
|
272
|
+
case EnumToken.IncludesTokenType:
|
|
183
273
|
return '~=';
|
|
184
|
-
case
|
|
274
|
+
case EnumToken.DashMatchTokenType:
|
|
185
275
|
return '|=';
|
|
186
|
-
case
|
|
276
|
+
case EnumToken.LtTokenType:
|
|
187
277
|
return '<';
|
|
188
|
-
case
|
|
278
|
+
case EnumToken.LteTokenType:
|
|
189
279
|
return '<=';
|
|
190
|
-
case
|
|
280
|
+
case EnumToken.GtTokenType:
|
|
191
281
|
return '>';
|
|
192
|
-
case
|
|
282
|
+
case EnumToken.GteTokenType:
|
|
193
283
|
return '>=';
|
|
194
|
-
case
|
|
284
|
+
case EnumToken.EndParensTokenType:
|
|
195
285
|
return ')';
|
|
196
|
-
case
|
|
286
|
+
case EnumToken.AttrStartTokenType:
|
|
197
287
|
return '[';
|
|
198
|
-
case
|
|
288
|
+
case EnumToken.AttrEndTokenType:
|
|
199
289
|
return ']';
|
|
200
|
-
case
|
|
290
|
+
case EnumToken.WhitespaceTokenType:
|
|
201
291
|
return ' ';
|
|
202
|
-
case
|
|
292
|
+
case EnumToken.ColonTokenType:
|
|
203
293
|
return ':';
|
|
204
|
-
case
|
|
294
|
+
case EnumToken.SemiColonTokenType:
|
|
205
295
|
return ';';
|
|
206
|
-
case
|
|
296
|
+
case EnumToken.CommaTokenType:
|
|
207
297
|
return ',';
|
|
208
|
-
case
|
|
298
|
+
case EnumToken.ImportantTokenType:
|
|
209
299
|
return '!important';
|
|
210
|
-
case
|
|
300
|
+
case EnumToken.AttrTokenType:
|
|
211
301
|
return '[' + token.chi.reduce(reducer, '') + ']';
|
|
212
|
-
case
|
|
213
|
-
case
|
|
214
|
-
case
|
|
215
|
-
case
|
|
216
|
-
case
|
|
217
|
-
case
|
|
302
|
+
case EnumToken.TimeTokenType:
|
|
303
|
+
case EnumToken.AngleTokenType:
|
|
304
|
+
case EnumToken.LengthTokenType:
|
|
305
|
+
case EnumToken.DimensionTokenType:
|
|
306
|
+
case EnumToken.FrequencyTokenType:
|
|
307
|
+
case EnumToken.ResolutionTokenType:
|
|
308
|
+
if (token.val.typ == EnumToken.BinaryExpressionTokenType) {
|
|
309
|
+
const result = renderToken(token.val, options, cache);
|
|
310
|
+
if (!('unit' in token)) {
|
|
311
|
+
return result;
|
|
312
|
+
}
|
|
313
|
+
if (!result.includes(' ')) {
|
|
314
|
+
return result + token.unit;
|
|
315
|
+
}
|
|
316
|
+
return `(${result})*1${token.unit}`;
|
|
317
|
+
}
|
|
218
318
|
let val = reduceNumber(token.val);
|
|
219
319
|
let unit = token.unit;
|
|
220
|
-
if (token.typ ==
|
|
320
|
+
if (token.typ == EnumToken.AngleTokenType) {
|
|
221
321
|
const angle = getAngle(token);
|
|
222
322
|
let v;
|
|
223
323
|
let value = val + unit;
|
|
@@ -262,40 +362,62 @@ function renderToken(token, options = {}, reducer, errors) {
|
|
|
262
362
|
}
|
|
263
363
|
}
|
|
264
364
|
if (val === '0') {
|
|
265
|
-
if (token.typ ==
|
|
365
|
+
if (token.typ == EnumToken.TimeTokenType) {
|
|
266
366
|
return '0s';
|
|
267
367
|
}
|
|
268
|
-
if (token.typ ==
|
|
368
|
+
if (token.typ == EnumToken.FrequencyTokenType) {
|
|
269
369
|
return '0Hz';
|
|
270
370
|
}
|
|
271
371
|
// @ts-ignore
|
|
272
|
-
if (token.typ ==
|
|
372
|
+
if (token.typ == EnumToken.ResolutionTokenType) {
|
|
273
373
|
return '0x';
|
|
274
374
|
}
|
|
275
375
|
return '0';
|
|
276
376
|
}
|
|
277
377
|
return val + unit;
|
|
278
|
-
case
|
|
378
|
+
case EnumToken.PercentageTokenType:
|
|
279
379
|
const perc = reduceNumber(token.val);
|
|
280
380
|
return options.minify && perc == '0' ? '0' : perc + '%';
|
|
281
|
-
case
|
|
381
|
+
case EnumToken.NumberTokenType:
|
|
282
382
|
return reduceNumber(token.val);
|
|
283
|
-
case
|
|
383
|
+
case EnumToken.CommentTokenType:
|
|
284
384
|
if (options.removeComments && (!options.preserveLicense || !token.val.startsWith('/*!'))) {
|
|
285
385
|
return '';
|
|
286
386
|
}
|
|
287
|
-
case
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
387
|
+
case EnumToken.UrlTokenTokenType:
|
|
388
|
+
if (token.typ == EnumToken.UrlTokenTokenType) {
|
|
389
|
+
if (options.output != null) {
|
|
390
|
+
if (!('original' in token)) {
|
|
391
|
+
// do not modify original token
|
|
392
|
+
token = { ...token };
|
|
393
|
+
Object.defineProperty(token, 'original', { enumerable: false, writable: false, value: token.val });
|
|
394
|
+
}
|
|
395
|
+
// @ts-ignore
|
|
396
|
+
if (!(token.original in cache)) {
|
|
397
|
+
let output = options.output ?? '';
|
|
398
|
+
const key = output + 'abs';
|
|
399
|
+
if (!(key in cache)) {
|
|
400
|
+
// @ts-ignore
|
|
401
|
+
cache[key] = options.dirname(options.resolve(output, options.cwd).absolute);
|
|
402
|
+
}
|
|
403
|
+
// @ts-ignore
|
|
404
|
+
cache[token.original] = options.resolve(token.original, cache[key]).relative;
|
|
405
|
+
}
|
|
406
|
+
// @ts-ignore
|
|
407
|
+
token.val = cache[token.original];
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
case EnumToken.AtRuleTokenType:
|
|
411
|
+
case EnumToken.HashTokenType:
|
|
412
|
+
case EnumToken.PseudoClassTokenType:
|
|
413
|
+
case EnumToken.LiteralTokenType:
|
|
414
|
+
case EnumToken.StringTokenType:
|
|
415
|
+
case EnumToken.IdenTokenType:
|
|
416
|
+
case EnumToken.DelimTokenType:
|
|
295
417
|
return /* options.minify && 'Pseudo-class' == token.typ && '::' == token.val.slice(0, 2) ? token.val.slice(1) : */ token.val;
|
|
296
418
|
}
|
|
297
419
|
errors?.push({ action: 'ignore', message: `render: unexpected token ${JSON.stringify(token, null, 1)}` });
|
|
298
420
|
return '';
|
|
299
421
|
}
|
|
300
422
|
|
|
301
|
-
export { colorsFunc,
|
|
423
|
+
export { colorsFunc, doRender, reduceNumber, renderToken };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// from https://github.com/Rich-Harris/vlq/tree/master
|
|
2
|
+
// credit: Rich Harris
|
|
3
|
+
const integer_to_char = {};
|
|
4
|
+
let i = 0;
|
|
5
|
+
for (const char of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=') {
|
|
6
|
+
integer_to_char[i++] = char;
|
|
7
|
+
}
|
|
8
|
+
function encode(value) {
|
|
9
|
+
if (typeof value === 'number') {
|
|
10
|
+
return encode_integer(value);
|
|
11
|
+
}
|
|
12
|
+
let result = '';
|
|
13
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
14
|
+
result += encode_integer(value[i]);
|
|
15
|
+
}
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
|
+
function encode_integer(num) {
|
|
19
|
+
let result = '';
|
|
20
|
+
if (num < 0) {
|
|
21
|
+
num = (-num << 1) | 1;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
num <<= 1;
|
|
25
|
+
}
|
|
26
|
+
do {
|
|
27
|
+
let clamped = num & 31;
|
|
28
|
+
num >>>= 5;
|
|
29
|
+
if (num > 0) {
|
|
30
|
+
clamped |= 32;
|
|
31
|
+
}
|
|
32
|
+
result += integer_to_char[clamped];
|
|
33
|
+
} while (num > 0);
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { encode };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { encode } from './lib/encode.js';
|
|
2
|
+
|
|
3
|
+
class SourceMap {
|
|
4
|
+
#version = 3;
|
|
5
|
+
#sources = [];
|
|
6
|
+
#map = new Map;
|
|
7
|
+
#line = -1;
|
|
8
|
+
lastLocation = null;
|
|
9
|
+
add(source, original) {
|
|
10
|
+
if (original.src !== '') {
|
|
11
|
+
if (!this.#sources.includes(original.src)) {
|
|
12
|
+
this.#sources.push(original.src);
|
|
13
|
+
}
|
|
14
|
+
const line = source.sta.lin - 1;
|
|
15
|
+
let record;
|
|
16
|
+
if (line > this.#line) {
|
|
17
|
+
this.#line = line;
|
|
18
|
+
}
|
|
19
|
+
if (!this.#map.has(line)) {
|
|
20
|
+
record = [Math.max(0, source.sta.col - 1), this.#sources.indexOf(original.src), original.sta.lin - 1, original.sta.col - 1];
|
|
21
|
+
this.#map.set(line, [record]);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
const arr = this.#map.get(line);
|
|
25
|
+
record = [Math.max(0, source.sta.col - 1 - arr[0][0]), this.#sources.indexOf(original.src) - arr[0][1], original.sta.lin - 1, original.sta.col - 1];
|
|
26
|
+
arr.push(record);
|
|
27
|
+
}
|
|
28
|
+
if (this.lastLocation != null) {
|
|
29
|
+
record[2] -= this.lastLocation.sta.lin - 1;
|
|
30
|
+
record[3] -= this.lastLocation.sta.col - 1;
|
|
31
|
+
}
|
|
32
|
+
this.lastLocation = original;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
toUrl() {
|
|
36
|
+
// /*# sourceMappingURL = ${url} */
|
|
37
|
+
return `data:application/json,${encodeURIComponent(JSON.stringify(this.toJSON()))}`;
|
|
38
|
+
}
|
|
39
|
+
toJSON() {
|
|
40
|
+
const mappings = [];
|
|
41
|
+
let i = 0;
|
|
42
|
+
for (; i <= this.#line; i++) {
|
|
43
|
+
if (!this.#map.has(i)) {
|
|
44
|
+
mappings.push('');
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
mappings.push(this.#map.get(i).reduce((acc, curr) => acc + (acc === '' ? '' : ',') + encode(curr), ''));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
version: this.#version,
|
|
52
|
+
sources: this.#sources.slice(),
|
|
53
|
+
mappings: mappings.join(';')
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { SourceMap };
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import { EnumToken } from '../../ast/types.js';
|
|
2
|
+
import '../../ast/minify.js';
|
|
3
|
+
import '../../parser/parse.js';
|
|
4
|
+
import '../sourcemap/lib/encode.js';
|
|
5
|
+
|
|
1
6
|
// name to color
|
|
2
7
|
const COLORS_NAMES = Object.seal({
|
|
3
8
|
'aliceblue': '#f0f8ff',
|
|
@@ -310,18 +315,18 @@ function rgb2Hex(token) {
|
|
|
310
315
|
// @ts-ignore
|
|
311
316
|
t = token.chi[i];
|
|
312
317
|
// @ts-ignore
|
|
313
|
-
value += Math.round(t.typ ==
|
|
318
|
+
value += Math.round(t.typ == EnumToken.PercentageTokenType ? 255 * t.val / 100 : t.val).toString(16).padStart(2, '0');
|
|
314
319
|
}
|
|
315
320
|
// @ts-ignore
|
|
316
321
|
if (token.chi.length == 7) {
|
|
317
322
|
// @ts-ignore
|
|
318
323
|
t = token.chi[6];
|
|
319
324
|
// @ts-ignore
|
|
320
|
-
if ((t.typ ==
|
|
325
|
+
if ((t.typ == EnumToken.NumberTokenType && t.val < 1) ||
|
|
321
326
|
// @ts-ignore
|
|
322
|
-
(t.typ ==
|
|
327
|
+
(t.typ == EnumToken.PercentageTokenType && t.val < 100)) {
|
|
323
328
|
// @ts-ignore
|
|
324
|
-
value += Math.round(255 * (t.typ ==
|
|
329
|
+
value += Math.round(255 * (t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val)).toString(16).padStart(2, '0');
|
|
325
330
|
}
|
|
326
331
|
}
|
|
327
332
|
return value;
|
|
@@ -333,21 +338,21 @@ function hsl2Hex(token) {
|
|
|
333
338
|
// @ts-ignore
|
|
334
339
|
t = token.chi[2];
|
|
335
340
|
// @ts-ignore
|
|
336
|
-
let s = t.typ ==
|
|
341
|
+
let s = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
|
|
337
342
|
// @ts-ignore
|
|
338
343
|
t = token.chi[4];
|
|
339
344
|
// @ts-ignore
|
|
340
|
-
let l = t.typ ==
|
|
345
|
+
let l = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
|
|
341
346
|
let a = null;
|
|
342
347
|
if (token.chi?.length == 7) {
|
|
343
348
|
// @ts-ignore
|
|
344
349
|
t = token.chi[6];
|
|
345
350
|
// @ts-ignore
|
|
346
|
-
if ((t.typ ==
|
|
351
|
+
if ((t.typ == EnumToken.PercentageTokenType && t.val < 100) ||
|
|
347
352
|
// @ts-ignore
|
|
348
|
-
(t.typ ==
|
|
353
|
+
(t.typ == EnumToken.NumberTokenType && t.val < 1)) {
|
|
349
354
|
// @ts-ignore
|
|
350
|
-
a = (t.typ ==
|
|
355
|
+
a = (t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val);
|
|
351
356
|
}
|
|
352
357
|
}
|
|
353
358
|
return `#${hsl2rgb(h, s, l, a).reduce((acc, curr) => acc + curr.toString(16).padStart(2, '0'), '')}`;
|
|
@@ -359,21 +364,21 @@ function hwb2hex(token) {
|
|
|
359
364
|
// @ts-ignore
|
|
360
365
|
t = token.chi[2];
|
|
361
366
|
// @ts-ignore
|
|
362
|
-
let white = t.typ ==
|
|
367
|
+
let white = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
|
|
363
368
|
// @ts-ignore
|
|
364
369
|
t = token.chi[4];
|
|
365
370
|
// @ts-ignore
|
|
366
|
-
let black = t.typ ==
|
|
371
|
+
let black = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
|
|
367
372
|
let a = null;
|
|
368
373
|
if (token.chi?.length == 7) {
|
|
369
374
|
// @ts-ignore
|
|
370
375
|
t = token.chi[6];
|
|
371
376
|
// @ts-ignore
|
|
372
|
-
if ((t.typ ==
|
|
377
|
+
if ((t.typ == EnumToken.PercentageTokenType && t.val < 100) ||
|
|
373
378
|
// @ts-ignore
|
|
374
|
-
(t.typ ==
|
|
379
|
+
(t.typ == EnumToken.NumberTokenType && t.val < 1)) {
|
|
375
380
|
// @ts-ignore
|
|
376
|
-
a = (t.typ ==
|
|
381
|
+
a = (t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val);
|
|
377
382
|
}
|
|
378
383
|
}
|
|
379
384
|
const rgb = hsl2rgb(h, 1, .5, a);
|
|
@@ -390,19 +395,19 @@ function cmyk2hex(token) {
|
|
|
390
395
|
// @ts-ignore
|
|
391
396
|
let t = token.chi[0];
|
|
392
397
|
// @ts-ignore
|
|
393
|
-
const c = t.typ ==
|
|
398
|
+
const c = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
|
|
394
399
|
// @ts-ignore
|
|
395
400
|
t = token.chi[2];
|
|
396
401
|
// @ts-ignore
|
|
397
|
-
const m = t.typ ==
|
|
402
|
+
const m = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
|
|
398
403
|
// @ts-ignore
|
|
399
404
|
t = token.chi[4];
|
|
400
405
|
// @ts-ignore
|
|
401
|
-
const y = t.typ ==
|
|
406
|
+
const y = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
|
|
402
407
|
// @ts-ignore
|
|
403
408
|
t = token.chi[6];
|
|
404
409
|
// @ts-ignore
|
|
405
|
-
const k = t.typ ==
|
|
410
|
+
const k = t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val;
|
|
406
411
|
const rgb = [
|
|
407
412
|
Math.round(255 * (1 - Math.min(1, c * (1 - k) + k))),
|
|
408
413
|
Math.round(255 * (1 - Math.min(1, m * (1 - k) + k))),
|
|
@@ -413,12 +418,12 @@ function cmyk2hex(token) {
|
|
|
413
418
|
// @ts-ignore
|
|
414
419
|
t = token.chi[8];
|
|
415
420
|
// @ts-ignore
|
|
416
|
-
rgb.push(Math.round(255 * (t.typ ==
|
|
421
|
+
rgb.push(Math.round(255 * (t.typ == EnumToken.PercentageTokenType ? t.val / 100 : t.val)));
|
|
417
422
|
}
|
|
418
423
|
return `#${rgb.reduce((acc, curr) => acc + curr.toString(16).padStart(2, '0'), '')}`;
|
|
419
424
|
}
|
|
420
425
|
function getAngle(token) {
|
|
421
|
-
if (token.typ ==
|
|
426
|
+
if (token.typ == EnumToken.AngleTokenType) {
|
|
422
427
|
switch (token.unit) {
|
|
423
428
|
case 'deg':
|
|
424
429
|
// @ts-ignore
|