@tbela99/css-parser 2.0.1 → 2.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.
Files changed (128) hide show
  1. package/CHANGELOG.md +86 -1
  2. package/README.md +15 -7
  3. package/architecture.png +0 -0
  4. package/dist/{src/data → data}/properties.json.js +86 -2
  5. package/dist/{src/data → data}/validation.json.js +4848 -3750
  6. package/dist/index-umd-web.js +18234 -9498
  7. package/dist/index.d.ts +1251 -373
  8. package/dist/{src/lib → lib}/ast/arena-node.js +119 -57
  9. package/dist/{src/lib → lib}/ast/arena.js +1034 -655
  10. package/dist/{src/lib → lib}/ast/cache.js +36 -31
  11. package/dist/{src/lib → lib}/ast/features/calc.js +37 -27
  12. package/dist/{src/lib → lib}/ast/features/if.js +24 -19
  13. package/dist/{src/lib → lib}/ast/features/index.js +3 -2
  14. package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +42 -23
  15. package/dist/lib/ast/features/mixin.js +953 -0
  16. package/dist/{src/lib → lib}/ast/features/prefix.js +82 -69
  17. package/dist/lib/ast/features/shorthand.js +129 -0
  18. package/dist/lib/ast/features/transform.js +188 -0
  19. package/dist/{src/lib → lib}/ast/features/type.js +2 -2
  20. package/dist/{src/lib → lib}/ast/math/expression.js +150 -150
  21. package/dist/{src/lib → lib}/ast/math/math.js +8 -8
  22. package/dist/{src/lib → lib}/ast/minify.js +932 -354
  23. package/dist/lib/ast/mixins/custom-function.js +393 -0
  24. package/dist/lib/ast/mixins/utils.js +53 -0
  25. package/dist/lib/ast/mixins/variables.js +678 -0
  26. package/dist/lib/ast/strings.js +906 -0
  27. package/dist/lib/ast/transform/compute.js +560 -0
  28. package/dist/lib/ast/transform/matrix.js +67 -0
  29. package/dist/lib/ast/transform/minify.js +325 -0
  30. package/dist/lib/ast/transform/perspective.js +24 -0
  31. package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
  32. package/dist/lib/ast/transform/scale.js +43 -0
  33. package/dist/lib/ast/transform/skew.js +38 -0
  34. package/dist/lib/ast/transform/translate.js +56 -0
  35. package/dist/lib/ast/transform/utils.js +390 -0
  36. package/dist/{src/lib → lib}/ast/types.js +7 -15
  37. package/dist/{src/lib → lib}/ast/walk.js +19 -19
  38. package/dist/{src/lib → lib}/fs/resolve.js +15 -15
  39. package/dist/{src/lib → lib}/parser/declaration/list.js +55 -26
  40. package/dist/{src/lib → lib}/parser/declaration/map.js +85 -72
  41. package/dist/{src/lib → lib}/parser/declaration/set.js +32 -27
  42. package/dist/lib/parser/input.js +85 -0
  43. package/dist/{src/lib → lib}/parser/linesmap.js +3 -16
  44. package/dist/{src/lib → lib}/parser/parse.js +852 -619
  45. package/dist/lib/parser/source.js +71 -0
  46. package/dist/{src/lib → lib}/parser/tokenize.js +886 -722
  47. package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
  48. package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +68 -35
  49. package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
  50. package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +31 -13
  51. package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +29 -16
  52. package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +77 -58
  53. package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +18 -20
  54. package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +9 -8
  55. package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
  56. package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
  57. package/dist/lib/parser/utils/cache.js +53 -0
  58. package/dist/lib/parser/utils/canonical.js +78 -0
  59. package/dist/lib/parser/utils/codes.js +93 -0
  60. package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
  61. package/dist/{src/lib → lib}/parser/utils/declaration.js +67 -49
  62. package/dist/{src/lib → lib}/parser/utils/eq.js +14 -14
  63. package/dist/{src/lib → lib}/parser/utils/hash.js +4 -4
  64. package/dist/lib/parser/utils/selector.js +720 -0
  65. package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
  66. package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
  67. package/dist/{src/lib → lib}/parser/utils/type.js +11 -9
  68. package/dist/{src/lib → lib}/printer/render.js +536 -243
  69. package/dist/lib/printer/result.js +148 -0
  70. package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +5 -8
  71. package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +13 -13
  72. package/dist/lib/printer/writer.js +252 -0
  73. package/dist/{src/lib → lib}/syntax/color/a98rgb.js +6 -6
  74. package/dist/{src/lib → lib}/syntax/color/alpha.js +12 -11
  75. package/dist/{src/lib → lib}/syntax/color/cmyk.js +11 -6
  76. package/dist/{src/lib → lib}/syntax/color/color-mix.js +78 -67
  77. package/dist/{src/lib → lib}/syntax/color/color.js +52 -47
  78. package/dist/{src/lib → lib}/syntax/color/hex.js +17 -17
  79. package/dist/{src/lib → lib}/syntax/color/hsl.js +8 -8
  80. package/dist/{src/lib → lib}/syntax/color/hwb.js +6 -7
  81. package/dist/{src/lib → lib}/syntax/color/lab.js +8 -8
  82. package/dist/{src/lib → lib}/syntax/color/lch.js +12 -11
  83. package/dist/{src/lib → lib}/syntax/color/oklab.js +9 -9
  84. package/dist/{src/lib → lib}/syntax/color/oklch.js +5 -5
  85. package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
  86. package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +4 -4
  87. package/dist/{src/lib → lib}/syntax/color/rec2020.js +3 -3
  88. package/dist/{src/lib → lib}/syntax/color/relative-color.js +41 -34
  89. package/dist/{src/lib → lib}/syntax/color/rgb.js +17 -17
  90. package/dist/{src/lib → lib}/syntax/color/srgb.js +20 -20
  91. package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +6 -4
  92. package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +12 -14
  93. package/dist/{src/lib → lib}/syntax/color/utils/components.js +13 -13
  94. package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
  95. package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +6 -4
  96. package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
  97. package/dist/{src/lib → lib}/syntax/color/xyz.js +5 -10
  98. package/dist/{src/lib → lib}/syntax/color/xyzd50.js +3 -3
  99. package/dist/{src/lib → lib}/syntax/constants.js +30 -3
  100. package/dist/{src/lib → lib}/syntax/syntax.js +151 -116
  101. package/dist/lib/validation/config.js +173 -0
  102. package/dist/lib/validation/first.js +571 -0
  103. package/dist/{src/lib → lib}/validation/match.js +1256 -579
  104. package/dist/{src/lib → lib}/validation/parser/parse.js +85 -59
  105. package/dist/{src/lib → lib}/validation/parser/typedef.js +6 -2
  106. package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
  107. package/dist/{src/node.js → node.js} +92 -80
  108. package/dist/{src/types.js → types.js} +2 -2
  109. package/dist/{src/utils → utils}/sync.js +13 -8
  110. package/dist/version.d.ts +4 -0
  111. package/dist/version.js +13 -0
  112. package/dist/{src/web.js → web.js} +80 -75
  113. package/package.json +13 -13
  114. package/dist/package.json.js +0 -10
  115. package/dist/src/lib/ast/features/shorthand.js +0 -87
  116. package/dist/src/lib/ast/features/transform.js +0 -85
  117. package/dist/src/lib/ast/transform/compute.js +0 -358
  118. package/dist/src/lib/ast/transform/matrix.js +0 -88
  119. package/dist/src/lib/ast/transform/minify.js +0 -267
  120. package/dist/src/lib/ast/transform/perspective.js +0 -16
  121. package/dist/src/lib/ast/transform/scale.js +0 -39
  122. package/dist/src/lib/ast/transform/skew.js +0 -30
  123. package/dist/src/lib/ast/transform/translate.js +0 -39
  124. package/dist/src/lib/ast/transform/utils.js +0 -287
  125. package/dist/src/lib/parser/source.js +0 -127
  126. package/dist/src/lib/parser/utils/cache.js +0 -54
  127. package/dist/src/lib/parser/utils/selector.js +0 -440
  128. package/dist/src/lib/validation/config.js +0 -120
@@ -1,21 +1,23 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.0.1
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { doParse, doParseSync } from './lib/parser/parse.js';
9
9
  export { parseDeclarations, parseString } from './lib/parser/parse.js';
10
10
  import { doRender } from './lib/printer/render.js';
11
- export { renderValue as renderToken } from './lib/printer/render.js';
11
+ export { renderValue } from './lib/printer/render.js';
12
+ import { TransformResult, RenderResult } from './lib/printer/result.js';
12
13
  import { ModuleScopeEnumOptions } from './lib/ast/types.js';
13
14
  export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, WalkerEvent, WalkerOptionEnum } from './lib/ast/types.js';
14
- import { Tokenizer } from './lib/parser/tokenize.js';
15
+ import { createParseInfo, Tokenizer } from './lib/parser/tokenize.js';
16
+ import { isCssInput } from './lib/parser/input.js';
15
17
  import { matchUrl, resolve, dirname } from './lib/fs/resolve.js';
16
18
  import { ResponseType } from './types.js';
17
19
  import { SourceFile } from './lib/parser/source.js';
18
- import { parseResult, validateSyncArguments } from './utils/sync.js';
20
+ import { validateSyncArguments, parseResult } from './utils/sync.js';
19
21
  export { minify } from './lib/ast/minify.js';
20
22
  export { convertColor } from './lib/syntax/color/color.js';
21
23
  export { isOkLabClose, okLabDistance } from './lib/syntax/color/utils/distance.js';
@@ -25,7 +27,7 @@ export { PropertyList } from './lib/parser/declaration/list.js';
25
27
  export { SourceMap } from './lib/printer/sourcemap/sourcemap.js';
26
28
  export { FeatureWalkMode } from './lib/ast/features/type.js';
27
29
  export { parseSyntax } from './lib/validation/parser/parse.js';
28
- export { version } from '../package.json.js';
30
+ export { version } from './version.js';
29
31
 
30
32
  /**
31
33
  * Load file or url
@@ -40,11 +42,11 @@ export { version } from '../package.json.js';
40
42
  * ```
41
43
  */
42
44
  async function load(url, currentDirectory = ".", responseType = false) {
43
- if (typeof responseType == "boolean") {
45
+ if (typeof responseType === "boolean") {
44
46
  responseType = responseType ? ResponseType.ReadableStream : ResponseType.Text;
45
47
  }
46
48
  let t;
47
- if (typeof url == "object") {
49
+ if (typeof url === "object") {
48
50
  t = new URL(url.absolute, self.origin);
49
51
  }
50
52
  else if (matchUrl.test(url)) {
@@ -57,21 +59,22 @@ async function load(url, currentDirectory = ".", responseType = false) {
57
59
  const path = resolve(url, currentDirectory).absolute;
58
60
  t = new URL(path, self.origin);
59
61
  }
60
- return fetch(t, t.origin != self.origin ? { mode: "cors" } : {}).then(async (response) => {
62
+ return fetch(t, t.origin !== self.origin ? { mode: "cors" } : {}).then(async (response) => {
61
63
  if (!response.ok) {
62
64
  throw new Error(`${response.status} ${response.statusText} ${response.url}`);
63
65
  }
64
- if (responseType == ResponseType.ArrayBuffer) {
66
+ if (responseType === ResponseType.ArrayBuffer) {
65
67
  return response.arrayBuffer();
66
68
  }
67
- if (responseType == ResponseType.JSON) {
69
+ if (responseType === ResponseType.JSON) {
68
70
  return response.json();
69
71
  }
70
- return responseType == ResponseType.ReadableStream ? response.body : response.text();
72
+ return responseType === ResponseType.ReadableStream ? response.body : response.text();
71
73
  });
72
74
  }
73
75
  /**
74
76
  * Render the ast tree
77
+ * @param arena
75
78
  * @param data
76
79
  * @param options
77
80
  * @param mapping
@@ -100,13 +103,16 @@ async function load(url, currentDirectory = ".", responseType = false) {
100
103
  * ```
101
104
  */
102
105
  function render(arena, data, options = {}, mapping) {
103
- return doRender(arena, data, Object.assign(options, {
106
+ return doRender(arena, data,
107
+ // the options of the caller are not modified
108
+ {
109
+ ...options,
104
110
  resolve,
105
111
  dirname,
106
112
  cwd: (options.cwd ?? self.location.pathname.endsWith("/"))
107
113
  ? self.location.pathname
108
114
  : dirname(self.location.pathname),
109
- }), mapping);
115
+ }, mapping);
110
116
  }
111
117
  /**
112
118
  * Parse CSS
@@ -127,7 +133,7 @@ function render(arena, data, options = {}, mapping) {
127
133
  function parseSync(...args) {
128
134
  let options;
129
135
  let stream;
130
- if (typeof args[0] === "string") {
136
+ if (isCssInput(args[0])) {
131
137
  stream = args[0];
132
138
  options = args[1];
133
139
  }
@@ -139,7 +145,15 @@ function parseSync(...args) {
139
145
  if (options != null) {
140
146
  validateSyncArguments(options);
141
147
  }
142
- options ??= {};
148
+ // the options of the caller are not modified: they may be reused
149
+ return parseSyncInput(stream, { ...options });
150
+ }
151
+ /**
152
+ * parse a css input. the options belong to the call: they are modified (source, parse info, ...)
153
+ * @param stream
154
+ * @param options
155
+ */
156
+ function parseSyncInput(stream, options) {
143
157
  options.src ??= "";
144
158
  options.sourcesMap ??= new Map();
145
159
  Object.assign(options, {
@@ -151,20 +165,11 @@ function parseSync(...args) {
151
165
  });
152
166
  options.src = resolve(options.src, options.cwd).relative;
153
167
  if (options.source == null) {
154
- const source = new SourceFile(typeof stream == "string" ? stream : "", [], options.src);
168
+ const source = new SourceFile(typeof stream === "string" ? stream : "", [], options.src);
155
169
  options.sourcesMap.set(source.id, source);
156
170
  options.source = source;
157
171
  }
158
- options.parseInfo = {
159
- stream,
160
- buffer: "",
161
- src: options.src ?? "",
162
- offset: 0,
163
- time: 0,
164
- source: options.source,
165
- position: 0,
166
- currentPosition: 0,
167
- };
172
+ options.parseInfo = createParseInfo(stream, options.source);
168
173
  const result = doParseSync(new Tokenizer(options.parseInfo), options);
169
174
  return options.module == null && options.inputSourceMap == null && !options.sourcemap
170
175
  ? result
@@ -187,7 +192,7 @@ function parseSync(...args) {
187
192
  function transformSync(...args) {
188
193
  let options;
189
194
  let stream;
190
- if (typeof args[0] === "string") {
195
+ if (isCssInput(args[0])) {
191
196
  stream = args[0];
192
197
  options = args[1];
193
198
  }
@@ -197,15 +202,18 @@ function transformSync(...args) {
197
202
  options = opt;
198
203
  stream = input;
199
204
  }
200
- options ??= {};
205
+ if (options != null) {
206
+ validateSyncArguments(options);
207
+ }
208
+ // the options of the caller are not modified: they may be reused
201
209
  options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
202
210
  const startTime = performance.now();
203
- const parseResult = parseSync(stream, options);
211
+ const parseResult = parseSyncInput(stream, options);
204
212
  let mapping = null;
205
213
  let importMapping = null;
206
- if ((typeof options.module == "number" && options.module & ModuleScopeEnumOptions.ICSS) ||
207
- (typeof options.module == "object" &&
208
- typeof options.module.scoped == "number" &&
214
+ if ((typeof options.module === "number" && options.module & ModuleScopeEnumOptions.ICSS) ||
215
+ (typeof options.module === "object" &&
216
+ typeof options.module.scoped === "number" &&
209
217
  options.module.scoped & ModuleScopeEnumOptions.ICSS)) {
210
218
  mapping = parseResult.mapping;
211
219
  importMapping = parseResult.importMapping;
@@ -215,17 +223,12 @@ function transformSync(...args) {
215
223
  ...options,
216
224
  expandNestingRules: false,
217
225
  }, mapping != null ? { mapping, importMapping } : null);
218
- return {
219
- ...parseResult,
220
- ...rendered,
221
- errors: parseResult.errors.concat(rendered.errors),
222
- stats: {
223
- bytesOut: rendered.code.length,
224
- ...parseResult.stats,
225
- render: rendered.stats.total,
226
- total: `${(performance.now() - startTime).toFixed(2)}ms`,
227
- },
228
- };
226
+ return new TransformResult(parseResult, rendered, {
227
+ bytesOut: RenderResult.codeLength(rendered),
228
+ ...parseResult.stats,
229
+ render: rendered.stats.total,
230
+ total: `${(performance.now() - startTime).toFixed(2)}ms`,
231
+ });
229
232
  }
230
233
  /**
231
234
  * Parse CSS
@@ -257,7 +260,7 @@ function transformSync(...args) {
257
260
  async function parse(...args) {
258
261
  let options;
259
262
  let stream;
260
- if (typeof args[0] === "string" || args[0] instanceof ReadableStream) {
263
+ if (isCssInput(args[0]) || args[0] instanceof ReadableStream) {
261
264
  stream = args[0];
262
265
  options = args[1];
263
266
  }
@@ -266,17 +269,29 @@ async function parse(...args) {
266
269
  const { file, input, ...opt } = args[0];
267
270
  options = opt;
268
271
  if (file != null) {
269
- return Promise.resolve((options.load ?? load)(file, "", options.asStream ?? false)).then((stream) => parse(stream, { src: options.file, ...options }));
272
+ return Promise.resolve((options.load != null
273
+ ? options.load(file, "", options.asStream ?? false)
274
+ : // the default loader loads the file as an ArrayBuffer: the preferred input of the tokenizer
275
+ load(file, "", options.asStream ? ResponseType.ReadableStream : ResponseType.ArrayBuffer))).then((stream) => parse(stream, { src: options.file, ...options }));
270
276
  }
271
277
  else {
272
278
  stream = input;
273
279
  }
274
280
  }
275
- options ??= {};
281
+ // the options of the caller are not modified: they may be reused
282
+ return parseInput(stream, { ...options });
283
+ }
284
+ /**
285
+ * parse a css input. the options belong to the call: they are modified (source, parse info, ...)
286
+ * @param stream
287
+ * @param options
288
+ */
289
+ async function parseInput(stream, options) {
276
290
  options.src ??= "";
277
291
  options.sourcesMap ??= new Map();
278
292
  Object.assign(options, {
279
- load,
293
+ // a custom loader is used to load the imported files
294
+ load: options.load ?? load,
280
295
  resolve,
281
296
  dirname,
282
297
  cwd: (options.cwd ?? self.location.pathname.endsWith("/"))
@@ -289,15 +304,7 @@ async function parse(...args) {
289
304
  options.sourcesMap.set(source.id, source);
290
305
  options.source = source;
291
306
  }
292
- options.parseInfo = {
293
- stream,
294
- buffer: "",
295
- offset: 0,
296
- time: 0,
297
- source: options.source,
298
- position: 0,
299
- currentPosition: 0,
300
- };
307
+ options.parseInfo = createParseInfo(stream, options.source);
301
308
  return doParse(stream instanceof ReadableStream
302
309
  ? new Tokenizer(options.parseInfo, stream).tokenizeStream()
303
310
  : new Tokenizer(options.parseInfo), options).then((result) => options.module == null && options.inputSourceMap == null && !options.sourcemap
@@ -328,7 +335,7 @@ async function parse(...args) {
328
335
  async function transform(...args) {
329
336
  let options;
330
337
  let stream;
331
- if (typeof args[0] === "string" || args[0] instanceof ReadableStream) {
338
+ if (isCssInput(args[0]) || args[0] instanceof ReadableStream) {
332
339
  stream = args[0];
333
340
  options = args[1];
334
341
  }
@@ -337,7 +344,10 @@ async function transform(...args) {
337
344
  const { file, input, ...opt } = args[0];
338
345
  options = opt;
339
346
  if (file != null) {
340
- return Promise.resolve((options.load ?? load)(file, "", options.asStream ?? false)).then((stream) => transform(stream, { src: options.file, ...options }));
347
+ return Promise.resolve((options.load != null
348
+ ? options.load(file, "", options.asStream ?? false)
349
+ : // the default loader loads the file as an ArrayBuffer: the preferred input of the tokenizer
350
+ load(file, "", options.asStream ? ResponseType.ReadableStream : ResponseType.ArrayBuffer))).then((stream) => transform(stream, { src: options.file, ...options }));
341
351
  }
342
352
  else {
343
353
  stream = input;
@@ -345,12 +355,12 @@ async function transform(...args) {
345
355
  }
346
356
  options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
347
357
  const startTime = performance.now();
348
- return parse(stream, options).then((parseResult) => {
358
+ return parseInput(stream, options).then((parseResult) => {
349
359
  let mapping = null;
350
360
  let importMapping = null;
351
- if ((typeof options.module == "number" && options.module & ModuleScopeEnumOptions.ICSS) ||
352
- (typeof options.module == "object" &&
353
- typeof options.module.scoped == "number" &&
361
+ if ((typeof options.module === "number" && options.module & ModuleScopeEnumOptions.ICSS) ||
362
+ (typeof options.module === "object" &&
363
+ typeof options.module.scoped === "number" &&
354
364
  options.module.scoped & ModuleScopeEnumOptions.ICSS)) {
355
365
  mapping = parseResult.mapping;
356
366
  importMapping = parseResult.importMapping;
@@ -360,18 +370,13 @@ async function transform(...args) {
360
370
  ...options,
361
371
  expandNestingRules: false,
362
372
  }, mapping != null ? { mapping, importMapping } : null);
363
- return {
364
- ...parseResult,
365
- ...rendered,
366
- errors: parseResult.errors.concat(rendered.errors),
367
- stats: {
368
- bytesOut: rendered.code.length,
369
- ...parseResult.stats,
370
- render: rendered.stats.total,
371
- total: `${(performance.now() - startTime).toFixed(2)}ms`,
372
- },
373
- };
373
+ return new TransformResult(parseResult, rendered, {
374
+ bytesOut: RenderResult.codeLength(rendered),
375
+ ...parseResult.stats,
376
+ render: rendered.stats.total,
377
+ total: `${(performance.now() - startTime).toFixed(2)}ms`,
378
+ });
374
379
  });
375
380
  }
376
381
 
377
- export { ModuleScopeEnumOptions, ResponseType, dirname, load, parse, parseSync, render, resolve, transform, transformSync };
382
+ export { ModuleScopeEnumOptions, RenderResult, ResponseType, TransformResult, dirname, load, parse, parseSync, render, resolve, transform, transformSync };
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@tbela99/css-parser",
3
- "description": "CSS parser, minifier and validator for node and the browser",
4
- "version": "2.0.1",
3
+ "description": "CSS parser, minifier and validator for node, deno, bun and the browser",
4
+ "version": "2.2.0",
5
5
  "exports": {
6
- ".": "./dist/src/node.js",
7
- "./node": "./dist/src/node.js",
8
- "./web": "./dist/src/web.js"
6
+ ".": "./dist/node.js",
7
+ "./node": "./dist/node.js",
8
+ "./web": "./dist/web.js"
9
9
  },
10
10
  "type": "module",
11
11
  "typings": "dist/index.d.ts",
@@ -14,7 +14,7 @@
14
14
  },
15
15
  "scripts": {
16
16
  "doc": "typedoc --tsconfig typedoc-tsconfig.jsonc",
17
- "build": "rollup -c;./build.sh ./dist/index.d.ts 'declare interface' 'declare type'",
17
+ "build": "rollup -c;sh build.sh ./dist/index.d.ts 'declare interface' 'declare type'",
18
18
  "watch": "rollup -c -w",
19
19
  "test:watch": "web-test-runner \"./test/specs/web.spec.js\" --timeout 30000 --node-resolve --playwright --browsers chromium firefox webkit --root-dir=. --watch",
20
20
  "test": "web-test-runner \"./test/specs/web.spec.js\" --timeout=10000 --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.; mocha --reporter-options='maxDiffSize=1801920' --timeout=10000 \"test/specs/node.spec.js\"",
@@ -24,7 +24,7 @@
24
24
  "test:web-cov": "web-test-runner -x 'test/specs/code/*.js' --node-resolve --playwright --browsers chromium firefox webkit --root-dir=. --coverage \"test/specs/web.spec.js\"",
25
25
  "profile": "deno run --allow-read --cpu-prof --cpu-prof-flamegraph test/inspect.js",
26
26
  "config-update": "esno scripts/shorthand.ts",
27
- "syntax-update": "esno scripts/validation.ts",
27
+ "syntax-update": "esno scripts/webref.ts",
28
28
  "debug:node": "mocha --reporter-options='maxDiffSize=1801920' --timeout=10000 \"test/specs/node.spec.js\"",
29
29
  "debug:web": "web-test-runner \"./test/specs/web.spec.js\" --manual --open --node-resolve --root-dir=."
30
30
  },
@@ -68,17 +68,17 @@
68
68
  "@types/chai-as-promised": "^8.0.2",
69
69
  "@types/mocha": "^10.0.10",
70
70
  "@types/node": "^26.6.6",
71
- "@types/web": "^0.0.359",
71
+ "@types/web": "^0.0.367",
72
72
  "@typescript/typescript6": "^6.0.2",
73
- "@web/test-runner": "^1.0.0",
74
- "@web/test-runner-playwright": "^1.0.0",
73
+ "@web/test-runner": "^1.0.1",
74
+ "@web/test-runner-playwright": "^1.0.2",
75
75
  "chai": "^6.3.0",
76
76
  "chai-as-promised": "^8.0.2",
77
77
  "esno": "^4.8.0",
78
78
  "markdown-it-task-lists": "^2.1.1",
79
79
  "mocha": "^12.0.3",
80
- "playwright": "^1.64.1",
81
- "rollup": "^4.67.2",
80
+ "playwright": "^1.65.1",
81
+ "rollup": "^4.68.5",
82
82
  "rollup-plugin-dts": "^6.5.1",
83
83
  "rollup-plugin-version-injector": "^1.3.3",
84
84
  "superc8": "^12.7.0",
@@ -90,4 +90,4 @@
90
90
  "typedoc-unhoax-theme": "^0.5.3"
91
91
  },
92
92
  "sideEffects": false
93
- }
93
+ }
@@ -1,10 +0,0 @@
1
- /*
2
- CSS parser, minifier and validator for node and the browser
3
-
4
- Author(s): Thierry Bela
5
- Version: 2.0.1
6
- License: MIT
7
- */
8
- var version = "2.0.1";
9
-
10
- export { version };
@@ -1,87 +0,0 @@
1
- /*
2
- CSS parser, minifier and validator for node and the browser
3
-
4
- Author(s): Thierry Bela
5
- Version: 2.0.1
6
- License: MIT
7
- */
8
- import { DeclarationMemo, PropertyList } from '../../parser/declaration/list.js';
9
- import { EnumToken, EnumAstNodeStatus } from '../types.js';
10
- import { FeatureWalkMode } from './type.js';
11
-
12
- class ComputeShorthandFeature {
13
- accept = new Set([
14
- EnumToken.RuleNodeType,
15
- EnumToken.AtRuleNodeType,
16
- EnumToken.KeyframesRuleNodeType,
17
- ]);
18
- get ordering() {
19
- return 5;
20
- }
21
- get processMode() {
22
- return FeatureWalkMode.Post;
23
- }
24
- static register(options) {
25
- if (options.computeShorthand) {
26
- // @ts-ignore
27
- options.features.push(new ComputeShorthandFeature(options));
28
- }
29
- }
30
- run(arena, ast, options, _parent, context) {
31
- if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren) || arena.firstChild[ast] == 0) {
32
- return null;
33
- }
34
- // the keys of the declarations are shared by the rules of the minification
35
- const memo = context == null ? null : (context.declarations ??= new DeclarationMemo());
36
- const list = new PropertyList(arena, options, memo);
37
- let doNotSort = false;
38
- let hasDeclarations = false;
39
- if (arena.dataTypes[ast] == EnumToken.AtRuleNodeType) {
40
- switch (arena.strings[arena.name[ast]]) {
41
- case "function":
42
- case "mixin":
43
- doNotSort = true;
44
- break;
45
- }
46
- }
47
- // the declarations (with their leading comments) are moved into the property list,
48
- // the rules (with their leading comments) stay in place
49
- let child = arena.firstChild[ast];
50
- while (child != 0) {
51
- let last = child;
52
- if (!doNotSort) {
53
- // capture comments with the next node
54
- while (arena.nextSibling[last] != 0 && arena.dataTypes[last] == EnumToken.CommentNodeType) {
55
- last = arena.nextSibling[last];
56
- }
57
- }
58
- const next = arena.nextSibling[last];
59
- // https://drafts.csswg.org/css-mixins/#:~:text=The%20value%20of%20the%20result%20descriptor%20is%20always%2016px%20in%20the%20above%20example
60
- if (doNotSort || arena.dataTypes[last] == EnumToken.DeclarationNodeType) {
61
- hasDeclarations = true;
62
- for (let node = child; node != next;) {
63
- const nextNode = arena.nextSibling[node];
64
- arena.detach(node);
65
- list.addDeclaration(node);
66
- node = nextNode;
67
- }
68
- }
69
- child = next;
70
- }
71
- if (hasDeclarations) {
72
- // the declarations are inserted before the rules
73
- const first = arena.firstChild[ast];
74
- for (const node of list) {
75
- if (first == 0) {
76
- arena.appendChild(ast, node);
77
- }
78
- else {
79
- arena.insertBefore(first, node);
80
- }
81
- }
82
- }
83
- return ast;
84
- }
85
- }
86
-
87
- export { ComputeShorthandFeature };
@@ -1,85 +0,0 @@
1
- /*
2
- CSS parser, minifier and validator for node and the browser
3
-
4
- Author(s): Thierry Bela
5
- Version: 2.0.1
6
- License: MIT
7
- */
8
- import { EnumToken, EnumAstNodeStatus } from '../types.js';
9
- import { compute } from '../transform/compute.js';
10
- import { filterValues, renderValue } from '../../printer/render.js';
11
- import { minifyTransformFunctions, eqMatrix } from '../transform/minify.js';
12
- import { FeatureWalkMode } from './type.js';
13
-
14
- /**
15
- * transform property and its prefixed versions
16
- */
17
- const transformProperty = /^(-[a-z]+-)?transform$/;
18
- class TransformCssFeature {
19
- accept = new Set([
20
- EnumToken.RuleNodeType,
21
- EnumToken.AtRuleNodeType,
22
- EnumToken.KeyframesRuleNodeType,
23
- ]);
24
- get ordering() {
25
- return 4;
26
- }
27
- get processMode() {
28
- return FeatureWalkMode.Post;
29
- }
30
- static register(options) {
31
- // @ts-ignore
32
- if (options.computeTransform) {
33
- // @ts-ignore
34
- options.features.push(new TransformCssFeature());
35
- }
36
- }
37
- run(arena, ast) {
38
- if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren)) {
39
- return null;
40
- }
41
- let node;
42
- for (node = arena.firstChild[ast]; node != 0; node = arena.nextSibling[node]) {
43
- if (arena.flags[node] & (EnumAstNodeStatus.Invalid | EnumAstNodeStatus.ValidationFailed)) {
44
- continue;
45
- }
46
- if (arena.dataTypes[node] != EnumToken.DeclarationNodeType ||
47
- !transformProperty.test(arena.strings[arena.name[node]])) {
48
- continue;
49
- }
50
- const children = [];
51
- for (const child of arena.tokens(node)) {
52
- children.push(arena.dataTypes[child] == EnumToken.TransformFunctionTokenType
53
- ? minifyTransformFunctions(arena, child)
54
- : child);
55
- }
56
- let { matrix, cumulative, minified } = compute(arena, children) ?? {
57
- matrix: null,
58
- cumulative: null,
59
- minified: null,
60
- };
61
- if (matrix == null || cumulative == null || minified == null) {
62
- arena.setTokenList(node, children);
63
- continue;
64
- }
65
- let r = [filterValues(arena, children)];
66
- if (eqMatrix(arena, matrix, cumulative)) {
67
- r.push(cumulative);
68
- }
69
- if (eqMatrix(arena, matrix, minified)) {
70
- r.push(minified);
71
- }
72
- const l = renderValue(arena, matrix).length;
73
- // inlined: arena.setTokens(node, ...r.reduce((acc: Token[], curr: Token[]): Token[] => { if (curr.reduce((acc: string, t: Token) => acc + renderValue(arena, t), "").length < l) { return curr; } return acc; }, [matrix]))
74
- arena.setTokenList(node, r.reduce((acc, curr) => {
75
- if (curr.reduce((acc, t) => acc + renderValue(arena, t), "").length < l) {
76
- return curr;
77
- }
78
- return acc;
79
- }, [matrix]));
80
- }
81
- return null;
82
- }
83
- }
84
-
85
- export { TransformCssFeature };