@tbela99/css-parser 0.8.0 → 0.9.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.
Files changed (64) hide show
  1. package/.editorconfig +484 -0
  2. package/README.md +13 -8
  3. package/dist/index-umd-web.js +2451 -2554
  4. package/dist/index.cjs +2605 -2708
  5. package/dist/index.d.ts +73 -23
  6. package/dist/lib/ast/expand.js +29 -4
  7. package/dist/lib/ast/math/expression.js +1 -1
  8. package/dist/lib/ast/minify.js +31 -17
  9. package/dist/lib/ast/types.js +2 -0
  10. package/dist/lib/ast/walk.js +12 -0
  11. package/dist/lib/parser/declaration/map.js +59 -52
  12. package/dist/lib/parser/declaration/set.js +0 -12
  13. package/dist/lib/parser/parse.js +204 -139
  14. package/dist/lib/parser/tokenize.js +15 -3
  15. package/dist/lib/renderer/color/color.js +2 -2
  16. package/dist/lib/renderer/color/hex.js +1 -1
  17. package/dist/lib/renderer/color/hsl.js +1 -1
  18. package/dist/lib/renderer/color/hwb.js +2 -2
  19. package/dist/lib/renderer/color/lab.js +3 -2
  20. package/dist/lib/renderer/color/lch.js +1 -1
  21. package/dist/lib/renderer/color/oklab.js +2 -2
  22. package/dist/lib/renderer/color/oklch.js +1 -1
  23. package/dist/lib/renderer/color/p3.js +1 -1
  24. package/dist/lib/renderer/color/prophotoRgb.js +2 -2
  25. package/dist/lib/renderer/color/prophotorgb.js +2 -2
  26. package/dist/lib/renderer/color/rgb.js +1 -1
  27. package/dist/lib/renderer/color/srgb.js +2 -2
  28. package/dist/lib/renderer/color/utils/constants.js +1 -1
  29. package/dist/lib/renderer/color/xyz.js +2 -18
  30. package/dist/lib/renderer/color/xyzd50.js +20 -2
  31. package/dist/lib/renderer/render.js +37 -8
  32. package/dist/lib/renderer/sourcemap/sourcemap.js +1 -1
  33. package/dist/lib/syntax/syntax.js +337 -1
  34. package/dist/lib/validation/at-rules/container.js +353 -0
  35. package/dist/lib/validation/at-rules/counter-style.js +2 -2
  36. package/dist/lib/validation/at-rules/custom-media.js +52 -0
  37. package/dist/lib/validation/at-rules/document.js +40 -60
  38. package/dist/lib/validation/at-rules/else.js +5 -0
  39. package/dist/lib/validation/at-rules/font-feature-values.js +3 -0
  40. package/dist/lib/validation/at-rules/import.js +64 -59
  41. package/dist/lib/validation/at-rules/layer.js +3 -0
  42. package/dist/lib/validation/at-rules/media.js +118 -29
  43. package/dist/lib/validation/at-rules/supports.js +51 -20
  44. package/dist/lib/validation/at-rules/when.js +178 -0
  45. package/dist/lib/validation/atrule.js +25 -10
  46. package/dist/lib/validation/config.js +20 -15
  47. package/dist/lib/validation/config.json.js +242 -74
  48. package/dist/lib/validation/declaration.js +32 -10
  49. package/dist/lib/validation/parser/parse.js +87 -103
  50. package/dist/lib/validation/parser/types.js +2 -2
  51. package/dist/lib/validation/selector.js +6 -3
  52. package/dist/lib/validation/syntax.js +86 -6
  53. package/dist/lib/validation/syntaxes/complex-selector-list.js +16 -12
  54. package/dist/lib/validation/syntaxes/complex-selector.js +17 -247
  55. package/dist/lib/validation/syntaxes/compound-selector.js +226 -0
  56. package/dist/lib/validation/syntaxes/image.js +29 -0
  57. package/dist/lib/validation/syntaxes/keyframe-block-list.js +1 -1
  58. package/dist/lib/validation/syntaxes/keyframe-selector.js +1 -1
  59. package/dist/lib/validation/syntaxes/layer-name.js +5 -16
  60. package/dist/lib/validation/syntaxes/relative-selector-list.js +43 -13
  61. package/dist/lib/validation/utils/list.js +2 -2
  62. package/dist/node/index.js +1 -1
  63. package/dist/web/index.js +1 -1
  64. package/package.json +10 -9
@@ -16,7 +16,13 @@ function consumeWhiteSpace(parseInfo) {
16
16
  return count;
17
17
  }
18
18
  function pushToken(token, parseInfo, hint) {
19
- const result = { token, hint, position: { ...parseInfo.position }, bytesIn: parseInfo.currentPosition.ind + 1 };
19
+ const result = {
20
+ token,
21
+ len: parseInfo.currentPosition.ind - parseInfo.position.ind,
22
+ hint,
23
+ position: { ...parseInfo.position },
24
+ bytesIn: parseInfo.currentPosition.ind + 1
25
+ };
20
26
  parseInfo.position.ind = parseInfo.currentPosition.ind;
21
27
  parseInfo.position.lin = parseInfo.currentPosition.lin;
22
28
  parseInfo.position.col = Math.max(parseInfo.currentPosition.col, 1);
@@ -136,6 +142,10 @@ function next(parseInfo, count = 1) {
136
142
  }
137
143
  return char;
138
144
  }
145
+ /**
146
+ * tokenize css string
147
+ * @param stream
148
+ */
139
149
  function* tokenize(stream) {
140
150
  const parseInfo = {
141
151
  stream,
@@ -184,8 +194,10 @@ function* tokenize(stream) {
184
194
  buffer += value;
185
195
  }
186
196
  }
187
- yield pushToken(buffer, parseInfo, EnumToken.BadCommentTokenType);
188
- buffer = '';
197
+ if (buffer.length > 0) {
198
+ yield pushToken(buffer, parseInfo, EnumToken.BadCommentTokenType);
199
+ buffer = '';
200
+ }
189
201
  }
190
202
  break;
191
203
  case '&':
@@ -15,9 +15,9 @@ import { xyz2srgb, lsrgb2srgbvalues, srgb2lsrgbvalues, lch2srgb, oklab2srgb, lab
15
15
  import { prophotorgb2srgbvalues, srgb2prophotorgbvalues } from './prophotorgb.js';
16
16
  import { a98rgb2srgbvalues, srgb2a98values } from './a98rgb.js';
17
17
  import { rec20202srgb, srgb2rec2020values } from './rec2020.js';
18
- import { xyzd502srgb, srgb2xyz } from './xyz.js';
18
+ import { srgb2xyz } from './xyz.js';
19
19
  import { p32srgbvalues, srgb2p3values } from './p3.js';
20
- import { XYZ_D65_to_D50 } from './xyzd50.js';
20
+ import { xyzd502srgb, XYZ_D65_to_D50 } from './xyzd50.js';
21
21
  import '../sourcemap/lib/encode.js';
22
22
  import '../../parser/utils/config.js';
23
23
 
@@ -2,7 +2,7 @@ import { EnumToken } from '../../ast/types.js';
2
2
  import '../../ast/minify.js';
3
3
  import '../../ast/walk.js';
4
4
  import '../../parser/parse.js';
5
- import { getNumber, minmax } from './color.js';
5
+ import { minmax, getNumber } from './color.js';
6
6
  import { hsl2rgb, hwb2rgb, cmyk2rgb, oklab2rgb, oklch2rgb, lab2rgb, lch2rgb } from './rgb.js';
7
7
  import { NAMES_COLORS } from './utils/constants.js';
8
8
  import { getComponents } from './utils/components.js';
@@ -1,6 +1,6 @@
1
1
  import { hwb2hsv } from './hsv.js';
2
2
  import { getNumber } from './color.js';
3
- import { hex2rgb, lab2rgb, lch2rgb, oklab2rgb, oklch2rgb } from './rgb.js';
3
+ import { lch2rgb, lab2rgb, oklch2rgb, oklab2rgb, hex2rgb } from './rgb.js';
4
4
  import './utils/constants.js';
5
5
  import { getComponents } from './utils/components.js';
6
6
  import { EnumToken } from '../../ast/types.js';
@@ -1,12 +1,12 @@
1
1
  import { hsl2hsv } from './hsv.js';
2
2
  import './utils/constants.js';
3
3
  import { getComponents } from './utils/components.js';
4
- import { getNumber, getAngle } from './color.js';
4
+ import { getAngle, getNumber } from './color.js';
5
5
  import { EnumToken } from '../../ast/types.js';
6
6
  import '../../ast/minify.js';
7
7
  import '../../ast/walk.js';
8
8
  import '../../parser/parse.js';
9
- import { lab2srgb, lch2srgb, oklab2srgb, oklch2srgb } from './srgb.js';
9
+ import { lch2srgb, lab2srgb, oklch2srgb, oklab2srgb } from './srgb.js';
10
10
  import '../sourcemap/lib/encode.js';
11
11
  import '../../parser/utils/config.js';
12
12
 
@@ -1,7 +1,7 @@
1
1
  import { e, k, D50 } from './utils/constants.js';
2
2
  import { getComponents } from './utils/components.js';
3
- import { srgb2xyz, xyzd502srgb } from './xyz.js';
4
- import { hex2srgb, rgb2srgb, hsl2srgb, hwb2srgb, oklch2srgb } from './srgb.js';
3
+ import { srgb2xyz } from './xyz.js';
4
+ import { oklch2srgb, hwb2srgb, hsl2srgb, rgb2srgb, hex2srgb } from './srgb.js';
5
5
  import { getLCHComponents } from './lch.js';
6
6
  import { OKLab_to_XYZ, getOKLABComponents } from './oklab.js';
7
7
  import { getNumber } from './color.js';
@@ -9,6 +9,7 @@ import { EnumToken } from '../../ast/types.js';
9
9
  import '../../ast/minify.js';
10
10
  import '../../ast/walk.js';
11
11
  import '../../parser/parse.js';
12
+ import { xyzd502srgb } from './xyzd50.js';
12
13
  import '../sourcemap/lib/encode.js';
13
14
  import '../../parser/utils/config.js';
14
15
 
@@ -5,7 +5,7 @@ import { EnumToken } from '../../ast/types.js';
5
5
  import '../../ast/minify.js';
6
6
  import '../../ast/walk.js';
7
7
  import '../../parser/parse.js';
8
- import { srgb2lab, xyz2lab, hex2lab, rgb2lab, hsl2lab, hwb2lab, getLABComponents, oklab2lab, oklch2lab } from './lab.js';
8
+ import { srgb2lab, xyz2lab, oklch2lab, oklab2lab, getLABComponents, hwb2lab, hsl2lab, rgb2lab, hex2lab } from './lab.js';
9
9
  import '../sourcemap/lib/encode.js';
10
10
  import '../../parser/utils/config.js';
11
11
 
@@ -1,7 +1,7 @@
1
1
  import { multiplyMatrices } from './utils/matrix.js';
2
2
  import './utils/constants.js';
3
3
  import { getComponents } from './utils/components.js';
4
- import { srgb2lsrgbvalues, hex2srgb, rgb2srgb, hsl2srgb, hwb2srgb, lab2srgb, lch2srgb, lsrgb2srgbvalues } from './srgb.js';
4
+ import { srgb2lsrgbvalues, lch2srgb, lab2srgb, hwb2srgb, hsl2srgb, rgb2srgb, hex2srgb, 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';
@@ -106,7 +106,7 @@ function OKLab_to_sRGB(l, a, b) {
106
106
  1.2914855378640917399 * b, 3);
107
107
  return lsrgb2srgbvalues(
108
108
  /* r: */
109
- +4.076741661347994 * L -
109
+ 4.076741661347994 * L -
110
110
  3.307711590408193 * M +
111
111
  0.230969928729428 * S,
112
112
  /* g: */
@@ -6,7 +6,7 @@ import '../../ast/minify.js';
6
6
  import '../../ast/walk.js';
7
7
  import '../../parser/parse.js';
8
8
  import { lab2lchvalues } from './lch.js';
9
- import { srgb2oklab, hex2oklab, rgb2oklab, hsl2oklab, hwb2oklab, lab2oklab, lch2oklab, getOKLABComponents } from './oklab.js';
9
+ import { srgb2oklab, lch2oklab, getOKLABComponents, lab2oklab, hwb2oklab, hsl2oklab, rgb2oklab, hex2oklab } from './oklab.js';
10
10
  import '../sourcemap/lib/encode.js';
11
11
  import '../../parser/utils/config.js';
12
12
 
@@ -1,4 +1,4 @@
1
- import { xyz2srgb, srgb2lsrgbvalues, lsrgb2srgbvalues } from './srgb.js';
1
+ import { xyz2srgb, lsrgb2srgbvalues, srgb2lsrgbvalues } from './srgb.js';
2
2
  import { multiplyMatrices } from './utils/matrix.js';
3
3
  import './utils/constants.js';
4
4
  import '../../ast/types.js';
@@ -1,5 +1,5 @@
1
- import { xyzd502srgb, srgb2xyz } from './xyz.js';
2
- import { XYZ_D65_to_D50 } from './xyzd50.js';
1
+ import { srgb2xyz } from './xyz.js';
2
+ import { XYZ_D65_to_D50, xyzd502srgb } from './xyzd50.js';
3
3
 
4
4
  function prophotorgb2srgbvalues(r, g, b, a = null) {
5
5
  // @ts-ignore
@@ -1,5 +1,5 @@
1
- import { xyzd502srgb, srgb2xyz } from './xyz.js';
2
- import { XYZ_D65_to_D50 } from './xyzd50.js';
1
+ import { srgb2xyz } from './xyz.js';
2
+ import { XYZ_D65_to_D50, xyzd502srgb } from './xyzd50.js';
3
3
 
4
4
  function prophotorgb2srgbvalues(r, g, b, a = null) {
5
5
  // @ts-ignore
@@ -5,7 +5,7 @@ import '../../ast/minify.js';
5
5
  import '../../ast/walk.js';
6
6
  import '../../parser/parse.js';
7
7
  import { expandHexValue } from './hex.js';
8
- import { hwb2srgb, hslvalues, hsl2srgbvalues, cmyk2srgb, oklab2srgb, oklch2srgb, lab2srgb, lch2srgb } from './srgb.js';
8
+ import { hslvalues, hsl2srgbvalues, hwb2srgb, cmyk2srgb, oklab2srgb, oklch2srgb, lab2srgb, lch2srgb } from './srgb.js';
9
9
  import '../sourcemap/lib/encode.js';
10
10
  import '../../parser/utils/config.js';
11
11
 
@@ -6,8 +6,8 @@ import '../../ast/minify.js';
6
6
  import '../../ast/walk.js';
7
7
  import '../../parser/parse.js';
8
8
  import { expandHexValue } from './hex.js';
9
- import { lch2labvalues, getLABComponents, Lab_to_sRGB } from './lab.js';
10
- import { getOKLABComponents, OKLab_to_sRGB } from './oklab.js';
9
+ import { lch2labvalues, Lab_to_sRGB, getLABComponents } from './lab.js';
10
+ import { OKLab_to_sRGB, getOKLABComponents } from './oklab.js';
11
11
  import { getLCHComponents } from './lch.js';
12
12
  import { getOKLCHComponents } from './oklch.js';
13
13
  import { XYZ_to_lin_sRGB } from './xyz.js';
@@ -28,7 +28,7 @@ const colorRange = {
28
28
  }
29
29
  };
30
30
  const colorFuncColorSpace = ['srgb', 'srgb-linear', 'display-p3', 'prophoto-rgb', 'a98-rgb', 'rec2020', 'xyz', 'xyz-d65', 'xyz-d50'];
31
- ({ typ: EnumToken.IdenTokenType, val: 'none' });
31
+ ({ typ: EnumToken.IdenTokenType});
32
32
  const D50 = [0.3457 / 0.3585, 1.00000, (1.0 - 0.3457 - 0.3585) / 0.3585];
33
33
  const k = Math.pow(29, 3) / Math.pow(3, 3);
34
34
  const e = Math.pow(6, 3) / Math.pow(29, 3);
@@ -4,26 +4,10 @@ import '../../ast/types.js';
4
4
  import '../../ast/minify.js';
5
5
  import '../../ast/walk.js';
6
6
  import '../../parser/parse.js';
7
- import { lsrgb2srgbvalues, srgb2lsrgbvalues } from './srgb.js';
7
+ import { srgb2lsrgbvalues } from './srgb.js';
8
8
  import '../sourcemap/lib/encode.js';
9
9
  import '../../parser/utils/config.js';
10
10
 
11
- function xyzd502srgb(x, y, z) {
12
- // @ts-ignore
13
- return lsrgb2srgbvalues(
14
- /* r: */
15
- x * 3.1341359569958707 -
16
- y * 1.6173863321612538 -
17
- 0.4906619460083532 * z,
18
- /* g: */
19
- x * -0.978795502912089 +
20
- y * 1.916254567259524 +
21
- 0.03344273116131949 * z,
22
- /* b: */
23
- x * 0.07195537988411677 -
24
- y * 0.2289768264158322 +
25
- 1.405386058324125 * z);
26
- }
27
11
  function XYZ_to_lin_sRGB(x, y, z) {
28
12
  // convert XYZ to linear-light sRGB
29
13
  const M = [
@@ -63,4 +47,4 @@ function srgb2xyz(r, g, b, alpha) {
63
47
  return rgb;
64
48
  }
65
49
 
66
- export { XYZ_D50_to_D65, XYZ_to_lin_sRGB, srgb2xyz, xyzd502srgb };
50
+ export { XYZ_D50_to_D65, XYZ_to_lin_sRGB, srgb2xyz };
@@ -1,3 +1,4 @@
1
+ import { lsrgb2srgbvalues } from './srgb.js';
1
2
  import { multiplyMatrices } from './utils/matrix.js';
2
3
  import './utils/constants.js';
3
4
  import '../../ast/types.js';
@@ -10,11 +11,12 @@ import { XYZ_D50_to_D65 } from './xyz.js';
10
11
  import '../sourcemap/lib/encode.js';
11
12
  import '../../parser/utils/config.js';
12
13
 
14
+ /*
15
+ */
13
16
  function xyzd502lch(x, y, z, alpha) {
14
17
  // @ts-ignore
15
18
  const [l, a, b] = xyz2lab(...XYZ_D50_to_D65(x, y, z));
16
19
  // L in range [0,100]. For use in CSS, add a percent
17
- // @ts-ignore
18
20
  return lab2lchvalues(l, a, b, alpha);
19
21
  }
20
22
  function XYZ_D65_to_D50(x, y, z) {
@@ -31,5 +33,21 @@ function XYZ_D65_to_D50(x, y, z) {
31
33
  ];
32
34
  return multiplyMatrices(M, [x, y, z]);
33
35
  }
36
+ function xyzd502srgb(x, y, z) {
37
+ // @ts-ignore
38
+ return lsrgb2srgbvalues(
39
+ /* r: */
40
+ x * 3.1341359569958707 -
41
+ y * 1.6173863321612538 -
42
+ 0.4906619460083532 * z,
43
+ /* g: */
44
+ x * -0.978795502912089 +
45
+ y * 1.916254567259524 +
46
+ 0.03344273116131949 * z,
47
+ /* b: */
48
+ x * 0.07195537988411677 -
49
+ y * 0.2289768264158322 +
50
+ 1.405386058324125 * z);
51
+ }
34
52
 
35
- export { XYZ_D65_to_D50, xyzd502lch };
53
+ export { XYZ_D65_to_D50, xyzd502lch, xyzd502srgb };
@@ -9,7 +9,7 @@ import { expand } from '../ast/expand.js';
9
9
  import { colorMix } from './color/colormix.js';
10
10
  import { parseRelativeColor } from './color/relativecolor.js';
11
11
  import { SourceMap } from './sourcemap/sourcemap.js';
12
- import { isColor, mathFuncs, isNewLine } from '../syntax/syntax.js';
12
+ import { isColor, pseudoElements, mathFuncs, isNewLine } from '../syntax/syntax.js';
13
13
 
14
14
  const colorsFunc = ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk', 'color-mix', 'color', 'oklab', 'lab', 'oklch', 'lch', 'light-dark'];
15
15
  function reduceNumber(val) {
@@ -41,17 +41,27 @@ function update(position, str) {
41
41
  }
42
42
  }
43
43
  }
44
+ /**
45
+ * render ast
46
+ * @param data
47
+ * @param options
48
+ */
44
49
  function doRender(data, options = {}) {
50
+ const minify = options.minify ?? true;
51
+ const beautify = options.beautify ?? !minify;
45
52
  options = {
46
- ...(options.minify ?? true ? {
53
+ ...(beautify ? {
54
+ indent: ' ',
55
+ newLine: '\n',
56
+ } : {
47
57
  indent: '',
48
58
  newLine: '',
59
+ }),
60
+ ...(minify ? {
49
61
  removeEmpty: true,
50
62
  removeComments: true
51
63
  } : {
52
- indent: ' ',
53
- newLine: '\n',
54
- compress: false,
64
+ removeEmpty: false,
55
65
  removeComments: false,
56
66
  }), sourcemap: false, convertColor: true, expandNestingRules: false, preserveLicense: false, ...options
57
67
  };
@@ -119,7 +129,18 @@ function updateSourceMap(node, options, cache, sourcemap, position, str) {
119
129
  }
120
130
  update(position, str);
121
131
  }
122
- // @ts-ignore
132
+ /**
133
+ * render ast node
134
+ * @param data
135
+ * @param options
136
+ * @param sourcemap
137
+ * @param position
138
+ * @param errors
139
+ * @param reducer
140
+ * @param cache
141
+ * @param level
142
+ * @param indents
143
+ */
123
144
  function renderAstNode(data, options, sourcemap, position, errors, reducer, cache, level = 0, indents = []) {
124
145
  if (indents.length < level + 1) {
125
146
  indents.push(options.indent.repeat(level));
@@ -212,8 +233,15 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
212
233
  // return renderToken(data as Token, options, cache, reducer, errors);
213
234
  throw new Error(`render: unexpected token ${JSON.stringify(data, null, 1)}`);
214
235
  }
215
- return '';
216
236
  }
237
+ /**
238
+ * render ast token
239
+ * @param token
240
+ * @param options
241
+ * @param cache
242
+ * @param reducer
243
+ * @param errors
244
+ */
217
245
  function renderToken(token, options = {}, cache = Object.create(null), reducer, errors) {
218
246
  if (reducer == null) {
219
247
  reducer = function (acc, curr) {
@@ -561,8 +589,9 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
561
589
  return '';
562
590
  }
563
591
  case EnumToken.PseudoClassTokenType:
592
+ case EnumToken.PseudoElementTokenType:
564
593
  // https://www.w3.org/TR/selectors-4/#single-colon-pseudos
565
- if (token.typ == EnumToken.PseudoClassTokenType && ['::before', '::after', '::first-line', '::first-letter'].includes(token.val)) {
594
+ if (token.typ == EnumToken.PseudoElementTokenType && pseudoElements.includes(token.val.slice(1))) {
566
595
  return token.val.slice(1);
567
596
  }
568
597
  case EnumToken.UrlTokenTokenType:
@@ -1,11 +1,11 @@
1
1
  import { encode } from './lib/encode.js';
2
2
 
3
3
  class SourceMap {
4
+ lastLocation = null;
4
5
  #version = 3;
5
6
  #sources = [];
6
7
  #map = new Map;
7
8
  #line = -1;
8
- lastLocation = null;
9
9
  add(source, original) {
10
10
  if (original.src !== '') {
11
11
  if (!this.#sources.includes(original.src)) {