@tbela99/css-parser 0.9.0 → 1.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/LICENSE.md +1 -1
- package/README.md +22 -12
- package/dist/index-umd-web.js +2678 -2838
- package/dist/index.cjs +2468 -2628
- package/dist/index.d.ts +71 -26
- package/dist/lib/ast/expand.js +15 -2
- package/dist/lib/ast/features/calc.js +7 -10
- package/dist/lib/ast/features/index.js +1 -0
- package/dist/lib/ast/features/inlinecssvariables.js +0 -5
- package/dist/lib/ast/features/prefix.js +2 -7
- package/dist/lib/ast/features/shorthand.js +6 -9
- package/dist/lib/ast/features/transform.js +60 -0
- package/dist/lib/ast/math/expression.js +14 -10
- package/dist/lib/ast/math/math.js +14 -2
- package/dist/lib/ast/minify.js +47 -6
- package/dist/lib/ast/transform/compute.js +336 -0
- package/dist/lib/ast/transform/convert.js +33 -0
- package/dist/lib/ast/transform/matrix.js +111 -0
- package/dist/lib/ast/transform/minify.js +296 -0
- package/dist/lib/ast/transform/perspective.js +10 -0
- package/dist/lib/ast/transform/rotate.js +40 -0
- package/dist/lib/ast/transform/scale.js +32 -0
- package/dist/lib/ast/transform/skew.js +23 -0
- package/dist/lib/ast/transform/translate.js +32 -0
- package/dist/lib/ast/transform/utils.js +198 -0
- package/dist/lib/ast/types.js +2 -0
- package/dist/lib/ast/walk.js +23 -17
- package/dist/lib/parser/parse.js +174 -127
- package/dist/lib/parser/utils/declaration.js +1 -1
- package/dist/lib/renderer/color/{colormix.js → color-mix.js} +6 -0
- package/dist/lib/renderer/color/color.js +96 -20
- package/dist/lib/renderer/color/hex.js +17 -7
- package/dist/lib/renderer/color/hsl.js +7 -2
- package/dist/lib/renderer/color/lab.js +10 -1
- package/dist/lib/renderer/color/lch.js +8 -0
- package/dist/lib/renderer/color/oklab.js +8 -0
- package/dist/lib/renderer/color/oklch.js +8 -0
- package/dist/lib/renderer/color/prophotorgb.js +2 -2
- package/dist/lib/renderer/color/relativecolor.js +10 -21
- package/dist/lib/renderer/color/rgb.js +10 -7
- package/dist/lib/renderer/color/srgb.js +30 -6
- package/dist/lib/renderer/color/utils/components.js +13 -2
- package/dist/lib/renderer/color/xyz.js +2 -18
- package/dist/lib/renderer/color/xyzd50.js +20 -2
- package/dist/lib/renderer/render.js +70 -32
- package/dist/lib/renderer/sourcemap/sourcemap.js +1 -1
- package/dist/lib/syntax/syntax.js +75 -56
- package/dist/lib/validation/at-rules/container.js +6 -6
- package/dist/lib/validation/at-rules/document.js +40 -60
- package/dist/lib/validation/at-rules/import.js +61 -59
- package/dist/lib/validation/at-rules/keyframes.js +1 -1
- package/dist/lib/validation/at-rules/media.js +1 -1
- package/dist/lib/validation/at-rules/supports.js +40 -9
- package/dist/lib/validation/atrule.js +0 -4
- package/dist/lib/validation/config.json.js +83 -35
- package/dist/lib/validation/parser/parse.js +1 -95
- package/dist/lib/validation/parser/types.js +1 -2
- package/dist/lib/validation/selector.js +5 -2
- package/dist/lib/validation/syntaxes/compound-selector.js +2 -2
- package/dist/lib/validation/syntaxes/keyframe-block-list.js +2 -2
- package/dist/lib/validation/syntaxes/keyframe-selector.js +11 -90
- package/dist/lib/validation/syntaxes/layer-name.js +5 -16
- package/dist/lib/validation/syntaxes/relative-selector.js +15 -14
- package/dist/lib/validation/utils/list.js +18 -1
- package/dist/node/load.js +1 -1
- package/package.json +13 -12
- package/dist/lib/renderer/color/prophotoRgb.js +0 -56
- package/dist/lib/validation/declaration.js +0 -102
- package/dist/lib/validation/syntax.js +0 -1475
- package/dist/lib/validation/syntaxes/image.js +0 -29
|
@@ -949,100 +949,6 @@ function move(position, chr) {
|
|
|
949
949
|
}
|
|
950
950
|
return position;
|
|
951
951
|
}
|
|
952
|
-
function renderSyntax(token, parent) {
|
|
953
|
-
let glue;
|
|
954
|
-
switch (token.typ) {
|
|
955
|
-
case ValidationTokenEnum.Root:
|
|
956
|
-
return token.chi.reduce((acc, curr) => acc + renderSyntax(curr), '');
|
|
957
|
-
case ValidationTokenEnum.Whitespace:
|
|
958
|
-
return ' ';
|
|
959
|
-
case ValidationTokenEnum.ValidationFunctionDefinition:
|
|
960
|
-
return '<' + token.val + '()>';
|
|
961
|
-
case ValidationTokenEnum.HashMark:
|
|
962
|
-
return '#';
|
|
963
|
-
case ValidationTokenEnum.Pipe:
|
|
964
|
-
return '|';
|
|
965
|
-
case ValidationTokenEnum.Column:
|
|
966
|
-
return '||';
|
|
967
|
-
case ValidationTokenEnum.PipeToken:
|
|
968
|
-
return token.chi.reduce((acc, curr) => acc + (acc.trim().length > 0 ? '|' : '') + curr.reduce((acc, curr) => acc + renderSyntax(curr), ''), '');
|
|
969
|
-
case ValidationTokenEnum.ColumnToken:
|
|
970
|
-
case ValidationTokenEnum.AmpersandToken:
|
|
971
|
-
glue = token.typ == ValidationTokenEnum.ColumnToken ? '||' : '&&';
|
|
972
|
-
return token.l.reduce((acc, curr) => acc + renderSyntax(curr), '') +
|
|
973
|
-
glue +
|
|
974
|
-
token.r.reduce((acc, curr) => acc + renderSyntax(curr), '');
|
|
975
|
-
case ValidationTokenEnum.Function:
|
|
976
|
-
case ValidationTokenEnum.PseudoClassFunctionToken:
|
|
977
|
-
case ValidationTokenEnum.Parens:
|
|
978
|
-
return token.val + '(' + token.chi.reduce((acc, curr) => acc + renderSyntax(curr), '') + ')' + renderAttributes(token);
|
|
979
|
-
case ValidationTokenEnum.Comma:
|
|
980
|
-
return ',';
|
|
981
|
-
case ValidationTokenEnum.Keyword:
|
|
982
|
-
return token.val + renderAttributes(token);
|
|
983
|
-
case ValidationTokenEnum.OpenBracket:
|
|
984
|
-
return '[';
|
|
985
|
-
case ValidationTokenEnum.Ampersand:
|
|
986
|
-
return '&&';
|
|
987
|
-
case ValidationTokenEnum.QuestionMark:
|
|
988
|
-
return '?';
|
|
989
|
-
case ValidationTokenEnum.Separator:
|
|
990
|
-
return '/';
|
|
991
|
-
case ValidationTokenEnum.Bracket:
|
|
992
|
-
return '[' + token.chi.reduce((acc, curr) => acc + renderSyntax(curr), '') + ']' + renderAttributes(token);
|
|
993
|
-
case ValidationTokenEnum.PropertyType:
|
|
994
|
-
return '<' + token.val + '>' + renderAttributes(token);
|
|
995
|
-
case ValidationTokenEnum.DeclarationType:
|
|
996
|
-
return "<'" + token.val + "'>" + renderAttributes(token);
|
|
997
|
-
case ValidationTokenEnum.Number:
|
|
998
|
-
case ValidationTokenEnum.PseudoClassToken:
|
|
999
|
-
case ValidationTokenEnum.StringToken:
|
|
1000
|
-
return token.val + '';
|
|
1001
|
-
case ValidationTokenEnum.SemiColon:
|
|
1002
|
-
return ';';
|
|
1003
|
-
case ValidationTokenEnum.AtRule:
|
|
1004
|
-
return '@' + token.val;
|
|
1005
|
-
case ValidationTokenEnum.AtRuleDefinition:
|
|
1006
|
-
return '@' + token.val +
|
|
1007
|
-
(token.prelude == null ? '' : ' ' + token.prelude.reduce((acc, curr) => acc + renderSyntax(curr), '')) +
|
|
1008
|
-
(token.chi == null ? '' : ' {\n' + token.chi.reduce((acc, curr) => acc + renderSyntax(curr), '')).slice(1, -1) + '\n}';
|
|
1009
|
-
case ValidationTokenEnum.Block:
|
|
1010
|
-
return '{' + token.chi.reduce((acc, t) => acc + renderSyntax(t), '') + '}';
|
|
1011
|
-
case ValidationTokenEnum.DeclarationDefinitionToken:
|
|
1012
|
-
return token.nam + ': ' + renderSyntax(token.val);
|
|
1013
|
-
case ValidationTokenEnum.ColumnArrayToken:
|
|
1014
|
-
return token.chi.reduce((acc, curr) => acc + (acc.trim().length > 0 ? '||' : '') + renderSyntax(curr), '');
|
|
1015
|
-
default:
|
|
1016
|
-
throw new Error('Unhandled token: ' + JSON.stringify({ token }));
|
|
1017
|
-
}
|
|
1018
|
-
}
|
|
1019
|
-
function renderAttributes(token) {
|
|
1020
|
-
let result = '';
|
|
1021
|
-
if (token.isList) {
|
|
1022
|
-
result += '#';
|
|
1023
|
-
}
|
|
1024
|
-
if (token.isOptional) {
|
|
1025
|
-
result += '?';
|
|
1026
|
-
}
|
|
1027
|
-
if (token.isRepeatableGroup) {
|
|
1028
|
-
result += '!';
|
|
1029
|
-
}
|
|
1030
|
-
if (token.isRepeatable) {
|
|
1031
|
-
result += '*';
|
|
1032
|
-
}
|
|
1033
|
-
if (token.atLeastOnce) {
|
|
1034
|
-
result += '+';
|
|
1035
|
-
}
|
|
1036
|
-
if (token.occurence != null) {
|
|
1037
|
-
if (token.occurence.max == 0 || token.occurence.max == token.occurence.min || Number.isNaN(token.occurence.max)) {
|
|
1038
|
-
result += '{' + token.occurence.min + '}';
|
|
1039
|
-
}
|
|
1040
|
-
else {
|
|
1041
|
-
result += '{' + token.occurence.min + ',' + token.occurence.max + '}';
|
|
1042
|
-
}
|
|
1043
|
-
}
|
|
1044
|
-
return result;
|
|
1045
|
-
}
|
|
1046
952
|
function minify(ast) {
|
|
1047
953
|
if (Array.isArray(ast)) {
|
|
1048
954
|
// @ts-ignore
|
|
@@ -1143,4 +1049,4 @@ function* walkValidationToken(token, parent, callback, key) {
|
|
|
1143
1049
|
}
|
|
1144
1050
|
}
|
|
1145
1051
|
|
|
1146
|
-
export { WalkValidationTokenEnum, WalkValidationTokenKeyTypeEnum, parseSyntax,
|
|
1052
|
+
export { WalkValidationTokenEnum, WalkValidationTokenKeyTypeEnum, parseSyntax, walkValidationToken };
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const specialValues = ['inherit', 'initial', 'unset', 'revert', 'revert-layer'];
|
|
2
1
|
var ValidationTokenEnum;
|
|
3
2
|
(function (ValidationTokenEnum) {
|
|
4
3
|
ValidationTokenEnum[ValidationTokenEnum["Root"] = 0] = "Root";
|
|
@@ -52,4 +51,4 @@ var ValidationSyntaxGroupEnum;
|
|
|
52
51
|
ValidationSyntaxGroupEnum["AtRules"] = "atRules";
|
|
53
52
|
})(ValidationSyntaxGroupEnum || (ValidationSyntaxGroupEnum = {}));
|
|
54
53
|
|
|
55
|
-
export { ValidationSyntaxGroupEnum, ValidationTokenEnum
|
|
54
|
+
export { ValidationSyntaxGroupEnum, ValidationTokenEnum };
|
|
@@ -19,7 +19,7 @@ function validateSelector(selector, options, root) {
|
|
|
19
19
|
}
|
|
20
20
|
// @ts-ignore
|
|
21
21
|
if (root.typ == EnumToken.AtRuleNodeType && root.nam.match(/^(-[a-z]+-)?keyframes$/)) {
|
|
22
|
-
return validateKeyframeBlockList(selector
|
|
22
|
+
return validateKeyframeBlockList(selector);
|
|
23
23
|
}
|
|
24
24
|
let isNested = root.typ == EnumToken.RuleNodeType ? 1 : 0;
|
|
25
25
|
let currentRoot = root.parent;
|
|
@@ -35,7 +35,10 @@ function validateSelector(selector, options, root) {
|
|
|
35
35
|
}
|
|
36
36
|
const nestedSelector = isNested > 0;
|
|
37
37
|
// @ts-ignore
|
|
38
|
-
return nestedSelector ? validateRelativeSelectorList(selector, root, {
|
|
38
|
+
return nestedSelector ? validateRelativeSelectorList(selector, root, {
|
|
39
|
+
...(options ?? {}),
|
|
40
|
+
nestedSelector
|
|
41
|
+
}) : validateSelectorList(selector, root, { ...(options ?? {}), nestedSelector });
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
export { validateSelector };
|
|
@@ -80,8 +80,8 @@ function validateCompoundSelector(tokens, root, options) {
|
|
|
80
80
|
tokens.shift();
|
|
81
81
|
consumeWhitespace(tokens);
|
|
82
82
|
}
|
|
83
|
-
while (tokens.length > 0 && tokens[0].typ == EnumToken.PseudoClassTokenType) {
|
|
84
|
-
const isPseudoElement = tokens[0].
|
|
83
|
+
while (tokens.length > 0 && (tokens[0].typ == EnumToken.PseudoElementTokenType || tokens[0].typ == EnumToken.PseudoClassTokenType)) {
|
|
84
|
+
const isPseudoElement = tokens[0].typ == EnumToken.PseudoElementTokenType;
|
|
85
85
|
if (
|
|
86
86
|
// https://developer.mozilla.org/en-US/docs/Web/CSS/WebKit_Extensions#pseudo-elements
|
|
87
87
|
!(isPseudoElement && tokens[0].val.startsWith('::-webkit-')) &&
|
|
@@ -13,7 +13,7 @@ function validateKeyframeBlockList(tokens, atRule, options) {
|
|
|
13
13
|
let result = null;
|
|
14
14
|
while (i + 1 < tokens.length) {
|
|
15
15
|
if (tokens[++i].typ == EnumToken.CommaTokenType) {
|
|
16
|
-
result = validateKeyframeSelector(tokens.slice(j, i)
|
|
16
|
+
result = validateKeyframeSelector(tokens.slice(j, i));
|
|
17
17
|
if (result.valid == ValidationLevel.Drop) {
|
|
18
18
|
return result;
|
|
19
19
|
}
|
|
@@ -21,7 +21,7 @@ function validateKeyframeBlockList(tokens, atRule, options) {
|
|
|
21
21
|
i = j;
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
-
return validateKeyframeSelector(i == j ? tokens.slice(i) : tokens.slice(j, i + 1)
|
|
24
|
+
return validateKeyframeSelector(i == j ? tokens.slice(i) : tokens.slice(j, i + 1));
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
export { validateKeyframeBlockList };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { consumeWhitespace } from '../utils/whitespace.js';
|
|
2
|
+
import { splitTokenList } from '../utils/list.js';
|
|
2
3
|
import { ValidationLevel, EnumToken } from '../../ast/types.js';
|
|
3
4
|
import '../../ast/minify.js';
|
|
4
5
|
import '../../ast/walk.js';
|
|
@@ -7,120 +8,40 @@ import '../../renderer/color/utils/constants.js';
|
|
|
7
8
|
import '../../renderer/sourcemap/lib/encode.js';
|
|
8
9
|
import '../../parser/utils/config.js';
|
|
9
10
|
|
|
10
|
-
function validateKeyframeSelector(tokens,
|
|
11
|
+
function validateKeyframeSelector(tokens, options) {
|
|
11
12
|
consumeWhitespace(tokens);
|
|
12
13
|
if (tokens.length == 0) {
|
|
13
14
|
// @ts-ignore
|
|
14
15
|
return {
|
|
15
16
|
valid: ValidationLevel.Drop,
|
|
16
17
|
matches: [],
|
|
17
|
-
node:
|
|
18
|
+
node: null,
|
|
18
19
|
syntax: null,
|
|
19
20
|
error: 'expected keyframe selector',
|
|
20
21
|
tokens
|
|
21
22
|
};
|
|
22
23
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
consumeWhitespace(tokens);
|
|
26
|
-
if (tokens.length == 0) {
|
|
27
|
-
// @ts-ignore
|
|
24
|
+
for (const t of splitTokenList(tokens)) {
|
|
25
|
+
if (t.length != 1) {
|
|
28
26
|
return {
|
|
29
|
-
valid: ValidationLevel.
|
|
27
|
+
valid: ValidationLevel.Drop,
|
|
30
28
|
matches: [],
|
|
31
|
-
node:
|
|
29
|
+
node: t[0] ?? null,
|
|
32
30
|
syntax: null,
|
|
33
|
-
error: '',
|
|
31
|
+
error: 'unexpected token',
|
|
34
32
|
tokens
|
|
35
33
|
};
|
|
36
34
|
}
|
|
37
|
-
|
|
38
|
-
return {
|
|
39
|
-
valid: ValidationLevel.Drop,
|
|
40
|
-
matches: [],
|
|
41
|
-
node: tokens[0],
|
|
42
|
-
syntax: null,
|
|
43
|
-
error: 'unexpected token',
|
|
44
|
-
tokens
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
if (tokens[0].typ != EnumToken.IdenTokenType) {
|
|
48
|
-
// @ts-ignore
|
|
49
|
-
return {
|
|
50
|
-
valid: ValidationLevel.Drop,
|
|
51
|
-
matches: [],
|
|
52
|
-
node: tokens[0],
|
|
53
|
-
// @ts-ignore
|
|
54
|
-
syntax: null,
|
|
55
|
-
error: 'expected keyframe selector',
|
|
56
|
-
tokens
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
if (['from', 'to'].includes(tokens[0].val)) {
|
|
60
|
-
tokens.shift();
|
|
61
|
-
consumeWhitespace(tokens);
|
|
62
|
-
if (tokens.length > 0) {
|
|
63
|
-
// @ts-ignore
|
|
35
|
+
if (t[0].typ != EnumToken.PercentageTokenType && !(t[0].typ == EnumToken.IdenTokenType && ['from', 'to', 'cover', 'contain', 'entry', 'exit', 'entry-crossing', 'exit-crossing'].includes(t[0].val))) {
|
|
64
36
|
return {
|
|
65
37
|
valid: ValidationLevel.Drop,
|
|
66
38
|
matches: [],
|
|
67
|
-
node:
|
|
39
|
+
node: t[0],
|
|
68
40
|
syntax: null,
|
|
69
|
-
error: '
|
|
41
|
+
error: 'expected keyframe selector',
|
|
70
42
|
tokens
|
|
71
43
|
};
|
|
72
44
|
}
|
|
73
|
-
// @ts-ignore
|
|
74
|
-
return {
|
|
75
|
-
valid: ValidationLevel.Valid,
|
|
76
|
-
matches: [],
|
|
77
|
-
node: null,
|
|
78
|
-
// @ts-ignore
|
|
79
|
-
syntax: null,
|
|
80
|
-
error: '',
|
|
81
|
-
tokens
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
if (!['cover', 'contain', 'entry', 'exit', 'entry-crossing', 'exit-crossing'].includes(tokens[0].val)) {
|
|
85
|
-
// @ts-ignore
|
|
86
|
-
return {
|
|
87
|
-
valid: ValidationLevel.Drop,
|
|
88
|
-
matches: [],
|
|
89
|
-
node: tokens[0],
|
|
90
|
-
// @ts-ignore
|
|
91
|
-
syntax: null,
|
|
92
|
-
error: 'unexpected token',
|
|
93
|
-
tokens
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
tokens.shift();
|
|
97
|
-
consumeWhitespace(tokens);
|
|
98
|
-
// @ts-ignore
|
|
99
|
-
if (tokens.length == 0 || tokens[0].typ != EnumToken.PercentageTokenType) {
|
|
100
|
-
// @ts-ignore
|
|
101
|
-
return {
|
|
102
|
-
valid: ValidationLevel.Drop,
|
|
103
|
-
matches: [],
|
|
104
|
-
node: tokens[0],
|
|
105
|
-
// @ts-ignore
|
|
106
|
-
syntax: null,
|
|
107
|
-
error: 'expecting percentage token',
|
|
108
|
-
tokens
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
tokens.shift();
|
|
112
|
-
consumeWhitespace(tokens);
|
|
113
|
-
if (tokens.length > 0) {
|
|
114
|
-
// @ts-ignore
|
|
115
|
-
return {
|
|
116
|
-
valid: ValidationLevel.Drop,
|
|
117
|
-
matches: [],
|
|
118
|
-
node: tokens[0],
|
|
119
|
-
// @ts-ignore
|
|
120
|
-
syntax: null,
|
|
121
|
-
error: 'unexpected token',
|
|
122
|
-
tokens
|
|
123
|
-
};
|
|
124
45
|
}
|
|
125
46
|
// @ts-ignore
|
|
126
47
|
return {
|
|
@@ -15,7 +15,7 @@ function validateLayerName(tokens) {
|
|
|
15
15
|
acc[acc.length - 1].push(curr);
|
|
16
16
|
}
|
|
17
17
|
return acc;
|
|
18
|
-
}, [[]])
|
|
18
|
+
}, [[]]);
|
|
19
19
|
for (let i = 0; i < slice.length; i++) {
|
|
20
20
|
if (slice[i].length == 0) {
|
|
21
21
|
// @ts-ignore
|
|
@@ -28,26 +28,15 @@ function validateLayerName(tokens) {
|
|
|
28
28
|
tokens
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
valid: ValidationLevel.Drop,
|
|
35
|
-
matches: tokens,
|
|
36
|
-
node: slice[i][0],
|
|
37
|
-
syntax: 'ident',
|
|
38
|
-
error: 'expecting ident',
|
|
39
|
-
tokens
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
for (let j = 1; j < slice[i].length; j++) {
|
|
43
|
-
if (slice[i][j].typ != EnumToken.ClassSelectorTokenType) {
|
|
31
|
+
for (let j = 0; j < slice[i].length; j++) {
|
|
32
|
+
if (slice[i][j].typ != EnumToken.IdenTokenType && slice[i][j].typ != EnumToken.ClassSelectorTokenType) {
|
|
44
33
|
// @ts-ignore
|
|
45
34
|
return {
|
|
46
35
|
valid: ValidationLevel.Drop,
|
|
47
36
|
matches: tokens,
|
|
48
37
|
node: slice[i][j],
|
|
49
|
-
syntax: 'layer-name',
|
|
50
|
-
error: 'expecting class selector',
|
|
38
|
+
syntax: '<layer-name>',
|
|
39
|
+
error: 'expecting ident or class selector',
|
|
51
40
|
tokens
|
|
52
41
|
};
|
|
53
42
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { consumeWhitespace } from '../utils/whitespace.js';
|
|
2
|
-
import
|
|
2
|
+
import '../../ast/types.js';
|
|
3
3
|
import '../../ast/minify.js';
|
|
4
4
|
import '../../ast/walk.js';
|
|
5
5
|
import '../../parser/parse.js';
|
|
@@ -12,19 +12,20 @@ import { combinatorsTokens } from './complex-selector.js';
|
|
|
12
12
|
function validateRelativeSelector(tokens, root, options) {
|
|
13
13
|
tokens = tokens.slice();
|
|
14
14
|
consumeWhitespace(tokens);
|
|
15
|
-
if (tokens.length == 0) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
15
|
+
// if (tokens.length == 0) {
|
|
16
|
+
//
|
|
17
|
+
// // @ts-ignore
|
|
18
|
+
// return {
|
|
19
|
+
// valid: ValidationLevel.Drop,
|
|
20
|
+
// matches: [],
|
|
21
|
+
// // @ts-ignore
|
|
22
|
+
// node: root,
|
|
23
|
+
// // @ts-ignore
|
|
24
|
+
// syntax: null,
|
|
25
|
+
// error: 'expected selector',
|
|
26
|
+
// tokens
|
|
27
|
+
// }
|
|
28
|
+
// }
|
|
28
29
|
// , EnumToken.DescendantCombinatorTokenType
|
|
29
30
|
if (combinatorsTokens.includes(tokens[0].typ)) {
|
|
30
31
|
tokens.shift();
|
|
@@ -6,6 +6,23 @@ import '../../renderer/color/utils/constants.js';
|
|
|
6
6
|
import '../../renderer/sourcemap/lib/encode.js';
|
|
7
7
|
import '../../parser/utils/config.js';
|
|
8
8
|
|
|
9
|
+
function stripCommaToken(tokenList) {
|
|
10
|
+
let result = [];
|
|
11
|
+
let last = null;
|
|
12
|
+
for (let i = 0; i < tokenList.length; i++) {
|
|
13
|
+
if (tokenList[i].typ == EnumToken.CommaTokenType && last != null && last.typ == EnumToken.CommaTokenType) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
if (tokenList[i].typ != EnumToken.WhitespaceTokenType) {
|
|
17
|
+
last = tokenList[i];
|
|
18
|
+
}
|
|
19
|
+
if (tokenList[i].typ == EnumToken.CommentTokenType || tokenList[i].typ == EnumToken.CommaTokenType) {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
result.push(tokenList[i]);
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
9
26
|
function splitTokenList(tokenList, split = [EnumToken.CommaTokenType]) {
|
|
10
27
|
return tokenList.reduce((acc, curr) => {
|
|
11
28
|
if (curr.typ == EnumToken.CommentTokenType) {
|
|
@@ -21,4 +38,4 @@ function splitTokenList(tokenList, split = [EnumToken.CommaTokenType]) {
|
|
|
21
38
|
}, [[]]);
|
|
22
39
|
}
|
|
23
40
|
|
|
24
|
-
export { splitTokenList };
|
|
41
|
+
export { splitTokenList, stripCommaToken };
|
package/dist/node/load.js
CHANGED
|
@@ -7,7 +7,7 @@ function parseResponse(response) {
|
|
|
7
7
|
}
|
|
8
8
|
return response.text();
|
|
9
9
|
}
|
|
10
|
-
async function load(url, currentFile) {
|
|
10
|
+
async function load(url, currentFile = '.') {
|
|
11
11
|
const resolved = resolve(url, currentFile);
|
|
12
12
|
return matchUrl.test(resolved.absolute) ? fetch(resolved.absolute).then(parseResponse) : readFile(resolved.absolute, { encoding: 'utf-8' });
|
|
13
13
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbela99/css-parser",
|
|
3
3
|
"description": "CSS parser for node and the browser",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "v1.0.0",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dist/node/index.js",
|
|
7
|
+
"./node": "./dist/node/index.js",
|
|
7
8
|
"./umd": "./dist/index-umd-web.js",
|
|
8
9
|
"./web": "./dist/web/index.js",
|
|
9
10
|
"./cjs": "./dist/index.cjs"
|
|
@@ -17,8 +18,8 @@
|
|
|
17
18
|
"build": "rollup -c;./build.sh dist/index.d.ts 'declare interface' 'declare type'",
|
|
18
19
|
"test": "web-test-runner \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.; mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
|
|
19
20
|
"test:node": "mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
|
|
20
|
-
"test:cov": "c8 --reporter=html --reporter=text --reporter=json-summary mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
|
|
21
|
-
"test:web-cov": "web-test-runner \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=. --coverage",
|
|
21
|
+
"test:cov": "c8 -x 'test/specs/**/*.js' -x dist/lib/validation/syntax.js -x 'dist/lib/validation/parser/*.js' --reporter=html --reporter=text --reporter=json-summary mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
|
|
22
|
+
"test:web-cov": "web-test-runner -x 'test/specs/**/*.js' -x dist/lib/validation/syntax.js,dist/lib/validation/parser \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=. --coverage",
|
|
22
23
|
"profile": "node --enable-source-maps --inspect-brk test/inspect.js",
|
|
23
24
|
"syntax-update": "esno tools/validation.ts",
|
|
24
25
|
"debug": "web-test-runner \"test/**/web.spec.js\" --manual --open --node-resolve --root-dir=."
|
|
@@ -50,22 +51,22 @@
|
|
|
50
51
|
"homepage": "https://github.com/tbela99/css-parser#readme",
|
|
51
52
|
"devDependencies": {
|
|
52
53
|
"@esm-bundle/chai": "^4.3.4-fix.0",
|
|
53
|
-
"@rollup/plugin-commonjs": "^28.0.
|
|
54
|
+
"@rollup/plugin-commonjs": "^28.0.3",
|
|
54
55
|
"@rollup/plugin-json": "^6.1.0",
|
|
55
|
-
"@rollup/plugin-node-resolve": "^16.0.
|
|
56
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
56
57
|
"@rollup/plugin-typescript": "^12.1.2",
|
|
57
|
-
"@types/chai": "^5.
|
|
58
|
+
"@types/chai": "^5.2.1",
|
|
58
59
|
"@types/mocha": "^10.0.10",
|
|
59
|
-
"@types/node": "^22.
|
|
60
|
-
"@types/web": "^0.0.
|
|
61
|
-
"@web/test-runner": "^0.20.
|
|
60
|
+
"@types/node": "^22.15.2",
|
|
61
|
+
"@types/web": "^0.0.226",
|
|
62
|
+
"@web/test-runner": "^0.20.1",
|
|
62
63
|
"@web/test-runner-playwright": "^0.11.0",
|
|
63
64
|
"c8": "^10.1.3",
|
|
64
65
|
"esno": "^4.8.0",
|
|
65
66
|
"mocha": "^11.1.0",
|
|
66
|
-
"playwright": "^1.
|
|
67
|
-
"rollup": "^4.
|
|
68
|
-
"rollup-plugin-dts": "^6.
|
|
67
|
+
"playwright": "^1.52.0",
|
|
68
|
+
"rollup": "^4.40.0",
|
|
69
|
+
"rollup-plugin-dts": "^6.2.1",
|
|
69
70
|
"tslib": "^2.8.1"
|
|
70
71
|
}
|
|
71
72
|
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { srgb2xyz, xyzd502srgb } from './xyz.js';
|
|
2
|
-
import { XYZ_D65_to_D50 } from './xyzd50.js';
|
|
3
|
-
|
|
4
|
-
function prophotorgb2srgbvalues(r, g, b, a = null) {
|
|
5
|
-
// @ts-ignore
|
|
6
|
-
return xyzd502srgb(...prophotorgb2xyz50(r, g, b, a));
|
|
7
|
-
}
|
|
8
|
-
function srgb2prophotorgbvalues(r, g, b, a) {
|
|
9
|
-
// @ts-ignore
|
|
10
|
-
return xyz50_to_prophotorgb(...XYZ_D65_to_D50(...srgb2xyz(r, g, b, a)));
|
|
11
|
-
}
|
|
12
|
-
function prophotorgb2lin_ProPhoto(r, g, b, a = null) {
|
|
13
|
-
return [r, g, b].map(v => {
|
|
14
|
-
let abs = Math.abs(v);
|
|
15
|
-
if (abs >= 16 / 512) {
|
|
16
|
-
return Math.sign(v) * Math.pow(abs, 1.8);
|
|
17
|
-
}
|
|
18
|
-
return v / 16;
|
|
19
|
-
}).concat(a == null || a == 1 ? [] : [a]);
|
|
20
|
-
}
|
|
21
|
-
function prophotorgb2xyz50(r, g, b, a = null) {
|
|
22
|
-
[r, g, b, a] = prophotorgb2lin_ProPhoto(r, g, b, a);
|
|
23
|
-
const xyz = [
|
|
24
|
-
0.7977666449006423 * r +
|
|
25
|
-
0.1351812974005331 * g +
|
|
26
|
-
0.0313477341283922 * b,
|
|
27
|
-
0.2880748288194013 * r +
|
|
28
|
-
0.7118352342418731 * g +
|
|
29
|
-
0.0000899369387256 * b,
|
|
30
|
-
0.8251046025104602 * b
|
|
31
|
-
];
|
|
32
|
-
return xyz.concat(a == null || a == 1 ? [] : [a]);
|
|
33
|
-
}
|
|
34
|
-
function xyz50_to_prophotorgb(x, y, z, a) {
|
|
35
|
-
// @ts-ignore
|
|
36
|
-
return gam_prophotorgb(...[
|
|
37
|
-
x * 1.3457868816471585 -
|
|
38
|
-
y * 0.2555720873797946 -
|
|
39
|
-
0.0511018649755453 * z,
|
|
40
|
-
x * -0.5446307051249019 +
|
|
41
|
-
y * 1.5082477428451466 +
|
|
42
|
-
0.0205274474364214 * z,
|
|
43
|
-
1.2119675456389452 * z
|
|
44
|
-
].concat(a == null || a == 1 ? [] : [a]));
|
|
45
|
-
}
|
|
46
|
-
function gam_prophotorgb(r, g, b, a) {
|
|
47
|
-
return [r, g, b].map(v => {
|
|
48
|
-
let abs = Math.abs(v);
|
|
49
|
-
if (abs >= 1 / 512) {
|
|
50
|
-
return Math.sign(v) * Math.pow(abs, 1 / 1.8);
|
|
51
|
-
}
|
|
52
|
-
return 16 * v;
|
|
53
|
-
}).concat(a == null || a == 1 ? [] : [a]);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export { prophotorgb2srgbvalues, srgb2prophotorgbvalues };
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { EnumToken, ValidationLevel } from '../ast/types.js';
|
|
2
|
-
import '../ast/minify.js';
|
|
3
|
-
import '../ast/walk.js';
|
|
4
|
-
import '../parser/parse.js';
|
|
5
|
-
import '../renderer/color/utils/constants.js';
|
|
6
|
-
import '../renderer/sourcemap/lib/encode.js';
|
|
7
|
-
import '../parser/utils/config.js';
|
|
8
|
-
import { getParsedSyntax, getSyntaxConfig } from './config.js';
|
|
9
|
-
import { validateSyntax } from './syntax.js';
|
|
10
|
-
|
|
11
|
-
function validateDeclaration(declaration, options, root) {
|
|
12
|
-
const config = getSyntaxConfig();
|
|
13
|
-
let name = declaration.nam;
|
|
14
|
-
if (!(name in config.declarations) && !(name in config.syntaxes)) {
|
|
15
|
-
if (name[0] == '-') {
|
|
16
|
-
const match = /^-([a-z]+)-(.*)$/.exec(name);
|
|
17
|
-
if (match != null) {
|
|
18
|
-
name = match[2];
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
// root is at-rule - check if declaration allowed
|
|
23
|
-
if (root?.typ == EnumToken.AtRuleNodeType) {
|
|
24
|
-
//
|
|
25
|
-
const syntax = getParsedSyntax("atRules" /* ValidationSyntaxGroupEnum.AtRules */, '@' + root.nam)?.[0];
|
|
26
|
-
// console.error({syntax});
|
|
27
|
-
if (syntax != null) {
|
|
28
|
-
if (!('chi' in syntax)) {
|
|
29
|
-
return {
|
|
30
|
-
valid: ValidationLevel.Drop,
|
|
31
|
-
node: declaration,
|
|
32
|
-
syntax,
|
|
33
|
-
error: 'declaration not allowed here'
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
if (name.startsWith('--')) {
|
|
37
|
-
return {
|
|
38
|
-
valid: ValidationLevel.Valid,
|
|
39
|
-
node: declaration,
|
|
40
|
-
syntax: null,
|
|
41
|
-
error: ''
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
// console.error({syntax});
|
|
45
|
-
const config = getSyntaxConfig();
|
|
46
|
-
// @ts-ignore
|
|
47
|
-
const obj = config["atRules" /* ValidationSyntaxGroupEnum.AtRules */]['@' + root.nam];
|
|
48
|
-
if ('descriptors' in obj) {
|
|
49
|
-
const descriptors = obj.descriptors;
|
|
50
|
-
if (!(name in descriptors)) {
|
|
51
|
-
return {
|
|
52
|
-
valid: ValidationLevel.Drop,
|
|
53
|
-
node: declaration,
|
|
54
|
-
syntax: `<${declaration.nam}>`,
|
|
55
|
-
error: ` declaration <${declaration.nam}> is not allowed in <@${root.nam}>`
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
const syntax = getParsedSyntax("atRules" /* ValidationSyntaxGroupEnum.AtRules */, ['@' + root.nam, 'descriptors', name]);
|
|
59
|
-
return validateSyntax(syntax, declaration.val, root, options);
|
|
60
|
-
}
|
|
61
|
-
// console.error({name});
|
|
62
|
-
// if (!(name in config.declarations) && !(name in config.syntaxes)) {
|
|
63
|
-
//
|
|
64
|
-
// return {
|
|
65
|
-
//
|
|
66
|
-
// valid: ValidationLevel.Drop,
|
|
67
|
-
// node: declaration,
|
|
68
|
-
// syntax: null,
|
|
69
|
-
// error: `unknown declaration "${declaration.nam}"`
|
|
70
|
-
// }
|
|
71
|
-
// }
|
|
72
|
-
//
|
|
73
|
-
// return validateSyntax((syntax as ValidationAtRuleDefinitionToken).chi as ValidationToken[], [declaration], root, options);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
if (name.startsWith('--')) {
|
|
77
|
-
return {
|
|
78
|
-
valid: ValidationLevel.Valid,
|
|
79
|
-
node: declaration,
|
|
80
|
-
syntax: null,
|
|
81
|
-
error: ''
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
if (!(name in config.declarations) && !(name in config.syntaxes)) {
|
|
85
|
-
return {
|
|
86
|
-
valid: ValidationLevel.Drop,
|
|
87
|
-
node: declaration,
|
|
88
|
-
syntax: `<${declaration.nam}>`,
|
|
89
|
-
error: `unknown declaration "${declaration.nam}"`
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
// return {
|
|
93
|
-
//
|
|
94
|
-
// valid: ValidationLevel.Valid,
|
|
95
|
-
// node: declaration,
|
|
96
|
-
// syntax: null,
|
|
97
|
-
// error: ''
|
|
98
|
-
// }
|
|
99
|
-
return validateSyntax(getParsedSyntax("declarations" /* ValidationSyntaxGroupEnum.Declarations */, name), declaration.val);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export { validateDeclaration };
|