@tenphi/tasty 0.0.0-snapshot.09c74e2

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 (243) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +539 -0
  3. package/dist/_virtual/_rolldown/runtime.js +8 -0
  4. package/dist/chunks/cacheKey.js +70 -0
  5. package/dist/chunks/cacheKey.js.map +1 -0
  6. package/dist/chunks/definitions.d.ts +37 -0
  7. package/dist/chunks/definitions.js +260 -0
  8. package/dist/chunks/definitions.js.map +1 -0
  9. package/dist/chunks/renderChunk.js +61 -0
  10. package/dist/chunks/renderChunk.js.map +1 -0
  11. package/dist/config.d.ts +280 -0
  12. package/dist/config.js +403 -0
  13. package/dist/config.js.map +1 -0
  14. package/dist/core/index.d.ts +33 -0
  15. package/dist/core/index.js +26 -0
  16. package/dist/debug.d.ts +204 -0
  17. package/dist/debug.js +733 -0
  18. package/dist/debug.js.map +1 -0
  19. package/dist/hooks/useGlobalStyles.d.ts +27 -0
  20. package/dist/hooks/useGlobalStyles.js +56 -0
  21. package/dist/hooks/useGlobalStyles.js.map +1 -0
  22. package/dist/hooks/useKeyframes.d.ts +56 -0
  23. package/dist/hooks/useKeyframes.js +54 -0
  24. package/dist/hooks/useKeyframes.js.map +1 -0
  25. package/dist/hooks/useProperty.d.ts +79 -0
  26. package/dist/hooks/useProperty.js +91 -0
  27. package/dist/hooks/useProperty.js.map +1 -0
  28. package/dist/hooks/useRawCSS.d.ts +53 -0
  29. package/dist/hooks/useRawCSS.js +28 -0
  30. package/dist/hooks/useRawCSS.js.map +1 -0
  31. package/dist/hooks/useStyles.d.ts +40 -0
  32. package/dist/hooks/useStyles.js +169 -0
  33. package/dist/hooks/useStyles.js.map +1 -0
  34. package/dist/index.d.ts +48 -0
  35. package/dist/index.js +33 -0
  36. package/dist/injector/index.d.ts +157 -0
  37. package/dist/injector/index.js +154 -0
  38. package/dist/injector/index.js.map +1 -0
  39. package/dist/injector/injector.d.ts +139 -0
  40. package/dist/injector/injector.js +404 -0
  41. package/dist/injector/injector.js.map +1 -0
  42. package/dist/injector/sheet-manager.d.ts +127 -0
  43. package/dist/injector/sheet-manager.js +714 -0
  44. package/dist/injector/sheet-manager.js.map +1 -0
  45. package/dist/injector/types.d.ts +135 -0
  46. package/dist/keyframes/index.js +206 -0
  47. package/dist/keyframes/index.js.map +1 -0
  48. package/dist/parser/classify.js +319 -0
  49. package/dist/parser/classify.js.map +1 -0
  50. package/dist/parser/const.js +33 -0
  51. package/dist/parser/const.js.map +1 -0
  52. package/dist/parser/lru.js +109 -0
  53. package/dist/parser/lru.js.map +1 -0
  54. package/dist/parser/parser.d.ts +25 -0
  55. package/dist/parser/parser.js +116 -0
  56. package/dist/parser/parser.js.map +1 -0
  57. package/dist/parser/tokenizer.js +69 -0
  58. package/dist/parser/tokenizer.js.map +1 -0
  59. package/dist/parser/types.d.ts +51 -0
  60. package/dist/parser/types.js +46 -0
  61. package/dist/parser/types.js.map +1 -0
  62. package/dist/pipeline/conditions.d.ts +134 -0
  63. package/dist/pipeline/conditions.js +406 -0
  64. package/dist/pipeline/conditions.js.map +1 -0
  65. package/dist/pipeline/exclusive.js +231 -0
  66. package/dist/pipeline/exclusive.js.map +1 -0
  67. package/dist/pipeline/index.d.ts +53 -0
  68. package/dist/pipeline/index.js +660 -0
  69. package/dist/pipeline/index.js.map +1 -0
  70. package/dist/pipeline/materialize.js +844 -0
  71. package/dist/pipeline/materialize.js.map +1 -0
  72. package/dist/pipeline/parseStateKey.d.ts +15 -0
  73. package/dist/pipeline/parseStateKey.js +438 -0
  74. package/dist/pipeline/parseStateKey.js.map +1 -0
  75. package/dist/pipeline/simplify.js +516 -0
  76. package/dist/pipeline/simplify.js.map +1 -0
  77. package/dist/pipeline/warnings.js +18 -0
  78. package/dist/pipeline/warnings.js.map +1 -0
  79. package/dist/plugins/okhsl-plugin.d.ts +35 -0
  80. package/dist/plugins/okhsl-plugin.js +371 -0
  81. package/dist/plugins/okhsl-plugin.js.map +1 -0
  82. package/dist/plugins/types.d.ts +69 -0
  83. package/dist/properties/index.js +158 -0
  84. package/dist/properties/index.js.map +1 -0
  85. package/dist/states/index.d.ts +49 -0
  86. package/dist/states/index.js +416 -0
  87. package/dist/states/index.js.map +1 -0
  88. package/dist/static/index.d.ts +5 -0
  89. package/dist/static/index.js +5 -0
  90. package/dist/static/tastyStatic.d.ts +46 -0
  91. package/dist/static/tastyStatic.js +31 -0
  92. package/dist/static/tastyStatic.js.map +1 -0
  93. package/dist/static/types.d.ts +49 -0
  94. package/dist/static/types.js +24 -0
  95. package/dist/static/types.js.map +1 -0
  96. package/dist/styles/align.d.ts +15 -0
  97. package/dist/styles/align.js +14 -0
  98. package/dist/styles/align.js.map +1 -0
  99. package/dist/styles/border.d.ts +25 -0
  100. package/dist/styles/border.js +114 -0
  101. package/dist/styles/border.js.map +1 -0
  102. package/dist/styles/color.d.ts +14 -0
  103. package/dist/styles/color.js +23 -0
  104. package/dist/styles/color.js.map +1 -0
  105. package/dist/styles/createStyle.js +77 -0
  106. package/dist/styles/createStyle.js.map +1 -0
  107. package/dist/styles/dimension.js +97 -0
  108. package/dist/styles/dimension.js.map +1 -0
  109. package/dist/styles/display.d.ts +37 -0
  110. package/dist/styles/display.js +67 -0
  111. package/dist/styles/display.js.map +1 -0
  112. package/dist/styles/fade.d.ts +15 -0
  113. package/dist/styles/fade.js +58 -0
  114. package/dist/styles/fade.js.map +1 -0
  115. package/dist/styles/fill.d.ts +42 -0
  116. package/dist/styles/fill.js +52 -0
  117. package/dist/styles/fill.js.map +1 -0
  118. package/dist/styles/flow.d.ts +16 -0
  119. package/dist/styles/flow.js +12 -0
  120. package/dist/styles/flow.js.map +1 -0
  121. package/dist/styles/gap.d.ts +31 -0
  122. package/dist/styles/gap.js +37 -0
  123. package/dist/styles/gap.js.map +1 -0
  124. package/dist/styles/height.d.ts +17 -0
  125. package/dist/styles/height.js +20 -0
  126. package/dist/styles/height.js.map +1 -0
  127. package/dist/styles/index.d.ts +2 -0
  128. package/dist/styles/index.js +9 -0
  129. package/dist/styles/index.js.map +1 -0
  130. package/dist/styles/inset.d.ts +52 -0
  131. package/dist/styles/inset.js +150 -0
  132. package/dist/styles/inset.js.map +1 -0
  133. package/dist/styles/justify.d.ts +15 -0
  134. package/dist/styles/justify.js +14 -0
  135. package/dist/styles/justify.js.map +1 -0
  136. package/dist/styles/list.d.ts +16 -0
  137. package/dist/styles/list.js +98 -0
  138. package/dist/styles/list.js.map +1 -0
  139. package/dist/styles/margin.d.ts +24 -0
  140. package/dist/styles/margin.js +104 -0
  141. package/dist/styles/margin.js.map +1 -0
  142. package/dist/styles/outline.d.ts +29 -0
  143. package/dist/styles/outline.js +65 -0
  144. package/dist/styles/outline.js.map +1 -0
  145. package/dist/styles/padding.d.ts +24 -0
  146. package/dist/styles/padding.js +104 -0
  147. package/dist/styles/padding.js.map +1 -0
  148. package/dist/styles/predefined.d.ts +73 -0
  149. package/dist/styles/predefined.js +241 -0
  150. package/dist/styles/predefined.js.map +1 -0
  151. package/dist/styles/preset.d.ts +47 -0
  152. package/dist/styles/preset.js +126 -0
  153. package/dist/styles/preset.js.map +1 -0
  154. package/dist/styles/radius.d.ts +14 -0
  155. package/dist/styles/radius.js +51 -0
  156. package/dist/styles/radius.js.map +1 -0
  157. package/dist/styles/scrollbar.d.ts +21 -0
  158. package/dist/styles/scrollbar.js +112 -0
  159. package/dist/styles/scrollbar.js.map +1 -0
  160. package/dist/styles/shadow.d.ts +14 -0
  161. package/dist/styles/shadow.js +24 -0
  162. package/dist/styles/shadow.js.map +1 -0
  163. package/dist/styles/styledScrollbar.d.ts +47 -0
  164. package/dist/styles/styledScrollbar.js +38 -0
  165. package/dist/styles/styledScrollbar.js.map +1 -0
  166. package/dist/styles/transition.d.ts +14 -0
  167. package/dist/styles/transition.js +158 -0
  168. package/dist/styles/transition.js.map +1 -0
  169. package/dist/styles/types.d.ts +498 -0
  170. package/dist/styles/width.d.ts +17 -0
  171. package/dist/styles/width.js +20 -0
  172. package/dist/styles/width.js.map +1 -0
  173. package/dist/tasty.d.ts +982 -0
  174. package/dist/tasty.js +206 -0
  175. package/dist/tasty.js.map +1 -0
  176. package/dist/tokens/typography.d.ts +19 -0
  177. package/dist/tokens/typography.js +237 -0
  178. package/dist/tokens/typography.js.map +1 -0
  179. package/dist/types.d.ts +184 -0
  180. package/dist/utils/cache-wrapper.js +26 -0
  181. package/dist/utils/cache-wrapper.js.map +1 -0
  182. package/dist/utils/case-converter.js +8 -0
  183. package/dist/utils/case-converter.js.map +1 -0
  184. package/dist/utils/colors.d.ts +5 -0
  185. package/dist/utils/colors.js +9 -0
  186. package/dist/utils/colors.js.map +1 -0
  187. package/dist/utils/css-types.d.ts +7 -0
  188. package/dist/utils/dotize.d.ts +26 -0
  189. package/dist/utils/dotize.js +122 -0
  190. package/dist/utils/dotize.js.map +1 -0
  191. package/dist/utils/filter-base-props.d.ts +15 -0
  192. package/dist/utils/filter-base-props.js +45 -0
  193. package/dist/utils/filter-base-props.js.map +1 -0
  194. package/dist/utils/get-display-name.d.ts +7 -0
  195. package/dist/utils/get-display-name.js +10 -0
  196. package/dist/utils/get-display-name.js.map +1 -0
  197. package/dist/utils/hsl-to-rgb.js +38 -0
  198. package/dist/utils/hsl-to-rgb.js.map +1 -0
  199. package/dist/utils/is-dev-env.js +19 -0
  200. package/dist/utils/is-dev-env.js.map +1 -0
  201. package/dist/utils/is-valid-element-type.js +15 -0
  202. package/dist/utils/is-valid-element-type.js.map +1 -0
  203. package/dist/utils/merge-styles.d.ts +7 -0
  204. package/dist/utils/merge-styles.js +146 -0
  205. package/dist/utils/merge-styles.js.map +1 -0
  206. package/dist/utils/mod-attrs.d.ts +8 -0
  207. package/dist/utils/mod-attrs.js +21 -0
  208. package/dist/utils/mod-attrs.js.map +1 -0
  209. package/dist/utils/okhsl-to-rgb.js +296 -0
  210. package/dist/utils/okhsl-to-rgb.js.map +1 -0
  211. package/dist/utils/process-tokens.d.ts +31 -0
  212. package/dist/utils/process-tokens.js +171 -0
  213. package/dist/utils/process-tokens.js.map +1 -0
  214. package/dist/utils/resolve-recipes.d.ts +17 -0
  215. package/dist/utils/resolve-recipes.js +147 -0
  216. package/dist/utils/resolve-recipes.js.map +1 -0
  217. package/dist/utils/string.js +8 -0
  218. package/dist/utils/string.js.map +1 -0
  219. package/dist/utils/styles.d.ts +178 -0
  220. package/dist/utils/styles.js +590 -0
  221. package/dist/utils/styles.js.map +1 -0
  222. package/dist/utils/typography.d.ts +36 -0
  223. package/dist/utils/typography.js +53 -0
  224. package/dist/utils/typography.js.map +1 -0
  225. package/dist/utils/warnings.d.ts +16 -0
  226. package/dist/utils/warnings.js +16 -0
  227. package/dist/utils/warnings.js.map +1 -0
  228. package/dist/zero/babel.d.ts +108 -0
  229. package/dist/zero/babel.js +282 -0
  230. package/dist/zero/babel.js.map +1 -0
  231. package/dist/zero/css-writer.d.ts +45 -0
  232. package/dist/zero/css-writer.js +74 -0
  233. package/dist/zero/css-writer.js.map +1 -0
  234. package/dist/zero/extractor.d.ts +24 -0
  235. package/dist/zero/extractor.js +150 -0
  236. package/dist/zero/extractor.js.map +1 -0
  237. package/dist/zero/index.d.ts +3 -0
  238. package/dist/zero/index.js +4 -0
  239. package/dist/zero/next.d.ts +60 -0
  240. package/dist/zero/next.js +78 -0
  241. package/dist/zero/next.js.map +1 -0
  242. package/package.json +189 -0
  243. package/tasty.config.ts +14 -0
@@ -0,0 +1,660 @@
1
+ import { Lru } from "../parser/lru.js";
2
+ import { createStateParserContext, extractLocalPredefinedStates } from "../states/index.js";
3
+ import { stringifyStyles } from "../utils/styles.js";
4
+ import { createStyle } from "../styles/createStyle.js";
5
+ import { STYLE_HANDLER_MAP } from "../styles/index.js";
6
+ import { and, falseCondition, not, or, trueCondition } from "./conditions.js";
7
+ import { simplifyCondition } from "./simplify.js";
8
+ import { buildExclusiveConditions, expandExclusiveOrs, expandOrConditions, isValueMapping, parseStyleEntries } from "./exclusive.js";
9
+ import { buildAtRulesFromVariant, conditionToCSS, modifierToCSS, parentGroupsToCSS, pseudoToCSS, rootConditionsToCSS, selectorConditionToCSS } from "./materialize.js";
10
+ import { emitWarning } from "./warnings.js";
11
+ import { parseStateKey } from "./parseStateKey.js";
12
+
13
+ //#region src/pipeline/index.ts
14
+ /**
15
+ * Tasty Style Rendering Pipeline
16
+ *
17
+ * This is the main entrypoint for the new pipeline implementation.
18
+ * It implements the complete flow from style objects to CSS rules.
19
+ *
20
+ * Pipeline stages:
21
+ * 1. PARSE CONDITIONS - Parse state keys into ConditionNode trees
22
+ * 2. BUILD EXCLUSIVE CONDITIONS - AND with negation of higher-priority conditions
23
+ * 3. SIMPLIFY CONDITIONS - Apply boolean algebra, detect contradictions
24
+ * 4. GROUP BY HANDLER - Collect styles per handler, compute combinations
25
+ * 5. COMPUTE CSS VALUES - Call handlers to get CSS declarations
26
+ * 6. MERGE BY VALUE - Merge rules with identical CSS output
27
+ * 7. MATERIALIZE CSS - Convert conditions to CSS selectors + at-rules
28
+ */
29
+ const pipelineCache = new Lru(5e3);
30
+ /**
31
+ * Clear the pipeline cache (for testing)
32
+ */
33
+ function clearPipelineCache() {
34
+ pipelineCache.clear();
35
+ }
36
+ function runPipeline(styles, parserContext) {
37
+ const allRules = [];
38
+ processStyles(styles, "", parserContext, allRules);
39
+ const seen = /* @__PURE__ */ new Set();
40
+ return allRules.filter((rule) => {
41
+ const key = `${rule.selector}|${rule.declarations}|${JSON.stringify(rule.atRules || [])}|${rule.rootPrefix || ""}`;
42
+ if (seen.has(key)) return false;
43
+ seen.add(key);
44
+ return true;
45
+ });
46
+ }
47
+ /**
48
+ * Process styles at a given nesting level
49
+ */
50
+ function processStyles(styles, selectorSuffix, parserContext, allRules) {
51
+ const keys = Object.keys(styles);
52
+ const selectorKeys = keys.filter((key) => isSelector(key));
53
+ const styleKeys = keys.filter((key) => !isSelector(key) && !key.startsWith("@"));
54
+ for (const key of selectorKeys) {
55
+ const nestedStyles = styles[key];
56
+ if (!nestedStyles || typeof nestedStyles !== "object") continue;
57
+ const suffixes = getAllSelectors(key, nestedStyles);
58
+ if (!suffixes) continue;
59
+ const { $: _$, ...cleanedStyles } = nestedStyles;
60
+ const subLocalStates = extractLocalPredefinedStates(cleanedStyles);
61
+ const hasSubStates = Object.keys(subLocalStates).length > 0;
62
+ const subContext = {
63
+ ...parserContext,
64
+ isSubElement: true,
65
+ localPredefinedStates: hasSubStates ? {
66
+ ...parserContext.localPredefinedStates,
67
+ ...subLocalStates
68
+ } : parserContext.localPredefinedStates
69
+ };
70
+ for (const suffix of suffixes) processStyles(cleanedStyles, selectorSuffix + suffix, subContext, allRules);
71
+ }
72
+ const handlerQueue = buildHandlerQueue(styleKeys, styles);
73
+ for (const { handler, styleMap } of handlerQueue) {
74
+ const lookupStyles = handler.__lookupStyles;
75
+ const exclusiveByStyle = /* @__PURE__ */ new Map();
76
+ for (const styleName of lookupStyles) {
77
+ const value = styleMap[styleName];
78
+ if (value === void 0) continue;
79
+ if (isValueMapping(value)) {
80
+ const fullyExpanded = expandExclusiveOrs(buildExclusiveConditions(expandOrConditions(parseStyleEntries(styleName, value, (stateKey) => parseStateKey(stateKey, { context: parserContext })))));
81
+ exclusiveByStyle.set(styleName, fullyExpanded);
82
+ } else exclusiveByStyle.set(styleName, [{
83
+ styleKey: styleName,
84
+ stateKey: "",
85
+ value,
86
+ condition: trueCondition(),
87
+ priority: 0,
88
+ exclusiveCondition: trueCondition()
89
+ }]);
90
+ }
91
+ const stateSnapshots = computeStateCombinations(exclusiveByStyle, lookupStyles);
92
+ const computedRules = [];
93
+ for (const snapshot of stateSnapshots) {
94
+ const result = handler(snapshot.values);
95
+ if (!result) continue;
96
+ const results = Array.isArray(result) ? result : [result];
97
+ for (const r of results) {
98
+ if (!r || typeof r !== "object") continue;
99
+ const { $, ...styleProps } = r;
100
+ const declarations = {};
101
+ for (const [prop, val] of Object.entries(styleProps)) if (val != null && val !== "") declarations[prop] = String(val);
102
+ if (Object.keys(declarations).length === 0) continue;
103
+ const suffixes = $ ? (Array.isArray($) ? $ : [$]).map((s) => selectorSuffix + normalizeSelectorSuffix(String(s))) : [selectorSuffix];
104
+ for (const suffix of suffixes) computedRules.push({
105
+ condition: snapshot.condition,
106
+ declarations,
107
+ selectorSuffix: suffix
108
+ });
109
+ }
110
+ }
111
+ const mergedRules = mergeByValue(computedRules);
112
+ for (const rule of mergedRules) {
113
+ const cssRules = materializeComputedRule(rule);
114
+ allRules.push(...cssRules);
115
+ }
116
+ }
117
+ }
118
+ /**
119
+ * Check if a key is a CSS selector
120
+ */
121
+ function isSelector(key) {
122
+ return key.startsWith("&") || key.startsWith(".") || /^[A-Z]/.test(key);
123
+ }
124
+ /**
125
+ * Get all selector suffixes for a sub-element key.
126
+ *
127
+ * Handles three types of selector keys:
128
+ * - `&` prefix: Raw selector suffix (e.g., `&:hover` → `:hover`)
129
+ * - `.` prefix: Class selector (e.g., `.active` → ` .active`)
130
+ * - Uppercase: Sub-element with optional `$` affix pattern
131
+ *
132
+ * @param key - The sub-element key (e.g., 'Label', '&:hover', '.active')
133
+ * @param styles - The styles object, may contain `$` property for selector affix
134
+ * @returns Array of selector suffixes, or null if invalid (with console warning)
135
+ *
136
+ * @example
137
+ * getAllSelectors('Label', {})
138
+ * // → [' [data-element="Label"]']
139
+ *
140
+ * getAllSelectors('Cell', { $: '>, >Body>' })
141
+ * // → ['> [data-element="Cell"]', ' [data-element="Body"] > [data-element="Cell"]']
142
+ */
143
+ function getAllSelectors(key, styles) {
144
+ if (key.startsWith("&")) return [key.slice(1)];
145
+ if (key.startsWith(".")) return [` ${key}`];
146
+ if (/^[A-Z]/.test(key)) {
147
+ const affix = styles?.$;
148
+ if (affix !== void 0) {
149
+ const result = processAffix(String(affix), key);
150
+ if (!result.valid) {
151
+ emitWarning("INVALID_SELECTOR_AFFIX", result.reason);
152
+ return null;
153
+ }
154
+ return result.selectors;
155
+ }
156
+ return [` [data-element="${key}"]`];
157
+ }
158
+ return null;
159
+ }
160
+ /**
161
+ * Process selector affix pattern and return selector(s)
162
+ *
163
+ * Supports:
164
+ * - Direct child: '>'
165
+ * - Chained elements: '>Body>Row>'
166
+ * - HTML tags: 'a', '>ul>li', 'button:hover'
167
+ * - Pseudo-elements on root: '::before'
168
+ * - Pseudo on sub-element: '@::before', '>@:hover'
169
+ * - Classes: '.active', '>@.active'
170
+ * - Multiple selectors: '>, >Body>'
171
+ * - Sibling combinators (after element): '>Item+', '>Item~'
172
+ */
173
+ function processAffix(affix, key) {
174
+ const trimmed = affix.trim();
175
+ if (!trimmed) return {
176
+ valid: true,
177
+ selectors: [` [data-element="${key}"]`]
178
+ };
179
+ const patterns = trimmed.split(",").map((p) => p.trim());
180
+ const selectors = [];
181
+ for (const pattern of patterns) {
182
+ const validation = validatePattern(pattern);
183
+ if (!validation.valid) return validation;
184
+ const selector = processSinglePattern(pattern, key);
185
+ selectors.push(selector);
186
+ }
187
+ return {
188
+ valid: true,
189
+ selectors
190
+ };
191
+ }
192
+ /**
193
+ * Recognized token patterns for selector affix validation.
194
+ *
195
+ * These patterns are used to tokenize and validate `$` affix strings.
196
+ * Order matters: more specific patterns must come first to avoid
197
+ * partial matches (e.g., `::before` must match before `:` alone).
198
+ *
199
+ * Unrecognized tokens (like `#id`, `*`, or numbers) will cause validation to fail.
200
+ */
201
+ const VALID_TOKEN_PATTERNS = [
202
+ /^[>+~]/,
203
+ /^[A-Z][a-zA-Z0-9]*/,
204
+ /^@/,
205
+ /^::?[a-z][a-z0-9-]*(?:\([^)]*\))?/,
206
+ /^\.[a-zA-Z_-][a-zA-Z0-9_-]*/,
207
+ /^\[[^\]]+\]/,
208
+ /^[a-z][a-z0-9-]*/,
209
+ /^\s+/,
210
+ /^&/
211
+ ];
212
+ /**
213
+ * Scan a pattern for unrecognized tokens.
214
+ *
215
+ * Iterates through the pattern, consuming recognized tokens until
216
+ * either the pattern is fully consumed (valid) or an unrecognized
217
+ * character sequence is found (invalid).
218
+ *
219
+ * @param pattern - The selector pattern to validate
220
+ * @returns The first unrecognized token found, or null if all tokens are valid
221
+ *
222
+ * @example
223
+ * findUnrecognizedTokens('>Body>Row>') // → null (valid)
224
+ * findUnrecognizedTokens('123') // → '123' (invalid)
225
+ * findUnrecognizedTokens('#myId') // → '#' (invalid)
226
+ */
227
+ function findUnrecognizedTokens(pattern) {
228
+ let remaining = pattern;
229
+ while (remaining.length > 0) {
230
+ let matched = false;
231
+ for (const regex of VALID_TOKEN_PATTERNS) {
232
+ const match = remaining.match(regex);
233
+ if (match) {
234
+ remaining = remaining.slice(match[0].length);
235
+ matched = true;
236
+ break;
237
+ }
238
+ }
239
+ if (!matched) {
240
+ const unrecognized = remaining.match(/^[^\s>+~@.:[\]A-Z]+/);
241
+ return unrecognized ? unrecognized[0] : remaining[0];
242
+ }
243
+ }
244
+ return null;
245
+ }
246
+ /**
247
+ * Validate a selector pattern for structural correctness.
248
+ *
249
+ * Checks for:
250
+ * 1. Out-of-scope selectors: Patterns starting with `+` or `~` target siblings
251
+ * of the root element, which is outside the component's DOM scope.
252
+ * 2. Consecutive combinators: Patterns like `>>` or `>+` are malformed CSS.
253
+ * 3. Unrecognized tokens: Characters/sequences not matching valid CSS selectors.
254
+ *
255
+ * @param pattern - A single selector pattern (already split by comma)
256
+ * @returns AffixResult indicating validity and error reason if invalid
257
+ *
258
+ * @example
259
+ * validatePattern('>Body>Row>') // → { valid: true, selectors: [] }
260
+ * validatePattern('+') // → { valid: false, reason: '...outside root scope...' }
261
+ * validatePattern('>>') // → { valid: false, reason: '...consecutive combinators...' }
262
+ */
263
+ function validatePattern(pattern) {
264
+ const trimmed = pattern.trim();
265
+ if (/^[+~]/.test(trimmed)) return {
266
+ valid: false,
267
+ reason: `Selector affix "${pattern}" targets elements outside the root scope. Sibling selectors (+, ~) must be preceded by an element inside the root. Use ">Element+" or ">Element~" instead.`
268
+ };
269
+ if (/[>+~]{2,}/.test(trimmed.replace(/\s+/g, ""))) return {
270
+ valid: false,
271
+ reason: `Selector affix "${pattern}" contains consecutive combinators.`
272
+ };
273
+ const unrecognized = findUnrecognizedTokens(trimmed);
274
+ if (unrecognized) return {
275
+ valid: false,
276
+ reason: `Selector affix "${pattern}" contains unrecognized token "${unrecognized}". Valid tokens: combinators (>, +, ~), element names (Uppercase), @ placeholder, pseudo (:hover, ::before), class (.name), attribute ([attr]).`
277
+ };
278
+ return {
279
+ valid: true,
280
+ selectors: []
281
+ };
282
+ }
283
+ /**
284
+ * Process a single selector pattern into a CSS selector suffix.
285
+ *
286
+ * This is the main transformation function that converts a `$` affix pattern
287
+ * into a valid CSS selector suffix. It handles:
288
+ *
289
+ * 1. `@` placeholder replacement with `[data-element="key"]`
290
+ * 2. Key injection based on pattern ending (see `shouldInjectKey`)
291
+ * 3. Proper spacing for descendant vs direct child selectors
292
+ *
293
+ * @param pattern - A single validated selector pattern
294
+ * @param key - The sub-element key to inject (e.g., 'Label', 'Cell')
295
+ * @returns CSS selector suffix ready to append to the root selector
296
+ *
297
+ * @example
298
+ * processSinglePattern('>', 'Row')
299
+ * // → '> [data-element="Row"]'
300
+ *
301
+ * processSinglePattern('>Body>Row>', 'Cell')
302
+ * // → '> [data-element="Body"] > [data-element="Row"] > [data-element="Cell"]'
303
+ *
304
+ * processSinglePattern('::before', 'Before')
305
+ * // → '::before' (no key injection for pseudo on root)
306
+ *
307
+ * processSinglePattern('>@:hover', 'Item')
308
+ * // → '> [data-element="Item"]:hover'
309
+ */
310
+ function processSinglePattern(pattern, key) {
311
+ const normalized = pattern.replace(/^&/, "").trim();
312
+ if (!normalized) return ` [data-element="${key}"]`;
313
+ const startsWithPseudo = /^::?[a-z]/.test(normalized);
314
+ let result = transformPattern(normalized);
315
+ if (result.includes("@")) {
316
+ result = result.replace(/@ (?=[.:])/g, "@");
317
+ result = result.replace(/@/g, `[data-element="${key}"]`);
318
+ if (!startsWithPseudo && !result.startsWith(" ")) result = " " + result;
319
+ return result;
320
+ }
321
+ if (shouldInjectKey(normalized)) result = result + ` [data-element="${key}"]`;
322
+ if (!startsWithPseudo && !result.startsWith(" ")) result = " " + result;
323
+ return result;
324
+ }
325
+ /**
326
+ * Transform a selector pattern by converting element names and normalizing spacing.
327
+ *
328
+ * This is a character-by-character tokenizer that:
329
+ * - Converts uppercase names to `[data-element="Name"]` selectors
330
+ * - Adds proper spacing around combinators (>, +, ~)
331
+ * - Preserves lowercase tags, classes, pseudos, and attributes as-is
332
+ * - Keeps @ placeholder for later replacement
333
+ *
334
+ * The tokenizer handles these token types in order:
335
+ * 1. Whitespace (skipped)
336
+ * 2. Combinators: >, +, ~ (add surrounding spaces)
337
+ * 3. Uppercase names: Body, Row (convert to [data-element="..."])
338
+ * 4. @ placeholder (keep for later replacement)
339
+ * 5. Pseudo: :hover, ::before (attach to previous token)
340
+ * 6. Tags: a, div, button (keep as-is with spacing)
341
+ * 7. Classes: .active (attach to previous element/tag/placeholder)
342
+ * 8. Attributes: [type="text"] (keep as-is)
343
+ *
344
+ * @param pattern - The raw selector pattern to transform
345
+ * @returns Transformed pattern with proper CSS selector syntax
346
+ *
347
+ * @example
348
+ * transformPattern('>Body>Row>')
349
+ * // → '> [data-element="Body"] > [data-element="Row"] >'
350
+ *
351
+ * transformPattern('button.primary:hover')
352
+ * // → 'button.primary:hover'
353
+ */
354
+ function transformPattern(pattern) {
355
+ let result = "";
356
+ let lastCh = "";
357
+ let i = 0;
358
+ while (i < pattern.length) {
359
+ const char = pattern[i];
360
+ if (/\s/.test(char)) {
361
+ i++;
362
+ continue;
363
+ }
364
+ if (/[>+~]/.test(char)) {
365
+ if (result && lastCh !== " ") result += " ";
366
+ result += char;
367
+ lastCh = char;
368
+ i++;
369
+ continue;
370
+ }
371
+ if (/[A-Z]/.test(char)) {
372
+ const nameStart = i;
373
+ while (i < pattern.length && /[a-zA-Z0-9]/.test(pattern[i])) i++;
374
+ if (result && lastCh !== " ") result += " ";
375
+ const segment = `[data-element="${pattern.slice(nameStart, i)}"]`;
376
+ result += segment;
377
+ lastCh = "]";
378
+ continue;
379
+ }
380
+ if (char === "@") {
381
+ if (result && lastCh !== " ") result += " ";
382
+ result += "@";
383
+ lastCh = "@";
384
+ i++;
385
+ continue;
386
+ }
387
+ if (char === ":") {
388
+ const pseudoStart = i;
389
+ while (i < pattern.length && !/[\s>+~,@]/.test(pattern[i]) && !/[A-Z]/.test(pattern[i])) i++;
390
+ const segment = pattern.slice(pseudoStart, i);
391
+ result += segment;
392
+ lastCh = segment[segment.length - 1] || lastCh;
393
+ continue;
394
+ }
395
+ if (/[a-z]/.test(char)) {
396
+ const tagStart = i;
397
+ while (i < pattern.length && /[a-z0-9-]/.test(pattern[i])) i++;
398
+ if (result && lastCh !== " ") result += " ";
399
+ const segment = pattern.slice(tagStart, i);
400
+ result += segment;
401
+ lastCh = segment[segment.length - 1] || lastCh;
402
+ continue;
403
+ }
404
+ if (char === ".") {
405
+ const attachToLast = lastCh === "]" || lastCh === "@" || /[a-zA-Z0-9-]/.test(lastCh);
406
+ if (result && !attachToLast && lastCh !== " ") result += " ";
407
+ const clsStart = i;
408
+ i++;
409
+ while (i < pattern.length && /[a-zA-Z0-9_-]/.test(pattern[i])) i++;
410
+ const segment = pattern.slice(clsStart, i);
411
+ result += segment;
412
+ lastCh = segment[segment.length - 1] || lastCh;
413
+ continue;
414
+ }
415
+ if (char === "[") {
416
+ const attachToLast = lastCh === "]" || lastCh === "@" || /[a-zA-Z0-9-]/.test(lastCh);
417
+ if (result && !attachToLast && lastCh !== " ") result += " ";
418
+ const attrStart = i;
419
+ let depth = 0;
420
+ while (i < pattern.length) {
421
+ if (pattern[i] === "[") depth++;
422
+ if (pattern[i] === "]") depth--;
423
+ i++;
424
+ if (depth === 0) break;
425
+ }
426
+ result += pattern.slice(attrStart, i);
427
+ lastCh = "]";
428
+ continue;
429
+ }
430
+ result += char;
431
+ lastCh = char;
432
+ i++;
433
+ }
434
+ return result;
435
+ }
436
+ /**
437
+ * Determine if the sub-element key should be auto-injected based on pattern ending.
438
+ *
439
+ * Key injection rules (when no @ placeholder is present):
440
+ *
441
+ * | Pattern Ending | Inject Key? | Example | Result |
442
+ * |----------------|-------------|---------|--------|
443
+ * | Combinator (>, +, ~) | Yes | `'>Body>'` | `> [data-element="Body"] > [el]` |
444
+ * | Uppercase element | Yes | `'>Body>Row'` | `> [el1] > [el2] [key]` |
445
+ * | Lowercase tag | Yes | `'>ul>li'` | `> ul > li [key]` |
446
+ * | Pseudo (:hover, ::before) | No | `'::before'` | `::before` |
447
+ * | Class (.active) | No | `'.active'` | `.active` |
448
+ * | Attribute ([type]) | No | `'[type="text"]'` | `[type="text"]` |
449
+ *
450
+ * @param pattern - The normalized pattern (after stripping &)
451
+ * @returns true if key should be injected, false otherwise
452
+ *
453
+ * @example
454
+ * shouldInjectKey('>') // → true (trailing combinator)
455
+ * shouldInjectKey('>Body>Row') // → true (ends with element)
456
+ * shouldInjectKey('>ul>li') // → true (ends with tag)
457
+ * shouldInjectKey('::before') // → false (ends with pseudo)
458
+ * shouldInjectKey('.active') // → false (ends with class)
459
+ * shouldInjectKey('a:hover') // → false (ends with pseudo)
460
+ * shouldInjectKey('button.primary') // → false (ends with class)
461
+ */
462
+ function shouldInjectKey(pattern) {
463
+ const trimmed = pattern.trim();
464
+ if (/[>+~]$/.test(trimmed)) return true;
465
+ if (/(?:^|[\s>+~\]:])[A-Z][a-zA-Z0-9]*$/.test(trimmed)) return true;
466
+ if (/(?<![:.])(?:^|[\s>+~])[a-z][a-z0-9-]*$/.test(trimmed)) return true;
467
+ return false;
468
+ }
469
+ /**
470
+ * Normalize selector suffix from $ property
471
+ */
472
+ function normalizeSelectorSuffix(suffix) {
473
+ if (!suffix) return "";
474
+ return suffix.startsWith("&") ? suffix.slice(1) : suffix;
475
+ }
476
+ /**
477
+ * Build handler queue from style keys
478
+ */
479
+ function buildHandlerQueue(styleKeys, styles) {
480
+ const queue = [];
481
+ const seenHandlers = /* @__PURE__ */ new Set();
482
+ for (const styleName of styleKeys) {
483
+ let handlers = STYLE_HANDLER_MAP[styleName];
484
+ if (!handlers) handlers = STYLE_HANDLER_MAP[styleName] = [createStyle(styleName)];
485
+ for (const handler of handlers) {
486
+ if (seenHandlers.has(handler)) continue;
487
+ seenHandlers.add(handler);
488
+ const lookupStyles = handler.__lookupStyles;
489
+ const styleMap = {};
490
+ for (const name of lookupStyles) {
491
+ const val = styles[name];
492
+ if (val !== void 0) styleMap[name] = val;
493
+ }
494
+ queue.push({
495
+ handler,
496
+ styleMap
497
+ });
498
+ }
499
+ }
500
+ return queue;
501
+ }
502
+ /**
503
+ * Compute all valid state combinations for a handler's lookup styles
504
+ */
505
+ function computeStateCombinations(exclusiveByStyle, lookupStyles) {
506
+ const combinations = cartesianProduct(lookupStyles.map((style) => exclusiveByStyle.get(style) || []));
507
+ const snapshots = [];
508
+ for (const combo of combinations) {
509
+ const simplified = simplifyCondition(and(...combo.map((e) => e.exclusiveCondition)));
510
+ if (simplified.kind === "false") continue;
511
+ const values = {};
512
+ for (const entry of combo) values[entry.styleKey] = entry.value;
513
+ snapshots.push({
514
+ condition: simplified,
515
+ values
516
+ });
517
+ }
518
+ return snapshots;
519
+ }
520
+ /**
521
+ * Cartesian product of arrays
522
+ */
523
+ function cartesianProduct(arrays) {
524
+ if (arrays.length === 0) return [[]];
525
+ const nonEmpty = arrays.filter((a) => a.length > 0);
526
+ if (nonEmpty.length === 0) return [[]];
527
+ let result = [[]];
528
+ for (const arr of nonEmpty) {
529
+ const next = [];
530
+ for (const combo of result) for (const item of arr) {
531
+ const newCombo = new Array(combo.length + 1);
532
+ for (let i = 0; i < combo.length; i++) newCombo[i] = combo[i];
533
+ newCombo[combo.length] = item;
534
+ next.push(newCombo);
535
+ }
536
+ result = next;
537
+ }
538
+ return result;
539
+ }
540
+ const declStringCache = /* @__PURE__ */ new WeakMap();
541
+ function stringifyDeclarations(decl) {
542
+ let cached = declStringCache.get(decl);
543
+ if (cached === void 0) {
544
+ cached = JSON.stringify(decl);
545
+ declStringCache.set(decl, cached);
546
+ }
547
+ return cached;
548
+ }
549
+ /**
550
+ * Merge rules with identical CSS output
551
+ */
552
+ function mergeByValue(rules) {
553
+ const groups = /* @__PURE__ */ new Map();
554
+ for (const rule of rules) {
555
+ const key = `${rule.selectorSuffix}|${stringifyDeclarations(rule.declarations)}`;
556
+ if (!groups.has(key)) groups.set(key, []);
557
+ groups.get(key).push(rule);
558
+ }
559
+ const merged = [];
560
+ for (const [, groupRules] of groups) if (groupRules.length === 1) merged.push(groupRules[0]);
561
+ else {
562
+ const mergedCondition = simplifyCondition(or(...groupRules.map((r) => r.condition)));
563
+ merged.push({
564
+ condition: mergedCondition,
565
+ declarations: groupRules[0].declarations,
566
+ selectorSuffix: groupRules[0].selectorSuffix
567
+ });
568
+ }
569
+ return merged;
570
+ }
571
+ /**
572
+ * Build selector fragment from a variant (without className prefix)
573
+ */
574
+ function buildSelectorFromVariant(variant, selectorSuffix) {
575
+ let selector = "";
576
+ for (const mod of variant.modifierConditions) selector += modifierToCSS(mod);
577
+ for (const pseudo of variant.pseudoConditions) selector += pseudoToCSS(pseudo);
578
+ if (variant.parentGroups.length > 0) selector += parentGroupsToCSS(variant.parentGroups);
579
+ selector += selectorSuffix;
580
+ for (const own of variant.ownConditions) selector += selectorConditionToCSS(own);
581
+ return selector;
582
+ }
583
+ /**
584
+ * Materialize a computed rule to final CSS format
585
+ *
586
+ * Returns an array because OR conditions may generate multiple CSS rules
587
+ * (when different branches have different at-rules)
588
+ */
589
+ function materializeComputedRule(rule) {
590
+ const components = conditionToCSS(rule.condition);
591
+ if (components.isImpossible || components.variants.length === 0) return [];
592
+ const declarations = Object.entries(rule.declarations).map(([prop, value]) => `${prop}: ${value};`).join(" ");
593
+ const getRootPrefixKey = (variant) => {
594
+ return variant.rootConditions.map((r) => selectorConditionToCSS(r)).sort().join("|");
595
+ };
596
+ const byAtRules = /* @__PURE__ */ new Map();
597
+ for (const variant of components.variants) {
598
+ const atRules = buildAtRulesFromVariant(variant);
599
+ const key = atRules.sort().join("|||") + "###" + getRootPrefixKey(variant);
600
+ const group = byAtRules.get(key);
601
+ if (group) group.variants.push(variant);
602
+ else byAtRules.set(key, {
603
+ variants: [variant],
604
+ atRules,
605
+ rootPrefix: rootConditionsToCSS(variant.rootConditions)
606
+ });
607
+ }
608
+ const rules = [];
609
+ for (const [, group] of byAtRules) {
610
+ const selectorFragments = group.variants.map((v) => buildSelectorFromVariant(v, rule.selectorSuffix));
611
+ const cssRule = {
612
+ selector: selectorFragments.length === 1 ? selectorFragments[0] : selectorFragments,
613
+ declarations
614
+ };
615
+ if (group.atRules.length > 0) cssRule.atRules = group.atRules;
616
+ if (group.rootPrefix) cssRule.rootPrefix = group.rootPrefix;
617
+ rules.push(cssRule);
618
+ }
619
+ return rules;
620
+ }
621
+ function renderStyles(styles, classNameOrSelector, options) {
622
+ const directSelector = !!classNameOrSelector;
623
+ if (!styles) return directSelector ? [] : { rules: [] };
624
+ const cacheKey = stringifyStyles(styles);
625
+ let rules = pipelineCache.get(cacheKey);
626
+ if (!rules) {
627
+ rules = runPipeline(styles, createStateParserContext(styles));
628
+ pipelineCache.set(cacheKey, rules);
629
+ }
630
+ if (directSelector) {
631
+ const shouldDouble = options?.doubleSelector ?? false;
632
+ return rules.map((rule) => {
633
+ const result = {
634
+ selector: (Array.isArray(rule.selector) ? rule.selector : rule.selector ? [rule.selector] : [""]).map((part) => {
635
+ let sel = part ? `${classNameOrSelector}${part}` : classNameOrSelector;
636
+ if (shouldDouble && sel.startsWith(".")) {
637
+ const classMatch = sel.match(/^\.[a-zA-Z_-][a-zA-Z0-9_-]*/);
638
+ if (classMatch) sel = classMatch[0] + sel;
639
+ }
640
+ if (rule.rootPrefix) sel = `${rule.rootPrefix} ${sel}`;
641
+ return sel;
642
+ }).join(", "),
643
+ declarations: rule.declarations
644
+ };
645
+ if (rule.atRules && rule.atRules.length > 0) result.atRules = rule.atRules;
646
+ return result;
647
+ });
648
+ }
649
+ return { rules: rules.map((r) => ({
650
+ selector: Array.isArray(r.selector) ? r.selector.join("|||") : r.selector,
651
+ declarations: r.declarations,
652
+ atRules: r.atRules,
653
+ needsClassName: true,
654
+ rootPrefix: r.rootPrefix
655
+ })) };
656
+ }
657
+
658
+ //#endregion
659
+ export { clearPipelineCache, isSelector, renderStyles };
660
+ //# sourceMappingURL=index.js.map