@tbela99/css-parser 1.4.2 → 1.4.4

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 (122) hide show
  1. package/CHANGELOG.md +354 -4
  2. package/README.md +69 -1077
  3. package/dist/config.json.js +76 -3
  4. package/dist/index-umd-web.js +26838 -18743
  5. package/dist/index.cjs +26819 -18731
  6. package/dist/index.d.ts +2471 -1884
  7. package/dist/lib/ast/clone.js +46 -0
  8. package/dist/lib/ast/expand.js +90 -65
  9. package/dist/lib/ast/features/calc.js +86 -43
  10. package/dist/lib/ast/features/if.js +221 -0
  11. package/dist/lib/ast/features/index.js +1 -0
  12. package/dist/lib/ast/features/inlinecssvariables.js +33 -28
  13. package/dist/lib/ast/features/prefix.js +406 -44
  14. package/dist/lib/ast/features/shorthand.js +6 -9
  15. package/dist/lib/ast/features/transform.js +14 -15
  16. package/dist/lib/ast/find.js +175 -0
  17. package/dist/lib/ast/math/expression.js +145 -99
  18. package/dist/lib/ast/math/math.js +30 -41
  19. package/dist/lib/ast/minify.js +551 -232
  20. package/dist/lib/ast/transform/compute.js +82 -69
  21. package/dist/lib/ast/transform/matrix.js +12 -26
  22. package/dist/lib/ast/transform/minify.js +154 -150
  23. package/dist/lib/ast/transform/perspective.js +1 -1
  24. package/dist/lib/ast/transform/rotate.js +1 -1
  25. package/dist/lib/ast/transform/scale.js +1 -1
  26. package/dist/lib/ast/transform/skew.js +1 -1
  27. package/dist/lib/ast/transform/translate.js +1 -1
  28. package/dist/lib/ast/transform/utils.js +3 -2
  29. package/dist/lib/ast/types.js +345 -30
  30. package/dist/lib/ast/walk.js +119 -64
  31. package/dist/lib/fs/resolve.js +94 -88
  32. package/dist/lib/parser/declaration/list.js +117 -26
  33. package/dist/lib/parser/declaration/map.js +141 -96
  34. package/dist/lib/parser/declaration/set.js +30 -25
  35. package/dist/lib/parser/parse.js +1810 -1739
  36. package/dist/lib/parser/tokenize.js +662 -403
  37. package/dist/lib/parser/utils/angle.js +29 -0
  38. package/dist/lib/parser/utils/at-rule-container.js +486 -0
  39. package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
  40. package/dist/lib/parser/utils/at-rule-generic.js +118 -0
  41. package/dist/lib/parser/utils/at-rule-import.js +393 -0
  42. package/dist/lib/parser/utils/at-rule-media.js +603 -0
  43. package/dist/lib/parser/utils/at-rule-support.js +431 -0
  44. package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
  45. package/dist/lib/parser/utils/at-rule.js +31 -0
  46. package/dist/lib/parser/utils/cache.js +19 -0
  47. package/dist/lib/parser/utils/config.js +1 -0
  48. package/dist/lib/parser/utils/declaration.js +672 -91
  49. package/dist/lib/parser/utils/hash.js +1 -1
  50. package/dist/lib/parser/utils/selector.js +518 -0
  51. package/dist/lib/parser/utils/text.js +17 -1
  52. package/dist/lib/parser/utils/token.js +104 -0
  53. package/dist/lib/parser/utils/type.js +29 -18
  54. package/dist/lib/renderer/render.js +870 -269
  55. package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
  56. package/dist/lib/syntax/color/a98rgb.js +8 -12
  57. package/dist/lib/syntax/color/alpha.js +64 -0
  58. package/dist/lib/syntax/color/cmyk.js +23 -21
  59. package/dist/lib/syntax/color/color-mix.js +311 -237
  60. package/dist/lib/syntax/color/color.js +98 -92
  61. package/dist/lib/syntax/color/hex.js +29 -33
  62. package/dist/lib/syntax/color/hsl.js +7 -14
  63. package/dist/lib/syntax/color/hwb.js +14 -27
  64. package/dist/lib/syntax/color/lab.js +23 -24
  65. package/dist/lib/syntax/color/lch.js +16 -18
  66. package/dist/lib/syntax/color/oklab.js +21 -40
  67. package/dist/lib/syntax/color/oklch.js +16 -18
  68. package/dist/lib/syntax/color/p3.js +0 -8
  69. package/dist/lib/syntax/color/rec2020.js +9 -13
  70. package/dist/lib/syntax/color/relativecolor.js +160 -48
  71. package/dist/lib/syntax/color/rgb.js +14 -18
  72. package/dist/lib/syntax/color/srgb.js +30 -29
  73. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  74. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  75. package/dist/lib/syntax/color/utils/components.js +36 -16
  76. package/dist/lib/syntax/color/utils/distance.js +21 -11
  77. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  78. package/dist/lib/syntax/color/xyz.js +10 -22
  79. package/dist/lib/syntax/color/xyzd50.js +6 -20
  80. package/dist/lib/syntax/constants.js +464 -0
  81. package/dist/lib/syntax/syntax.js +825 -592
  82. package/dist/lib/validation/config.js +72 -33
  83. package/dist/lib/validation/config.json.js +1185 -85
  84. package/dist/lib/validation/match.js +3120 -0
  85. package/dist/lib/validation/parser/parse.js +683 -935
  86. package/dist/lib/validation/parser/typedef.js +106 -0
  87. package/dist/lib/validation/utils/list.js +4 -11
  88. package/dist/lib/validation/utils/whitespace.js +2 -8
  89. package/dist/node.js +102 -57
  90. package/dist/web.js +100 -48
  91. package/package.json +31 -17
  92. package/dist/lib/syntax/color/utils/constants.js +0 -214
  93. package/dist/lib/syntax/utils.js +0 -70
  94. package/dist/lib/validation/at-rules/container.js +0 -342
  95. package/dist/lib/validation/at-rules/counter-style.js +0 -90
  96. package/dist/lib/validation/at-rules/custom-media.js +0 -50
  97. package/dist/lib/validation/at-rules/document.js +0 -89
  98. package/dist/lib/validation/at-rules/else.js +0 -5
  99. package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
  100. package/dist/lib/validation/at-rules/import.js +0 -150
  101. package/dist/lib/validation/at-rules/keyframes.js +0 -67
  102. package/dist/lib/validation/at-rules/layer.js +0 -41
  103. package/dist/lib/validation/at-rules/media.js +0 -255
  104. package/dist/lib/validation/at-rules/namespace.js +0 -81
  105. package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
  106. package/dist/lib/validation/at-rules/page.js +0 -100
  107. package/dist/lib/validation/at-rules/supports.js +0 -295
  108. package/dist/lib/validation/at-rules/when.js +0 -185
  109. package/dist/lib/validation/atrule.js +0 -184
  110. package/dist/lib/validation/selector.js +0 -36
  111. package/dist/lib/validation/syntax.js +0 -1073
  112. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
  113. package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
  114. package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
  115. package/dist/lib/validation/syntaxes/family-name.js +0 -57
  116. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
  117. package/dist/lib/validation/syntaxes/layer-name.js +0 -57
  118. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
  119. package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
  120. package/dist/lib/validation/syntaxes/selector-list.js +0 -5
  121. package/dist/lib/validation/syntaxes/selector.js +0 -5
  122. package/dist/lib/validation/syntaxes/url.js +0 -40
@@ -0,0 +1,106 @@
1
+ var ValidationTokenEnum;
2
+ (function (ValidationTokenEnum) {
3
+ ValidationTokenEnum[ValidationTokenEnum["Root"] = 0] = "Root";
4
+ ValidationTokenEnum[ValidationTokenEnum["Keyword"] = 1] = "Keyword";
5
+ ValidationTokenEnum[ValidationTokenEnum["PropertyType"] = 2] = "PropertyType";
6
+ ValidationTokenEnum[ValidationTokenEnum["DeclarationType"] = 3] = "DeclarationType";
7
+ ValidationTokenEnum[ValidationTokenEnum["AtRule"] = 4] = "AtRule";
8
+ ValidationTokenEnum[ValidationTokenEnum["FunctionDefinition"] = 5] = "FunctionDefinition";
9
+ ValidationTokenEnum[ValidationTokenEnum["OpenBracket"] = 6] = "OpenBracket";
10
+ ValidationTokenEnum[ValidationTokenEnum["CloseBracket"] = 7] = "CloseBracket";
11
+ ValidationTokenEnum[ValidationTokenEnum["OpenParenthesis"] = 8] = "OpenParenthesis";
12
+ ValidationTokenEnum[ValidationTokenEnum["CloseParenthesis"] = 9] = "CloseParenthesis";
13
+ ValidationTokenEnum[ValidationTokenEnum["Comma"] = 10] = "Comma";
14
+ ValidationTokenEnum[ValidationTokenEnum["Pipe"] = 11] = "Pipe";
15
+ ValidationTokenEnum[ValidationTokenEnum["Column"] = 12] = "Column";
16
+ ValidationTokenEnum[ValidationTokenEnum["Star"] = 13] = "Star";
17
+ ValidationTokenEnum[ValidationTokenEnum["OpenCurlyBrace"] = 14] = "OpenCurlyBrace";
18
+ ValidationTokenEnum[ValidationTokenEnum["CloseCurlyBrace"] = 15] = "CloseCurlyBrace";
19
+ ValidationTokenEnum[ValidationTokenEnum["HashMark"] = 16] = "HashMark";
20
+ ValidationTokenEnum[ValidationTokenEnum["QuestionMark"] = 17] = "QuestionMark";
21
+ ValidationTokenEnum[ValidationTokenEnum["Function"] = 18] = "Function";
22
+ ValidationTokenEnum[ValidationTokenEnum["Number"] = 19] = "Number";
23
+ ValidationTokenEnum[ValidationTokenEnum["Whitespace"] = 20] = "Whitespace";
24
+ ValidationTokenEnum[ValidationTokenEnum["Parenthesis"] = 21] = "Parenthesis";
25
+ ValidationTokenEnum[ValidationTokenEnum["Bracket"] = 22] = "Bracket";
26
+ ValidationTokenEnum[ValidationTokenEnum["Block"] = 23] = "Block";
27
+ ValidationTokenEnum[ValidationTokenEnum["Plus"] = 24] = "Plus";
28
+ ValidationTokenEnum[ValidationTokenEnum["Separator"] = 25] = "Separator";
29
+ ValidationTokenEnum[ValidationTokenEnum["Exclamation"] = 26] = "Exclamation";
30
+ ValidationTokenEnum[ValidationTokenEnum["Ampersand"] = 27] = "Ampersand";
31
+ ValidationTokenEnum[ValidationTokenEnum["PipeToken"] = 28] = "PipeToken";
32
+ ValidationTokenEnum[ValidationTokenEnum["ColumnToken"] = 29] = "ColumnToken";
33
+ ValidationTokenEnum[ValidationTokenEnum["AmpersandToken"] = 30] = "AmpersandToken";
34
+ ValidationTokenEnum[ValidationTokenEnum["Parens"] = 31] = "Parens";
35
+ ValidationTokenEnum[ValidationTokenEnum["PseudoClassToken"] = 32] = "PseudoClassToken";
36
+ ValidationTokenEnum[ValidationTokenEnum["PseudoClassFunctionToken"] = 33] = "PseudoClassFunctionToken";
37
+ ValidationTokenEnum[ValidationTokenEnum["StringToken"] = 34] = "StringToken";
38
+ ValidationTokenEnum[ValidationTokenEnum["AtRuleDefinition"] = 35] = "AtRuleDefinition";
39
+ ValidationTokenEnum[ValidationTokenEnum["DeclarationNameToken"] = 36] = "DeclarationNameToken";
40
+ ValidationTokenEnum[ValidationTokenEnum["DeclarationDefinitionToken"] = 37] = "DeclarationDefinitionToken";
41
+ ValidationTokenEnum[ValidationTokenEnum["SemiColon"] = 38] = "SemiColon";
42
+ ValidationTokenEnum[ValidationTokenEnum["Character"] = 39] = "Character";
43
+ ValidationTokenEnum[ValidationTokenEnum["InfinityToken"] = 40] = "InfinityToken";
44
+ ValidationTokenEnum[ValidationTokenEnum["LessThan"] = 41] = "LessThan";
45
+ ValidationTokenEnum[ValidationTokenEnum["GreaterThan"] = 42] = "GreaterThan";
46
+ /**
47
+ * end of token stream
48
+ */
49
+ ValidationTokenEnum[ValidationTokenEnum["EOF"] = 43] = "EOF";
50
+ /**
51
+ * optional group or tokens, used to group validation tokens
52
+ *
53
+ * ```ts
54
+ * // <bg-layer>#? , <final-bg-layer> -> [<bg-layer>#? ,]? <final-bg-layer>
55
+ * // , <angular-color-stop> ]#? -> [, <angular-color-stop> ]#?]?
56
+ * ```
57
+ */
58
+ ValidationTokenEnum[ValidationTokenEnum["OptionalGroupToken"] = 44] = "OptionalGroupToken";
59
+ /**
60
+ * dimension token
61
+ *
62
+ * ```ts
63
+ * // <time [0s,∞]> -> {
64
+ * // typ: ValidationTokenEnum.PropertyType
65
+ * // val: 'time',
66
+ * // range: {
67
+ * // min: ValidationNumberToken,
68
+ * // max: null | ValidationNumberToken | ValidationInfinityToken
69
+ * // }
70
+ * // }
71
+ * ```
72
+ */
73
+ ValidationTokenEnum[ValidationTokenEnum["Dimension"] = 45] = "Dimension";
74
+ ValidationTokenEnum[ValidationTokenEnum["DisallowWhitespace"] = 46] = "DisallowWhitespace";
75
+ ValidationTokenEnum[ValidationTokenEnum["Colon"] = 47] = "Colon";
76
+ })(ValidationTokenEnum || (ValidationTokenEnum = {}));
77
+ /**
78
+ * Keys of the validation config object
79
+ */
80
+ var ValidationSyntaxGroupEnum;
81
+ (function (ValidationSyntaxGroupEnum) {
82
+ ValidationSyntaxGroupEnum["Declarations"] = "declarations";
83
+ ValidationSyntaxGroupEnum["Functions"] = "functions";
84
+ ValidationSyntaxGroupEnum["Syntaxes"] = "syntaxes";
85
+ ValidationSyntaxGroupEnum["Selectors"] = "selectors";
86
+ ValidationSyntaxGroupEnum["AtRules"] = "atRules";
87
+ ValidationSyntaxGroupEnum["Units"] = "units";
88
+ ValidationSyntaxGroupEnum["Languages"] = "languages";
89
+ ValidationSyntaxGroupEnum["mediaFeatures"] = "mediaFeatures";
90
+ })(ValidationSyntaxGroupEnum || (ValidationSyntaxGroupEnum = {}));
91
+ /**
92
+ * Types of media features
93
+ */
94
+ var MediaFeatureType;
95
+ (function (MediaFeatureType) {
96
+ MediaFeatureType["BooleanType"] = "boolean";
97
+ MediaFeatureType["IntergerType"] = "integer";
98
+ MediaFeatureType["KeywordType"] = "keyword";
99
+ MediaFeatureType["LengthType"] = "length";
100
+ MediaFeatureType["NumberType"] = "number";
101
+ MediaFeatureType["RatioType"] = "ratio";
102
+ MediaFeatureType["ResolutionType"] = "resolution";
103
+ MediaFeatureType["StringType"] = "string";
104
+ })(MediaFeatureType || (MediaFeatureType = {}));
105
+
106
+ export { MediaFeatureType, ValidationSyntaxGroupEnum, ValidationTokenEnum };
@@ -1,18 +1,8 @@
1
1
  import { EnumToken } from '../../ast/types.js';
2
- import '../../ast/minify.js';
3
- import '../../ast/walk.js';
4
- import '../../parser/parse.js';
5
- import '../../parser/tokenize.js';
6
- import '../../parser/utils/config.js';
7
- import '../../syntax/color/utils/constants.js';
8
- import '../../renderer/sourcemap/lib/encode.js';
9
2
 
10
3
  function stripCommaToken(tokenList) {
11
4
  let result = [];
12
5
  for (let i = 0; i < tokenList.length; i++) {
13
- if (tokenList[i].typ != EnumToken.WhitespaceTokenType) {
14
- tokenList[i];
15
- }
16
6
  if (tokenList[i].typ == EnumToken.CommentTokenType || tokenList[i].typ == EnumToken.CommaTokenType) {
17
7
  continue;
18
8
  }
@@ -20,9 +10,12 @@ function stripCommaToken(tokenList) {
20
10
  }
21
11
  return result;
22
12
  }
23
- function splitTokenList(tokenList, split = [EnumToken.CommaTokenType]) {
13
+ function splitTokenList(tokenList, split = [EnumToken.CommaTokenType], includeSplitToken = false) {
24
14
  return tokenList.reduce((acc, curr) => {
25
15
  if (split.includes(curr.typ)) {
16
+ if (includeSplitToken && Array.isArray(acc[acc.length - 1])) {
17
+ acc[acc.length - 1].push(curr);
18
+ }
26
19
  acc.push([]);
27
20
  }
28
21
  else {
@@ -1,11 +1,4 @@
1
1
  import { EnumToken } from '../../ast/types.js';
2
- import '../../ast/minify.js';
3
- import '../../ast/walk.js';
4
- import '../../parser/parse.js';
5
- import '../../parser/tokenize.js';
6
- import '../../parser/utils/config.js';
7
- import '../../syntax/color/utils/constants.js';
8
- import '../../renderer/sourcemap/lib/encode.js';
9
2
 
10
3
  function consumeWhitespace(tokens) {
11
4
  if (tokens.length == 0) {
@@ -14,7 +7,8 @@ function consumeWhitespace(tokens) {
14
7
  if (tokens[0].typ != EnumToken.WhitespaceTokenType && tokens[0].typ != EnumToken.DescendantCombinatorTokenType) {
15
8
  return false;
16
9
  }
17
- while (tokens.length > 0 && (tokens[0].typ == EnumToken.WhitespaceTokenType || tokens[0].typ == EnumToken.DescendantCombinatorTokenType)) {
10
+ while (tokens.length > 0 &&
11
+ (tokens[0].typ == EnumToken.WhitespaceTokenType || tokens[0].typ == EnumToken.DescendantCombinatorTokenType)) {
18
12
  tokens.shift();
19
13
  }
20
14
  return true;
package/dist/node.js CHANGED
@@ -1,32 +1,30 @@
1
1
  import process from 'node:process';
2
+ import { Readable } from 'node:stream';
3
+ import { createReadStream } from 'node:fs';
4
+ import { lstat, readFile } from 'node:fs/promises';
5
+ import { doParse } from './lib/parser/parse.js';
6
+ export { parseDeclarations, parseString } from './lib/parser/parse.js';
7
+ import { doRender } from './lib/renderer/render.js';
8
+ export { renderValue as renderToken } from './lib/renderer/render.js';
2
9
  import { ModuleScopeEnumOptions } from './lib/ast/types.js';
3
- export { ColorType, EnumToken, ModuleCaseTransformEnum, ValidationLevel } from './lib/ast/types.js';
10
+ export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, ValidationLevel } from './lib/ast/types.js';
11
+ import { tokenizeStream, tokenize } from './lib/parser/tokenize.js';
12
+ import { dirname, resolve, matchUrl } from './lib/fs/resolve.js';
13
+ import { ResponseType } from './types.js';
14
+ import { deprecate } from 'util';
4
15
  export { minify } from './lib/ast/minify.js';
5
- export { WalkerEvent, WalkerOptionEnum, walk, walkValues } from './lib/ast/walk.js';
6
16
  export { expand } from './lib/ast/expand.js';
7
- import { doRender } from './lib/renderer/render.js';
8
- export { renderToken } from './lib/renderer/render.js';
9
- export { SourceMap } from './lib/renderer/sourcemap/sourcemap.js';
10
- import { doParse } from './lib/parser/parse.js';
11
- export { parseDeclarations, parseString, parseTokens } from './lib/parser/parse.js';
12
- import { tokenizeStream, tokenize } from './lib/parser/tokenize.js';
13
- import './lib/parser/utils/config.js';
17
+ export { WalkerEvent, WalkerOptionEnum, walk, walkValues } from './lib/ast/walk.js';
14
18
  export { convertColor } from './lib/syntax/color/color.js';
15
- import './lib/syntax/color/utils/constants.js';
16
19
  export { isOkLabClose, okLabDistance } from './lib/syntax/color/utils/distance.js';
17
- import './lib/validation/config.js';
18
- import './lib/validation/parser/parse.js';
19
- import './lib/validation/syntaxes/complex-selector.js';
20
- import './lib/validation/syntax.js';
21
- import { resolve, matchUrl, dirname } from './lib/fs/resolve.js';
22
- import { Readable } from 'node:stream';
23
- import { createReadStream } from 'node:fs';
24
- import { lstat, readFile } from 'node:fs/promises';
25
- import { ResponseType } from './types.js';
20
+ export { find, findAll, findByValue, findLast } from './lib/ast/find.js';
21
+ export { cloneNode } from './lib/ast/clone.js';
22
+ export { replaceNodeOrValue } from './lib/parser/utils/token.js';
23
+ export { SourceMap } from './lib/renderer/sourcemap/sourcemap.js';
26
24
  export { FeatureWalkMode } from './lib/ast/features/type.js';
27
25
 
28
26
  /**
29
- * load file or url
27
+ * Load file or url
30
28
  * @param url
31
29
  * @param currentDirectory
32
30
  * @param responseType
@@ -37,9 +35,9 @@ export { FeatureWalkMode } from './lib/ast/features/type.js';
37
35
  * const result = await load(file, '.', ResponseType.ArrayBuffer) as ArrayBuffer;
38
36
  * ```
39
37
  */
40
- async function load(url, currentDirectory = '.', responseType = false) {
41
- const resolved = resolve(url, currentDirectory);
42
- if (typeof responseType == 'boolean') {
38
+ async function load(url, currentDirectory = ".", responseType = false) {
39
+ const resolved = typeof url == "string" ? resolve(url, currentDirectory) : url;
40
+ if (typeof responseType == "boolean") {
43
41
  responseType = responseType ? ResponseType.ReadableStream : ResponseType.Text;
44
42
  }
45
43
  if (matchUrl.test(resolved.absolute)) {
@@ -50,21 +48,23 @@ async function load(url, currentDirectory = '.', responseType = false) {
50
48
  if (responseType == ResponseType.ArrayBuffer) {
51
49
  return response.arrayBuffer();
52
50
  }
53
- return responseType == ResponseType.ReadableStream ? response.body : response.text();
51
+ return responseType == ResponseType.ReadableStream
52
+ ? response.body
53
+ : response.text();
54
54
  });
55
55
  }
56
56
  try {
57
57
  const stats = await lstat(resolved.absolute);
58
58
  if (stats.isFile()) {
59
59
  if (responseType == ResponseType.Text) {
60
- return readFile(resolved.absolute, 'utf-8');
60
+ return readFile(resolved.absolute, "utf-8");
61
61
  }
62
62
  if (responseType == ResponseType.ArrayBuffer) {
63
- return readFile(resolved.absolute).then(buffer => buffer.buffer);
63
+ return readFile(resolved.absolute).then((buffer) => buffer.buffer);
64
64
  }
65
65
  return Readable.toWeb(createReadStream(resolved.absolute, {
66
- encoding: 'utf-8',
67
- highWaterMark: 64 * 1024
66
+ encoding: "utf-8",
67
+ highWaterMark: 64 * 1024,
68
68
  }));
69
69
  }
70
70
  }
@@ -74,7 +74,7 @@ async function load(url, currentDirectory = '.', responseType = false) {
74
74
  throw new Error(`File not found: '${resolved.absolute || url}'`);
75
75
  }
76
76
  /**
77
- * render the ast tree
77
+ * Render the ast tree
78
78
  * @param data
79
79
  * @param options
80
80
  * @param mapping
@@ -106,11 +106,12 @@ function render(data, options = {}, mapping) {
106
106
  return doRender(data, Object.assign(options, { resolve, dirname, cwd: options.cwd ?? process.cwd() }), mapping);
107
107
  }
108
108
  /**
109
- * parse css file
109
+ * Parse css file
110
110
  * @param file url or path
111
111
  * @param options
112
112
  * @param asStream load file as stream
113
113
  *
114
+ * @deprecated
114
115
  * @throws Error file not found
115
116
  *
116
117
  * Example:
@@ -128,14 +129,14 @@ function render(data, options = {}, mapping) {
128
129
  * console.log(result.ast);
129
130
  * ```
130
131
  */
131
- async function parseFile(file, options = {}, asStream = false) {
132
- return Promise.resolve((options.load ?? load)(file, '.', asStream)).then(stream => parse(stream, { src: file, ...options }));
133
- }
132
+ const parseFile = deprecate(async (file, options = {}, asStream = false) => parse({ file, asStream, ...options }), "parseFile is deprecated, use parse instead as parse({file, asStream, ...options})");
134
133
  /**
135
- * parse css
134
+ * Parse css
136
135
  * @param stream
137
136
  * @param options
138
137
  *
138
+ * @throws Error file not found
139
+ *
139
140
  * Example:
140
141
  *
141
142
  * ```ts
@@ -174,51 +175,75 @@ async function parseFile(file, options = {}, asStream = false) {
174
175
  * console.log(result.ast);
175
176
  * ```
176
177
  */
177
- async function parse(stream, options = {}) {
178
- return doParse(stream instanceof ReadableStream ? tokenizeStream(stream) : tokenize({
178
+ async function parse(...args) {
179
+ let options;
180
+ let stream;
181
+ if (typeof args[0] === "string" || args[0] instanceof ReadableStream) {
182
+ stream = args[0];
183
+ options = args[1];
184
+ }
185
+ else {
186
+ // @ts-expect-error
187
+ const { file, input, ...opt } = args[0];
188
+ options = opt;
189
+ if (file != null) {
190
+ return Promise.resolve((options.load ?? load)(file, ".", options.asStream ?? false)).then((stream) => parse(stream, { src: file, ...options }));
191
+ }
192
+ else {
193
+ stream = input;
194
+ }
195
+ }
196
+ options ??= {};
197
+ options.parseInfo = {
179
198
  stream,
180
- buffer: '',
199
+ buffer: "",
200
+ src: options.src ?? "",
181
201
  offset: 0,
182
- position: { ind: 0, lin: 1, col: 1 },
183
- currentPosition: { ind: -1, lin: 1, col: 0 }
184
- }), Object.assign(options, {
202
+ time: 0,
203
+ position: { ind: 0, lin: 1, col: 0 },
204
+ currentPosition: { ind: -1, lin: 1, col: 0 },
205
+ };
206
+ return doParse(stream instanceof ReadableStream ? tokenizeStream(stream, options.parseInfo) : tokenize(options.parseInfo), Object.assign(options, {
185
207
  load,
186
208
  resolve,
187
209
  dirname,
188
- cwd: options.cwd ?? process.cwd()
189
- })).then(result => {
210
+ cwd: options.cwd ?? process.cwd(),
211
+ })).then((result) => {
190
212
  const { revMapping, ...res } = result;
191
213
  return res;
192
214
  });
193
215
  }
194
216
  /**
195
- * transform css file
217
+ * Transform css file
196
218
  * @param file url or path
197
219
  * @param options
198
220
  * @param asStream load file as stream
199
221
  *
222
+ * @deprecated Use transform() instead.
200
223
  * @throws Error file not found
201
224
  *
202
225
  * Example:
203
226
  *
204
227
  * ```ts
205
228
  *
206
- * import {transformFile} from '@tbela99/css-parser';
229
+ * import {transform} from '@tbela99/css-parser';
207
230
  *
208
231
  * // remote file
209
- * let result = await transformFile('https://docs.deno.com/styles.css');
232
+ * let result = await transform({file: 'https://docs.deno.com/styles.css'});
210
233
  * console.log(result.code);
211
234
  *
212
235
  * // local file
213
- * result = await transformFile('./css/styles.css');
236
+ * result = await transform({file: './css/styles.css'});
214
237
  * console.log(result.code);
215
238
  * ```
216
239
  */
217
- async function transformFile(file, options = {}, asStream = false) {
218
- return Promise.resolve((options.load ?? load)(file, '.', asStream)).then(stream => transform(stream, { src: file, ...options }));
219
- }
240
+ const transformFile = deprecate(async (file, options = {}, asStream = false) => transform({
241
+ file,
242
+ asStream,
243
+ ...options,
244
+ }), "transformFile is deprecated, use transform instead as transform({file, asStream, ...options})");
220
245
  /**
221
- * transform css
246
+ * Transform css
222
247
  * @param css
223
248
  * @param options
224
249
  *
@@ -260,24 +285,44 @@ async function transformFile(file, options = {}, asStream = false) {
260
285
  * console.log(result.code);
261
286
  * ```
262
287
  */
263
- async function transform(css, options = {}) {
288
+ async function transform(...args) {
289
+ let options;
290
+ let stream;
291
+ if (typeof args[0] === "string" || args[0] instanceof ReadableStream) {
292
+ stream = args[0];
293
+ options = args[1];
294
+ }
295
+ else {
296
+ // @ts-expect-error
297
+ const { file, input, ...opt } = args[0];
298
+ options = opt;
299
+ if (file != null) {
300
+ return Promise.resolve((options.load ?? load)(file, ".", options.asStream ?? false)).then((stream) => transform(stream, { src: file, ...options }));
301
+ }
302
+ else {
303
+ stream = input;
304
+ }
305
+ }
306
+ options ??= {};
264
307
  options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
265
308
  const startTime = performance.now();
266
- return parse(css, options).then((parseResult) => {
309
+ return parse(stream, options).then((parseResult) => {
267
310
  let mapping = null;
268
311
  let importMapping = null;
269
- if (typeof options.module == 'number' && (options.module & ModuleScopeEnumOptions.ICSS)) {
312
+ if (typeof options.module == "number" && options.module & ModuleScopeEnumOptions.ICSS) {
270
313
  mapping = parseResult.mapping;
271
314
  importMapping = parseResult.importMapping;
272
315
  }
273
- else if (typeof options.module == 'object' && typeof options.module.scoped == 'number' && (options.module.scoped & ModuleScopeEnumOptions.ICSS)) {
316
+ else if (typeof options.module == "object" &&
317
+ typeof options.module.scoped == "number" &&
318
+ options.module.scoped & ModuleScopeEnumOptions.ICSS) {
274
319
  mapping = parseResult.mapping;
275
320
  importMapping = parseResult.importMapping;
276
321
  }
277
322
  // ast already expanded by parse
278
323
  const rendered = render(parseResult.ast, {
279
324
  ...options,
280
- expandNestingRules: false
325
+ expandNestingRules: false,
281
326
  }, mapping != null ? { mapping, importMapping } : null);
282
327
  return {
283
328
  ...parseResult,
@@ -287,8 +332,8 @@ async function transform(css, options = {}) {
287
332
  bytesOut: rendered.code.length,
288
333
  ...parseResult.stats,
289
334
  render: rendered.stats.total,
290
- total: `${(performance.now() - startTime).toFixed(2)}ms`
291
- }
335
+ total: `${(performance.now() - startTime).toFixed(2)}ms`,
336
+ },
292
337
  };
293
338
  });
294
339
  }