@tbela99/css-parser 0.0.1 → 0.2.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 +318 -34
- package/dist/config.json.js +386 -4
- package/dist/index-umd-web.js +3260 -1563
- package/dist/index.cjs +3259 -1564
- package/dist/index.d.ts +687 -536
- package/dist/lib/ast/expand.js +14 -14
- package/dist/lib/ast/features/calc.js +225 -0
- package/dist/lib/ast/features/index.js +3 -0
- package/dist/lib/ast/features/inlinecssvariables.js +130 -0
- package/dist/lib/ast/features/shorthand.js +46 -0
- package/dist/lib/ast/features/utils/math.js +95 -0
- package/dist/lib/ast/minify.js +401 -372
- package/dist/lib/ast/types.js +101 -0
- package/dist/lib/ast/utils/minifyfeature.js +8 -0
- package/dist/lib/ast/walk.js +37 -9
- package/dist/lib/iterable/set.js +48 -0
- package/dist/lib/iterable/weakmap.js +53 -0
- package/dist/lib/parser/declaration/list.js +18 -4
- package/dist/lib/parser/declaration/map.js +102 -33
- package/dist/lib/parser/declaration/set.js +18 -12
- package/dist/lib/parser/parse.js +661 -421
- package/dist/lib/parser/tokenize.js +82 -46
- package/dist/lib/parser/utils/syntax.js +13 -10
- package/dist/lib/parser/utils/type.js +23 -6
- package/dist/lib/renderer/render.js +253 -84
- 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 +30 -15
- package/dist/web/index.js +36 -19
- package/package.json +9 -6
- package/dist/lib/transform.js +0 -24
|
@@ -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
|
package/dist/node/index.js
CHANGED
|
@@ -1,23 +1,38 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { EnumToken } from '../lib/ast/types.js';
|
|
2
|
+
export { minify } from '../lib/ast/minify.js';
|
|
2
3
|
export { walk, walkValues } from '../lib/ast/walk.js';
|
|
3
|
-
export { expand
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export { funcList, matchType } from '../lib/parser/utils/type.js';
|
|
11
|
-
import { transform as transform$1 } from '../lib/transform.js';
|
|
4
|
+
export { expand } from '../lib/ast/expand.js';
|
|
5
|
+
import { doRender } from '../lib/renderer/render.js';
|
|
6
|
+
export { renderToken } from '../lib/renderer/render.js';
|
|
7
|
+
import { doParse } from '../lib/parser/parse.js';
|
|
8
|
+
export { parseString, parseTokens } from '../lib/parser/parse.js';
|
|
9
|
+
import '../lib/renderer/utils/color.js';
|
|
10
|
+
import { resolve, dirname } from '../lib/fs/resolve.js';
|
|
12
11
|
import { load } from './load.js';
|
|
13
|
-
import { resolve } from '../lib/fs/resolve.js';
|
|
14
|
-
export { dirname, matchUrl } from '../lib/fs/resolve.js';
|
|
15
12
|
|
|
13
|
+
function render(data, options = {}) {
|
|
14
|
+
return doRender(data, Object.assign(options, { load, resolve, dirname, cwd: options.cwd ?? process.cwd() }));
|
|
15
|
+
}
|
|
16
16
|
async function parse(iterator, opt = {}) {
|
|
17
|
-
return
|
|
17
|
+
return doParse(iterator, Object.assign(opt, { load, resolve, dirname, cwd: opt.cwd ?? process.cwd() }));
|
|
18
18
|
}
|
|
19
19
|
async function transform(css, options = {}) {
|
|
20
|
-
|
|
20
|
+
options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
|
|
21
|
+
const startTime = performance.now();
|
|
22
|
+
return parse(css, options).then((parseResult) => {
|
|
23
|
+
const rendered = render(parseResult.ast, options);
|
|
24
|
+
return {
|
|
25
|
+
...parseResult,
|
|
26
|
+
...rendered,
|
|
27
|
+
errors: parseResult.errors.concat(rendered.errors),
|
|
28
|
+
stats: {
|
|
29
|
+
bytesOut: rendered.code.length,
|
|
30
|
+
...parseResult.stats,
|
|
31
|
+
render: rendered.stats.total,
|
|
32
|
+
total: `${(performance.now() - startTime).toFixed(2)}ms`
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
});
|
|
21
36
|
}
|
|
22
37
|
|
|
23
|
-
export { load, parse, resolve, transform };
|
|
38
|
+
export { dirname, load, parse, render, resolve, transform };
|
package/dist/web/index.js
CHANGED
|
@@ -1,31 +1,48 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { EnumToken } from '../lib/ast/types.js';
|
|
2
|
+
export { minify } from '../lib/ast/minify.js';
|
|
2
3
|
export { walk, walkValues } from '../lib/ast/walk.js';
|
|
3
|
-
export { expand
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export {
|
|
8
|
-
|
|
9
|
-
export { getConfig } from '../lib/parser/utils/config.js';
|
|
10
|
-
export { funcList, matchType } from '../lib/parser/utils/type.js';
|
|
11
|
-
import { transform as transform$1 } from '../lib/transform.js';
|
|
12
|
-
import { load } from './load.js';
|
|
4
|
+
export { expand } from '../lib/ast/expand.js';
|
|
5
|
+
import { doRender } from '../lib/renderer/render.js';
|
|
6
|
+
export { renderToken } from '../lib/renderer/render.js';
|
|
7
|
+
import { doParse } from '../lib/parser/parse.js';
|
|
8
|
+
export { parseString, parseTokens } from '../lib/parser/parse.js';
|
|
9
|
+
import '../lib/renderer/utils/color.js';
|
|
13
10
|
import { resolve, dirname } from '../lib/fs/resolve.js';
|
|
14
|
-
|
|
11
|
+
import { load } from './load.js';
|
|
15
12
|
|
|
16
|
-
|
|
17
|
-
return
|
|
13
|
+
function render(data, options = {}) {
|
|
14
|
+
return doRender(data, Object.assign(options, {
|
|
18
15
|
load,
|
|
19
16
|
resolve,
|
|
20
|
-
|
|
17
|
+
dirname,
|
|
18
|
+
cwd: options.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
|
|
21
19
|
}));
|
|
22
20
|
}
|
|
23
|
-
async function
|
|
24
|
-
return
|
|
21
|
+
async function parse(iterator, opt = {}) {
|
|
22
|
+
return doParse(iterator, Object.assign(opt, {
|
|
25
23
|
load,
|
|
26
24
|
resolve,
|
|
27
|
-
|
|
25
|
+
dirname,
|
|
26
|
+
cwd: opt.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
|
|
28
27
|
}));
|
|
29
28
|
}
|
|
29
|
+
async function transform(css, options = {}) {
|
|
30
|
+
options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
|
|
31
|
+
const startTime = performance.now();
|
|
32
|
+
return parse(css, options).then((parseResult) => {
|
|
33
|
+
const rendered = render(parseResult.ast, options);
|
|
34
|
+
return {
|
|
35
|
+
...parseResult,
|
|
36
|
+
...rendered,
|
|
37
|
+
errors: parseResult.errors.concat(rendered.errors),
|
|
38
|
+
stats: {
|
|
39
|
+
bytesOut: rendered.code.length,
|
|
40
|
+
...parseResult.stats,
|
|
41
|
+
render: rendered.stats.total,
|
|
42
|
+
total: `${(performance.now() - startTime).toFixed(2)}ms`
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
}
|
|
30
47
|
|
|
31
|
-
export { dirname, load, parse, resolve, transform };
|
|
48
|
+
export { dirname, load, parse, render, resolve, transform };
|
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.0
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dist/node/index.js",
|
|
7
7
|
"./umd": "./dist/index-umd-web.js",
|
|
@@ -11,11 +11,12 @@
|
|
|
11
11
|
"type": "module",
|
|
12
12
|
"typings": "dist/index.d.ts",
|
|
13
13
|
"scripts": {
|
|
14
|
-
"build": "rollup -c",
|
|
15
|
-
"test": "web-test-runner \"test/**/*.web
|
|
16
|
-
"test:cov": "
|
|
14
|
+
"build": "rollup -c;./build.sh dist/index.d.ts 'declare interface' 'declare type'",
|
|
15
|
+
"test": "web-test-runner \"test/**/*.web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.; mocha --reporter-options='maxDiffSize=1801920' \"test/**/*.node.spec.js\"",
|
|
16
|
+
"test:cov": "c8 --reporter=html --reporter=text --reporter=json-summary mocha --reporter-options='maxDiffSize=1801920' \"test/**/*.node.spec.js\"",
|
|
17
|
+
"test:web-cov": "web-test-runner --playwright --browsers chromium firefox webkit \"test/**/*.web.spec.js\" --node-resolve --root-dir=. --coverage",
|
|
17
18
|
"profile": "node --inspect-brk test/inspect.mjs",
|
|
18
|
-
"debug": "web-test-runner \"test/**/*.web.js\" --manual --open --node-resolve --root-dir=."
|
|
19
|
+
"debug": "web-test-runner \"test/**/*.web.spec.js\" --manual --open --node-resolve --root-dir=."
|
|
19
20
|
},
|
|
20
21
|
"repository": {
|
|
21
22
|
"type": "git",
|
|
@@ -33,7 +34,8 @@
|
|
|
33
34
|
"browser",
|
|
34
35
|
"css-nesting",
|
|
35
36
|
"css-compiler",
|
|
36
|
-
"nested-css"
|
|
37
|
+
"nested-css",
|
|
38
|
+
"walker"
|
|
37
39
|
],
|
|
38
40
|
"author": "Thierry Bela",
|
|
39
41
|
"license": "MIT OR LGPL-3.0",
|
|
@@ -51,6 +53,7 @@
|
|
|
51
53
|
"@types/mocha": "^10.0.1",
|
|
52
54
|
"@types/node": "^20.4.10",
|
|
53
55
|
"@web/test-runner": "^0.17.0",
|
|
56
|
+
"@web/test-runner-playwright": "^0.11.0",
|
|
54
57
|
"c8": "^8.0.1",
|
|
55
58
|
"mocha": "^10.2.0",
|
|
56
59
|
"rollup": "^3.28.0",
|
package/dist/lib/transform.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { parse } from './parser/parse.js';
|
|
2
|
-
import { render } from './renderer/render.js';
|
|
3
|
-
import './renderer/utils/color.js';
|
|
4
|
-
|
|
5
|
-
async function transform(css, options = {}) {
|
|
6
|
-
options = { minify: true, removeEmpty: true, ...options };
|
|
7
|
-
const startTime = performance.now();
|
|
8
|
-
return parse(css, options).then((parseResult) => {
|
|
9
|
-
const rendered = render(parseResult.ast, options);
|
|
10
|
-
return {
|
|
11
|
-
...parseResult,
|
|
12
|
-
...rendered,
|
|
13
|
-
errors: parseResult.errors.concat(rendered.errors),
|
|
14
|
-
stats: {
|
|
15
|
-
bytesOut: rendered.code.length,
|
|
16
|
-
...parseResult.stats,
|
|
17
|
-
render: rendered.stats.total,
|
|
18
|
-
total: `${(performance.now() - startTime).toFixed(2)}ms`
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export { transform };
|