@tbela99/css-parser 0.4.0 → 0.5.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,12 +1,11 @@
1
1
  import { multiplyMatrices } from './utils/matrix.js';
2
- import { powerlessColorComponent } from './utils/constants.js';
2
+ import './utils/constants.js';
3
3
  import { getComponents } from './utils/components.js';
4
4
  import { srgb2lsrgbvalues, hex2srgb, rgb2srgb, hsl2srgb, hwb2srgb, lab2srgb, lch2srgb, lsrgb2srgbvalues } from './srgb.js';
5
5
  import { getNumber } from './color.js';
6
6
  import { EnumToken } from '../../ast/types.js';
7
7
  import '../../ast/minify.js';
8
8
  import '../../parser/parse.js';
9
- import { eq } from '../../parser/utils/eq.js';
10
9
  import { lch2labvalues } from './lab.js';
11
10
  import { getOKLCHComponents } from './oklch.js';
12
11
  import '../sourcemap/lib/encode.js';
@@ -66,7 +65,7 @@ function getOKLABComponents(token) {
66
65
  // @ts-ignore
67
66
  t = components[3];
68
67
  // @ts-ignore
69
- const alpha = t == null || eq(t, powerlessColorComponent) ? 1 : getNumber(t);
68
+ const alpha = t == null || (t.typ == EnumToken.IdenTokenType && t.val == 'none') ? 1 : getNumber(t);
70
69
  const rgb = [l, a, b];
71
70
  if (alpha != 1 && alpha != null) {
72
71
  rgb.push(alpha);
@@ -1,4 +1,4 @@
1
- import { powerlessColorComponent } from './utils/constants.js';
1
+ import './utils/constants.js';
2
2
  import { getComponents } from './utils/components.js';
3
3
  import { getNumber, getAngle } from './color.js';
4
4
  import { EnumToken } from '../../ast/types.js';
@@ -6,7 +6,6 @@ import '../../ast/minify.js';
6
6
  import '../../parser/parse.js';
7
7
  import { lab2lchvalues } from './lch.js';
8
8
  import { srgb2oklab, hex2oklab, rgb2oklab, hsl2oklab, hwb2oklab, lab2oklab, lch2oklab, getOKLABComponents } from './oklab.js';
9
- import { eq } from '../../parser/utils/eq.js';
10
9
  import '../sourcemap/lib/encode.js';
11
10
 
12
11
  function hex2oklch(token) {
@@ -58,7 +57,7 @@ function getOKLCHComponents(token) {
58
57
  // @ts-ignore
59
58
  t = components[3];
60
59
  // @ts-ignore
61
- const alpha = t == null || eq(t, powerlessColorComponent) ? 1 : getNumber(t);
60
+ const alpha = t == null || (t.typ == EnumToken.IdenTokenType && t.val == 'none') ? 1 : getNumber(t);
62
61
  return [l, c, h, alpha];
63
62
  }
64
63
 
@@ -5,7 +5,6 @@ import { walkValues } from '../../ast/walk.js';
5
5
  import '../../parser/parse.js';
6
6
  import { reduceNumber } from '../render.js';
7
7
  import { colorRange } from './utils/constants.js';
8
- import { eq } from '../../parser/utils/eq.js';
9
8
  import { evaluate } from '../../ast/math/expression.js';
10
9
 
11
10
  function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
@@ -29,10 +28,7 @@ function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
29
28
  [names[1]]: getValue(g, converted, names[1]), // string,
30
29
  [names[2]]: getValue(b, converted, names[2]),
31
30
  // @ts-ignore
32
- alpha: alpha == null || eq(alpha, {
33
- typ: EnumToken.IdenTokenType,
34
- val: 'none'
35
- }) ? {
31
+ alpha: alpha == null || (alpha.typ == EnumToken.IdenTokenType && alpha.val == 'none') ? {
36
32
  typ: EnumToken.NumberTokenType,
37
33
  val: '1'
38
34
  } : (alpha.typ == EnumToken.PercentageTokenType ? {
@@ -45,7 +41,7 @@ function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
45
41
  [names[1]]: getValue(gExp, converted, names[1]),
46
42
  [names[2]]: getValue(bExp, converted, names[2]),
47
43
  // @ts-ignore
48
- alpha: getValue(aExp == null || eq(aExp, { typ: EnumToken.IdenTokenType, val: 'none' }) ? {
44
+ alpha: getValue(aExp == null || (aExp.typ == EnumToken.IdenTokenType && aExp.val == 'none') ? {
49
45
  typ: EnumToken.NumberTokenType,
50
46
  val: '1'
51
47
  } : aExp)
@@ -10,7 +10,6 @@ import { getOKLABComponents, OKLab_to_sRGB } from './oklab.js';
10
10
  import { getLCHComponents } from './lch.js';
11
11
  import { getOKLCHComponents } from './oklch.js';
12
12
  import { XYZ_to_lin_sRGB } from './xyz.js';
13
- import { eq } from '../../parser/utils/eq.js';
14
13
  import '../sourcemap/lib/encode.js';
15
14
 
16
15
  // from https://www.w3.org/TR/css-color-4/#color-conversion-code
@@ -43,10 +42,7 @@ function srgbvalues(token) {
43
42
  return null;
44
43
  }
45
44
  function rgb2srgb(token) {
46
- return getComponents(token).map((t, index) => index == 3 ? (eq(t, {
47
- typ: EnumToken.IdenTokenType,
48
- val: 'none'
49
- }) ? 1 : getNumber(t)) : (t.typ == EnumToken.PercentageTokenType ? 255 : 1) * getNumber(t) / 255);
45
+ return getComponents(token).map((t, index) => index == 3 ? ((t.typ == EnumToken.IdenTokenType && t.val == 'none') ? 1 : getNumber(t)) : (t.typ == EnumToken.PercentageTokenType ? 255 : 1) * getNumber(t) / 255);
50
46
  }
51
47
  function hex2srgb(token) {
52
48
  const value = expandHexValue(token.kin == 'lit' ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
@@ -26,7 +26,7 @@ const colorRange = {
26
26
  }
27
27
  };
28
28
  const colorFuncColorSpace = ['srgb', 'srgb-linear', 'display-p3', 'prophoto-rgb', 'a98-rgb', 'rec2020', 'xyz', 'xyz-d65', 'xyz-d50'];
29
- const powerlessColorComponent = { typ: EnumToken.IdenTokenType, val: 'none' };
29
+ ({ typ: EnumToken.IdenTokenType, val: 'none' });
30
30
  const D50 = [0.3457 / 0.3585, 1.00000, (1.0 - 0.3457 - 0.3585) / 0.3585];
31
31
  const k = Math.pow(29, 3) / Math.pow(3, 3);
32
32
  const e = Math.pow(6, 3) / Math.pow(29, 3);
@@ -188,4 +188,4 @@ const NAMES_COLORS = Object.seal(Object.entries(COLORS_NAMES).reduce((acc, [key,
188
188
  return acc;
189
189
  }, Object.create(null)));
190
190
 
191
- export { COLORS_NAMES, D50, NAMES_COLORS, colorFuncColorSpace, colorRange, e, k, powerlessColorComponent };
191
+ export { COLORS_NAMES, D50, NAMES_COLORS, colorFuncColorSpace, colorRange, e, k };
@@ -54,6 +54,19 @@ function doRender(data, options = {}) {
54
54
  removeComments: false,
55
55
  }), sourcemap: false, convertColor: true, expandNestingRules: false, preserveLicense: false, ...options
56
56
  };
57
+ if (options.withParents) {
58
+ // @ts-ignore
59
+ let parent = data.parent;
60
+ // @ts-ignore
61
+ while (data.parent != null) {
62
+ // @ts-ignore
63
+ parent = { ...data.parent, chi: [{ ...data }] };
64
+ // @ts-ignore
65
+ parent.parent = data.parent.parent;
66
+ // @ts-ignore
67
+ data = parent;
68
+ }
69
+ }
57
70
  const startTime = performance.now();
58
71
  const errors = [];
59
72
  const sourcemap = options.sourcemap ? new SourceMap : null;
@@ -120,7 +133,7 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
120
133
  return `${data.nam}:${options.indent}${data.val.reduce(reducer, '')}`;
121
134
  case EnumToken.CommentNodeType:
122
135
  case EnumToken.CDOCOMMNodeType:
123
- if (data.val.startsWith('# sourceMappingURL=')) {
136
+ if (data.val.startsWith('/*# sourceMappingURL=')) {
124
137
  // ignore sourcemap
125
138
  return '';
126
139
  }
@@ -132,12 +145,12 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
132
145
  return css;
133
146
  }
134
147
  if (css === '') {
135
- if (sourcemap != null) {
148
+ if (sourcemap != null && node.loc != null) {
136
149
  updateSourceMap(node, options, cache, sourcemap, position, str);
137
150
  }
138
151
  return str;
139
152
  }
140
- if (sourcemap != null) {
153
+ if (sourcemap != null && node.loc != null) {
141
154
  update(position, options.newLine);
142
155
  updateSourceMap(node, options, cache, sourcemap, position, str);
143
156
  }
package/dist/web/load.js CHANGED
@@ -13,8 +13,10 @@ async function load(url, currentFile) {
13
13
  if (matchUrl.test(currentFile)) {
14
14
  return fetch(new URL(url, currentFile)).then(parseResponse);
15
15
  }
16
+ const path = resolve(url, currentFile).absolute;
17
+ const t = new URL(path, self.origin);
16
18
  // return fetch(new URL(url, new URL(currentFile, self.location.href).href)).then(parseResponse);
17
- return fetch(resolve(url, currentFile).absolute).then(parseResponse);
19
+ return fetch(url, t.origin != self.location.origin ? { mode: 'cors' } : {}).then(parseResponse);
18
20
  }
19
21
 
20
22
  export { load };
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.4.0",
4
+ "version": "0.5.0",
5
5
  "exports": {
6
6
  ".": "./dist/node/index.js",
7
7
  "./umd": "./dist/index-umd-web.js",
@@ -13,9 +13,10 @@
13
13
  "scripts": {
14
14
  "build": "rollup -c;./build.sh dist/index.d.ts 'declare interface' 'declare type'",
15
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:node": "mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
16
17
  "test:cov": "c8 --reporter=html --reporter=text --reporter=json-summary mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
17
18
  "test:web-cov": "web-test-runner \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=. --coverage",
18
- "profile": "node --inspect-brk test/inspect.mjs",
19
+ "profile": "node --enable-source-maps --inspect-brk test/inspect.js",
19
20
  "debug": "web-test-runner \"test/**/web.spec.js\" --manual --open --node-resolve --root-dir=."
20
21
  },
21
22
  "repository": {
package/quickjs.sh DELETED
@@ -1 +0,0 @@
1
- qjsc -e -o c/css.c dist/index-umd-web.js