@tbela99/css-parser 1.3.2 → 1.3.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.
- package/CHANGELOG.md +12 -0
- package/README.md +59 -20
- package/dist/index-umd-web.js +1846 -1075
- package/dist/index.cjs +1941 -1202
- package/dist/index.d.ts +914 -181
- package/dist/lib/ast/expand.js +5 -10
- package/dist/lib/ast/features/calc.js +8 -8
- package/dist/lib/ast/features/inlinecssvariables.js +9 -8
- package/dist/lib/ast/features/prefix.js +5 -15
- package/dist/lib/ast/features/shorthand.js +5 -6
- package/dist/lib/ast/features/transform.js +18 -25
- package/dist/lib/ast/features/type.js +4 -2
- package/dist/lib/ast/minify.js +56 -112
- package/dist/lib/ast/transform/compute.js +2 -4
- package/dist/lib/ast/transform/matrix.js +20 -20
- package/dist/lib/ast/transform/minify.js +105 -12
- package/dist/lib/ast/transform/rotate.js +11 -11
- package/dist/lib/ast/transform/scale.js +6 -6
- package/dist/lib/ast/transform/skew.js +4 -4
- package/dist/lib/ast/transform/translate.js +3 -3
- package/dist/lib/ast/transform/utils.js +30 -37
- package/dist/lib/ast/types.js +16 -4
- package/dist/lib/ast/walk.js +172 -70
- package/dist/lib/fs/resolve.js +12 -7
- package/dist/lib/parser/declaration/list.js +3 -1
- package/dist/lib/parser/parse.js +441 -161
- package/dist/lib/parser/tokenize.js +12 -14
- package/dist/lib/renderer/render.js +7 -7
- package/dist/lib/syntax/color/cmyk.js +6 -3
- package/dist/lib/syntax/color/color-mix.js +2 -3
- package/dist/lib/syntax/color/color.js +28 -6
- package/dist/lib/syntax/color/hex.js +3 -0
- package/dist/lib/syntax/color/hsl.js +18 -7
- package/dist/lib/syntax/color/hwb.js +3 -3
- package/dist/lib/syntax/color/lab.js +4 -4
- package/dist/lib/syntax/color/lch.js +7 -4
- package/dist/lib/syntax/color/oklab.js +4 -4
- package/dist/lib/syntax/color/oklch.js +18 -6
- package/dist/lib/syntax/color/relativecolor.js +9 -56
- package/dist/lib/syntax/color/srgb.js +1 -1
- package/dist/lib/syntax/syntax.js +36 -18
- package/dist/lib/validation/at-rules/container.js +11 -0
- package/dist/lib/validation/at-rules/counter-style.js +11 -0
- package/dist/lib/validation/at-rules/font-feature-values.js +11 -0
- package/dist/lib/validation/at-rules/keyframes.js +11 -0
- package/dist/lib/validation/at-rules/layer.js +11 -0
- package/dist/lib/validation/at-rules/media.js +11 -0
- package/dist/lib/validation/at-rules/page-margin-box.js +11 -0
- package/dist/lib/validation/at-rules/page.js +11 -0
- package/dist/lib/validation/at-rules/supports.js +11 -0
- package/dist/lib/validation/at-rules/when.js +11 -0
- package/dist/lib/validation/config.js +0 -2
- package/dist/lib/validation/config.json.js +21 -9
- package/dist/lib/validation/parser/parse.js +53 -2
- package/dist/lib/validation/syntax.js +199 -36
- package/dist/node.js +63 -36
- package/dist/web.js +84 -25
- package/package.json +7 -5
- package/dist/lib/validation/parser/types.js +0 -54
package/dist/web.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { ColorType, EnumToken, ValidationLevel } from './lib/ast/types.js';
|
|
2
2
|
export { minify } from './lib/ast/minify.js';
|
|
3
|
-
export {
|
|
3
|
+
export { WalkerEvent, WalkerOptionEnum, 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';
|
|
@@ -13,7 +13,6 @@ export { convertColor } from './lib/syntax/color/color.js';
|
|
|
13
13
|
import './lib/syntax/color/utils/constants.js';
|
|
14
14
|
export { isOkLabClose, okLabDistance } from './lib/syntax/color/utils/distance.js';
|
|
15
15
|
import './lib/validation/config.js';
|
|
16
|
-
import './lib/validation/parser/types.js';
|
|
17
16
|
import './lib/validation/parser/parse.js';
|
|
18
17
|
import './lib/validation/syntaxes/complex-selector.js';
|
|
19
18
|
import './lib/validation/syntax.js';
|
|
@@ -21,17 +20,14 @@ import { matchUrl, resolve, dirname } from './lib/fs/resolve.js';
|
|
|
21
20
|
export { FeatureWalkMode } from './lib/ast/features/type.js';
|
|
22
21
|
|
|
23
22
|
/**
|
|
24
|
-
*
|
|
25
|
-
* @module web
|
|
26
|
-
*/
|
|
27
|
-
/**
|
|
28
|
-
* load file or url as stream
|
|
23
|
+
* default file or url loader
|
|
29
24
|
* @param url
|
|
30
25
|
* @param currentFile
|
|
31
26
|
*
|
|
27
|
+
* @param asStream
|
|
32
28
|
* @private
|
|
33
29
|
*/
|
|
34
|
-
async function
|
|
30
|
+
async function load(url, currentFile = '.', asStream = false) {
|
|
35
31
|
let t;
|
|
36
32
|
if (matchUrl.test(url)) {
|
|
37
33
|
t = new URL(url);
|
|
@@ -41,25 +37,45 @@ async function getStream(url, currentFile = '.') {
|
|
|
41
37
|
}
|
|
42
38
|
else {
|
|
43
39
|
const path = resolve(url, currentFile).absolute;
|
|
44
|
-
// @ts-ignore
|
|
45
40
|
t = new URL(path, self.origin);
|
|
46
41
|
}
|
|
47
|
-
|
|
48
|
-
return fetch(t, t.origin != self.origin ? { mode: 'cors' } : {}).then((response) => {
|
|
42
|
+
return fetch(t, t.origin != self.origin ? { mode: 'cors' } : {}).then(async (response) => {
|
|
49
43
|
if (!response.ok) {
|
|
50
44
|
throw new Error(`${response.status} ${response.statusText} ${response.url}`);
|
|
51
45
|
}
|
|
52
|
-
return response.body;
|
|
46
|
+
return asStream ? response.body : await response.text();
|
|
53
47
|
});
|
|
54
48
|
}
|
|
55
49
|
/**
|
|
56
|
-
* render ast
|
|
50
|
+
* render the ast tree
|
|
57
51
|
* @param data
|
|
58
52
|
* @param options
|
|
53
|
+
*
|
|
54
|
+
* Example:
|
|
55
|
+
*
|
|
56
|
+
* ```ts
|
|
57
|
+
*
|
|
58
|
+
* import {render, ColorType} from '@tbela99/css-parser';
|
|
59
|
+
*
|
|
60
|
+
* const css = 'body { color: color(from hsl(0 100% 50%) xyz x y z); }';
|
|
61
|
+
* const parseResult = await parse(css);
|
|
62
|
+
*
|
|
63
|
+
* let renderResult = render(parseResult.ast);
|
|
64
|
+
* console.log(result.code);
|
|
65
|
+
*
|
|
66
|
+
* // body{color:red}
|
|
67
|
+
*
|
|
68
|
+
*
|
|
69
|
+
* renderResult = render(parseResult.ast, {beautify: true, convertColor: ColorType.SRGB});
|
|
70
|
+
* console.log(renderResult.code);
|
|
71
|
+
*
|
|
72
|
+
* // body {
|
|
73
|
+
* // color: color(srgb 1 0 0)
|
|
74
|
+
* // }
|
|
75
|
+
* ```
|
|
59
76
|
*/
|
|
60
77
|
function render(data, options = {}) {
|
|
61
78
|
return doRender(data, Object.assign(options, {
|
|
62
|
-
getStream,
|
|
63
79
|
resolve,
|
|
64
80
|
dirname,
|
|
65
81
|
cwd: options.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
|
|
@@ -69,32 +85,74 @@ function render(data, options = {}) {
|
|
|
69
85
|
* parse css file
|
|
70
86
|
* @param file url or path
|
|
71
87
|
* @param options
|
|
88
|
+
* @param asStream load file as stream
|
|
89
|
+
*
|
|
90
|
+
* @throws Error file not found
|
|
91
|
+
*
|
|
92
|
+
* Example:
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
*
|
|
96
|
+
* import {parseFile} from '@tbela99/css-parser/web';
|
|
97
|
+
*
|
|
98
|
+
* // remote file
|
|
99
|
+
* let result = await parseFile('https://docs.deno.com/styles.css');
|
|
100
|
+
* console.log(result.ast);
|
|
101
|
+
*
|
|
102
|
+
* // local file
|
|
103
|
+
* result = await parseFile('./css/styles.css');
|
|
104
|
+
* console.log(result.ast);
|
|
105
|
+
* ```
|
|
72
106
|
*/
|
|
73
|
-
async function parseFile(file, options = {}) {
|
|
74
|
-
return
|
|
107
|
+
async function parseFile(file, options = {}, asStream = false) {
|
|
108
|
+
return Promise.resolve((options.load ?? load)(file, '.', asStream)).then(stream => parse(stream, { src: file, ...options }));
|
|
75
109
|
}
|
|
76
110
|
/**
|
|
77
111
|
* parse css
|
|
78
112
|
* @param stream
|
|
79
|
-
* @param
|
|
113
|
+
* @param options
|
|
114
|
+
*
|
|
115
|
+
* Example:
|
|
116
|
+
*
|
|
117
|
+
* ```ts
|
|
118
|
+
*
|
|
119
|
+
* import {parse} from '@tbela99/css-parser/web';
|
|
120
|
+
*
|
|
121
|
+
* // css string
|
|
122
|
+
* const result = await parse(css);
|
|
123
|
+
* console.log(result.ast);
|
|
124
|
+
* ```
|
|
125
|
+
*
|
|
126
|
+
* Example using fetch and readable stream
|
|
127
|
+
*
|
|
128
|
+
* ```ts
|
|
129
|
+
*
|
|
130
|
+
* import {parse} from '@tbela99/css-parser/web';
|
|
131
|
+
*
|
|
132
|
+
* const response = await fetch('https://docs.deno.com/styles.css');
|
|
133
|
+
* const result = await parse(response.body, {beautify: true});
|
|
134
|
+
*
|
|
135
|
+
* console.log(result.ast);
|
|
136
|
+
* ```
|
|
80
137
|
*/
|
|
81
|
-
async function parse(stream,
|
|
138
|
+
async function parse(stream, options = {}) {
|
|
82
139
|
return doParse(stream instanceof ReadableStream ? tokenizeStream(stream) : tokenize({
|
|
83
140
|
stream,
|
|
84
141
|
buffer: '',
|
|
85
142
|
position: { ind: 0, lin: 1, col: 1 },
|
|
86
143
|
currentPosition: { ind: -1, lin: 1, col: 0 }
|
|
87
|
-
}), Object.assign(
|
|
88
|
-
|
|
144
|
+
}), Object.assign(options, {
|
|
145
|
+
load,
|
|
89
146
|
resolve,
|
|
90
147
|
dirname,
|
|
91
|
-
cwd:
|
|
148
|
+
cwd: options.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
|
|
92
149
|
}));
|
|
93
150
|
}
|
|
94
151
|
/**
|
|
95
152
|
* transform css file
|
|
96
153
|
* @param file url or path
|
|
97
154
|
* @param options
|
|
155
|
+
* @param asStream load file as stream
|
|
98
156
|
*
|
|
99
157
|
* Example:
|
|
100
158
|
*
|
|
@@ -111,8 +169,8 @@ async function parse(stream, opt = {}) {
|
|
|
111
169
|
* console.log(result.code);
|
|
112
170
|
* ```
|
|
113
171
|
*/
|
|
114
|
-
async function transformFile(file, options = {}) {
|
|
115
|
-
return
|
|
172
|
+
async function transformFile(file, options = {}, asStream = false) {
|
|
173
|
+
return Promise.resolve((options.load ?? load)(file, '.', asStream)).then(stream => transform(stream, { src: file, ...options }));
|
|
116
174
|
}
|
|
117
175
|
/**
|
|
118
176
|
* transform css
|
|
@@ -140,7 +198,8 @@ async function transform(css, options = {}) {
|
|
|
140
198
|
options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
|
|
141
199
|
const startTime = performance.now();
|
|
142
200
|
return parse(css, options).then((parseResult) => {
|
|
143
|
-
|
|
201
|
+
// ast already expanded by parse
|
|
202
|
+
const rendered = render(parseResult.ast, { ...options, expandNestingRules: false });
|
|
144
203
|
return {
|
|
145
204
|
...parseResult,
|
|
146
205
|
...rendered,
|
|
@@ -155,4 +214,4 @@ async function transform(css, options = {}) {
|
|
|
155
214
|
});
|
|
156
215
|
}
|
|
157
216
|
|
|
158
|
-
export { dirname,
|
|
217
|
+
export { dirname, load, parse, parseFile, render, resolve, transform, transformFile };
|
package/package.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbela99/css-parser",
|
|
3
|
-
"description": "CSS parser for node and the browser",
|
|
4
|
-
"version": "v1.3.
|
|
3
|
+
"description": "CSS parser, minifier and validator for node and the browser",
|
|
4
|
+
"version": "v1.3.4",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dist/node.js",
|
|
7
7
|
"./node": "./dist/node.js",
|
|
8
|
-
"./umd": "./dist/index-umd-web.js",
|
|
9
8
|
"./web": "./dist/web.js",
|
|
10
9
|
"./cjs": "./dist/index.cjs"
|
|
11
10
|
},
|
|
@@ -43,7 +42,10 @@
|
|
|
43
42
|
"css-nesting",
|
|
44
43
|
"css-compiler",
|
|
45
44
|
"nested-css",
|
|
46
|
-
"walker"
|
|
45
|
+
"walker",
|
|
46
|
+
"stream",
|
|
47
|
+
"streaming",
|
|
48
|
+
"streaming-parser"
|
|
47
49
|
],
|
|
48
50
|
"author": "Thierry Bela",
|
|
49
51
|
"license": "MIT OR LGPL-3.0",
|
|
@@ -70,7 +72,7 @@
|
|
|
70
72
|
"rollup": "^4.48.0",
|
|
71
73
|
"rollup-plugin-dts": "^6.2.1",
|
|
72
74
|
"tslib": "^2.8.1",
|
|
73
|
-
"typedoc": "^0.28.
|
|
75
|
+
"typedoc": "^0.28.13",
|
|
74
76
|
"typedoc-material-theme": "^1.4.0"
|
|
75
77
|
}
|
|
76
78
|
}
|
|
@@ -1,54 +0,0 @@
|
|
|
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["ValidationFunctionDefinition"] = 5] = "ValidationFunctionDefinition";
|
|
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["AtLeastOnce"] = 24] = "AtLeastOnce";
|
|
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 = {}));
|
|
45
|
-
var ValidationSyntaxGroupEnum;
|
|
46
|
-
(function (ValidationSyntaxGroupEnum) {
|
|
47
|
-
ValidationSyntaxGroupEnum["Declarations"] = "declarations";
|
|
48
|
-
ValidationSyntaxGroupEnum["Functions"] = "functions";
|
|
49
|
-
ValidationSyntaxGroupEnum["Syntaxes"] = "syntaxes";
|
|
50
|
-
ValidationSyntaxGroupEnum["Selectors"] = "selectors";
|
|
51
|
-
ValidationSyntaxGroupEnum["AtRules"] = "atRules";
|
|
52
|
-
})(ValidationSyntaxGroupEnum || (ValidationSyntaxGroupEnum = {}));
|
|
53
|
-
|
|
54
|
-
export { ValidationSyntaxGroupEnum, ValidationTokenEnum };
|