@tbela99/css-parser 1.4.11 → 1.6.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.
- package/CHANGELOG.md +39 -0
- package/README.md +18 -11
- package/dist/index-umd-web.js +6558 -5310
- package/dist/index.cjs +6576 -5313
- package/dist/index.d.ts +805 -279
- package/dist/lib/ast/clone.js +13 -8
- package/dist/lib/ast/expand.js +65 -15
- package/dist/lib/ast/features/calc.js +19 -56
- package/dist/lib/ast/features/if.js +28 -4
- package/dist/lib/ast/features/inlinecssvariables.js +6 -5
- package/dist/lib/ast/features/prefix.js +52 -73
- package/dist/lib/ast/features/shorthand.js +11 -6
- package/dist/lib/ast/features/transform.js +6 -3
- package/dist/lib/ast/find.js +7 -5
- package/dist/lib/ast/math/expression.js +190 -32
- package/dist/lib/ast/minify.js +117 -49
- package/dist/lib/ast/node.js +52 -0
- package/dist/lib/ast/transform/compute.js +63 -4
- package/dist/lib/ast/transform/minify.js +6 -6
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.js +1 -1
- package/dist/lib/ast/transform/scale.js +1 -1
- package/dist/lib/ast/transform/skew.js +1 -1
- package/dist/lib/ast/transform/translate.js +1 -1
- package/dist/lib/ast/transform/utils.js +46 -18
- package/dist/lib/ast/types.js +1 -1
- package/dist/lib/ast/walk.js +55 -11
- package/dist/lib/fs/resolve.js +68 -27
- package/dist/lib/parser/declaration/list.js +22 -7
- package/dist/lib/parser/declaration/map.js +40 -13
- package/dist/lib/parser/declaration/set.js +3 -1
- package/dist/lib/parser/linesmap.js +2 -12
- package/dist/lib/parser/parse.js +843 -912
- package/dist/lib/parser/source.js +16 -2
- package/dist/lib/parser/tokenize.js +1489 -725
- package/dist/lib/parser/utils/at-rule-container.js +29 -196
- package/dist/lib/parser/utils/at-rule-generic.js +6 -7
- package/dist/lib/parser/utils/at-rule-import.js +17 -13
- package/dist/lib/parser/utils/at-rule-media.js +36 -19
- package/dist/lib/parser/utils/at-rule-support.js +19 -9
- package/dist/lib/parser/utils/at-rule-when-else.js +11 -38
- package/dist/lib/parser/utils/at-rule.js +0 -18
- package/dist/lib/parser/utils/declaration.js +30 -95
- package/dist/lib/parser/utils/hash.js +4 -4
- package/dist/lib/parser/utils/selector.js +164 -141
- package/dist/lib/parser/utils/text.js +4 -2
- package/dist/lib/renderer/render.js +230 -125
- package/dist/lib/renderer/sourcemap/lib/codec.js +78 -0
- package/dist/lib/renderer/sourcemap/sourcemap.js +187 -34
- package/dist/lib/syntax/color/a98rgb.js +14 -4
- package/dist/lib/syntax/color/cmyk.js +10 -18
- package/dist/lib/syntax/color/color-mix.js +21 -34
- package/dist/lib/syntax/color/color.js +32 -49
- package/dist/lib/syntax/color/hsl.js +15 -15
- package/dist/lib/syntax/color/hwb.js +18 -17
- package/dist/lib/syntax/color/lab.js +17 -17
- package/dist/lib/syntax/color/lch.js +13 -13
- package/dist/lib/syntax/color/oklab.js +10 -11
- package/dist/lib/syntax/color/oklch.js +15 -18
- package/dist/lib/syntax/color/p3.js +18 -12
- package/dist/lib/syntax/color/prophotorgb.js +26 -28
- package/dist/lib/syntax/color/rec2020.js +8 -4
- package/dist/lib/syntax/color/relative-color.js +26 -10
- package/dist/lib/syntax/color/srgb.js +5 -4
- package/dist/lib/syntax/color/utils/distance.js +1 -1
- package/dist/lib/syntax/color/xyz.js +2 -2
- package/dist/lib/syntax/color/xyzd50.js +2 -2
- package/dist/lib/syntax/constants.js +12 -2
- package/dist/lib/syntax/syntax.js +45 -258
- package/dist/lib/validation/config.json.js +3 -0
- package/dist/lib/validation/match.js +110 -27
- package/dist/node.js +55 -33
- package/dist/types.d.ts +5 -1
- package/dist/types.js +4 -0
- package/dist/utils/sync.d.ts +16 -0
- package/dist/utils/sync.js +73 -0
- package/dist/web.js +37 -30
- package/package.json +1 -1
- package/dist/lib/renderer/sourcemap/lib/encode.js +0 -37
- package/llms.txt +0 -73
package/dist/web.js
CHANGED
|
@@ -4,10 +4,11 @@ import { doRender } from './lib/renderer/render.js';
|
|
|
4
4
|
export { renderValue as renderToken } from './lib/renderer/render.js';
|
|
5
5
|
import { ModuleScopeEnumOptions } from './lib/ast/types.js';
|
|
6
6
|
export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, ValidationLevel } from './lib/ast/types.js';
|
|
7
|
-
import {
|
|
7
|
+
import { Tokenizer } from './lib/parser/tokenize.js';
|
|
8
8
|
import { matchUrl, resolve, dirname } from './lib/fs/resolve.js';
|
|
9
9
|
import { ResponseType } from './types.js';
|
|
10
10
|
import { SourceFile } from './lib/parser/source.js';
|
|
11
|
+
import { parseResult, validateSyncArguments } from './utils/sync.js';
|
|
11
12
|
export { minify } from './lib/ast/minify.js';
|
|
12
13
|
export { expand } from './lib/ast/expand.js';
|
|
13
14
|
export { WalkerEvent, WalkerOptionEnum, walk, walkValues } from './lib/ast/walk.js';
|
|
@@ -18,6 +19,7 @@ export { cloneNode } from './lib/ast/clone.js';
|
|
|
18
19
|
export { replaceNodeOrValue } from './lib/parser/utils/token.js';
|
|
19
20
|
export { SourceMap } from './lib/renderer/sourcemap/sourcemap.js';
|
|
20
21
|
export { FeatureWalkMode } from './lib/ast/features/type.js';
|
|
22
|
+
export { getNodeProperty, setNodeProperty } from './lib/ast/node.js';
|
|
21
23
|
|
|
22
24
|
/**
|
|
23
25
|
* Load file or url
|
|
@@ -27,7 +29,7 @@ export { FeatureWalkMode } from './lib/ast/features/type.js';
|
|
|
27
29
|
* @throws Error file not found
|
|
28
30
|
*
|
|
29
31
|
* ```ts
|
|
30
|
-
* import {load, ResponseType} from '@tbela99/css-parser';
|
|
32
|
+
* import {load, ResponseType} from '@tbela99/css-parser/web';
|
|
31
33
|
* const result = await load(file, '.', ResponseType.ArrayBuffer) as ArrayBuffer;
|
|
32
34
|
* ```
|
|
33
35
|
*/
|
|
@@ -56,6 +58,9 @@ async function load(url, currentDirectory = ".", responseType = false) {
|
|
|
56
58
|
if (responseType == ResponseType.ArrayBuffer) {
|
|
57
59
|
return response.arrayBuffer();
|
|
58
60
|
}
|
|
61
|
+
if (responseType == ResponseType.JSON) {
|
|
62
|
+
return response.json();
|
|
63
|
+
}
|
|
59
64
|
return responseType == ResponseType.ReadableStream ? response.body : response.text();
|
|
60
65
|
});
|
|
61
66
|
}
|
|
@@ -69,7 +74,7 @@ async function load(url, currentDirectory = ".", responseType = false) {
|
|
|
69
74
|
*
|
|
70
75
|
* ```ts
|
|
71
76
|
*
|
|
72
|
-
* import {render, ColorType} from '@tbela99/css-parser';
|
|
77
|
+
* import {render, ColorType} from '@tbela99/css-parser/web';
|
|
73
78
|
*
|
|
74
79
|
* const css = 'body { color: color(from hsl(0 100% 50%) xyz x y z); }';
|
|
75
80
|
* const parseResult = await parse(css);
|
|
@@ -98,7 +103,7 @@ function render(data, options = {}, mapping) {
|
|
|
98
103
|
}), mapping);
|
|
99
104
|
}
|
|
100
105
|
/**
|
|
101
|
-
* Parse
|
|
106
|
+
* Parse CSS file
|
|
102
107
|
* @param file url or path
|
|
103
108
|
* @param options
|
|
104
109
|
* @param asStream load file as stream
|
|
@@ -126,17 +131,17 @@ async function parseFile(file, options = {}, asStream = false) {
|
|
|
126
131
|
return parse({ file, asStream, ...options });
|
|
127
132
|
}
|
|
128
133
|
/**
|
|
129
|
-
* Parse
|
|
134
|
+
* Parse CSS
|
|
130
135
|
* @param args
|
|
131
136
|
*
|
|
132
137
|
* Parsing a string
|
|
133
138
|
*
|
|
134
139
|
* ```ts
|
|
135
140
|
*
|
|
136
|
-
* import {
|
|
141
|
+
* import {parseSync} from '@tbela99/css-parser/web';
|
|
137
142
|
*
|
|
138
143
|
* // css string
|
|
139
|
-
* let result = await
|
|
144
|
+
* let result = await parseSync(css, {nestingRules: true});
|
|
140
145
|
* console.log(result.ast);
|
|
141
146
|
* ```
|
|
142
147
|
*
|
|
@@ -153,9 +158,12 @@ function parseSync(...args) {
|
|
|
153
158
|
options = opt;
|
|
154
159
|
stream = input;
|
|
155
160
|
}
|
|
161
|
+
if (options != null) {
|
|
162
|
+
validateSyncArguments(options);
|
|
163
|
+
}
|
|
156
164
|
options ??= {};
|
|
157
165
|
options.src ??= "";
|
|
158
|
-
options.sourcesMap ??= new Map;
|
|
166
|
+
options.sourcesMap ??= new Map();
|
|
159
167
|
Object.assign(options, {
|
|
160
168
|
resolve,
|
|
161
169
|
dirname,
|
|
@@ -177,26 +185,26 @@ function parseSync(...args) {
|
|
|
177
185
|
time: 0,
|
|
178
186
|
source: options.source,
|
|
179
187
|
position: 0,
|
|
180
|
-
currentPosition:
|
|
188
|
+
currentPosition: 0,
|
|
181
189
|
};
|
|
182
|
-
const result = doParseSync(
|
|
183
|
-
|
|
184
|
-
|
|
190
|
+
const result = doParseSync(new Tokenizer(options.parseInfo), options);
|
|
191
|
+
return options.module == null && options.inputSourceMap == null && !options.sourcemap
|
|
192
|
+
? result
|
|
193
|
+
: parseResult(result, options);
|
|
185
194
|
}
|
|
186
195
|
/**
|
|
187
|
-
* Transform
|
|
188
|
-
* @param css
|
|
189
|
-
* @param options
|
|
196
|
+
* Transform CSS
|
|
190
197
|
*
|
|
191
198
|
* ```ts
|
|
192
199
|
*
|
|
193
|
-
* import {
|
|
200
|
+
* import {transformSync} from '@tbela99/css-parser/web';
|
|
194
201
|
*
|
|
195
202
|
* // css string
|
|
196
|
-
* const result =
|
|
203
|
+
* const result = transformSync(css);
|
|
197
204
|
* console.log(result.code);
|
|
198
205
|
* ```
|
|
199
206
|
*
|
|
207
|
+
* @param args
|
|
200
208
|
*/
|
|
201
209
|
function transformSync(...args) {
|
|
202
210
|
let options;
|
|
@@ -245,9 +253,7 @@ function transformSync(...args) {
|
|
|
245
253
|
};
|
|
246
254
|
}
|
|
247
255
|
/**
|
|
248
|
-
* Parse
|
|
249
|
-
* @param stream
|
|
250
|
-
* @param options
|
|
256
|
+
* Parse CSS
|
|
251
257
|
*
|
|
252
258
|
* Example:
|
|
253
259
|
*
|
|
@@ -271,6 +277,7 @@ function transformSync(...args) {
|
|
|
271
277
|
*
|
|
272
278
|
* console.log(result.ast);
|
|
273
279
|
* ```
|
|
280
|
+
* @param args
|
|
274
281
|
*/
|
|
275
282
|
async function parse(...args) {
|
|
276
283
|
let options;
|
|
@@ -292,7 +299,7 @@ async function parse(...args) {
|
|
|
292
299
|
}
|
|
293
300
|
options ??= {};
|
|
294
301
|
options.src ??= "";
|
|
295
|
-
options.sourcesMap ??= new Map;
|
|
302
|
+
options.sourcesMap ??= new Map();
|
|
296
303
|
Object.assign(options, {
|
|
297
304
|
load,
|
|
298
305
|
resolve,
|
|
@@ -314,15 +321,16 @@ async function parse(...args) {
|
|
|
314
321
|
time: 0,
|
|
315
322
|
source: options.source,
|
|
316
323
|
position: 0,
|
|
317
|
-
currentPosition:
|
|
324
|
+
currentPosition: 0,
|
|
318
325
|
};
|
|
319
|
-
return doParse(stream instanceof ReadableStream
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
326
|
+
return doParse(stream instanceof ReadableStream
|
|
327
|
+
? new Tokenizer(options.parseInfo, stream).tokenizeStream()
|
|
328
|
+
: new Tokenizer(options.parseInfo), options).then((result) => options.module == null && options.inputSourceMap == null && !options.sourcemap
|
|
329
|
+
? result
|
|
330
|
+
: parseResult(result, options));
|
|
323
331
|
}
|
|
324
332
|
/**
|
|
325
|
-
* Transform
|
|
333
|
+
* Transform CSS file
|
|
326
334
|
* @param file url or path
|
|
327
335
|
* @param options
|
|
328
336
|
* @param asStream load file as stream
|
|
@@ -352,9 +360,7 @@ async function transformFile(file, options = {}, asStream = false) {
|
|
|
352
360
|
});
|
|
353
361
|
}
|
|
354
362
|
/**
|
|
355
|
-
* Transform
|
|
356
|
-
* @param css
|
|
357
|
-
* @param options
|
|
363
|
+
* Transform CSS
|
|
358
364
|
*
|
|
359
365
|
* Example:
|
|
360
366
|
*
|
|
@@ -372,6 +378,7 @@ async function transformFile(file, options = {}, asStream = false) {
|
|
|
372
378
|
*
|
|
373
379
|
* console.log(result.code);
|
|
374
380
|
* ```
|
|
381
|
+
* @param args
|
|
375
382
|
*/
|
|
376
383
|
async function transform(...args) {
|
|
377
384
|
let options;
|
package/package.json
CHANGED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
// from https://github.com/Rich-Harris/vlq/tree/master
|
|
2
|
-
// credit: Rich Harris
|
|
3
|
-
const integer_to_char = {};
|
|
4
|
-
let i = 0;
|
|
5
|
-
for (const char of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=') {
|
|
6
|
-
integer_to_char[i++] = char;
|
|
7
|
-
}
|
|
8
|
-
function encode(value) {
|
|
9
|
-
if (typeof value === 'number') {
|
|
10
|
-
return encode_integer(value);
|
|
11
|
-
}
|
|
12
|
-
let result = '';
|
|
13
|
-
for (let i = 0; i < value.length; i += 1) {
|
|
14
|
-
result += encode_integer(value[i]);
|
|
15
|
-
}
|
|
16
|
-
return result;
|
|
17
|
-
}
|
|
18
|
-
function encode_integer(num) {
|
|
19
|
-
let result = '';
|
|
20
|
-
if (num < 0) {
|
|
21
|
-
num = (-num << 1) | 1;
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
num <<= 1;
|
|
25
|
-
}
|
|
26
|
-
do {
|
|
27
|
-
let clamped = num & 31;
|
|
28
|
-
num >>>= 5;
|
|
29
|
-
if (num > 0) {
|
|
30
|
-
clamped |= 32;
|
|
31
|
-
}
|
|
32
|
-
result += integer_to_char[clamped];
|
|
33
|
-
} while (num > 0);
|
|
34
|
-
return result;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export { encode };
|
package/llms.txt
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
# css-parser
|
|
2
|
-
|
|
3
|
-
## Project overview
|
|
4
|
-
- This repository contains css-parser, a dependency-free CSS parser, transformer, minifier, and validator for Node.js and browsers.
|
|
5
|
-
- The library follows the CSS Syntax Module Level 3 specification and uses MDN syntax data for validation.
|
|
6
|
-
- It is designed for fault-tolerant parsing, AST manipulation, CSS Modules, minification, color processing, and syntax lowering.
|
|
7
|
-
|
|
8
|
-
## Installation
|
|
9
|
-
- npm: npm install @tbela99/css-parser
|
|
10
|
-
- jsr: deno add @tbela99/css-parser
|
|
11
|
-
- Main entry points:
|
|
12
|
-
- @tbela99/css-parser or @tbela99/css-parser/node
|
|
13
|
-
- @tbela99/css-parser/web
|
|
14
|
-
|
|
15
|
-
## Core APIs
|
|
16
|
-
- parse(css, options): async api to parse CSS and returns an AST, errors, and stats.
|
|
17
|
-
- parseSync(css, options): sync api to parse CSS and returns an AST, errors, and stats.
|
|
18
|
-
- render(ast, options): renders an AST back to CSS text.
|
|
19
|
-
- transform(css, options): async api to parse CSS and generate output in one step.
|
|
20
|
-
- transformSync(css, options): sync api to parse CSS and generate output in one step.
|
|
21
|
-
- walk(ast, filter): traverses the AST for custom transformations.
|
|
22
|
-
|
|
23
|
-
## Common capabilities
|
|
24
|
-
- Parse CSS into an AST and inspect or modify it.
|
|
25
|
-
- Validate declarations against CSS syntax definitions.
|
|
26
|
-
- Support CSS Modules with scoped class generation.
|
|
27
|
-
- Minify CSS safely with options such as inlineCssVariables, computeCalcExpression, removeDuplicateDeclarations, and beautify.
|
|
28
|
-
- Transform ASTs through visitors and custom traversal logic.
|
|
29
|
-
- Lower modern CSS syntax such as nested CSS and if() to broadly compatible output.
|
|
30
|
-
- Generate source maps and handle advanced color functions and conversions.
|
|
31
|
-
|
|
32
|
-
## Typical usage
|
|
33
|
-
```ts
|
|
34
|
-
import {transform, ColorType} from '@tbela99/css-parser';
|
|
35
|
-
|
|
36
|
-
const css = `.foo { color: red; }`;
|
|
37
|
-
const result = await transform(css, {
|
|
38
|
-
beautify: true,
|
|
39
|
-
convertColor: ColorType.SRGB,
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
console.log(result.code);
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
## Important behavior notes
|
|
46
|
-
- parse() and transform() are lenient by default and preserve unknown constructs unless configured otherwise.
|
|
47
|
-
- Comments are removed by default; preserve them with removeComments: false or preserveLicense: true.
|
|
48
|
-
- Validation errors are available through the parse/transform result and through node.state and node.errors.
|
|
49
|
-
- The library prioritizes compact output while preserving semantics.
|
|
50
|
-
|
|
51
|
-
## Useful concepts
|
|
52
|
-
- parse() returns AST, errors, and stats.
|
|
53
|
-
- render() turns an AST into CSS text.
|
|
54
|
-
- transform() performs parsing and rendering together.
|
|
55
|
-
- AST node types include StyleSheet, Rule, AtRule, Declaration, Comment, and Keyframes variants.
|
|
56
|
-
|
|
57
|
-
## Documentation files
|
|
58
|
-
- files/getting-started.md
|
|
59
|
-
- files/usage.md
|
|
60
|
-
- files/validation.md
|
|
61
|
-
- files/css-module.md
|
|
62
|
-
- files/minification.md
|
|
63
|
-
- files/transform.md
|
|
64
|
-
- files/syntax-lowering.md
|
|
65
|
-
- files/ast.md
|
|
66
|
-
- files/utilities.md
|
|
67
|
-
|
|
68
|
-
## Repository layout
|
|
69
|
-
- src/: parser implementation and core library code
|
|
70
|
-
- test/: regression and behavior tests
|
|
71
|
-
- files/: documentation content used by the docs site
|
|
72
|
-
- benchmark/: performance and minification benchmarks
|
|
73
|
-
- scripts/: helper scripts and patches
|