@tbela99/css-parser 1.8.1 → 2.0.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 +66 -0
- package/README.md +129 -31
- package/dist/index-umd-web.js +27382 -24086
- package/dist/index.d.ts +2737 -2200
- package/dist/package.json.js +2 -2
- package/dist/src/data/properties.json.js +1 -1
- package/dist/src/data/validation.json.js +1 -1
- package/dist/src/lib/ast/arena-node.js +548 -0
- package/dist/src/lib/ast/arena.js +3198 -0
- package/dist/src/lib/ast/features/calc.js +85 -76
- package/dist/src/lib/ast/features/if.js +155 -115
- package/dist/src/lib/ast/features/index.js +1 -1
- package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
- package/dist/src/lib/ast/features/prefix.js +184 -183
- package/dist/src/lib/ast/features/shorthand.js +40 -46
- package/dist/src/lib/ast/features/transform.js +24 -24
- package/dist/src/lib/ast/features/type.js +1 -1
- package/dist/src/lib/ast/fingerprint.js +333 -0
- package/dist/src/lib/ast/math/expression.js +458 -543
- package/dist/src/lib/ast/math/math.js +19 -41
- package/dist/src/lib/ast/minify.js +590 -647
- package/dist/src/lib/ast/transform/compute.js +82 -85
- package/dist/src/lib/ast/transform/matrix.js +21 -38
- package/dist/src/lib/ast/transform/minify.js +82 -193
- package/dist/src/lib/ast/transform/perspective.js +2 -3
- package/dist/src/lib/ast/transform/rotate.js +1 -1
- package/dist/src/lib/ast/transform/scale.js +1 -1
- package/dist/src/lib/ast/transform/skew.js +1 -1
- package/dist/src/lib/ast/transform/translate.js +1 -1
- package/dist/src/lib/ast/transform/utils.js +2 -2
- package/dist/src/lib/ast/types.js +263 -258
- package/dist/src/lib/ast/walk.js +246 -610
- package/dist/src/lib/fs/resolve.js +1 -1
- package/dist/src/lib/parser/declaration/list.js +223 -139
- package/dist/src/lib/parser/declaration/map.js +251 -238
- package/dist/src/lib/parser/declaration/set.js +74 -66
- package/dist/src/lib/parser/linesmap.js +1 -1
- package/dist/src/lib/parser/parse.js +1379 -1662
- package/dist/src/lib/parser/source.js +7 -4
- package/dist/src/lib/parser/tokenize.js +406 -199
- package/dist/src/lib/parser/utils/angle.js +11 -13
- package/dist/src/lib/parser/utils/at-rule-container.js +183 -168
- package/dist/src/lib/parser/utils/at-rule-font-feature-values.js +5 -5
- package/dist/src/lib/parser/utils/at-rule-generic.js +31 -24
- package/dist/src/lib/parser/utils/at-rule-import.js +44 -44
- package/dist/src/lib/parser/utils/at-rule-media.js +216 -205
- package/dist/src/lib/parser/utils/at-rule-support.js +83 -88
- package/dist/src/lib/parser/utils/at-rule-when-else.js +47 -47
- package/dist/src/lib/parser/utils/at-rule.js +5 -5
- package/dist/src/lib/parser/utils/base64.js +2 -2
- package/dist/src/lib/parser/utils/cache.js +1 -1
- package/dist/src/lib/parser/utils/config.js +1 -1
- package/dist/src/lib/parser/utils/declaration.js +233 -234
- package/dist/src/lib/parser/utils/eq.js +50 -3
- package/dist/src/lib/parser/utils/hash.js +2 -35
- package/dist/src/lib/parser/utils/selector.js +309 -387
- package/dist/src/lib/parser/utils/text.js +18 -4
- package/dist/src/lib/parser/utils/token.js +16 -69
- package/dist/src/lib/parser/utils/type.js +37 -24
- package/dist/src/lib/printer/render.js +965 -511
- package/dist/src/lib/printer/sourcemap/lib/codec.js +1 -1
- package/dist/src/lib/printer/sourcemap/sourcemap.js +1 -1
- package/dist/src/lib/syntax/color/a98rgb.js +1 -1
- package/dist/src/lib/syntax/color/alpha.js +36 -37
- package/dist/src/lib/syntax/color/cmyk.js +35 -52
- package/dist/src/lib/syntax/color/color-mix.js +96 -132
- package/dist/src/lib/syntax/color/color.js +222 -231
- package/dist/src/lib/syntax/color/hex.js +45 -55
- package/dist/src/lib/syntax/color/hsl.js +54 -62
- package/dist/src/lib/syntax/color/hwb.js +60 -68
- package/dist/src/lib/syntax/color/lab.js +59 -67
- package/dist/src/lib/syntax/color/lch.js +59 -67
- package/dist/src/lib/syntax/color/oklab.js +60 -68
- package/dist/src/lib/syntax/color/oklch.js +60 -67
- package/dist/src/lib/syntax/color/p3.js +1 -1
- package/dist/src/lib/syntax/color/prophotorgb.js +1 -1
- package/dist/src/lib/syntax/color/rec2020.js +1 -1
- package/dist/src/lib/syntax/color/relative-color.js +94 -144
- package/dist/src/lib/syntax/color/rgb.js +54 -57
- package/dist/src/lib/syntax/color/srgb.js +55 -53
- package/dist/src/lib/syntax/color/utils/color-type.js +13 -10
- package/dist/src/lib/syntax/color/utils/colorspace.js +11 -11
- package/dist/src/lib/syntax/color/utils/components.js +19 -20
- package/dist/src/lib/syntax/color/utils/distance.js +15 -13
- package/dist/src/lib/syntax/color/utils/make-color.js +15 -19
- package/dist/src/lib/syntax/color/utils/matrix.js +2 -2
- package/dist/src/lib/syntax/color/xyz.js +1 -1
- package/dist/src/lib/syntax/color/xyzd50.js +1 -1
- package/dist/src/lib/syntax/constants.js +2 -16
- package/dist/src/lib/syntax/syntax.js +301 -407
- package/dist/src/lib/validation/config.js +1 -1
- package/dist/src/lib/validation/match.js +959 -723
- package/dist/src/lib/validation/parser/parse.js +230 -112
- package/dist/src/lib/validation/parser/typedef.js +1 -1
- package/dist/src/lib/validation/utils/list.js +27 -12
- package/dist/src/node.js +12 -72
- package/dist/src/types.js +1 -1
- package/dist/src/utils/sync.js +9 -6
- package/dist/src/web.js +11 -73
- package/package.json +9 -10
- package/dist/index.cjs +0 -35874
- package/dist/src/lib/ast/clone.js +0 -51
- package/dist/src/lib/ast/expand.js +0 -311
- package/dist/src/lib/ast/find.js +0 -192
- package/dist/src/lib/ast/node.js +0 -59
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
CSS parser, minifier and validator for node and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version:
|
|
5
|
+
Version: 2.0.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
/**
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* @returns
|
|
12
12
|
*/
|
|
13
13
|
function dasherize(value) {
|
|
14
|
-
return value.replace(/
|
|
14
|
+
return value.replace(/[A-Z]/g, (one) => `-${one.toLowerCase()}`);
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
17
|
*
|
|
@@ -19,7 +19,21 @@ function dasherize(value) {
|
|
|
19
19
|
* @returns
|
|
20
20
|
*/
|
|
21
21
|
function camelize(value) {
|
|
22
|
-
return value.replace(/-
|
|
22
|
+
return value.replace(/-[a-z]/g, (match) => match[1].toUpperCase());
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* lowercase string, like String.prototype.toLowerCase(). the string is returned when it has no ascii uppercase and
|
|
26
|
+
* no non ascii character: no new string is created
|
|
27
|
+
* @param value
|
|
28
|
+
*/
|
|
29
|
+
function lowercase(value) {
|
|
30
|
+
for (let i = 0; i < value.length; i++) {
|
|
31
|
+
const code = value.charCodeAt(i);
|
|
32
|
+
if ((code >= 65 && code <= 90) || code > 0x7f) {
|
|
33
|
+
return value.toLowerCase();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
23
37
|
}
|
|
24
38
|
/**
|
|
25
39
|
*
|
|
@@ -46,4 +60,4 @@ function equalsIgnoreCase(a, b) {
|
|
|
46
60
|
return true;
|
|
47
61
|
}
|
|
48
62
|
|
|
49
|
-
export { camelize, dasherize, equalsIgnoreCase };
|
|
63
|
+
export { camelize, dasherize, equalsIgnoreCase, lowercase };
|
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
CSS parser, minifier and validator for node and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version:
|
|
5
|
+
Version: 2.0.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { EnumToken } from '../../ast/types.js';
|
|
9
|
-
import {
|
|
9
|
+
import { tokensfuncSet } from '../../syntax/constants.js';
|
|
10
10
|
|
|
11
11
|
const trimTokenSpace = new Set([
|
|
12
12
|
EnumToken.CommaTokenType,
|
|
@@ -28,94 +28,41 @@ const trimSpaceAfter = new Set([
|
|
|
28
28
|
EnumToken.PipeTokenType,
|
|
29
29
|
]);
|
|
30
30
|
/**
|
|
31
|
-
*
|
|
32
|
-
* @param
|
|
33
|
-
* @param
|
|
34
|
-
* @param replacement
|
|
35
|
-
* @throws TypeError replacement is null
|
|
36
|
-
* @throws ReferenceError node not found in parent
|
|
31
|
+
* remove leading, trailing and redundant whitespace tokens (in place)
|
|
32
|
+
* @param arena
|
|
33
|
+
* @param tokens
|
|
37
34
|
*/
|
|
38
|
-
function
|
|
39
|
-
if (replacement == null || (Array.isArray(replacement) && replacement.length === 0)) {
|
|
40
|
-
throw new TypeError(`replacement is null`);
|
|
41
|
-
}
|
|
42
|
-
if (!Array.isArray(replacement)) {
|
|
43
|
-
if (replacement[PARENT] != parent) {
|
|
44
|
-
replacement[PARENT] = parent;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
for (const newNode of replacement) {
|
|
49
|
-
if (newNode[PARENT] != parent) {
|
|
50
|
-
newNode[PARENT] = parent;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
if (parent.typ == EnumToken.BinaryExpressionTokenType) {
|
|
55
|
-
if (parent.l == node) {
|
|
56
|
-
parent.l = replacement;
|
|
57
|
-
}
|
|
58
|
-
else if (parent.r == node) {
|
|
59
|
-
parent.r = replacement;
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
throw new ReferenceError("Node not found");
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
const target = "val" in parent && Array.isArray(parent.val)
|
|
67
|
-
? parent.val
|
|
68
|
-
: (parent.chi ?? parent);
|
|
69
|
-
if (Array.isArray(target)) {
|
|
70
|
-
// @ts-ignore
|
|
71
|
-
const index = target.indexOf(node);
|
|
72
|
-
if (index == -1) {
|
|
73
|
-
throw new ReferenceError("Node not found");
|
|
74
|
-
}
|
|
75
|
-
target.splice(index, 1, ...(Array.isArray(replacement) ? replacement : [replacement]));
|
|
76
|
-
}
|
|
77
|
-
else if ("l" in target && target.l == node) {
|
|
78
|
-
target.l = replacement;
|
|
79
|
-
}
|
|
80
|
-
else if ("r" in target && target.r == node) {
|
|
81
|
-
target.r = replacement;
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
throw new ReferenceError("Node not found");
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
node[PARENT] = null;
|
|
88
|
-
return true;
|
|
89
|
-
}
|
|
90
|
-
function trimWhiteSpaceTokens(tokens) {
|
|
35
|
+
function trimWhiteSpaceTokens(arena, tokens) {
|
|
91
36
|
let i = 0;
|
|
92
|
-
if (tokens[0]
|
|
37
|
+
if (tokens.length > 0 && arena.dataTypes[tokens[0]] === EnumToken.WhitespaceTokenType) {
|
|
93
38
|
tokens.shift();
|
|
94
39
|
}
|
|
95
|
-
if (tokens[tokens.length - 1]
|
|
40
|
+
if (tokens.length > 0 && arena.dataTypes[tokens[tokens.length - 1]] === EnumToken.WhitespaceTokenType) {
|
|
96
41
|
tokens.pop();
|
|
97
42
|
}
|
|
98
43
|
if (tokens.length === 1) {
|
|
99
|
-
if (tokens[0]
|
|
44
|
+
if (arena.dataTypes[tokens[0]] === EnumToken.WhitespaceTokenType) {
|
|
100
45
|
tokens.length = 0;
|
|
101
46
|
}
|
|
102
47
|
return tokens;
|
|
103
48
|
}
|
|
104
49
|
for (; i < tokens.length; i++) {
|
|
105
|
-
if (trimTokenSpace.has(tokens[i]
|
|
106
|
-
if (tokens[i + 1]
|
|
50
|
+
if (trimTokenSpace.has(arena.dataTypes[tokens[i]])) {
|
|
51
|
+
if (i + 1 < tokens.length && arena.dataTypes[tokens[i + 1]] === EnumToken.WhitespaceTokenType) {
|
|
107
52
|
tokens.splice(i + 1, 1);
|
|
108
53
|
}
|
|
109
|
-
if (i > 0 && tokens[i - 1]
|
|
54
|
+
if (i > 0 && arena.dataTypes[tokens[i - 1]] === EnumToken.WhitespaceTokenType) {
|
|
110
55
|
tokens.splice(--i, 1);
|
|
111
56
|
continue;
|
|
112
57
|
}
|
|
113
58
|
}
|
|
114
|
-
if (trimSpaceAfter.has(tokens[i]
|
|
59
|
+
if (trimSpaceAfter.has(arena.dataTypes[tokens[i]]) &&
|
|
60
|
+
i + 1 < tokens.length &&
|
|
61
|
+
arena.dataTypes[tokens[i + 1]] === EnumToken.WhitespaceTokenType) {
|
|
115
62
|
tokens.splice(i + 1, 1);
|
|
116
63
|
}
|
|
117
64
|
}
|
|
118
65
|
return tokens;
|
|
119
66
|
}
|
|
120
67
|
|
|
121
|
-
export {
|
|
68
|
+
export { trimWhiteSpaceTokens };
|
|
@@ -2,25 +2,49 @@
|
|
|
2
2
|
CSS parser, minifier and validator for node and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version:
|
|
5
|
+
Version: 2.0.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { EnumToken } from '../../ast/types.js';
|
|
9
9
|
import { tokensfuncSet } from '../../syntax/constants.js';
|
|
10
10
|
import { isColor } from '../../syntax/syntax.js';
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
const mathTokenTypes = [
|
|
13
|
+
EnumToken.Add,
|
|
14
|
+
EnumToken.Mul,
|
|
15
|
+
EnumToken.Div,
|
|
16
|
+
EnumToken.Sub,
|
|
17
|
+
EnumToken.LiteralTokenType,
|
|
18
|
+
EnumToken.CommaTokenType,
|
|
19
|
+
EnumToken.WhitespaceTokenType,
|
|
20
|
+
EnumToken.DimensionTokenType,
|
|
21
|
+
EnumToken.NumberTokenType,
|
|
22
|
+
EnumToken.LengthTokenType,
|
|
23
|
+
EnumToken.AngleTokenType,
|
|
24
|
+
EnumToken.PercentageTokenType,
|
|
25
|
+
EnumToken.ResolutionTokenType,
|
|
26
|
+
EnumToken.TimeTokenType,
|
|
27
|
+
EnumToken.BinaryExpressionTokenType,
|
|
28
|
+
];
|
|
29
|
+
/**
|
|
30
|
+
* check if a token matches a shorthand property type
|
|
31
|
+
* @param arena
|
|
32
|
+
* @param val
|
|
33
|
+
* @param properties
|
|
34
|
+
*/
|
|
35
|
+
function matchType(arena, val, properties) {
|
|
36
|
+
const typ = arena.dataTypes[val];
|
|
37
|
+
if ((typ === EnumToken.IdenTokenType && properties.keywords.includes(arena.strings[arena.stringValue[val]])) ||
|
|
14
38
|
properties.types.some(
|
|
15
39
|
// @ts-expect-error
|
|
16
40
|
(t) =>
|
|
17
41
|
// @ts-expect-error
|
|
18
|
-
(
|
|
42
|
+
(typ === EnumToken.IdenTokenType && EnumToken[t] === EnumToken.ColorTokenType && isColor(arena, val)) ||
|
|
19
43
|
// @ts-expect-error
|
|
20
|
-
EnumToken[t] ===
|
|
44
|
+
EnumToken[t] === typ)) {
|
|
21
45
|
return true;
|
|
22
46
|
}
|
|
23
|
-
if (
|
|
47
|
+
if (typ === EnumToken.NumberTokenType && arena.numericValue[val] === 0) {
|
|
24
48
|
// @ts-ignore
|
|
25
49
|
return properties.types.some((type) => {
|
|
26
50
|
// @ts-ignore
|
|
@@ -28,24 +52,13 @@ function matchType(val, properties) {
|
|
|
28
52
|
return typ == EnumToken.LengthTokenType || typ == EnumToken.AngleTokenType;
|
|
29
53
|
});
|
|
30
54
|
}
|
|
31
|
-
if (tokensfuncSet.has(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
EnumToken.CommaTokenType,
|
|
39
|
-
EnumToken.WhitespaceTokenType,
|
|
40
|
-
EnumToken.DimensionTokenType,
|
|
41
|
-
EnumToken.NumberTokenType,
|
|
42
|
-
EnumToken.LengthTokenType,
|
|
43
|
-
EnumToken.AngleTokenType,
|
|
44
|
-
EnumToken.PercentageTokenType,
|
|
45
|
-
EnumToken.ResolutionTokenType,
|
|
46
|
-
EnumToken.TimeTokenType,
|
|
47
|
-
EnumToken.BinaryExpressionTokenType,
|
|
48
|
-
].includes(t.typ) || matchType(t, properties));
|
|
55
|
+
if (tokensfuncSet.has(typ) && typ === EnumToken.MathFunctionTokenType) {
|
|
56
|
+
for (let t = arena.firstToken[val]; t != 0; t = arena.nextSibling[t]) {
|
|
57
|
+
if (!mathTokenTypes.includes(arena.dataTypes[t]) && !matchType(arena, t, properties)) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return true;
|
|
49
62
|
}
|
|
50
63
|
return false;
|
|
51
64
|
}
|