@tbela99/css-parser 0.9.1 → 1.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.
Files changed (99) hide show
  1. package/CHANGELOG.md +265 -0
  2. package/LICENSE.md +1 -1
  3. package/README.md +29 -17
  4. package/dist/index-umd-web.js +7461 -4360
  5. package/dist/index.cjs +8608 -5507
  6. package/dist/index.d.ts +203 -61
  7. package/dist/lib/ast/expand.js +2 -1
  8. package/dist/lib/ast/features/calc.js +19 -11
  9. package/dist/lib/ast/features/index.js +1 -0
  10. package/dist/lib/ast/features/inlinecssvariables.js +47 -29
  11. package/dist/lib/ast/features/prefix.js +117 -91
  12. package/dist/lib/ast/features/shorthand.js +34 -14
  13. package/dist/lib/ast/features/transform.js +67 -0
  14. package/dist/lib/ast/features/type.js +7 -0
  15. package/dist/lib/ast/math/expression.js +20 -10
  16. package/dist/lib/ast/math/math.js +20 -2
  17. package/dist/lib/ast/minify.js +209 -80
  18. package/dist/lib/ast/transform/compute.js +337 -0
  19. package/dist/lib/ast/transform/convert.js +33 -0
  20. package/dist/lib/ast/transform/matrix.js +112 -0
  21. package/dist/lib/ast/transform/minify.js +296 -0
  22. package/dist/lib/ast/transform/perspective.js +10 -0
  23. package/dist/lib/ast/transform/rotate.js +40 -0
  24. package/dist/lib/ast/transform/scale.js +32 -0
  25. package/dist/lib/ast/transform/skew.js +23 -0
  26. package/dist/lib/ast/transform/translate.js +32 -0
  27. package/dist/lib/ast/transform/utils.js +198 -0
  28. package/dist/lib/ast/types.js +18 -15
  29. package/dist/lib/ast/walk.js +54 -22
  30. package/dist/lib/fs/resolve.js +10 -0
  31. package/dist/lib/parser/declaration/list.js +48 -45
  32. package/dist/lib/parser/declaration/map.js +1 -0
  33. package/dist/lib/parser/declaration/set.js +2 -1
  34. package/dist/lib/parser/parse.js +449 -340
  35. package/dist/lib/parser/tokenize.js +147 -72
  36. package/dist/lib/parser/utils/declaration.js +5 -4
  37. package/dist/lib/parser/utils/type.js +2 -1
  38. package/dist/lib/renderer/color/a98rgb.js +2 -1
  39. package/dist/lib/renderer/color/{colormix.js → color-mix.js} +16 -7
  40. package/dist/lib/renderer/color/color.js +264 -170
  41. package/dist/lib/renderer/color/hex.js +19 -8
  42. package/dist/lib/renderer/color/hsl.js +9 -3
  43. package/dist/lib/renderer/color/hwb.js +2 -1
  44. package/dist/lib/renderer/color/lab.js +10 -1
  45. package/dist/lib/renderer/color/lch.js +10 -1
  46. package/dist/lib/renderer/color/oklab.js +10 -1
  47. package/dist/lib/renderer/color/oklch.js +10 -1
  48. package/dist/lib/renderer/color/p3.js +2 -1
  49. package/dist/lib/renderer/color/rec2020.js +2 -1
  50. package/dist/lib/renderer/color/relativecolor.js +27 -32
  51. package/dist/lib/renderer/color/rgb.js +14 -10
  52. package/dist/lib/renderer/color/srgb.js +48 -23
  53. package/dist/lib/renderer/color/utils/components.js +18 -6
  54. package/dist/lib/renderer/color/utils/constants.js +47 -3
  55. package/dist/lib/renderer/color/xyz.js +2 -1
  56. package/dist/lib/renderer/color/xyzd50.js +2 -1
  57. package/dist/lib/renderer/render.js +108 -43
  58. package/dist/lib/syntax/syntax.js +267 -136
  59. package/dist/lib/validation/at-rules/container.js +81 -103
  60. package/dist/lib/validation/at-rules/counter-style.js +9 -8
  61. package/dist/lib/validation/at-rules/custom-media.js +13 -15
  62. package/dist/lib/validation/at-rules/document.js +22 -27
  63. package/dist/lib/validation/at-rules/font-feature-values.js +8 -8
  64. package/dist/lib/validation/at-rules/import.js +30 -81
  65. package/dist/lib/validation/at-rules/keyframes.js +19 -23
  66. package/dist/lib/validation/at-rules/layer.js +5 -5
  67. package/dist/lib/validation/at-rules/media.js +42 -53
  68. package/dist/lib/validation/at-rules/namespace.js +19 -23
  69. package/dist/lib/validation/at-rules/page-margin-box.js +15 -18
  70. package/dist/lib/validation/at-rules/page.js +8 -7
  71. package/dist/lib/validation/at-rules/supports.js +73 -82
  72. package/dist/lib/validation/at-rules/when.js +32 -36
  73. package/dist/lib/validation/atrule.js +15 -18
  74. package/dist/lib/validation/config.js +24 -1
  75. package/dist/lib/validation/config.json.js +563 -63
  76. package/dist/lib/validation/parser/parse.js +196 -185
  77. package/dist/lib/validation/parser/types.js +1 -1
  78. package/dist/lib/validation/selector.js +8 -5
  79. package/dist/lib/validation/syntax.js +724 -1405
  80. package/dist/lib/validation/syntaxes/complex-selector-list.js +10 -11
  81. package/dist/lib/validation/syntaxes/complex-selector.js +10 -11
  82. package/dist/lib/validation/syntaxes/compound-selector.js +40 -50
  83. package/dist/lib/validation/syntaxes/family-name.js +9 -8
  84. package/dist/lib/validation/syntaxes/keyframe-block-list.js +6 -5
  85. package/dist/lib/validation/syntaxes/keyframe-selector.js +23 -105
  86. package/dist/lib/validation/syntaxes/layer-name.js +6 -5
  87. package/dist/lib/validation/syntaxes/relative-selector-list.js +7 -6
  88. package/dist/lib/validation/syntaxes/relative-selector.js +17 -15
  89. package/dist/lib/validation/syntaxes/url.js +18 -22
  90. package/dist/lib/validation/utils/list.js +20 -2
  91. package/dist/lib/validation/utils/whitespace.js +2 -1
  92. package/dist/node/index.js +4 -2
  93. package/dist/node/load.js +6 -1
  94. package/dist/web/index.js +4 -2
  95. package/dist/web/load.js +5 -0
  96. package/package.json +16 -15
  97. package/dist/lib/renderer/color/prophotoRgb.js +0 -56
  98. package/dist/lib/validation/declaration.js +0 -94
  99. package/dist/lib/validation/syntaxes/image.js +0 -29
@@ -1,30 +1,32 @@
1
1
  import { consumeWhitespace } from '../utils/whitespace.js';
2
- import { ValidationLevel } from '../../ast/types.js';
2
+ import '../../ast/types.js';
3
3
  import '../../ast/minify.js';
4
4
  import '../../ast/walk.js';
5
5
  import '../../parser/parse.js';
6
+ import '../../parser/tokenize.js';
7
+ import '../../parser/utils/config.js';
6
8
  import '../../renderer/color/utils/constants.js';
7
9
  import '../../renderer/sourcemap/lib/encode.js';
8
- import '../../parser/utils/config.js';
9
10
  import { validateSelector } from './selector.js';
10
11
  import { combinatorsTokens } from './complex-selector.js';
11
12
 
12
13
  function validateRelativeSelector(tokens, root, options) {
13
14
  tokens = tokens.slice();
14
15
  consumeWhitespace(tokens);
15
- if (tokens.length == 0) {
16
- // @ts-ignore
17
- return {
18
- valid: ValidationLevel.Drop,
19
- matches: [],
20
- // @ts-ignore
21
- node: root,
22
- // @ts-ignore
23
- syntax: null,
24
- error: 'expected selector',
25
- tokens
26
- };
27
- }
16
+ // if (tokens.length == 0) {
17
+ //
18
+ // // @ts-ignore
19
+ // return {
20
+ // valid: ValidationLevel.Drop,
21
+ // matches: [],
22
+ // // @ts-ignore
23
+ // node: root,
24
+ // // @ts-ignore
25
+ // syntax: null,
26
+ // error: 'expected selector',
27
+ // tokens
28
+ // }
29
+ // }
28
30
  // , EnumToken.DescendantCombinatorTokenType
29
31
  if (combinatorsTokens.includes(tokens[0].typ)) {
30
32
  tokens.shift();
@@ -1,35 +1,34 @@
1
- import { EnumToken, ValidationLevel } from '../../ast/types.js';
1
+ import { EnumToken, SyntaxValidationResult } from '../../ast/types.js';
2
2
  import '../../ast/minify.js';
3
3
  import '../../ast/walk.js';
4
4
  import '../../parser/parse.js';
5
+ import '../../parser/tokenize.js';
6
+ import '../../parser/utils/config.js';
5
7
  import '../../renderer/color/utils/constants.js';
6
8
  import '../../renderer/sourcemap/lib/encode.js';
7
- import '../../parser/utils/config.js';
8
9
  import { consumeWhitespace } from '../utils/whitespace.js';
9
10
 
10
11
  function validateURL(token) {
11
12
  if (token.typ == EnumToken.UrlTokenTokenType) {
12
13
  // @ts-ignore
13
14
  return {
14
- valid: ValidationLevel.Valid,
15
- matches: [],
15
+ valid: SyntaxValidationResult.Valid,
16
+ context: [],
16
17
  node: token,
17
18
  // @ts-ignore
18
19
  syntax: 'url()',
19
- error: '',
20
- tokens: []
20
+ error: ''
21
21
  };
22
22
  }
23
23
  if (token.typ != EnumToken.UrlFunctionTokenType) {
24
24
  // @ts-ignore
25
25
  return {
26
- valid: ValidationLevel.Drop,
27
- matches: [],
26
+ valid: SyntaxValidationResult.Drop,
27
+ context: [],
28
28
  node: token,
29
29
  // @ts-ignore
30
30
  syntax: 'url()',
31
- error: 'expected url()',
32
- tokens: []
31
+ error: 'expected url()'
33
32
  };
34
33
  }
35
34
  const children = token.chi.slice();
@@ -37,13 +36,12 @@ function validateURL(token) {
37
36
  if (children.length == 0 || ![EnumToken.UrlTokenTokenType, EnumToken.StringTokenType, EnumToken.HashTokenType].includes(children[0].typ)) {
38
37
  // @ts-ignore
39
38
  return {
40
- valid: ValidationLevel.Drop,
41
- matches: [],
39
+ valid: SyntaxValidationResult.Drop,
40
+ context: [],
42
41
  node: children[0] ?? token,
43
42
  // @ts-ignore
44
43
  syntax: 'url()',
45
- error: 'expected url-token',
46
- tokens: children
44
+ error: 'expected url-token'
47
45
  };
48
46
  }
49
47
  children.shift();
@@ -51,24 +49,22 @@ function validateURL(token) {
51
49
  if (children.length > 0) {
52
50
  // @ts-ignore
53
51
  return {
54
- valid: ValidationLevel.Drop,
55
- matches: [],
52
+ valid: SyntaxValidationResult.Drop,
53
+ context: [],
56
54
  node: children[0] ?? token,
57
55
  // @ts-ignore
58
56
  syntax: 'url()',
59
- error: 'unexpected token',
60
- tokens: children
57
+ error: 'unexpected token'
61
58
  };
62
59
  }
63
60
  // @ts-ignore
64
61
  return {
65
- valid: ValidationLevel.Valid,
66
- matches: [],
62
+ valid: SyntaxValidationResult.Valid,
63
+ context: [],
67
64
  node: token,
68
65
  // @ts-ignore
69
66
  syntax: 'url()',
70
- error: '',
71
- tokens: []
67
+ error: ''
72
68
  };
73
69
  }
74
70
 
@@ -2,10 +2,28 @@ 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 '../../parser/tokenize.js';
6
+ import '../../parser/utils/config.js';
5
7
  import '../../renderer/color/utils/constants.js';
6
8
  import '../../renderer/sourcemap/lib/encode.js';
7
- import '../../parser/utils/config.js';
8
9
 
10
+ function stripCommaToken(tokenList) {
11
+ let result = [];
12
+ let last = null;
13
+ for (let i = 0; i < tokenList.length; i++) {
14
+ if (tokenList[i].typ == EnumToken.CommaTokenType && last != null && last.typ == EnumToken.CommaTokenType) {
15
+ return null;
16
+ }
17
+ if (tokenList[i].typ != EnumToken.WhitespaceTokenType) {
18
+ last = tokenList[i];
19
+ }
20
+ if (tokenList[i].typ == EnumToken.CommentTokenType || tokenList[i].typ == EnumToken.CommaTokenType) {
21
+ continue;
22
+ }
23
+ result.push(tokenList[i]);
24
+ }
25
+ return result;
26
+ }
9
27
  function splitTokenList(tokenList, split = [EnumToken.CommaTokenType]) {
10
28
  return tokenList.reduce((acc, curr) => {
11
29
  if (curr.typ == EnumToken.CommentTokenType) {
@@ -21,4 +39,4 @@ function splitTokenList(tokenList, split = [EnumToken.CommaTokenType]) {
21
39
  }, [[]]);
22
40
  }
23
41
 
24
- export { splitTokenList };
42
+ export { splitTokenList, stripCommaToken };
@@ -2,9 +2,10 @@ 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 '../../parser/tokenize.js';
6
+ import '../../parser/utils/config.js';
5
7
  import '../../renderer/color/utils/constants.js';
6
8
  import '../../renderer/sourcemap/lib/encode.js';
7
- import '../../parser/utils/config.js';
8
9
 
9
10
  function consumeWhitespace(tokens) {
10
11
  if (tokens.length == 0) {
@@ -1,18 +1,20 @@
1
1
  import process from 'node:process';
2
- export { EnumToken } from '../lib/ast/types.js';
2
+ export { EnumToken, ValidationLevel } from '../lib/ast/types.js';
3
3
  export { minify } from '../lib/ast/minify.js';
4
4
  export { walk, walkValues } from '../lib/ast/walk.js';
5
5
  export { expand } from '../lib/ast/expand.js';
6
6
  import { doRender } from '../lib/renderer/render.js';
7
7
  export { renderToken } from '../lib/renderer/render.js';
8
+ import '../lib/renderer/color/utils/constants.js';
8
9
  import { doParse } from '../lib/parser/parse.js';
9
10
  export { parseString, parseTokens } from '../lib/parser/parse.js';
10
- import '../lib/renderer/color/utils/constants.js';
11
+ import '../lib/parser/tokenize.js';
11
12
  import '../lib/parser/utils/config.js';
12
13
  import '../lib/validation/config.js';
13
14
  import '../lib/validation/parser/types.js';
14
15
  import '../lib/validation/parser/parse.js';
15
16
  import '../lib/validation/syntaxes/complex-selector.js';
17
+ import '../lib/validation/syntax.js';
16
18
  import { dirname, resolve } from '../lib/fs/resolve.js';
17
19
  import { load } from './load.js';
18
20
 
package/dist/node/load.js CHANGED
@@ -7,7 +7,12 @@ function parseResponse(response) {
7
7
  }
8
8
  return response.text();
9
9
  }
10
- async function load(url, currentFile) {
10
+ /**
11
+ * load file
12
+ * @param url
13
+ * @param currentFile
14
+ */
15
+ async function load(url, currentFile = '.') {
11
16
  const resolved = resolve(url, currentFile);
12
17
  return matchUrl.test(resolved.absolute) ? fetch(resolved.absolute).then(parseResponse) : readFile(resolved.absolute, { encoding: 'utf-8' });
13
18
  }
package/dist/web/index.js CHANGED
@@ -1,17 +1,19 @@
1
- export { EnumToken } from '../lib/ast/types.js';
1
+ export { EnumToken, ValidationLevel } from '../lib/ast/types.js';
2
2
  export { minify } from '../lib/ast/minify.js';
3
3
  export { walk, walkValues } from '../lib/ast/walk.js';
4
4
  export { expand } from '../lib/ast/expand.js';
5
5
  import { doRender } from '../lib/renderer/render.js';
6
6
  export { renderToken } from '../lib/renderer/render.js';
7
+ import '../lib/renderer/color/utils/constants.js';
7
8
  import { doParse } from '../lib/parser/parse.js';
8
9
  export { parseString, parseTokens } from '../lib/parser/parse.js';
9
- import '../lib/renderer/color/utils/constants.js';
10
+ import '../lib/parser/tokenize.js';
10
11
  import '../lib/parser/utils/config.js';
11
12
  import '../lib/validation/config.js';
12
13
  import '../lib/validation/parser/types.js';
13
14
  import '../lib/validation/parser/parse.js';
14
15
  import '../lib/validation/syntaxes/complex-selector.js';
16
+ import '../lib/validation/syntax.js';
15
17
  import { dirname, resolve } from '../lib/fs/resolve.js';
16
18
  import { load } from './load.js';
17
19
 
package/dist/web/load.js CHANGED
@@ -6,6 +6,11 @@ function parseResponse(response) {
6
6
  }
7
7
  return response.text();
8
8
  }
9
+ /**
10
+ * load file
11
+ * @param url
12
+ * @param currentFile
13
+ */
9
14
  async function load(url, currentFile) {
10
15
  let t;
11
16
  if (matchUrl.test(url)) {
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": "v0.9.1",
4
+ "version": "v1.1.0",
5
5
  "exports": {
6
6
  ".": "./dist/node/index.js",
7
7
  "./node": "./dist/node/index.js",
@@ -17,9 +17,10 @@
17
17
  "scripts": {
18
18
  "build": "rollup -c;./build.sh dist/index.d.ts 'declare interface' 'declare type'",
19
19
  "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\"",
20
+ "test:web": "web-test-runner \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.",
20
21
  "test:node": "mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
21
- "test:cov": "c8 -x dist/lib/validation/syntax.js -x 'dist/lib/validation/parser/*.js' --reporter=html --reporter=text --reporter=json-summary mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
22
- "test:web-cov": "web-test-runner -x dist/lib/validation/syntax.js,dist/lib/validation/parser \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=. --coverage",
22
+ "test:cov": "c8 -x 'test/specs/**/*.js' -x dist/lib/validation/syntax.js -x 'dist/lib/validation/parser/*.js' --reporter=html --reporter=text --reporter=json-summary mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
23
+ "test:web-cov": "web-test-runner -x 'test/specs/**/*.js' -x dist/lib/validation/syntax.js,dist/lib/validation/parser \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=. --coverage",
23
24
  "profile": "node --enable-source-maps --inspect-brk test/inspect.js",
24
25
  "syntax-update": "esno tools/validation.ts",
25
26
  "debug": "web-test-runner \"test/**/web.spec.js\" --manual --open --node-resolve --root-dir=."
@@ -51,22 +52,22 @@
51
52
  "homepage": "https://github.com/tbela99/css-parser#readme",
52
53
  "devDependencies": {
53
54
  "@esm-bundle/chai": "^4.3.4-fix.0",
54
- "@rollup/plugin-commonjs": "^28.0.2",
55
+ "@rollup/plugin-commonjs": "^28.0.6",
55
56
  "@rollup/plugin-json": "^6.1.0",
56
- "@rollup/plugin-node-resolve": "^16.0.0",
57
- "@rollup/plugin-typescript": "^12.1.2",
58
- "@types/chai": "^5.0.1",
57
+ "@rollup/plugin-node-resolve": "^16.0.1",
58
+ "@rollup/plugin-typescript": "^12.1.4",
59
+ "@types/chai": "^5.2.2",
59
60
  "@types/mocha": "^10.0.10",
60
- "@types/node": "^22.13.5",
61
- "@types/web": "^0.0.206",
62
- "@web/test-runner": "^0.20.0",
63
- "@web/test-runner-playwright": "^0.11.0",
61
+ "@types/node": "^24.0.10",
62
+ "@types/web": "^0.0.245",
63
+ "@web/test-runner": "^0.20.2",
64
+ "@web/test-runner-playwright": "^0.11.1",
64
65
  "c8": "^10.1.3",
65
66
  "esno": "^4.8.0",
66
- "mocha": "^11.1.0",
67
- "playwright": "^1.50.1",
68
- "rollup": "^4.34.8",
69
- "rollup-plugin-dts": "^6.1.1",
67
+ "mocha": "^11.7.1",
68
+ "playwright": "^1.53.2",
69
+ "rollup": "^4.44.2",
70
+ "rollup-plugin-dts": "^6.2.1",
70
71
  "tslib": "^2.8.1"
71
72
  }
72
73
  }
@@ -1,56 +0,0 @@
1
- import { srgb2xyz } from './xyz.js';
2
- import { XYZ_D65_to_D50, xyzd502srgb } from './xyzd50.js';
3
-
4
- function prophotorgb2srgbvalues(r, g, b, a = null) {
5
- // @ts-ignore
6
- return xyzd502srgb(...prophotorgb2xyz50(r, g, b, a));
7
- }
8
- function srgb2prophotorgbvalues(r, g, b, a) {
9
- // @ts-ignore
10
- return xyz50_to_prophotorgb(...XYZ_D65_to_D50(...srgb2xyz(r, g, b, a)));
11
- }
12
- function prophotorgb2lin_ProPhoto(r, g, b, a = null) {
13
- return [r, g, b].map(v => {
14
- let abs = Math.abs(v);
15
- if (abs >= 16 / 512) {
16
- return Math.sign(v) * Math.pow(abs, 1.8);
17
- }
18
- return v / 16;
19
- }).concat(a == null || a == 1 ? [] : [a]);
20
- }
21
- function prophotorgb2xyz50(r, g, b, a = null) {
22
- [r, g, b, a] = prophotorgb2lin_ProPhoto(r, g, b, a);
23
- const xyz = [
24
- 0.7977666449006423 * r +
25
- 0.1351812974005331 * g +
26
- 0.0313477341283922 * b,
27
- 0.2880748288194013 * r +
28
- 0.7118352342418731 * g +
29
- 0.0000899369387256 * b,
30
- 0.8251046025104602 * b
31
- ];
32
- return xyz.concat(a == null || a == 1 ? [] : [a]);
33
- }
34
- function xyz50_to_prophotorgb(x, y, z, a) {
35
- // @ts-ignore
36
- return gam_prophotorgb(...[
37
- x * 1.3457868816471585 -
38
- y * 0.2555720873797946 -
39
- 0.0511018649755453 * z,
40
- x * -0.5446307051249019 +
41
- y * 1.5082477428451466 +
42
- 0.0205274474364214 * z,
43
- 1.2119675456389452 * z
44
- ].concat(a == null || a == 1 ? [] : [a]));
45
- }
46
- function gam_prophotorgb(r, g, b, a) {
47
- return [r, g, b].map(v => {
48
- let abs = Math.abs(v);
49
- if (abs >= 1 / 512) {
50
- return Math.sign(v) * Math.pow(abs, 1 / 1.8);
51
- }
52
- return 16 * v;
53
- }).concat(a == null || a == 1 ? [] : [a]);
54
- }
55
-
56
- export { prophotorgb2srgbvalues, srgb2prophotorgbvalues };
@@ -1,94 +0,0 @@
1
- import { EnumToken, ValidationLevel } from '../ast/types.js';
2
- import '../ast/minify.js';
3
- import '../ast/walk.js';
4
- import '../parser/parse.js';
5
- import '../renderer/color/utils/constants.js';
6
- import '../renderer/sourcemap/lib/encode.js';
7
- import '../parser/utils/config.js';
8
- import { getSyntaxConfig, getParsedSyntax } from './config.js';
9
- import { validateSyntax } from './syntax.js';
10
-
11
- function validateDeclaration(declaration, options, root) {
12
- const config = getSyntaxConfig();
13
- let name = declaration.nam;
14
- if (!(name in config.declarations) && !(name in config.syntaxes)) {
15
- if (name[0] == '-') {
16
- const match = /^-([a-z]+)-(.*)$/.exec(name);
17
- if (match != null) {
18
- name = match[2];
19
- }
20
- }
21
- }
22
- // root is at-rule - check if declaration allowed
23
- if (root?.typ == EnumToken.AtRuleNodeType) {
24
- //
25
- const syntax = getParsedSyntax("atRules" /* ValidationSyntaxGroupEnum.AtRules */, '@' + root.nam)?.[0];
26
- if (syntax != null) {
27
- if (!('chi' in syntax)) {
28
- return {
29
- valid: ValidationLevel.Drop,
30
- node: declaration,
31
- syntax,
32
- error: 'declaration not allowed here'
33
- };
34
- }
35
- if (name.startsWith('--')) {
36
- return {
37
- valid: ValidationLevel.Valid,
38
- node: declaration,
39
- syntax: null,
40
- error: ''
41
- };
42
- }
43
- // console.error({syntax});
44
- const config = getSyntaxConfig();
45
- // @ts-ignore
46
- const obj = config["atRules" /* ValidationSyntaxGroupEnum.AtRules */]['@' + root.nam];
47
- if ('descriptors' in obj) {
48
- const descriptors = obj.descriptors;
49
- if (!(name in descriptors)) {
50
- return {
51
- valid: ValidationLevel.Drop,
52
- node: declaration,
53
- syntax: `<${declaration.nam}>`,
54
- error: `declaration <${declaration.nam}> is not allowed in <@${root.nam}>`
55
- };
56
- }
57
- const syntax = getParsedSyntax("atRules" /* ValidationSyntaxGroupEnum.AtRules */, ['@' + root.nam, 'descriptors', name]);
58
- return validateSyntax(syntax, declaration.val, root, options);
59
- }
60
- // console.error({name});
61
- // if (!(name in config.declarations) && !(name in config.syntaxes)) {
62
- //
63
- // return {
64
- //
65
- // valid: ValidationLevel.Drop,
66
- // node: declaration,
67
- // syntax: null,
68
- // error: `unknown declaration "${declaration.nam}"`
69
- // }
70
- // }
71
- //
72
- // return validateSyntax((syntax as ValidationAtRuleDefinitionToken).chi as ValidationToken[], [declaration], root, options);
73
- }
74
- }
75
- if (name.startsWith('--')) {
76
- return {
77
- valid: ValidationLevel.Valid,
78
- node: declaration,
79
- syntax: null,
80
- error: ''
81
- };
82
- }
83
- if (!(name in config.declarations) && !(name in config.syntaxes)) {
84
- return {
85
- valid: ValidationLevel.Drop,
86
- node: declaration,
87
- syntax: `<${declaration.nam}>`,
88
- error: `unknown declaration "${declaration.nam}"`
89
- };
90
- }
91
- return validateSyntax(getParsedSyntax("declarations" /* ValidationSyntaxGroupEnum.Declarations */, name), declaration.val);
92
- }
93
-
94
- export { validateDeclaration };
@@ -1,29 +0,0 @@
1
- import { EnumToken, ValidationLevel } from '../../ast/types.js';
2
- import '../../ast/minify.js';
3
- import '../../ast/walk.js';
4
- import '../../parser/parse.js';
5
- import '../../renderer/color/utils/constants.js';
6
- import '../../renderer/sourcemap/lib/encode.js';
7
- import '../../parser/utils/config.js';
8
- import { validateSyntax } from '../syntax.js';
9
- import { getParsedSyntax } from '../config.js';
10
- import { validateURL } from './url.js';
11
-
12
- function validateImage(token) {
13
- if (token.typ == EnumToken.UrlFunctionTokenType) {
14
- return validateURL(token);
15
- }
16
- if (token.typ == EnumToken.ImageFunctionTokenType) {
17
- return validateSyntax(getParsedSyntax("syntaxes" /* ValidationSyntaxGroupEnum.Syntaxes */, token.val + '()'), token.chi);
18
- }
19
- return {
20
- valid: ValidationLevel.Drop,
21
- matches: [],
22
- node: token,
23
- syntax: 'image()',
24
- error: 'expected <image> or <url>',
25
- tokens: []
26
- };
27
- }
28
-
29
- export { validateImage };