@tbela99/css-parser 1.4.1 → 1.4.3
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/.nyc_output/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +1 -0
- package/.nyc_output/6193bc4c-6f5f-4898-8950-c628825e6342.json +1 -0
- package/.nyc_output/processinfo/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +1 -0
- package/.nyc_output/processinfo/6193bc4c-6f5f-4898-8950-c628825e6342.json +1 -0
- package/.repl_history +4 -0
- package/CHANGELOG.md +168 -4
- package/README.md +82 -0
- package/badges/coverage.svg +20 -0
- package/deno.lock +2861 -0
- package/dist/config.json.js +33 -1
- package/dist/index-umd-web.js +24393 -18479
- package/dist/index.cjs +27430 -21519
- package/dist/index.d.ts +1403 -933
- package/dist/lib/ast/clone.d.ts +10 -0
- package/dist/lib/ast/clone.js +45 -0
- package/dist/lib/ast/expand.d.ts +14 -0
- package/dist/lib/ast/expand.js +89 -64
- package/dist/lib/ast/features/calc.d.ts +10 -0
- package/dist/lib/ast/features/calc.js +62 -24
- package/dist/lib/ast/features/if.d.ts +10 -0
- package/dist/lib/ast/features/if.js +215 -0
- package/dist/lib/ast/features/index.d.ts +6 -0
- package/dist/lib/ast/features/index.js +1 -0
- package/dist/lib/ast/features/inlinecssvariables.d.ts +15 -0
- package/dist/lib/ast/features/inlinecssvariables.js +32 -27
- package/dist/lib/ast/features/prefix.d.ts +8 -0
- package/dist/lib/ast/features/prefix.js +68 -43
- package/dist/lib/ast/features/shorthand.d.ts +12 -0
- package/dist/lib/ast/features/shorthand.js +6 -9
- package/dist/lib/ast/features/transform.d.ts +10 -0
- package/dist/lib/ast/features/transform.js +9 -13
- package/dist/lib/ast/features/type.d.ts +15 -0
- package/dist/lib/ast/find.d.ts +165 -0
- package/dist/lib/ast/find.js +175 -0
- package/dist/lib/ast/math/expression.d.ts +18 -0
- package/dist/lib/ast/math/expression.js +140 -98
- package/dist/lib/ast/math/math.d.ts +6 -0
- package/dist/lib/ast/math/math.js +30 -41
- package/dist/lib/ast/minify.d.ts +19 -0
- package/dist/lib/ast/minify.js +543 -215
- package/dist/lib/ast/transform/compute.d.ts +8 -0
- package/dist/lib/ast/transform/compute.js +82 -69
- package/dist/lib/ast/transform/matrix.d.ts +22 -0
- package/dist/lib/ast/transform/matrix.js +12 -26
- package/dist/lib/ast/transform/minify.d.ts +5 -0
- package/dist/lib/ast/transform/minify.js +20 -20
- package/dist/lib/ast/transform/perspective.d.ts +3 -0
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.d.ts +12 -0
- package/dist/lib/ast/transform/rotate.js +1 -1
- package/dist/lib/ast/transform/scale.d.ts +6 -0
- package/dist/lib/ast/transform/scale.js +1 -1
- package/dist/lib/ast/transform/skew.d.ts +4 -0
- package/dist/lib/ast/transform/skew.js +1 -1
- package/dist/lib/ast/transform/translate.d.ts +6 -0
- package/dist/lib/ast/transform/translate.js +1 -1
- package/dist/lib/ast/transform/utils.d.ts +9 -0
- package/dist/lib/ast/types.d.ts +903 -0
- package/dist/lib/ast/types.js +277 -23
- package/dist/lib/ast/walk.d.ts +162 -0
- package/dist/lib/ast/walk.js +116 -60
- package/dist/lib/fs/resolve.d.ts +20 -0
- package/dist/lib/fs/resolve.js +37 -45
- package/dist/lib/parser/declaration/list.d.ts +16 -0
- package/dist/lib/parser/declaration/list.js +26 -24
- package/dist/lib/parser/declaration/map.d.ts +15 -0
- package/dist/lib/parser/declaration/map.js +140 -95
- package/dist/lib/parser/declaration/set.d.ts +9 -0
- package/dist/lib/parser/declaration/set.js +30 -25
- package/dist/lib/parser/node.d.ts +7 -0
- package/dist/lib/parser/parse.d.ts +107 -0
- package/dist/lib/parser/parse.js +1454 -1445
- package/dist/lib/parser/tokenize.d.ts +57 -0
- package/dist/lib/parser/tokenize.js +557 -404
- package/dist/lib/parser/utils/at-rule-container.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-container.js +486 -0
- package/dist/lib/parser/utils/at-rule-font-feature-values.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
- package/dist/lib/parser/utils/at-rule-generic.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-generic.js +118 -0
- package/dist/lib/parser/utils/at-rule-import.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-import.js +393 -0
- package/dist/lib/parser/utils/at-rule-media.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-media.js +603 -0
- package/dist/lib/parser/utils/at-rule-page.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-page.js +28 -0
- package/dist/lib/parser/utils/at-rule-support.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-support.js +366 -0
- package/dist/lib/parser/utils/at-rule-token.d.ts +1 -0
- package/dist/lib/parser/utils/at-rule-when-else.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
- package/dist/lib/parser/utils/at-rule.d.ts +13 -0
- package/dist/lib/parser/utils/at-rule.js +37 -0
- package/dist/lib/parser/utils/cache.d.ts +6 -0
- package/dist/lib/parser/utils/cache.js +19 -0
- package/dist/lib/parser/utils/config.d.ts +2 -0
- package/dist/lib/parser/utils/config.js +1 -0
- package/dist/lib/parser/utils/declaration-list.d.ts +5 -0
- package/dist/lib/parser/utils/declaration.d.ts +18 -0
- package/dist/lib/parser/utils/declaration.js +569 -91
- package/dist/lib/parser/utils/eq.d.ts +1 -0
- package/dist/lib/parser/utils/hash.d.ts +21 -0
- package/dist/lib/parser/utils/hash.js +1 -1
- package/dist/lib/parser/utils/selector.d.ts +5 -0
- package/dist/lib/parser/utils/selector.js +476 -0
- package/dist/lib/parser/utils/text.d.ts +3 -0
- package/dist/lib/parser/utils/text.js +17 -1
- package/dist/lib/parser/utils/token.d.ts +14 -0
- package/dist/lib/parser/utils/token.js +102 -0
- package/dist/lib/parser/utils/type.d.ts +2 -0
- package/dist/lib/parser/utils/type.js +29 -18
- package/dist/lib/renderer/render.d.ts +28 -0
- package/dist/lib/renderer/render.js +421 -262
- package/dist/lib/renderer/sourcemap/lib/encode.d.ts +1 -0
- package/dist/lib/renderer/sourcemap/sourcemap.d.ts +26 -0
- package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
- package/dist/lib/syntax/color/a98rgb.d.ts +2 -0
- package/dist/lib/syntax/color/a98rgb.js +8 -12
- package/dist/lib/syntax/color/cmyk.d.ts +10 -0
- package/dist/lib/syntax/color/cmyk.js +23 -21
- package/dist/lib/syntax/color/color-mix.d.ts +2 -0
- package/dist/lib/syntax/color/color-mix.js +88 -77
- package/dist/lib/syntax/color/color.d.ts +42 -0
- package/dist/lib/syntax/color/color.js +65 -68
- package/dist/lib/syntax/color/hex.d.ts +16 -0
- package/dist/lib/syntax/color/hex.js +27 -31
- package/dist/lib/syntax/color/hsl.d.ts +20 -0
- package/dist/lib/syntax/color/hsl.js +5 -12
- package/dist/lib/syntax/color/hsv.d.ts +2 -0
- package/dist/lib/syntax/color/hwb.d.ts +21 -0
- package/dist/lib/syntax/color/hwb.js +8 -21
- package/dist/lib/syntax/color/lab.d.ts +25 -0
- package/dist/lib/syntax/color/lab.js +20 -21
- package/dist/lib/syntax/color/lch.d.ts +23 -0
- package/dist/lib/syntax/color/lch.js +13 -15
- package/dist/lib/syntax/color/oklab.d.ts +22 -0
- package/dist/lib/syntax/color/oklab.js +20 -39
- package/dist/lib/syntax/color/oklch.d.ts +20 -0
- package/dist/lib/syntax/color/oklch.js +14 -16
- package/dist/lib/syntax/color/p3.d.ts +6 -0
- package/dist/lib/syntax/color/p3.js +0 -8
- package/dist/lib/syntax/color/prophotorgb.d.ts +2 -0
- package/dist/lib/syntax/color/rec2020.d.ts +2 -0
- package/dist/lib/syntax/color/rec2020.js +9 -13
- package/dist/lib/syntax/color/relativecolor.d.ts +13 -0
- package/dist/lib/syntax/color/relativecolor.js +68 -41
- package/dist/lib/syntax/color/rgb.d.ts +20 -0
- package/dist/lib/syntax/color/rgb.js +14 -18
- package/dist/lib/syntax/color/srgb.d.ts +23 -0
- package/dist/lib/syntax/color/srgb.js +27 -26
- package/dist/lib/syntax/color/utils/components.d.ts +2 -0
- package/dist/lib/syntax/color/utils/components.js +30 -14
- package/dist/lib/syntax/color/utils/distance.d.ts +18 -0
- package/dist/lib/syntax/color/utils/distance.js +1 -8
- package/dist/lib/syntax/color/utils/matrix.d.ts +6 -0
- package/dist/lib/syntax/color/xyz.d.ts +5 -0
- package/dist/lib/syntax/color/xyz.js +8 -20
- package/dist/lib/syntax/color/xyzd50.d.ts +4 -0
- package/dist/lib/syntax/color/xyzd50.js +6 -20
- package/dist/lib/syntax/constants.d.ts +67 -0
- package/dist/lib/syntax/constants.js +436 -0
- package/dist/lib/syntax/syntax.d.ts +38 -0
- package/dist/lib/syntax/syntax.js +533 -568
- package/dist/lib/validation/config.d.ts +14 -0
- package/dist/lib/validation/config.js +72 -33
- package/dist/lib/validation/config.json.js +1159 -74
- package/dist/lib/validation/json.d.ts +2 -0
- package/dist/lib/validation/match.d.ts +38 -0
- package/dist/lib/validation/match.js +2985 -0
- package/dist/lib/validation/parser/parse.d.ts +8 -0
- package/dist/lib/validation/parser/parse.js +684 -935
- package/dist/lib/validation/parser/typedef.d.ts +95 -0
- package/dist/lib/validation/parser/typedef.js +100 -0
- package/dist/lib/validation/utils/list.d.ts +4 -0
- package/dist/lib/validation/utils/list.js +4 -11
- package/dist/lib/validation/utils/whitespace.d.ts +2 -0
- package/dist/lib/validation/utils/whitespace.js +2 -8
- package/dist/node.d.ts +207 -0
- package/dist/node.js +53 -47
- package/dist/web.d.ts +169 -0
- package/dist/web.js +50 -41
- package/package.json +18 -13
- package/playground/index.html +1328 -0
- package/playground/sw.js +55 -0
- package/playground/tree.js +176 -0
- package/dist/lib/syntax/color/utils/constants.js +0 -214
- package/dist/lib/syntax/utils.js +0 -70
- package/dist/lib/validation/at-rules/container.js +0 -342
- package/dist/lib/validation/at-rules/counter-style.js +0 -90
- package/dist/lib/validation/at-rules/custom-media.js +0 -50
- package/dist/lib/validation/at-rules/document.js +0 -89
- package/dist/lib/validation/at-rules/else.js +0 -5
- package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
- package/dist/lib/validation/at-rules/import.js +0 -150
- package/dist/lib/validation/at-rules/keyframes.js +0 -67
- package/dist/lib/validation/at-rules/layer.js +0 -41
- package/dist/lib/validation/at-rules/media.js +0 -255
- package/dist/lib/validation/at-rules/namespace.js +0 -81
- package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
- package/dist/lib/validation/at-rules/page.js +0 -100
- package/dist/lib/validation/at-rules/supports.js +0 -295
- package/dist/lib/validation/at-rules/when.js +0 -185
- package/dist/lib/validation/atrule.js +0 -184
- package/dist/lib/validation/selector.js +0 -36
- package/dist/lib/validation/syntax.js +0 -1073
- package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
- package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
- package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
- package/dist/lib/validation/syntaxes/family-name.js +0 -57
- package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
- package/dist/lib/validation/syntaxes/layer-name.js +0 -57
- package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
- package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
- package/dist/lib/validation/syntaxes/selector-list.js +0 -5
- package/dist/lib/validation/syntaxes/selector.js +0 -5
- package/dist/lib/validation/syntaxes/url.js +0 -40
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function eq(a: any, b: any): boolean;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const LOWER = "abcdefghijklmnopqrstuvwxyz";
|
|
2
|
+
export declare const DIGITS = "0123456789";
|
|
3
|
+
export declare const FULL_ALPHABET: string[];
|
|
4
|
+
export declare const FIRST_ALPHABET: string[];
|
|
5
|
+
/**
|
|
6
|
+
* supported hash algorithms
|
|
7
|
+
*/
|
|
8
|
+
export declare const hashAlgorithms: string[];
|
|
9
|
+
/**
|
|
10
|
+
* generate a hash id
|
|
11
|
+
* @param input
|
|
12
|
+
* @param length
|
|
13
|
+
*/
|
|
14
|
+
export declare function hashId(input: string, length?: number): string;
|
|
15
|
+
/**
|
|
16
|
+
* generate a hash
|
|
17
|
+
* @param input
|
|
18
|
+
* @param length
|
|
19
|
+
* @param algo
|
|
20
|
+
*/
|
|
21
|
+
export declare function hash(input: string, length?: number, algo?: string): Promise<string>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Token, AstRule, AstAtRule, AstKeyFrameRule, AstKeyframesAtRule, AstStyleSheet, ParserOptions, ErrorDescription, AstInvalidRule, AtRuleToken } from "../../../@types/index.d.ts";
|
|
2
|
+
/**
|
|
3
|
+
* parse selector
|
|
4
|
+
*/
|
|
5
|
+
export declare function parseSelector(tokens: Token[], context: AtRuleToken | AstRule | AstAtRule | AstKeyFrameRule | AstKeyframesAtRule | AstStyleSheet | null, options: ParserOptions, errors: ErrorDescription[]): AstRule | AstInvalidRule | AstKeyFrameRule;
|
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
import { EnumToken } from '../../ast/types.js';
|
|
2
|
+
import { renderToken } from '../../renderer/render.js';
|
|
3
|
+
import { definedPropertySettings, tokensfuncDefMap, combinators } from '../../syntax/constants.js';
|
|
4
|
+
import { pseudoElements, isIdent, isHash } from '../../syntax/syntax.js';
|
|
5
|
+
import { getParsedSyntax, getSyntaxConfig } from '../../validation/config.js';
|
|
6
|
+
import { matchAllSyntax, createValidationContext, trimArray, matchSelectorSyntax } from '../../validation/match.js';
|
|
7
|
+
import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
|
|
8
|
+
import { splitTokenList } from '../../validation/utils/list.js';
|
|
9
|
+
import { trimWhiteSpace } from '../parse.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* parse selector
|
|
13
|
+
*/
|
|
14
|
+
function parseSelector(tokens, context, options, errors) {
|
|
15
|
+
if (context?.typ === EnumToken.KeyframesAtRuleNodeType) {
|
|
16
|
+
const result = matchAllSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "keyframe-selectors"), createValidationContext(tokens), options);
|
|
17
|
+
const parts = splitTokenList(tokens);
|
|
18
|
+
for (const part of parts) {
|
|
19
|
+
trimArray(part);
|
|
20
|
+
if (options.minify) {
|
|
21
|
+
const filtered = part.filter((token) => token.typ !== EnumToken.WhitespaceTokenType && token.typ !== EnumToken.CommentTokenType);
|
|
22
|
+
if (filtered.length === 1) {
|
|
23
|
+
if (filtered[0].typ === EnumToken.IdenTokenType &&
|
|
24
|
+
"from" === filtered[0].val.toLowerCase()) {
|
|
25
|
+
filtered[0] = Object.defineProperty({
|
|
26
|
+
typ: EnumToken.PercentageTokenType,
|
|
27
|
+
val: 0,
|
|
28
|
+
}, "loc", {
|
|
29
|
+
...definedPropertySettings,
|
|
30
|
+
value: filtered[0].loc,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
else if (filtered[0].typ === EnumToken.PercentageTokenType &&
|
|
34
|
+
100 === filtered[0].val) {
|
|
35
|
+
filtered[0] = Object.defineProperty({
|
|
36
|
+
typ: EnumToken.IdenTokenType,
|
|
37
|
+
val: "to",
|
|
38
|
+
}, "loc", {
|
|
39
|
+
...definedPropertySettings,
|
|
40
|
+
value: filtered[0].loc,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
part.splice(0, part.length, ...filtered);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
tokens.splice(0, tokens.length, ...parts.reduce((acc, curr) => {
|
|
48
|
+
if (acc.length > 0) {
|
|
49
|
+
acc.push({ typ: EnumToken.CommaTokenType });
|
|
50
|
+
}
|
|
51
|
+
acc.push(...curr);
|
|
52
|
+
return acc;
|
|
53
|
+
}, []));
|
|
54
|
+
return Object.defineProperties({
|
|
55
|
+
typ: result.success ? EnumToken.KeyFramesRuleNodeType : EnumToken.InvalidRuleNodeType,
|
|
56
|
+
sel: [
|
|
57
|
+
...splitTokenList(trimArray(tokens)).reduce((acc, curr) => {
|
|
58
|
+
acc.add(curr.reduce((acc, curr) => acc + renderToken(curr, { minify: false }), ""));
|
|
59
|
+
return acc;
|
|
60
|
+
}, new Set()),
|
|
61
|
+
].join(),
|
|
62
|
+
chi: [],
|
|
63
|
+
}, {
|
|
64
|
+
tokens: { ...definedPropertySettings, value: tokens.length === 0 ? null : tokens },
|
|
65
|
+
loc: {
|
|
66
|
+
...definedPropertySettings,
|
|
67
|
+
value: {
|
|
68
|
+
...tokens[0].loc,
|
|
69
|
+
end: { ...(tokens[tokens.length - 1]?.loc?.end ?? tokens[0]?.loc?.end) },
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
const stack = [];
|
|
75
|
+
const uniq = new Map();
|
|
76
|
+
let i = 0;
|
|
77
|
+
let index;
|
|
78
|
+
let parent = context;
|
|
79
|
+
let nested = false;
|
|
80
|
+
let val;
|
|
81
|
+
do {
|
|
82
|
+
if (parent?.typ === EnumToken.AtRuleNodeType && "media" === parent.nam) {
|
|
83
|
+
parent = parent.parent;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
nested = parent?.typ == EnumToken.RuleNodeType;
|
|
87
|
+
parent = parent?.parent;
|
|
88
|
+
} while (!nested && parent != null);
|
|
89
|
+
for (; i < tokens.length; i++) {
|
|
90
|
+
if (tokens[i].typ == EnumToken.ColonTokenType) {
|
|
91
|
+
if (tokens[i + 1]?.typ == EnumToken.IdenTokenType) {
|
|
92
|
+
Object.assign(tokens[i], {
|
|
93
|
+
typ: EnumToken.PseudoElementTokenType,
|
|
94
|
+
val: ":" + tokens[i + 1].val,
|
|
95
|
+
});
|
|
96
|
+
tokens[i].loc.end = tokens[i + 1].loc.end;
|
|
97
|
+
tokens.splice(i + 1, 1);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (tokens[i + 1]?.typ == EnumToken.FunctionTokenDefType) {
|
|
101
|
+
val = ":" + tokens[i + 1].val;
|
|
102
|
+
Object.assign(tokens[i], {
|
|
103
|
+
typ: val + "()" in getSyntaxConfig().selectors
|
|
104
|
+
? EnumToken.PseudoClassFunctionTokenDefType
|
|
105
|
+
: tokens[i + 1].typ,
|
|
106
|
+
val,
|
|
107
|
+
});
|
|
108
|
+
tokens[i].loc.end = tokens[i + 1].loc.end;
|
|
109
|
+
tokens.splice(i + 1, 1);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (tokens[i].typ == EnumToken.DoubleColonTokenType) {
|
|
114
|
+
val = ":" + tokens[i + 1].val;
|
|
115
|
+
if (tokens[i + 1]?.typ == EnumToken.IdenTokenType) {
|
|
116
|
+
Object.assign(tokens[i], {
|
|
117
|
+
typ: EnumToken.PseudoClassTokenType,
|
|
118
|
+
val: (pseudoElements.includes(val) ? "" : ":") + val,
|
|
119
|
+
});
|
|
120
|
+
tokens[i].loc.end = tokens[i + 1].loc.end;
|
|
121
|
+
tokens.splice(i + 1, 1);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (tokens[i + 1]?.typ == EnumToken.FunctionTokenDefType) {
|
|
125
|
+
val = "::" + tokens[i + 1].val;
|
|
126
|
+
Object.assign(tokens[i], {
|
|
127
|
+
typ: val + "()" in getSyntaxConfig().selectors
|
|
128
|
+
? EnumToken.PseudoClassFunctionTokenDefType
|
|
129
|
+
: EnumToken.FunctionTokenDefType,
|
|
130
|
+
val,
|
|
131
|
+
});
|
|
132
|
+
tokens[i].loc.end = tokens[i + 1].loc.end;
|
|
133
|
+
tokens.splice(i + 1, 1);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (tokens[i].typ == EnumToken.ColorTokenType) {
|
|
138
|
+
if (isIdent(tokens[i].val)) {
|
|
139
|
+
Object.assign(tokens[i], {
|
|
140
|
+
typ: EnumToken.IdenTokenType,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
else if (isHash(tokens[i].val)) {
|
|
144
|
+
Object.assign(tokens[i], {
|
|
145
|
+
typ: EnumToken.HashTokenType,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const result = matchSelectorSyntax(tokens, errors, options, nested === true);
|
|
151
|
+
trimArray(tokens);
|
|
152
|
+
if (result.success) {
|
|
153
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
154
|
+
const token = tokens[i];
|
|
155
|
+
switch (token.typ) {
|
|
156
|
+
case EnumToken.AttrStartTokenType:
|
|
157
|
+
stack.push(token);
|
|
158
|
+
break;
|
|
159
|
+
case EnumToken.AttrEndTokenType:
|
|
160
|
+
if (stack.at(-1)?.typ == EnumToken.AttrStartTokenType) {
|
|
161
|
+
index = tokens.indexOf(stack.at(-1));
|
|
162
|
+
// @ts-expect-error
|
|
163
|
+
const { val, ...attr } = stack.at(-1);
|
|
164
|
+
Object.defineProperty(attr, "loc", {
|
|
165
|
+
...definedPropertySettings,
|
|
166
|
+
value: {
|
|
167
|
+
...stack.at(-1).loc,
|
|
168
|
+
end: token.loc.end,
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
tokens.splice(i, 1);
|
|
172
|
+
Object.assign(attr, {
|
|
173
|
+
typ: EnumToken.AttrTokenType,
|
|
174
|
+
chi: tokens.splice(index + 1, i - index - 1),
|
|
175
|
+
});
|
|
176
|
+
tokens[index] = attr;
|
|
177
|
+
i = index;
|
|
178
|
+
stack.pop();
|
|
179
|
+
}
|
|
180
|
+
break;
|
|
181
|
+
case EnumToken.PseudoClassFunctionTokenDefType:
|
|
182
|
+
stack.push(token);
|
|
183
|
+
break;
|
|
184
|
+
case EnumToken.EndParensTokenType:
|
|
185
|
+
if (stack.at(-1)?.typ == EnumToken.PseudoClassFunctionTokenDefType) {
|
|
186
|
+
const func = stack.at(-1);
|
|
187
|
+
index = tokens.indexOf(func);
|
|
188
|
+
stack.at(-1).loc.end = token.loc.end;
|
|
189
|
+
tokens.splice(i, 1);
|
|
190
|
+
if (tokensfuncDefMap.has(func.typ)) {
|
|
191
|
+
// @ts-expect-error
|
|
192
|
+
func.typ = tokensfuncDefMap.get(func.typ);
|
|
193
|
+
func.chi = tokens.splice(index + 1, i - index - 1);
|
|
194
|
+
}
|
|
195
|
+
if (result.success && options.minify) {
|
|
196
|
+
// parse an+b
|
|
197
|
+
// an+b produces an ident such as 'n-0', a literal such as '+2n-0' or a list of tokens such as[2n\s?[+-]\s?3]
|
|
198
|
+
if (func.val == ":nth-child" ||
|
|
199
|
+
func.val == ":nth-last-child" ||
|
|
200
|
+
func.val == ":nth-of-type" ||
|
|
201
|
+
func.val == ":nth-last-of-type") {
|
|
202
|
+
const token = func.chi.find((t) => t.typ != EnumToken.WhitespaceTokenType && t.typ != EnumToken.CommentTokenType);
|
|
203
|
+
if (token?.typ == EnumToken.IdenTokenType || token?.typ == EnumToken.LiteralTokenType) {
|
|
204
|
+
if (token.typ == EnumToken.IdenTokenType &&
|
|
205
|
+
(token.val == "odd" || token.val == "even")) {
|
|
206
|
+
if (token.val == "even") {
|
|
207
|
+
Object.assign(token, {
|
|
208
|
+
typ: EnumToken.DimensionTokenType,
|
|
209
|
+
val: 2,
|
|
210
|
+
unit: "n",
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
if (!/\d+$/.test(token.val)) {
|
|
216
|
+
let index = func.chi.indexOf(token);
|
|
217
|
+
let i = index + 1;
|
|
218
|
+
let sign = null;
|
|
219
|
+
let num = null;
|
|
220
|
+
for (; i < func.chi.length; i++) {
|
|
221
|
+
if (func.chi[i].typ == EnumToken.WhitespaceTokenType ||
|
|
222
|
+
func.chi[i].typ == EnumToken.CommentTokenType) {
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
if (func.chi[i].typ == EnumToken.NumberTokenType) {
|
|
226
|
+
num = func.chi[i];
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
sign = func.chi[i];
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (num != null) {
|
|
234
|
+
if (num.val === 0) {
|
|
235
|
+
func.chi.splice(index + 1, i - index);
|
|
236
|
+
if (token.val == "-n") {
|
|
237
|
+
token.val = "n";
|
|
238
|
+
}
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
if (sign == null) {
|
|
242
|
+
func.chi.splice(index + 1, i - index - 1);
|
|
243
|
+
if (Math.sign(num.val) === 1) {
|
|
244
|
+
func.chi.splice(index + 1, 0, {
|
|
245
|
+
typ: EnumToken.LiteralTokenType,
|
|
246
|
+
val: "+",
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
else if (token.val == "-n") {
|
|
252
|
+
token.val = "n";
|
|
253
|
+
}
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
const matches = /^(([+-]?[0-9]*)?n)?([+-]?[0-9]+)?$/.exec(token.val);
|
|
257
|
+
if (matches != null) {
|
|
258
|
+
const a1 = matches[2] === "" ? 1 : matches[2] === "-" ? -1 : +matches[2];
|
|
259
|
+
const b1 = +matches[3];
|
|
260
|
+
if (a1 === 0) {
|
|
261
|
+
if (b1 === 1) {
|
|
262
|
+
let hasSelector = false;
|
|
263
|
+
let i = func.chi.indexOf(token);
|
|
264
|
+
let j = i + 1;
|
|
265
|
+
for (; j < func.chi.length; j++) {
|
|
266
|
+
if (func.chi[j].typ == EnumToken.IdenTokenType &&
|
|
267
|
+
func.chi[j].val == "of") {
|
|
268
|
+
hasSelector = true;
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
if (hasSelector) {
|
|
273
|
+
Object.assign(token, {
|
|
274
|
+
typ: EnumToken.NumberTokenType,
|
|
275
|
+
val: b1,
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
// :first-child
|
|
280
|
+
tokens[tokens.indexOf(func)] = Object.defineProperties({
|
|
281
|
+
typ: EnumToken.PseudoClassTokenType,
|
|
282
|
+
val: ":first-child",
|
|
283
|
+
}, {
|
|
284
|
+
loc: {
|
|
285
|
+
...definedPropertySettings,
|
|
286
|
+
value: func.loc,
|
|
287
|
+
},
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
Object.assign(token, { typ: EnumToken.NumberTokenType, val: b1 });
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
else if (b1 === 0) {
|
|
297
|
+
Object.assign(token, Math.abs(a1) === 1
|
|
298
|
+
? {
|
|
299
|
+
typ: EnumToken.IdenTokenType,
|
|
300
|
+
val: "n",
|
|
301
|
+
}
|
|
302
|
+
: {
|
|
303
|
+
typ: EnumToken.DimensionTokenType,
|
|
304
|
+
val: a1 * Math.sign(a1),
|
|
305
|
+
unit: "n",
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
else if (Math.abs(a1) === 2) {
|
|
309
|
+
if (b1 === 0) {
|
|
310
|
+
Object.assign(token, {
|
|
311
|
+
typ: EnumToken.DimensionTokenType,
|
|
312
|
+
val: a1,
|
|
313
|
+
unit: "n",
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
else if (Math.abs(b1) === 1) {
|
|
317
|
+
Object.assign(token, { typ: EnumToken.IdenTokenType, val: "odd" });
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
else if (token?.typ === EnumToken.DimensionTokenType) {
|
|
324
|
+
const index = func.chi.indexOf(token);
|
|
325
|
+
let sign = null;
|
|
326
|
+
let num = null;
|
|
327
|
+
let i = index + 1;
|
|
328
|
+
for (; i < func.chi.length; i++) {
|
|
329
|
+
if (func.chi[i].typ == EnumToken.WhitespaceTokenType ||
|
|
330
|
+
func.chi[i].typ == EnumToken.CommentTokenType) {
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
if (func.chi[i].typ == EnumToken.NumberTokenType) {
|
|
334
|
+
num = func.chi[i];
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
sign = func.chi[i];
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
if (num != null) {
|
|
342
|
+
if (token.val === 0) {
|
|
343
|
+
if (num.val === 0) {
|
|
344
|
+
func.chi.splice(0, i);
|
|
345
|
+
}
|
|
346
|
+
else if (num.val === 1) {
|
|
347
|
+
let hasSelector = false;
|
|
348
|
+
let j = i + 1;
|
|
349
|
+
for (; j < func.chi.length; j++) {
|
|
350
|
+
if (func.chi[j].typ == EnumToken.IdenTokenType &&
|
|
351
|
+
func.chi[j].val == "of") {
|
|
352
|
+
hasSelector = true;
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
if (hasSelector) {
|
|
357
|
+
func.chi.splice(0, i);
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
tokens[tokens.indexOf(func)] = Object.defineProperties({
|
|
361
|
+
typ: EnumToken.PseudoClassTokenType,
|
|
362
|
+
val: ":first-child",
|
|
363
|
+
}, {
|
|
364
|
+
loc: {
|
|
365
|
+
...definedPropertySettings,
|
|
366
|
+
value: func.loc,
|
|
367
|
+
},
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
break;
|
|
371
|
+
}
|
|
372
|
+
else {
|
|
373
|
+
func.chi.splice(0, i);
|
|
374
|
+
}
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
else if (num.val === 0) {
|
|
378
|
+
func.chi.splice(index + 1, i - index);
|
|
379
|
+
if (token.val < 0) {
|
|
380
|
+
token.val *= -1;
|
|
381
|
+
}
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
else if (token.val === 2 &&
|
|
385
|
+
Math.abs(num.val) === 1) {
|
|
386
|
+
Object.assign(token, {
|
|
387
|
+
typ: EnumToken.IdenTokenType,
|
|
388
|
+
val: "odd",
|
|
389
|
+
});
|
|
390
|
+
func.chi.splice(index + 1, i - index);
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
if (token.val === 1) {
|
|
394
|
+
Object.assign(token, {
|
|
395
|
+
typ: EnumToken.IdenTokenType,
|
|
396
|
+
val: Math.sign(token.val) === 1
|
|
397
|
+
? "n"
|
|
398
|
+
: "-n",
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
if (sign == null) {
|
|
402
|
+
func.chi.splice(index + 1, i - index - 1);
|
|
403
|
+
if (Math.sign(num.val) === 1) {
|
|
404
|
+
func.chi.splice(index + 1, 0, {
|
|
405
|
+
typ: EnumToken.LiteralTokenType,
|
|
406
|
+
val: "+",
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
i = index;
|
|
415
|
+
stack.pop();
|
|
416
|
+
}
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return Object.defineProperties({
|
|
422
|
+
typ: result.success ? EnumToken.RuleNodeType : EnumToken.InvalidRuleNodeType,
|
|
423
|
+
sel: [
|
|
424
|
+
...tokens
|
|
425
|
+
.reduce((acc, curr, index, array) => {
|
|
426
|
+
if (curr.typ == EnumToken.CommentTokenType) {
|
|
427
|
+
return acc;
|
|
428
|
+
}
|
|
429
|
+
if (curr.typ == EnumToken.WhitespaceTokenType) {
|
|
430
|
+
if (trimWhiteSpace.includes(array[index - 1]?.typ) ||
|
|
431
|
+
trimWhiteSpace.includes(array[index + 1]?.typ) ||
|
|
432
|
+
combinators.includes(array[index - 1]?.val) ||
|
|
433
|
+
combinators.includes(array[index + 1]?.val)) {
|
|
434
|
+
return acc;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
let t = renderToken(curr, { minify: false });
|
|
438
|
+
if (t == ",") {
|
|
439
|
+
acc.push([]);
|
|
440
|
+
}
|
|
441
|
+
else {
|
|
442
|
+
acc[acc.length - 1].push(t);
|
|
443
|
+
}
|
|
444
|
+
return acc;
|
|
445
|
+
}, [[]])
|
|
446
|
+
.reduce((acc, curr) => {
|
|
447
|
+
let i = 0;
|
|
448
|
+
for (; i < curr.length; i++) {
|
|
449
|
+
if (i + 1 < curr.length && curr[i] == "*") {
|
|
450
|
+
if (curr[i] == "*") {
|
|
451
|
+
let index = curr[i + 1] == " " ? 2 : 1;
|
|
452
|
+
if (![">", "~", "+"].includes(curr[index])) {
|
|
453
|
+
curr.splice(i, index);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
acc.set(curr.join(""), curr);
|
|
459
|
+
return acc;
|
|
460
|
+
}, uniq)
|
|
461
|
+
.keys(),
|
|
462
|
+
].join(","),
|
|
463
|
+
chi: [],
|
|
464
|
+
}, {
|
|
465
|
+
tokens: { ...definedPropertySettings, value: tokens },
|
|
466
|
+
loc: {
|
|
467
|
+
...definedPropertySettings,
|
|
468
|
+
value: {
|
|
469
|
+
...tokens[0].loc,
|
|
470
|
+
end: tokens[tokens.length - 1].loc.end,
|
|
471
|
+
},
|
|
472
|
+
},
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
export { parseSelector };
|
|
@@ -4,5 +4,21 @@ function dasherize(value) {
|
|
|
4
4
|
function camelize(value) {
|
|
5
5
|
return value.replace(/-([a-z])/g, (all, one) => one.toUpperCase());
|
|
6
6
|
}
|
|
7
|
+
function equalsIgnoreCase(a, b) {
|
|
8
|
+
if (a.length !== b.length)
|
|
9
|
+
return false;
|
|
10
|
+
for (let i = 0; i < a.length; i++) {
|
|
11
|
+
let ca = a.charCodeAt(i);
|
|
12
|
+
let cb = b.charCodeAt(i);
|
|
13
|
+
// Normalize A-Z to a-z
|
|
14
|
+
if (ca >= 65 && ca <= 90)
|
|
15
|
+
ca += 32;
|
|
16
|
+
if (cb >= 65 && cb <= 90)
|
|
17
|
+
cb += 32;
|
|
18
|
+
if (ca !== cb)
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
7
23
|
|
|
8
|
-
export { camelize, dasherize };
|
|
24
|
+
export { camelize, dasherize, equalsIgnoreCase };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AstNode } from "../../../@types/ast.d.ts";
|
|
2
|
+
import type { BinaryExpressionToken, Token } from "../../../@types/token.d.ts";
|
|
3
|
+
/**
|
|
4
|
+
* replace token in its parent node
|
|
5
|
+
* @param parent
|
|
6
|
+
* @param value
|
|
7
|
+
* @param replacement
|
|
8
|
+
*/
|
|
9
|
+
export declare function replaceToken(parent: BinaryExpressionToken | (AstNode & ({
|
|
10
|
+
chi: Token[];
|
|
11
|
+
} | {
|
|
12
|
+
val: Token[];
|
|
13
|
+
})), value: Token, replacement: Token | Token[]): boolean;
|
|
14
|
+
export declare function trimWhiteSpaceTokens(tokens: Token[]): Token[];
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { EnumToken } from '../../ast/types.js';
|
|
2
|
+
import { definedPropertySettings, tokensfuncSet } from '../../syntax/constants.js';
|
|
3
|
+
|
|
4
|
+
const trimTokenSpace = new Set([
|
|
5
|
+
EnumToken.CommaTokenType,
|
|
6
|
+
EnumToken.DelimTokenType,
|
|
7
|
+
EnumToken.GtTokenType,
|
|
8
|
+
EnumToken.GteTokenType,
|
|
9
|
+
EnumToken.LtTokenType,
|
|
10
|
+
EnumToken.LteTokenType,
|
|
11
|
+
EnumToken.ChildCombinatorTokenType,
|
|
12
|
+
]);
|
|
13
|
+
const trimSpaceAfter = new Set([
|
|
14
|
+
...tokensfuncSet.values(),
|
|
15
|
+
EnumToken.ChildCombinatorTokenType,
|
|
16
|
+
EnumToken.SupportsQueryConditionTokenType,
|
|
17
|
+
EnumToken.SupportsQueryUnaryConditionTokenType,
|
|
18
|
+
]);
|
|
19
|
+
/**
|
|
20
|
+
* replace token in its parent node
|
|
21
|
+
* @param parent
|
|
22
|
+
* @param value
|
|
23
|
+
* @param replacement
|
|
24
|
+
*/
|
|
25
|
+
function replaceToken(parent, value, replacement) {
|
|
26
|
+
if (replacement == null || (Array.isArray(replacement) && replacement.length === 0)) {
|
|
27
|
+
throw new TypeError(`replacement is null`);
|
|
28
|
+
}
|
|
29
|
+
for (const node of Array.isArray(replacement) ? replacement : [replacement]) {
|
|
30
|
+
if ("parent" in value && value.parent != node.parent) {
|
|
31
|
+
Object.defineProperty(node, "parent", {
|
|
32
|
+
...definedPropertySettings,
|
|
33
|
+
value: value.parent,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (parent.typ == EnumToken.BinaryExpressionTokenType) {
|
|
38
|
+
if (parent.l == value) {
|
|
39
|
+
parent.l = replacement;
|
|
40
|
+
}
|
|
41
|
+
else if (parent.r == value) {
|
|
42
|
+
parent.r = replacement;
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
throw new ReferenceError("Node not found");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
const target = "val" in parent && Array.isArray(parent.val)
|
|
50
|
+
? parent.val
|
|
51
|
+
: (parent.chi ?? parent);
|
|
52
|
+
if (Array.isArray(target)) {
|
|
53
|
+
// @ts-ignore
|
|
54
|
+
const index = target.indexOf(value);
|
|
55
|
+
if (index == -1) {
|
|
56
|
+
throw new ReferenceError("Node not found");
|
|
57
|
+
}
|
|
58
|
+
target.splice(index, 1, ...(Array.isArray(replacement) ? replacement : [replacement]));
|
|
59
|
+
}
|
|
60
|
+
else if ("l" in target && target.l == value) {
|
|
61
|
+
target.l = replacement;
|
|
62
|
+
}
|
|
63
|
+
else if ("r" in target && target.r == value) {
|
|
64
|
+
target.r = replacement;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
throw new ReferenceError("Node not found");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
function trimWhiteSpaceTokens(tokens) {
|
|
73
|
+
let i = 0;
|
|
74
|
+
if (tokens[0]?.typ === EnumToken.WhitespaceTokenType) {
|
|
75
|
+
tokens.shift();
|
|
76
|
+
}
|
|
77
|
+
if (tokens[tokens.length - 1]?.typ === EnumToken.WhitespaceTokenType) {
|
|
78
|
+
tokens.pop();
|
|
79
|
+
}
|
|
80
|
+
if (tokens.length === 1) {
|
|
81
|
+
if (tokens[0].typ === EnumToken.WhitespaceTokenType) {
|
|
82
|
+
tokens.length = 0;
|
|
83
|
+
}
|
|
84
|
+
return tokens;
|
|
85
|
+
}
|
|
86
|
+
for (; i < tokens.length; i++) {
|
|
87
|
+
if (trimSpaceAfter.has(tokens[i].typ) && tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
|
|
88
|
+
tokens.splice(i + 1, 1);
|
|
89
|
+
}
|
|
90
|
+
else if (trimTokenSpace.has(tokens[i].typ)) {
|
|
91
|
+
if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
|
|
92
|
+
tokens.splice(i + 1, 1);
|
|
93
|
+
}
|
|
94
|
+
if (i > 0 && tokens[i - 1].typ === EnumToken.Whitespace) {
|
|
95
|
+
tokens.splice(--i, 1);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return tokens;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export { replaceToken, trimWhiteSpaceTokens };
|