@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.
Files changed (216) hide show
  1. package/.nyc_output/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +1 -0
  2. package/.nyc_output/6193bc4c-6f5f-4898-8950-c628825e6342.json +1 -0
  3. package/.nyc_output/processinfo/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +1 -0
  4. package/.nyc_output/processinfo/6193bc4c-6f5f-4898-8950-c628825e6342.json +1 -0
  5. package/.repl_history +4 -0
  6. package/CHANGELOG.md +168 -4
  7. package/README.md +82 -0
  8. package/badges/coverage.svg +20 -0
  9. package/deno.lock +2861 -0
  10. package/dist/config.json.js +33 -1
  11. package/dist/index-umd-web.js +24393 -18479
  12. package/dist/index.cjs +27430 -21519
  13. package/dist/index.d.ts +1403 -933
  14. package/dist/lib/ast/clone.d.ts +10 -0
  15. package/dist/lib/ast/clone.js +45 -0
  16. package/dist/lib/ast/expand.d.ts +14 -0
  17. package/dist/lib/ast/expand.js +89 -64
  18. package/dist/lib/ast/features/calc.d.ts +10 -0
  19. package/dist/lib/ast/features/calc.js +62 -24
  20. package/dist/lib/ast/features/if.d.ts +10 -0
  21. package/dist/lib/ast/features/if.js +215 -0
  22. package/dist/lib/ast/features/index.d.ts +6 -0
  23. package/dist/lib/ast/features/index.js +1 -0
  24. package/dist/lib/ast/features/inlinecssvariables.d.ts +15 -0
  25. package/dist/lib/ast/features/inlinecssvariables.js +32 -27
  26. package/dist/lib/ast/features/prefix.d.ts +8 -0
  27. package/dist/lib/ast/features/prefix.js +68 -43
  28. package/dist/lib/ast/features/shorthand.d.ts +12 -0
  29. package/dist/lib/ast/features/shorthand.js +6 -9
  30. package/dist/lib/ast/features/transform.d.ts +10 -0
  31. package/dist/lib/ast/features/transform.js +9 -13
  32. package/dist/lib/ast/features/type.d.ts +15 -0
  33. package/dist/lib/ast/find.d.ts +165 -0
  34. package/dist/lib/ast/find.js +175 -0
  35. package/dist/lib/ast/math/expression.d.ts +18 -0
  36. package/dist/lib/ast/math/expression.js +140 -98
  37. package/dist/lib/ast/math/math.d.ts +6 -0
  38. package/dist/lib/ast/math/math.js +30 -41
  39. package/dist/lib/ast/minify.d.ts +19 -0
  40. package/dist/lib/ast/minify.js +543 -215
  41. package/dist/lib/ast/transform/compute.d.ts +8 -0
  42. package/dist/lib/ast/transform/compute.js +82 -69
  43. package/dist/lib/ast/transform/matrix.d.ts +22 -0
  44. package/dist/lib/ast/transform/matrix.js +12 -26
  45. package/dist/lib/ast/transform/minify.d.ts +5 -0
  46. package/dist/lib/ast/transform/minify.js +20 -20
  47. package/dist/lib/ast/transform/perspective.d.ts +3 -0
  48. package/dist/lib/ast/transform/perspective.js +1 -1
  49. package/dist/lib/ast/transform/rotate.d.ts +12 -0
  50. package/dist/lib/ast/transform/rotate.js +1 -1
  51. package/dist/lib/ast/transform/scale.d.ts +6 -0
  52. package/dist/lib/ast/transform/scale.js +1 -1
  53. package/dist/lib/ast/transform/skew.d.ts +4 -0
  54. package/dist/lib/ast/transform/skew.js +1 -1
  55. package/dist/lib/ast/transform/translate.d.ts +6 -0
  56. package/dist/lib/ast/transform/translate.js +1 -1
  57. package/dist/lib/ast/transform/utils.d.ts +9 -0
  58. package/dist/lib/ast/types.d.ts +903 -0
  59. package/dist/lib/ast/types.js +277 -23
  60. package/dist/lib/ast/walk.d.ts +162 -0
  61. package/dist/lib/ast/walk.js +116 -60
  62. package/dist/lib/fs/resolve.d.ts +20 -0
  63. package/dist/lib/fs/resolve.js +37 -45
  64. package/dist/lib/parser/declaration/list.d.ts +16 -0
  65. package/dist/lib/parser/declaration/list.js +26 -24
  66. package/dist/lib/parser/declaration/map.d.ts +15 -0
  67. package/dist/lib/parser/declaration/map.js +140 -95
  68. package/dist/lib/parser/declaration/set.d.ts +9 -0
  69. package/dist/lib/parser/declaration/set.js +30 -25
  70. package/dist/lib/parser/node.d.ts +7 -0
  71. package/dist/lib/parser/parse.d.ts +107 -0
  72. package/dist/lib/parser/parse.js +1454 -1445
  73. package/dist/lib/parser/tokenize.d.ts +57 -0
  74. package/dist/lib/parser/tokenize.js +557 -404
  75. package/dist/lib/parser/utils/at-rule-container.d.ts +5 -0
  76. package/dist/lib/parser/utils/at-rule-container.js +486 -0
  77. package/dist/lib/parser/utils/at-rule-font-feature-values.d.ts +5 -0
  78. package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
  79. package/dist/lib/parser/utils/at-rule-generic.d.ts +5 -0
  80. package/dist/lib/parser/utils/at-rule-generic.js +118 -0
  81. package/dist/lib/parser/utils/at-rule-import.d.ts +5 -0
  82. package/dist/lib/parser/utils/at-rule-import.js +393 -0
  83. package/dist/lib/parser/utils/at-rule-media.d.ts +5 -0
  84. package/dist/lib/parser/utils/at-rule-media.js +603 -0
  85. package/dist/lib/parser/utils/at-rule-page.d.ts +5 -0
  86. package/dist/lib/parser/utils/at-rule-page.js +28 -0
  87. package/dist/lib/parser/utils/at-rule-support.d.ts +5 -0
  88. package/dist/lib/parser/utils/at-rule-support.js +366 -0
  89. package/dist/lib/parser/utils/at-rule-token.d.ts +1 -0
  90. package/dist/lib/parser/utils/at-rule-when-else.d.ts +5 -0
  91. package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
  92. package/dist/lib/parser/utils/at-rule.d.ts +13 -0
  93. package/dist/lib/parser/utils/at-rule.js +37 -0
  94. package/dist/lib/parser/utils/cache.d.ts +6 -0
  95. package/dist/lib/parser/utils/cache.js +19 -0
  96. package/dist/lib/parser/utils/config.d.ts +2 -0
  97. package/dist/lib/parser/utils/config.js +1 -0
  98. package/dist/lib/parser/utils/declaration-list.d.ts +5 -0
  99. package/dist/lib/parser/utils/declaration.d.ts +18 -0
  100. package/dist/lib/parser/utils/declaration.js +569 -91
  101. package/dist/lib/parser/utils/eq.d.ts +1 -0
  102. package/dist/lib/parser/utils/hash.d.ts +21 -0
  103. package/dist/lib/parser/utils/hash.js +1 -1
  104. package/dist/lib/parser/utils/selector.d.ts +5 -0
  105. package/dist/lib/parser/utils/selector.js +476 -0
  106. package/dist/lib/parser/utils/text.d.ts +3 -0
  107. package/dist/lib/parser/utils/text.js +17 -1
  108. package/dist/lib/parser/utils/token.d.ts +14 -0
  109. package/dist/lib/parser/utils/token.js +102 -0
  110. package/dist/lib/parser/utils/type.d.ts +2 -0
  111. package/dist/lib/parser/utils/type.js +29 -18
  112. package/dist/lib/renderer/render.d.ts +28 -0
  113. package/dist/lib/renderer/render.js +421 -262
  114. package/dist/lib/renderer/sourcemap/lib/encode.d.ts +1 -0
  115. package/dist/lib/renderer/sourcemap/sourcemap.d.ts +26 -0
  116. package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
  117. package/dist/lib/syntax/color/a98rgb.d.ts +2 -0
  118. package/dist/lib/syntax/color/a98rgb.js +8 -12
  119. package/dist/lib/syntax/color/cmyk.d.ts +10 -0
  120. package/dist/lib/syntax/color/cmyk.js +23 -21
  121. package/dist/lib/syntax/color/color-mix.d.ts +2 -0
  122. package/dist/lib/syntax/color/color-mix.js +88 -77
  123. package/dist/lib/syntax/color/color.d.ts +42 -0
  124. package/dist/lib/syntax/color/color.js +65 -68
  125. package/dist/lib/syntax/color/hex.d.ts +16 -0
  126. package/dist/lib/syntax/color/hex.js +27 -31
  127. package/dist/lib/syntax/color/hsl.d.ts +20 -0
  128. package/dist/lib/syntax/color/hsl.js +5 -12
  129. package/dist/lib/syntax/color/hsv.d.ts +2 -0
  130. package/dist/lib/syntax/color/hwb.d.ts +21 -0
  131. package/dist/lib/syntax/color/hwb.js +8 -21
  132. package/dist/lib/syntax/color/lab.d.ts +25 -0
  133. package/dist/lib/syntax/color/lab.js +20 -21
  134. package/dist/lib/syntax/color/lch.d.ts +23 -0
  135. package/dist/lib/syntax/color/lch.js +13 -15
  136. package/dist/lib/syntax/color/oklab.d.ts +22 -0
  137. package/dist/lib/syntax/color/oklab.js +20 -39
  138. package/dist/lib/syntax/color/oklch.d.ts +20 -0
  139. package/dist/lib/syntax/color/oklch.js +14 -16
  140. package/dist/lib/syntax/color/p3.d.ts +6 -0
  141. package/dist/lib/syntax/color/p3.js +0 -8
  142. package/dist/lib/syntax/color/prophotorgb.d.ts +2 -0
  143. package/dist/lib/syntax/color/rec2020.d.ts +2 -0
  144. package/dist/lib/syntax/color/rec2020.js +9 -13
  145. package/dist/lib/syntax/color/relativecolor.d.ts +13 -0
  146. package/dist/lib/syntax/color/relativecolor.js +68 -41
  147. package/dist/lib/syntax/color/rgb.d.ts +20 -0
  148. package/dist/lib/syntax/color/rgb.js +14 -18
  149. package/dist/lib/syntax/color/srgb.d.ts +23 -0
  150. package/dist/lib/syntax/color/srgb.js +27 -26
  151. package/dist/lib/syntax/color/utils/components.d.ts +2 -0
  152. package/dist/lib/syntax/color/utils/components.js +30 -14
  153. package/dist/lib/syntax/color/utils/distance.d.ts +18 -0
  154. package/dist/lib/syntax/color/utils/distance.js +1 -8
  155. package/dist/lib/syntax/color/utils/matrix.d.ts +6 -0
  156. package/dist/lib/syntax/color/xyz.d.ts +5 -0
  157. package/dist/lib/syntax/color/xyz.js +8 -20
  158. package/dist/lib/syntax/color/xyzd50.d.ts +4 -0
  159. package/dist/lib/syntax/color/xyzd50.js +6 -20
  160. package/dist/lib/syntax/constants.d.ts +67 -0
  161. package/dist/lib/syntax/constants.js +436 -0
  162. package/dist/lib/syntax/syntax.d.ts +38 -0
  163. package/dist/lib/syntax/syntax.js +533 -568
  164. package/dist/lib/validation/config.d.ts +14 -0
  165. package/dist/lib/validation/config.js +72 -33
  166. package/dist/lib/validation/config.json.js +1159 -74
  167. package/dist/lib/validation/json.d.ts +2 -0
  168. package/dist/lib/validation/match.d.ts +38 -0
  169. package/dist/lib/validation/match.js +2985 -0
  170. package/dist/lib/validation/parser/parse.d.ts +8 -0
  171. package/dist/lib/validation/parser/parse.js +684 -935
  172. package/dist/lib/validation/parser/typedef.d.ts +95 -0
  173. package/dist/lib/validation/parser/typedef.js +100 -0
  174. package/dist/lib/validation/utils/list.d.ts +4 -0
  175. package/dist/lib/validation/utils/list.js +4 -11
  176. package/dist/lib/validation/utils/whitespace.d.ts +2 -0
  177. package/dist/lib/validation/utils/whitespace.js +2 -8
  178. package/dist/node.d.ts +207 -0
  179. package/dist/node.js +53 -47
  180. package/dist/web.d.ts +169 -0
  181. package/dist/web.js +50 -41
  182. package/package.json +18 -13
  183. package/playground/index.html +1328 -0
  184. package/playground/sw.js +55 -0
  185. package/playground/tree.js +176 -0
  186. package/dist/lib/syntax/color/utils/constants.js +0 -214
  187. package/dist/lib/syntax/utils.js +0 -70
  188. package/dist/lib/validation/at-rules/container.js +0 -342
  189. package/dist/lib/validation/at-rules/counter-style.js +0 -90
  190. package/dist/lib/validation/at-rules/custom-media.js +0 -50
  191. package/dist/lib/validation/at-rules/document.js +0 -89
  192. package/dist/lib/validation/at-rules/else.js +0 -5
  193. package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
  194. package/dist/lib/validation/at-rules/import.js +0 -150
  195. package/dist/lib/validation/at-rules/keyframes.js +0 -67
  196. package/dist/lib/validation/at-rules/layer.js +0 -41
  197. package/dist/lib/validation/at-rules/media.js +0 -255
  198. package/dist/lib/validation/at-rules/namespace.js +0 -81
  199. package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
  200. package/dist/lib/validation/at-rules/page.js +0 -100
  201. package/dist/lib/validation/at-rules/supports.js +0 -295
  202. package/dist/lib/validation/at-rules/when.js +0 -185
  203. package/dist/lib/validation/atrule.js +0 -184
  204. package/dist/lib/validation/selector.js +0 -36
  205. package/dist/lib/validation/syntax.js +0 -1073
  206. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
  207. package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
  208. package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
  209. package/dist/lib/validation/syntaxes/family-name.js +0 -57
  210. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
  211. package/dist/lib/validation/syntaxes/layer-name.js +0 -57
  212. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
  213. package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
  214. package/dist/lib/validation/syntaxes/selector-list.js +0 -5
  215. package/dist/lib/validation/syntaxes/selector.js +0 -5
  216. package/dist/lib/validation/syntaxes/url.js +0 -40
@@ -0,0 +1,215 @@
1
+ import { EnumToken } from '../types.js';
2
+ import { renderToken } from '../../renderer/render.js';
3
+ import { FeatureWalkMode } from './type.js';
4
+ import { definedPropertySettings } from '../../syntax/constants.js';
5
+ import { equalsIgnoreCase } from '../../parser/utils/text.js';
6
+ import { replaceToken } from '../../parser/utils/token.js';
7
+ import { cloneNode } from '../clone.js';
8
+ import { trimArray } from '../../validation/match.js';
9
+ import { findByValue } from '../find.js';
10
+ import { walk } from '../walk.js';
11
+ import { eq } from '../../parser/utils/eq.js';
12
+
13
+ const nodeMatcher = (value) => value.typ === EnumToken.IfConditionTokenType ||
14
+ (value.typ === EnumToken.WildCardFunctionTokenType && equalsIgnoreCase("if", value.val));
15
+ function substituteIfElseNode(declaration, node, wrapper, parentWrapper, cache) {
16
+ const result = [];
17
+ let nodeMap = new Map();
18
+ let clonedDeclaration;
19
+ let targetParentWrapper = parentWrapper;
20
+ let targetWrapper = wrapper;
21
+ if (node.typ === EnumToken.IfElseConditionTokenType) {
22
+ //
23
+ clonedDeclaration = cloneNode(declaration, true, nodeMap);
24
+ let replaceRight = true;
25
+ // replace else: ... with the actual value
26
+ if (node.r.typ === EnumToken.IfConditionTokenType) {
27
+ const target = node.r.l.find((t) => t.typ != EnumToken.CommentTokenType && t.typ != EnumToken.WhitespaceTokenType);
28
+ if (target == null) {
29
+ return result;
30
+ }
31
+ //
32
+ if (target.typ === EnumToken.IdenTokenType && equalsIgnoreCase("else", target.val)) {
33
+ replaceToken(nodeMap.get(targetParentWrapper), nodeMap.get(targetWrapper), node.r.r.at(-1)?.typ ===
34
+ EnumToken.SemiColonTokenType
35
+ ? trimArray(node.r.r.slice(0, -1))
36
+ : node.r.r);
37
+ if (targetParentWrapper.typ != EnumToken.DeclarationNodeType) {
38
+ let index = targetParentWrapper.chi.indexOf(targetWrapper);
39
+ if (index != -1) {
40
+ let i;
41
+ let k;
42
+ let siblingWrapper;
43
+ let left = node.l.l.find((t) => t.typ != EnumToken.CommentTokenType && t.typ != EnumToken.WhitespaceTokenType);
44
+ for (i = index + 1; i < targetParentWrapper.chi.length; i++) {
45
+ if (targetParentWrapper.chi[i].typ === targetWrapper.typ) {
46
+ siblingWrapper = targetParentWrapper.chi[i];
47
+ for (k = 0; k < siblingWrapper.chi.length; k++) {
48
+ if (siblingWrapper.chi[k].typ === EnumToken.IfElseConditionTokenType) {
49
+ let leftSide = siblingWrapper.chi[k].l.l.find((t) => t.typ != EnumToken.CommentTokenType &&
50
+ t.typ != EnumToken.WhitespaceTokenType);
51
+ if (eq(left, leftSide)) {
52
+ replaceToken(nodeMap.get(targetParentWrapper), nodeMap.get(targetParentWrapper.chi[i]), siblingWrapper.chi[k]
53
+ .r.r.at(-1)?.typ === EnumToken.SemiColonTokenType
54
+ ? trimArray(siblingWrapper.chi[k]
55
+ .r.r.slice(0, -1))
56
+ : siblingWrapper.chi[k]
57
+ .r.r);
58
+ cache.add(siblingWrapper.chi[k].l);
59
+ }
60
+ }
61
+ }
62
+ }
63
+ }
64
+ }
65
+ }
66
+ replaceRight = false;
67
+ }
68
+ }
69
+ if (replaceRight) {
70
+ replaceToken(nodeMap.get(targetParentWrapper), nodeMap.get(targetWrapper), node.r);
71
+ }
72
+ result.push(clonedDeclaration);
73
+ nodeMap.clear();
74
+ clonedDeclaration = cloneNode(declaration, true, nodeMap);
75
+ replaceToken(nodeMap.get(targetParentWrapper), nodeMap.get(targetWrapper), node.l);
76
+ result.push(clonedDeclaration);
77
+ }
78
+ else if (node.typ === EnumToken.IfConditionTokenType) {
79
+ const left = node.l.find((t) => t.typ != EnumToken.CommentTokenType && t.typ != EnumToken.WhitespaceTokenType);
80
+ if (left == null) {
81
+ return result;
82
+ }
83
+ if (left.typ === EnumToken.IdenTokenType && equalsIgnoreCase("else", left.val)) {
84
+ clonedDeclaration = cloneNode(declaration, true, nodeMap);
85
+ replaceToken(nodeMap.get(parentWrapper), nodeMap.get(targetWrapper.typ === EnumToken.DeclarationNodeType ? node : targetWrapper), node.r.at(-1)?.typ === EnumToken.SemiColonTokenType ? trimArray(node.r.slice(0, -1)) : node.r);
86
+ result.push(clonedDeclaration);
87
+ }
88
+ else if (left?.typ === EnumToken.WhenElseFunctionTokenType) {
89
+ const atRule = Object.assign(cloneNode(declaration), {
90
+ typ: EnumToken.AtRuleNodeType,
91
+ nam: left.val,
92
+ chi: [],
93
+ });
94
+ Object.defineProperty(atRule, "tokens", {
95
+ ...definedPropertySettings,
96
+ value: [{ typ: EnumToken.ParensTokenType, chi: left.chi.slice() }],
97
+ });
98
+ atRule.val = atRule.tokens.reduce((acc, curr) => acc + renderToken(curr), "");
99
+ clonedDeclaration = cloneNode(declaration, true, nodeMap);
100
+ replaceToken(nodeMap.get(targetWrapper), nodeMap.get(node), node.r.at(-1)?.typ === EnumToken.SemiColonTokenType ? trimArray(node.r.slice(0, -1)) : node.r);
101
+ clonedDeclaration.parent = atRule;
102
+ atRule.chi.push(clonedDeclaration);
103
+ result.push(atRule);
104
+ processNode(clonedDeclaration, cache);
105
+ // nodeMap.clear();
106
+ }
107
+ else if (left.typ === EnumToken.ContainerFunctionTokenType) {
108
+ const atRule = Object.assign(cloneNode(declaration), {
109
+ typ: EnumToken.AtRuleNodeType,
110
+ nam: "container",
111
+ chi: [],
112
+ });
113
+ Object.defineProperty(atRule, "tokens", { ...definedPropertySettings, value: [left] });
114
+ atRule.val = atRule.tokens.reduce((acc, curr) => acc + renderToken(curr), "");
115
+ clonedDeclaration = cloneNode(declaration, true, nodeMap);
116
+ replaceToken(nodeMap.get(targetWrapper.typ === EnumToken.WildCardFunctionTokenType ? targetParentWrapper : targetWrapper), nodeMap.get(targetWrapper.typ === EnumToken.WildCardFunctionTokenType ? targetWrapper : node), node.r.at(-1)?.typ === EnumToken.SemiColonTokenType
117
+ ? trimArray(node.r.slice(0, -1))
118
+ : node.r);
119
+ clonedDeclaration.parent = atRule;
120
+ atRule.chi.push(clonedDeclaration);
121
+ result.push(atRule);
122
+ processNode(clonedDeclaration, cache);
123
+ }
124
+ }
125
+ else if (wrapper.typ === EnumToken.WildCardFunctionTokenType) {
126
+ clonedDeclaration = cloneNode(declaration, true, nodeMap);
127
+ replaceToken(nodeMap.get(parentWrapper), nodeMap.get(wrapper), node);
128
+ result.push(clonedDeclaration);
129
+ }
130
+ return result;
131
+ }
132
+ function processNode(declarationNode, cache) {
133
+ let i;
134
+ let k = -1;
135
+ let astNode;
136
+ const result = [];
137
+ const stack = [declarationNode];
138
+ while (++k < stack.length) {
139
+ astNode = stack[k];
140
+ const { node: declaration, value: node } = findByValue(astNode, nodeMatcher) ?? {};
141
+ if (node != null && cache.has(node.node)) {
142
+ continue;
143
+ }
144
+ if (declaration == null || node == null) {
145
+ while (astNode.parent != null && astNode.parent != declarationNode.parent) {
146
+ astNode = astNode.parent;
147
+ }
148
+ result.push(astNode);
149
+ continue;
150
+ }
151
+ // @ts-expect-error
152
+ const parents = [...node.parents?.()];
153
+ const parentWrapper = node.parent ?? parents.find((node) => !nodeMatcher(node));
154
+ if (node.node.typ === EnumToken.WildCardFunctionTokenType) {
155
+ for (i = 0; i < node.node.chi.length; i++) {
156
+ if (cache.has(node.node.chi[i])) {
157
+ continue;
158
+ }
159
+ stack.push(...substituteIfElseNode(declaration, node.node.chi[i], node.node, parentWrapper, cache));
160
+ }
161
+ }
162
+ else {
163
+ stack.push(...substituteIfElseNode(declaration, node.node, parentWrapper, parents[parents.indexOf(parentWrapper) + 1] ?? declaration, cache));
164
+ }
165
+ }
166
+ if (result.length > 0) {
167
+ replaceToken(declarationNode.parent, declarationNode, result);
168
+ }
169
+ // else remove node?
170
+ return result;
171
+ }
172
+ class ExpandIfFeature {
173
+ accept = new Set([EnumToken.DeclarationNodeType]);
174
+ get ordering() {
175
+ return 5;
176
+ }
177
+ get processMode() {
178
+ return FeatureWalkMode.Pre;
179
+ }
180
+ static register(options) {
181
+ if (options.expandIfSyntax) {
182
+ // @ts-ignore
183
+ options.features.push(new ExpandIfFeature());
184
+ }
185
+ }
186
+ run(declaration) {
187
+ const cache = new Set();
188
+ const result = processNode(declaration, cache);
189
+ let i;
190
+ for (const n of result) {
191
+ for (const { node } of walk(n)) {
192
+ if (node.typ === EnumToken.AtRuleNodeType && Array.isArray(node.chi)) {
193
+ for (i = 0; i < node.chi.length; i++) {
194
+ if (node.chi[i].typ === EnumToken.AtRuleNodeType &&
195
+ node.chi[i + 1]?.typ === EnumToken.AtRuleNodeType &&
196
+ node.chi[i].nam === node.chi[i + 1].nam &&
197
+ node.chi[i].val === node.chi[i + 1].val) {
198
+ node.chi[i].chi.push(...node.chi[i + 1].chi);
199
+ node.chi.splice(i + 1, 1);
200
+ }
201
+ }
202
+ for (i = 0; i < node.chi.length; i++) {
203
+ if (node.chi[i].typ === EnumToken.AtRuleNodeType &&
204
+ node.chi[i].nam === node.nam &&
205
+ node.chi[i].val === node.val) {
206
+ node.chi.splice(i, 1, ...node.chi[i].chi);
207
+ }
208
+ }
209
+ }
210
+ }
211
+ }
212
+ }
213
+ }
214
+
215
+ export { ExpandIfFeature };
@@ -0,0 +1,6 @@
1
+ export * from './prefix.ts';
2
+ export * from './inlinecssvariables.ts';
3
+ export * from './shorthand.ts';
4
+ export * from './calc.ts';
5
+ export * from './transform.ts';
6
+ export * from './if.ts';
@@ -3,3 +3,4 @@ export { InlineCssVariablesFeature } from './inlinecssvariables.js';
3
3
  export { ComputeShorthandFeature } from './shorthand.js';
4
4
  export { ComputeCalcExpressionFeature } from './calc.js';
5
5
  export { TransformCssFeature } from './transform.js';
6
+ export { ExpandIfFeature } from './if.js';
@@ -0,0 +1,15 @@
1
+ import type { AstAtRule, AstNode, AstRule, AstStyleSheet, ParserOptions } from "../../../@types/index.d.ts";
2
+ import { EnumToken } from "../types.ts";
3
+ import { FeatureWalkMode } from "./type.ts";
4
+ export declare class InlineCssVariablesFeature {
5
+ accept: Set<EnumToken>;
6
+ get ordering(): number;
7
+ get processMode(): FeatureWalkMode;
8
+ static register(options: ParserOptions): void;
9
+ run(ast: AstRule | AstAtRule, options: ParserOptions | undefined, parent: AstRule | AstAtRule | AstStyleSheet, context: {
10
+ [key: string]: any;
11
+ }): AstNode | null;
12
+ cleanup(ast: AstStyleSheet, options: ParserOptions | undefined, context: {
13
+ [key: string]: any;
14
+ }): void;
15
+ }
@@ -1,20 +1,20 @@
1
1
  import { EnumToken } from '../types.js';
2
2
  import { walkValues } from '../walk.js';
3
3
  import { renderToken } from '../../renderer/render.js';
4
- import '../../renderer/sourcemap/lib/encode.js';
5
- import { mathFuncs } from '../../syntax/syntax.js';
6
4
  import { splitRule } from '../minify.js';
7
- import '../../parser/parse.js';
8
- import '../../parser/tokenize.js';
9
- import '../../parser/utils/config.js';
10
5
  import { FeatureWalkMode } from './type.js';
6
+ import { mathFuncs } from '../../syntax/constants.js';
11
7
 
12
8
  function inlineExpression(token) {
13
9
  const result = [];
14
10
  if (token.typ == EnumToken.BinaryExpressionTokenType) {
15
11
  result.push({
16
12
  typ: EnumToken.ParensTokenType,
17
- chi: [...inlineExpression(token.l), { typ: token.op }, ...inlineExpression(token.r)]
13
+ chi: [
14
+ ...inlineExpression(token.l),
15
+ { typ: token.op },
16
+ ...inlineExpression(token.r),
17
+ ],
18
18
  });
19
19
  }
20
20
  else {
@@ -24,15 +24,17 @@ function inlineExpression(token) {
24
24
  }
25
25
  function replace(node, variableScope) {
26
26
  for (const { value, parent: parentValue } of walkValues(node.val)) {
27
- if (value.typ == EnumToken.BinaryExpressionTokenType && parentValue != null && 'chi' in parentValue) {
27
+ if (value.typ == EnumToken.BinaryExpressionTokenType && parentValue != null && "chi" in parentValue) {
28
28
  // @ts-ignore
29
29
  parentValue.chi.splice(parentValue.chi.indexOf(value), 1, ...inlineExpression(value));
30
30
  }
31
31
  }
32
32
  for (const { value, parent: parentValue } of walkValues(node.val)) {
33
- if (value.typ == EnumToken.FunctionTokenType && value.val == 'var') {
34
- if (value.chi.length == 1 && value.chi[0].typ == EnumToken.DashedIdenTokenType) {
35
- const info = variableScope.get(value.chi[0].val);
33
+ if (value.typ == EnumToken.MathFunctionTokenDefType ||
34
+ (value.typ == EnumToken.WildCardFunctionTokenType && value.val == "var")) {
35
+ if (value.chi.length == 1 &&
36
+ value.chi[0].typ == EnumToken.DashedIdenTokenType) {
37
+ const info = (variableScope.get(value.chi[0].val));
36
38
  if (info?.replaceable) {
37
39
  if (parentValue != null) {
38
40
  let i = 0;
@@ -66,24 +68,25 @@ class InlineCssVariablesFeature {
66
68
  }
67
69
  }
68
70
  run(ast, options = {}, parent, context) {
69
- // if (!('chi' in ast)) {
70
- //
71
- // return null;
72
- // }
73
- if (!('variableScope' in context)) {
74
- context.variableScope = new Map;
71
+ if (!("chi" in ast)) {
72
+ return null;
73
+ }
74
+ if (!("variableScope" in context)) {
75
+ context.variableScope = new Map();
75
76
  }
76
77
  // [':root', 'html']
77
- const isRoot = parent.typ == EnumToken.StyleSheetNodeType && ast.typ == EnumToken.RuleNodeType && (ast.raw ?? splitRule(ast.sel)).some(segment => segment.some(s => s == ':root' || s == 'html'));
78
+ const isRoot = parent.typ == EnumToken.StyleSheetNodeType &&
79
+ ast.typ == EnumToken.RuleNodeType &&
80
+ (ast.raw ?? splitRule(ast.sel)).some((segment) => segment.some((s) => s == ":root" || s == "html"));
78
81
  const variableScope = context.variableScope;
79
82
  // @ts-ignore
80
83
  for (const node of ast.chi) {
81
- if (node.typ != EnumToken.DeclarationNodeType) {
84
+ if (node.typ !== EnumToken.DeclarationNodeType) {
82
85
  continue;
83
86
  }
84
87
  // css variable
85
- if (node.nam.startsWith('--')) {
86
- if (!variableScope.has(node.nam)) {
88
+ if (node.nam.startsWith("--")) {
89
+ if (!variableScope.has(node.val)) {
87
90
  const info = {
88
91
  globalScope: isRoot,
89
92
  // @ts-ignore
@@ -91,13 +94,16 @@ class InlineCssVariablesFeature {
91
94
  declarationCount: 1,
92
95
  replaceable: isRoot,
93
96
  node: node,
94
- values: structuredClone(node.val)
97
+ values: structuredClone(node.val),
95
98
  };
96
99
  info.parent.add(ast);
97
100
  variableScope.set(node.nam, info);
98
101
  let recursive = false;
99
102
  for (const { value } of walkValues(node.val)) {
100
- if (value?.typ == EnumToken.FunctionTokenType && (mathFuncs.includes(value.val) || value.val == 'var')) {
103
+ if (value?.typ == EnumToken.MathFunctionTokenDefType ||
104
+ (value?.typ == EnumToken.WildCardFunctionTokenType &&
105
+ (mathFuncs.includes(value.val) ||
106
+ value.val === "var"))) {
101
107
  recursive = true;
102
108
  break;
103
109
  }
@@ -115,10 +121,9 @@ class InlineCssVariablesFeature {
115
121
  }
116
122
  cleanup(ast, options = {}, context) {
117
123
  const variableScope = context.variableScope;
118
- // if (variableScope == null) {
119
- //
120
- // return;
121
- // }
124
+ if (variableScope == null) {
125
+ return;
126
+ }
122
127
  for (const info of variableScope.values()) {
123
128
  if (info.replaceable) {
124
129
  let i;
@@ -130,7 +135,7 @@ class InlineCssVariablesFeature {
130
135
  // @ts-ignore
131
136
  parent.chi.splice(i, 1, {
132
137
  typ: EnumToken.CommentTokenType,
133
- val: `/* ${info.node.nam}: ${info.values.reduce((acc, curr) => acc + renderToken(curr, { convertColor: false }), '')} */`
138
+ val: `/* ${info.node.nam}: ${info.values.reduce((acc, curr) => acc + renderToken(curr, { convertColor: false }), "")} */`,
134
139
  });
135
140
  break;
136
141
  }
@@ -0,0 +1,8 @@
1
+ import type { AstNode, ParserOptions } from "../../../@types/index.d.ts";
2
+ import { FeatureWalkMode } from "./type.ts";
3
+ export declare class ComputePrefixFeature {
4
+ get ordering(): number;
5
+ get processMode(): FeatureWalkMode;
6
+ static register(options: ParserOptions): void;
7
+ run(node: AstNode): AstNode | null;
8
+ }
@@ -1,48 +1,65 @@
1
- import { EnumToken, SyntaxValidationResult } from '../types.js';
2
- import { getSyntaxConfig } from '../../validation/config.js';
3
- import '../../validation/parser/parse.js';
4
- import { splitRule } from '../minify.js';
1
+ import { EnumToken } from '../types.js';
5
2
  import { walkValues } from '../walk.js';
6
- import '../../parser/parse.js';
7
- import '../../parser/tokenize.js';
8
- import '../../parser/utils/config.js';
9
3
  import { pseudoAliasMap } from '../../syntax/syntax.js';
4
+ import { splitRule } from '../minify.js';
10
5
  import { renderToken } from '../../renderer/render.js';
11
- import '../../renderer/sourcemap/lib/encode.js';
12
- import '../../validation/syntaxes/complex-selector.js';
13
- import { evaluateSyntax } from '../../validation/syntax.js';
14
- import { funcLike } from '../../syntax/color/utils/constants.js';
6
+ import { funcLike } from '../../syntax/constants.js';
15
7
  import { FeatureWalkMode } from './type.js';
8
+ import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
9
+ import { getSyntaxConfig } from '../../validation/config.js';
10
+ import { splitTokenList } from '../../validation/utils/list.js';
16
11
 
17
12
  const config = getSyntaxConfig();
18
13
  function replacePseudo(tokens) {
19
- return tokens.map((raw) => raw.map(r => {
20
- if (r.includes('(')) {
21
- const index = r.indexOf('(');
22
- const name = r.slice(0, index) + '()';
14
+ return tokens.map((raw) => raw.map((r) => {
15
+ if (r.includes("(")) {
16
+ const index = r.indexOf("(");
17
+ const name = r.slice(0, index) + "()";
23
18
  if (name in pseudoAliasMap) {
24
19
  return pseudoAliasMap[name] + r.slice(index);
25
20
  }
26
21
  return r;
27
22
  }
28
- return r in pseudoAliasMap && pseudoAliasMap[r] in config["selectors" /* ValidationSyntaxGroupEnum.Selectors */] ? pseudoAliasMap[r] : r;
23
+ return r in pseudoAliasMap && pseudoAliasMap[r] in config[ValidationSyntaxGroupEnum.Selectors]
24
+ ? pseudoAliasMap[r]
25
+ : r;
29
26
  }));
30
27
  }
31
28
  function replaceAstNodes(tokens, root) {
32
29
  let result = false;
33
30
  for (const { value, parent } of walkValues(tokens, root)) {
34
- if (value.typ == EnumToken.IdenTokenType || value.typ == EnumToken.PseudoClassFuncTokenType || value.typ == EnumToken.PseudoClassTokenType || value.typ == EnumToken.PseudoElementTokenType) {
35
- let key = value.val + (value.typ == EnumToken.PseudoClassFuncTokenType ? '()' : '');
31
+ if (value.typ == EnumToken.MediaQueryConditionTokenType) {
32
+ const token = value.l.find((t) => t.typ == EnumToken.IdenTokenType);
33
+ if (token != null) {
34
+ if (token.val in pseudoAliasMap) {
35
+ token.val = pseudoAliasMap[token.val];
36
+ if (["min-resolution", "max-resolution"].includes(token.val) &&
37
+ value.r?.[0]?.typ == EnumToken.NumberTokenType) {
38
+ Object.assign(value.r?.[0], {
39
+ typ: EnumToken.ResolutionTokenType,
40
+ unit: "x",
41
+ });
42
+ result = true;
43
+ }
44
+ }
45
+ }
46
+ }
47
+ else if (value.typ == EnumToken.IdenTokenType ||
48
+ value.typ == EnumToken.PseudoClassFuncTokenType ||
49
+ value.typ == EnumToken.PseudoClassTokenType ||
50
+ value.typ == EnumToken.PseudoElementTokenType) {
51
+ let key = value.val +
52
+ (value.typ == EnumToken.PseudoClassFuncTokenType ? "()" : "");
36
53
  if (key in pseudoAliasMap) {
37
- const isPseudClass = pseudoAliasMap[key].startsWith('::');
54
+ const isPseudClass = pseudoAliasMap[key].startsWith("::");
38
55
  value.val = pseudoAliasMap[key];
39
56
  if (value.typ == EnumToken.IdenTokenType &&
40
- ['min-resolution', 'max-resolution'].includes(value.val) &&
57
+ ["min-resolution", "max-resolution"].includes(value.val) &&
41
58
  parent?.typ == EnumToken.MediaQueryConditionTokenType &&
42
59
  parent.r?.[0]?.typ == EnumToken.NumberTokenType) {
43
60
  Object.assign(parent.r?.[0], {
44
61
  typ: EnumToken.ResolutionTokenType,
45
- unit: 'x',
62
+ unit: "x",
46
63
  });
47
64
  }
48
65
  else if (isPseudClass && value.typ == EnumToken.PseudoElementTokenType) {
@@ -53,6 +70,24 @@ function replaceAstNodes(tokens, root) {
53
70
  }
54
71
  }
55
72
  }
73
+ if (tokens.find((t) => t.typ == EnumToken.CommaTokenType) != null) {
74
+ const set = new Set();
75
+ const split = splitTokenList(tokens, [EnumToken.CommaTokenType]);
76
+ tokens.length = 0;
77
+ tokens.push(...split.reduce((acc, curr) => {
78
+ const str = curr.reduce((acc, curr) => acc + renderToken(curr), "");
79
+ if (set.has(str)) {
80
+ return acc;
81
+ }
82
+ set.add(str);
83
+ if (acc.length > 0) {
84
+ tokens.push({
85
+ typ: EnumToken.CommaTokenType,
86
+ });
87
+ }
88
+ return acc.concat(curr);
89
+ }, []));
90
+ }
56
91
  return result;
57
92
  }
58
93
  class ComputePrefixFeature {
@@ -70,22 +105,13 @@ class ComputePrefixFeature {
70
105
  }
71
106
  run(node) {
72
107
  if (node.typ == EnumToken.RuleNodeType) {
73
- node.sel = replacePseudo(splitRule(node.sel)).reduce((acc, curr, index) => acc + (index > 0 ? ',' : '') + curr.join(''), '');
74
- // if ((node as AstRule).raw != null) {
75
- //
76
- // (node as AstRule).raw = replacePseudo((node as AstRule).raw as string[][]);
77
- // }
78
- //
79
- // if ((node as AstRule).optimized != null) {
80
- //
81
- // (node as AstRule).optimized!.selector = replacePseudo((node as AstRule).optimized!.selector as string[][]);
82
- // }
108
+ node.sel = replacePseudo(splitRule(node.sel)).reduce((acc, curr, index) => acc + (index > 0 ? "," : "") + curr.join(""), "");
83
109
  if (node.tokens != null) {
84
110
  replaceAstNodes(node.tokens);
85
111
  }
86
112
  }
87
113
  else if (node.typ == EnumToken.DeclarationNodeType) {
88
- if (node.nam.charAt(0) == '-') {
114
+ if (node.nam.charAt(0) == "-") {
89
115
  const match = node.nam.match(/^-([^-]+)-(.+)$/);
90
116
  if (match != null) {
91
117
  let nam = match[2];
@@ -101,8 +127,10 @@ class ComputePrefixFeature {
101
127
  }
102
128
  let hasPrefix = false;
103
129
  for (const { value } of walkValues(node.val)) {
104
- if ((value.typ == EnumToken.IdenTokenType || (value.typ != EnumToken.ParensTokenType && funcLike.includes(value.typ))) && value.val.match(/^-([^-]+)-(.+)$/) != null) {
105
- if (value.val.endsWith('-gradient')) {
130
+ if ((value.typ == EnumToken.IdenTokenType ||
131
+ (value.typ != EnumToken.ParensTokenType && funcLike.includes(value.typ))) &&
132
+ value.val.match(/^-([^-]+)-(.+)$/) != null) {
133
+ if (value.val.endsWith("-gradient")) {
106
134
  // not supported yet
107
135
  break;
108
136
  }
@@ -113,29 +141,26 @@ class ComputePrefixFeature {
113
141
  if (hasPrefix) {
114
142
  const nodes = structuredClone(node.val);
115
143
  for (const { value } of walkValues(nodes)) {
116
- if ((value.typ == EnumToken.IdenTokenType || funcLike.includes(value.typ))) {
144
+ if (value.typ == EnumToken.IdenTokenType || funcLike.includes(value.typ)) {
117
145
  const match = value.val.match(/^-([^-]+)-(.+)$/);
118
146
  if (match != null) {
119
147
  value.val = match[2];
120
148
  }
121
149
  }
122
150
  }
123
- // @ts-ignore
124
- if (SyntaxValidationResult.Valid == evaluateSyntax({ ...node, val: nodes }, {}).valid) {
125
- node.val = nodes;
126
- }
151
+ node.val = nodes;
127
152
  }
128
153
  }
129
154
  else if (node.typ == EnumToken.AtRuleNodeType || node.typ == EnumToken.KeyframesAtRuleNodeType) {
130
- if (node.nam.startsWith('-')) {
155
+ if (node.nam.startsWith("-")) {
131
156
  const match = node.nam.match(/^-([^-]+)-(.+)$/);
132
- if (match != null && '@' + match[2] in config.atRules) {
157
+ if (match != null && "@" + match[2] in config.atRules) {
133
158
  node.nam = match[2];
134
159
  }
135
160
  }
136
- if (node.typ == EnumToken.AtRuleNodeType && node.val !== '') {
161
+ if (node.typ == EnumToken.AtRuleNodeType && node.val !== "") {
137
162
  if (replaceAstNodes(node.tokens)) {
138
- node.val = node.tokens.reduce((acc, curr) => acc + renderToken(curr), '');
163
+ node.val = node.tokens.reduce((acc, curr) => acc + renderToken(curr), "");
139
164
  }
140
165
  }
141
166
  }
@@ -0,0 +1,12 @@
1
+ import { EnumToken } from "../types.ts";
2
+ import type { AstAtRule, AstNode, AstRule, AstStyleSheet, ParserOptions, PropertyListOptions } from "../../../@types/index.d.ts";
3
+ import { FeatureWalkMode } from "./type.ts";
4
+ export declare class ComputeShorthandFeature {
5
+ accept: Set<EnumToken>;
6
+ get ordering(): number;
7
+ get processMode(): FeatureWalkMode;
8
+ static register(options: ParserOptions): void;
9
+ run(ast: AstRule | AstAtRule, options: PropertyListOptions | undefined, parent: AstRule | AstAtRule | AstStyleSheet, context: {
10
+ [key: string]: any;
11
+ }): AstNode | null;
12
+ }
@@ -1,16 +1,13 @@
1
1
  import { PropertyList } from '../../parser/declaration/list.js';
2
2
  import { EnumToken } from '../types.js';
3
- import '../minify.js';
4
- import '../walk.js';
5
- import '../../parser/parse.js';
6
- import '../../parser/tokenize.js';
7
- import '../../parser/utils/config.js';
8
- import '../../syntax/color/utils/constants.js';
9
- import '../../renderer/sourcemap/lib/encode.js';
10
3
  import { FeatureWalkMode } from './type.js';
11
4
 
12
5
  class ComputeShorthandFeature {
13
- accept = new Set([EnumToken.RuleNodeType, EnumToken.AtRuleNodeType, EnumToken.KeyFramesRuleNodeType]);
6
+ accept = new Set([
7
+ EnumToken.RuleNodeType,
8
+ EnumToken.AtRuleNodeType,
9
+ EnumToken.KeyFramesRuleNodeType,
10
+ ]);
14
11
  get ordering() {
15
12
  return 3;
16
13
  }
@@ -24,7 +21,7 @@ class ComputeShorthandFeature {
24
21
  }
25
22
  }
26
23
  run(ast, options = {}, parent, context) {
27
- if (!('chi' in ast)) {
24
+ if (!("chi" in ast)) {
28
25
  return null;
29
26
  }
30
27
  // @ts-ignore
@@ -0,0 +1,10 @@
1
+ import type { AstAtRule, AstNode, AstRule, ParserOptions } from "../../../@types/index.d.ts";
2
+ import { EnumToken } from "../types.ts";
3
+ import { FeatureWalkMode } from "./type.ts";
4
+ export declare class TransformCssFeature {
5
+ accept: Set<EnumToken>;
6
+ get ordering(): number;
7
+ get processMode(): FeatureWalkMode;
8
+ static register(options: ParserOptions): void;
9
+ run(ast: AstRule | AstAtRule): AstNode | null;
10
+ }