@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.
@@ -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 == 'Perc' ? 255 * t.val / 100 : t.val).toString(16).padStart(2, '0');
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 == 'Number' && t.val < 1) ||
325
+ if ((t.typ == EnumToken.NumberTokenType && t.val < 1) ||
321
326
  // @ts-ignore
322
- (t.typ == 'Perc' && t.val < 100)) {
327
+ (t.typ == EnumToken.PercentageTokenType && t.val < 100)) {
323
328
  // @ts-ignore
324
- value += Math.round(255 * (t.typ == 'Perc' ? t.val / 100 : t.val)).toString(16).padStart(2, '0');
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 == 'Perc' ? t.val / 100 : t.val;
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 == 'Perc' ? t.val / 100 : t.val;
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 == 'Perc' && t.val < 100) ||
351
+ if ((t.typ == EnumToken.PercentageTokenType && t.val < 100) ||
347
352
  // @ts-ignore
348
- (t.typ == 'Number' && t.val < 1)) {
353
+ (t.typ == EnumToken.NumberTokenType && t.val < 1)) {
349
354
  // @ts-ignore
350
- a = (t.typ == 'Perc' ? t.val / 100 : t.val);
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 == 'Perc' ? t.val / 100 : t.val;
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 == 'Perc' ? t.val / 100 : t.val;
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 == 'Perc' && t.val < 100) ||
377
+ if ((t.typ == EnumToken.PercentageTokenType && t.val < 100) ||
373
378
  // @ts-ignore
374
- (t.typ == 'Number' && t.val < 1)) {
379
+ (t.typ == EnumToken.NumberTokenType && t.val < 1)) {
375
380
  // @ts-ignore
376
- a = (t.typ == 'Perc' ? t.val / 100 : t.val);
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 == 'Perc' ? t.val / 100 : t.val;
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 == 'Perc' ? t.val / 100 : t.val;
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 == 'Perc' ? t.val / 100 : t.val;
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 == 'Perc' ? t.val / 100 : t.val;
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 == 'Perc' ? t.val / 100 : t.val)));
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 == 'Angle') {
426
+ if (token.typ == EnumToken.AngleTokenType) {
422
427
  switch (token.unit) {
423
428
  case 'deg':
424
429
  // @ts-ignore
@@ -1,23 +1,38 @@
1
- export { combinators, hasDeclaration, minify, minifyRule, reduceSelector, splitRule } from '../lib/ast/minify.js';
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, replaceCompound } from '../lib/ast/expand.js';
4
- export { colorsFunc, render, renderToken } from '../lib/renderer/render.js';
5
- import { parse as parse$1 } from '../lib/parser/parse.js';
6
- export { parseString, urlTokenMatcher } from '../lib/parser/parse.js';
7
- export { tokenize } from '../lib/parser/tokenize.js';
8
- export { isAngle, isAtKeyword, isColor, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNonPrintable, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension } from '../lib/parser/utils/syntax.js';
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';
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 parse$1(iterator, Object.assign(opt, { load, resolve, cwd: opt.cwd ?? process.cwd() }));
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
- return transform$1(css, Object.assign(options, { load, resolve, cwd: options.cwd ?? process.cwd() }));
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 { combinators, hasDeclaration, minify, minifyRule, reduceSelector, splitRule } from '../lib/ast/minify.js';
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, replaceCompound } from '../lib/ast/expand.js';
4
- export { colorsFunc, render, renderToken } from '../lib/renderer/render.js';
5
- import { parse as parse$1 } from '../lib/parser/parse.js';
6
- export { parseString, urlTokenMatcher } from '../lib/parser/parse.js';
7
- export { tokenize } from '../lib/parser/tokenize.js';
8
- export { isAngle, isAtKeyword, isColor, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNonPrintable, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension } from '../lib/parser/utils/syntax.js';
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
- export { matchUrl } from '../lib/fs/resolve.js';
11
+ import { load } from './load.js';
15
12
 
16
- async function parse(iterator, opt = {}) {
17
- return parse$1(iterator, Object.assign(opt, {
13
+ function render(data, options = {}) {
14
+ return doRender(data, Object.assign(options, {
18
15
  load,
19
16
  resolve,
20
- cwd: opt.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
17
+ dirname,
18
+ cwd: options.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
21
19
  }));
22
20
  }
23
- async function transform(css, options = {}) {
24
- return transform$1(css, Object.assign(options, {
21
+ async function parse(iterator, opt = {}) {
22
+ return doParse(iterator, Object.assign(opt, {
25
23
  load,
26
24
  resolve,
27
- cwd: options.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
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.1",
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-spec.js\" --node-resolve --root-dir=.; mocha --reporter-options='maxDiffSize=1801920' \"test/**/*.spec.js\"",
16
- "test:cov": "web-test-runner \"test/**/*.web-spec.js\" --node-resolve --root-dir=. --coverage; c8 --reporter=html --reporter=text --reporter=json-summary mocha --reporter-options='maxDiffSize=1801920' \"test/**/*.spec.js\"",
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",
@@ -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 };