@tenphi/tasty 0.0.0-snapshot.2705ab9 → 0.0.0-snapshot.27699f6

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 (336) hide show
  1. package/README.md +35 -27
  2. package/dist/{ssr/async-storage.js → async-storage-B7_o6FKt.js} +17 -8
  3. package/dist/async-storage-B7_o6FKt.js.map +1 -0
  4. package/dist/{ssr/collector.d.ts → collector-LuU1vZ68.d.ts} +11 -15
  5. package/dist/{ssr/collector.js → collector-QwpPODsy.js} +37 -34
  6. package/dist/collector-QwpPODsy.js.map +1 -0
  7. package/dist/config-rrijM-U2.js +10137 -0
  8. package/dist/config-rrijM-U2.js.map +1 -0
  9. package/dist/config-vuCRkBWX.d.ts +884 -0
  10. package/dist/context-CkSg-kDT.js +24 -0
  11. package/dist/context-CkSg-kDT.js.map +1 -0
  12. package/dist/core/index.d.ts +5 -34
  13. package/dist/core/index.js +6 -27
  14. package/dist/core-BvTG0J_M.js +1592 -0
  15. package/dist/core-BvTG0J_M.js.map +1 -0
  16. package/dist/{zero/extractor.js → css-writer-C7HJGEmN.js} +75 -13
  17. package/dist/css-writer-C7HJGEmN.js.map +1 -0
  18. package/dist/{ssr/format-global-rules.js → format-global-rules-Dbc_1tc3.js} +3 -3
  19. package/dist/format-global-rules-Dbc_1tc3.js.map +1 -0
  20. package/dist/format-rules-BvokPr1L.js +143 -0
  21. package/dist/format-rules-BvokPr1L.js.map +1 -0
  22. package/dist/hydrate-Dsv7Pwon.js +45 -0
  23. package/dist/hydrate-Dsv7Pwon.js.map +1 -0
  24. package/dist/index-ZRxZWzlj.d.ts +1602 -0
  25. package/dist/index-dUtwpOux.d.ts +1266 -0
  26. package/dist/index.d.ts +5 -51
  27. package/dist/index.js +731 -35
  28. package/dist/index.js.map +1 -0
  29. package/dist/keyframes-DDqRjwa9.js +587 -0
  30. package/dist/keyframes-DDqRjwa9.js.map +1 -0
  31. package/dist/{utils/merge-styles.js → merge-styles-B8jV6zu3.js} +4 -6
  32. package/dist/merge-styles-B8jV6zu3.js.map +1 -0
  33. package/dist/{utils/merge-styles.d.ts → merge-styles-CtDJMhpJ.d.ts} +3 -3
  34. package/dist/{utils/resolve-recipes.js → resolve-recipes-BnTZz2I_.js} +5 -8
  35. package/dist/resolve-recipes-BnTZz2I_.js.map +1 -0
  36. package/dist/ssr/astro-client.d.ts +1 -0
  37. package/dist/ssr/astro-client.js +19 -0
  38. package/dist/ssr/astro-client.js.map +1 -0
  39. package/dist/ssr/astro-middleware.d.ts +15 -0
  40. package/dist/ssr/astro-middleware.js +19 -0
  41. package/dist/ssr/astro-middleware.js.map +1 -0
  42. package/dist/ssr/astro.d.ts +89 -10
  43. package/dist/ssr/astro.js +116 -32
  44. package/dist/ssr/astro.js.map +1 -1
  45. package/dist/ssr/index.d.ts +44 -5
  46. package/dist/ssr/index.js +7 -9
  47. package/dist/ssr/index.js.map +1 -1
  48. package/dist/ssr/next.d.ts +7 -6
  49. package/dist/ssr/next.js +18 -14
  50. package/dist/ssr/next.js.map +1 -1
  51. package/dist/static/index.d.ts +91 -5
  52. package/dist/static/index.js +49 -4
  53. package/dist/static/index.js.map +1 -0
  54. package/dist/static/inject.d.ts +5 -0
  55. package/dist/static/inject.js +17 -0
  56. package/dist/static/inject.js.map +1 -0
  57. package/dist/zero/babel.d.ts +24 -110
  58. package/dist/zero/babel.js +139 -47
  59. package/dist/zero/babel.js.map +1 -1
  60. package/dist/zero/index.d.ts +67 -3
  61. package/dist/zero/index.js +2 -4
  62. package/dist/zero/next.d.ts +12 -0
  63. package/dist/zero/next.js +5 -4
  64. package/dist/zero/next.js.map +1 -1
  65. package/docs/README.md +4 -4
  66. package/docs/adoption.md +5 -3
  67. package/docs/comparison.md +24 -25
  68. package/docs/configuration.md +69 -1
  69. package/docs/debug.md +11 -9
  70. package/docs/design-system.md +22 -10
  71. package/docs/dsl.md +24 -8
  72. package/docs/getting-started.md +10 -10
  73. package/docs/injector.md +43 -27
  74. package/docs/methodology.md +52 -3
  75. package/docs/{PIPELINE.md → pipeline.md} +204 -50
  76. package/docs/{runtime.md → react-api.md} +104 -32
  77. package/docs/ssr.md +139 -81
  78. package/docs/styles.md +17 -0
  79. package/docs/tasty-static.md +101 -2
  80. package/package.json +23 -11
  81. package/dist/_virtual/_rolldown/runtime.js +0 -8
  82. package/dist/chunks/cacheKey.js +0 -78
  83. package/dist/chunks/cacheKey.js.map +0 -1
  84. package/dist/chunks/definitions.d.ts +0 -37
  85. package/dist/chunks/definitions.js +0 -259
  86. package/dist/chunks/definitions.js.map +0 -1
  87. package/dist/chunks/renderChunk.js +0 -60
  88. package/dist/chunks/renderChunk.js.map +0 -1
  89. package/dist/config.d.ts +0 -366
  90. package/dist/config.js +0 -504
  91. package/dist/config.js.map +0 -1
  92. package/dist/counter-style/index.js +0 -51
  93. package/dist/counter-style/index.js.map +0 -1
  94. package/dist/debug.d.ts +0 -89
  95. package/dist/debug.js +0 -454
  96. package/dist/debug.js.map +0 -1
  97. package/dist/font-face/index.js +0 -63
  98. package/dist/font-face/index.js.map +0 -1
  99. package/dist/hooks/resolve-ssr-collector.js +0 -15
  100. package/dist/hooks/resolve-ssr-collector.js.map +0 -1
  101. package/dist/hooks/useCounterStyle.d.ts +0 -50
  102. package/dist/hooks/useCounterStyle.js +0 -47
  103. package/dist/hooks/useCounterStyle.js.map +0 -1
  104. package/dist/hooks/useFontFace.d.ts +0 -43
  105. package/dist/hooks/useFontFace.js +0 -71
  106. package/dist/hooks/useFontFace.js.map +0 -1
  107. package/dist/hooks/useGlobalStyles.d.ts +0 -30
  108. package/dist/hooks/useGlobalStyles.js +0 -79
  109. package/dist/hooks/useGlobalStyles.js.map +0 -1
  110. package/dist/hooks/useKeyframes.d.ts +0 -56
  111. package/dist/hooks/useKeyframes.js +0 -65
  112. package/dist/hooks/useKeyframes.js.map +0 -1
  113. package/dist/hooks/useProperty.d.ts +0 -79
  114. package/dist/hooks/useProperty.js +0 -110
  115. package/dist/hooks/useProperty.js.map +0 -1
  116. package/dist/hooks/useRawCSS.d.ts +0 -53
  117. package/dist/hooks/useRawCSS.js +0 -36
  118. package/dist/hooks/useRawCSS.js.map +0 -1
  119. package/dist/hooks/useStyles.d.ts +0 -45
  120. package/dist/hooks/useStyles.js +0 -238
  121. package/dist/hooks/useStyles.js.map +0 -1
  122. package/dist/injector/index.d.ts +0 -183
  123. package/dist/injector/index.js +0 -180
  124. package/dist/injector/index.js.map +0 -1
  125. package/dist/injector/injector.d.ts +0 -166
  126. package/dist/injector/injector.js +0 -465
  127. package/dist/injector/injector.js.map +0 -1
  128. package/dist/injector/sheet-manager.d.ts +0 -136
  129. package/dist/injector/sheet-manager.js +0 -734
  130. package/dist/injector/sheet-manager.js.map +0 -1
  131. package/dist/injector/types.d.ts +0 -204
  132. package/dist/keyframes/index.js +0 -206
  133. package/dist/keyframes/index.js.map +0 -1
  134. package/dist/parser/classify.js +0 -320
  135. package/dist/parser/classify.js.map +0 -1
  136. package/dist/parser/const.js +0 -49
  137. package/dist/parser/const.js.map +0 -1
  138. package/dist/parser/lru.js +0 -109
  139. package/dist/parser/lru.js.map +0 -1
  140. package/dist/parser/parser.d.ts +0 -25
  141. package/dist/parser/parser.js +0 -116
  142. package/dist/parser/parser.js.map +0 -1
  143. package/dist/parser/tokenizer.js +0 -69
  144. package/dist/parser/tokenizer.js.map +0 -1
  145. package/dist/parser/types.d.ts +0 -51
  146. package/dist/parser/types.js +0 -46
  147. package/dist/parser/types.js.map +0 -1
  148. package/dist/pipeline/conditions.d.ts +0 -134
  149. package/dist/pipeline/conditions.js +0 -406
  150. package/dist/pipeline/conditions.js.map +0 -1
  151. package/dist/pipeline/exclusive.js +0 -231
  152. package/dist/pipeline/exclusive.js.map +0 -1
  153. package/dist/pipeline/index.d.ts +0 -55
  154. package/dist/pipeline/index.js +0 -709
  155. package/dist/pipeline/index.js.map +0 -1
  156. package/dist/pipeline/materialize.js +0 -1104
  157. package/dist/pipeline/materialize.js.map +0 -1
  158. package/dist/pipeline/parseStateKey.d.ts +0 -15
  159. package/dist/pipeline/parseStateKey.js +0 -447
  160. package/dist/pipeline/parseStateKey.js.map +0 -1
  161. package/dist/pipeline/simplify.js +0 -516
  162. package/dist/pipeline/simplify.js.map +0 -1
  163. package/dist/pipeline/warnings.js +0 -18
  164. package/dist/pipeline/warnings.js.map +0 -1
  165. package/dist/plugins/okhsl-plugin.d.ts +0 -35
  166. package/dist/plugins/okhsl-plugin.js +0 -98
  167. package/dist/plugins/okhsl-plugin.js.map +0 -1
  168. package/dist/plugins/types.d.ts +0 -76
  169. package/dist/properties/index.js +0 -223
  170. package/dist/properties/index.js.map +0 -1
  171. package/dist/properties/property-type-resolver.d.ts +0 -24
  172. package/dist/properties/property-type-resolver.js +0 -91
  173. package/dist/properties/property-type-resolver.js.map +0 -1
  174. package/dist/ssr/async-storage.d.ts +0 -17
  175. package/dist/ssr/async-storage.js.map +0 -1
  176. package/dist/ssr/collect-auto-properties.js +0 -40
  177. package/dist/ssr/collect-auto-properties.js.map +0 -1
  178. package/dist/ssr/collector.js.map +0 -1
  179. package/dist/ssr/context.d.ts +0 -8
  180. package/dist/ssr/context.js +0 -14
  181. package/dist/ssr/context.js.map +0 -1
  182. package/dist/ssr/format-global-rules.js.map +0 -1
  183. package/dist/ssr/format-keyframes.js +0 -70
  184. package/dist/ssr/format-keyframes.js.map +0 -1
  185. package/dist/ssr/format-property.js +0 -50
  186. package/dist/ssr/format-property.js.map +0 -1
  187. package/dist/ssr/format-rules.js +0 -73
  188. package/dist/ssr/format-rules.js.map +0 -1
  189. package/dist/ssr/hydrate.d.ts +0 -22
  190. package/dist/ssr/hydrate.js +0 -50
  191. package/dist/ssr/hydrate.js.map +0 -1
  192. package/dist/ssr/ssr-collector-ref.js +0 -12
  193. package/dist/ssr/ssr-collector-ref.js.map +0 -1
  194. package/dist/states/index.d.ts +0 -49
  195. package/dist/states/index.js +0 -171
  196. package/dist/states/index.js.map +0 -1
  197. package/dist/static/tastyStatic.d.ts +0 -46
  198. package/dist/static/tastyStatic.js +0 -31
  199. package/dist/static/tastyStatic.js.map +0 -1
  200. package/dist/static/types.d.ts +0 -49
  201. package/dist/static/types.js +0 -24
  202. package/dist/static/types.js.map +0 -1
  203. package/dist/styles/align.d.ts +0 -15
  204. package/dist/styles/align.js +0 -14
  205. package/dist/styles/align.js.map +0 -1
  206. package/dist/styles/border.d.ts +0 -25
  207. package/dist/styles/border.js +0 -114
  208. package/dist/styles/border.js.map +0 -1
  209. package/dist/styles/color.d.ts +0 -14
  210. package/dist/styles/color.js +0 -27
  211. package/dist/styles/color.js.map +0 -1
  212. package/dist/styles/createStyle.js +0 -80
  213. package/dist/styles/createStyle.js.map +0 -1
  214. package/dist/styles/dimension.js +0 -97
  215. package/dist/styles/dimension.js.map +0 -1
  216. package/dist/styles/display.d.ts +0 -37
  217. package/dist/styles/display.js +0 -67
  218. package/dist/styles/display.js.map +0 -1
  219. package/dist/styles/fade.d.ts +0 -15
  220. package/dist/styles/fade.js +0 -58
  221. package/dist/styles/fade.js.map +0 -1
  222. package/dist/styles/fill.d.ts +0 -42
  223. package/dist/styles/fill.js +0 -52
  224. package/dist/styles/fill.js.map +0 -1
  225. package/dist/styles/flow.d.ts +0 -16
  226. package/dist/styles/flow.js +0 -12
  227. package/dist/styles/flow.js.map +0 -1
  228. package/dist/styles/gap.d.ts +0 -31
  229. package/dist/styles/gap.js +0 -37
  230. package/dist/styles/gap.js.map +0 -1
  231. package/dist/styles/height.d.ts +0 -17
  232. package/dist/styles/height.js +0 -20
  233. package/dist/styles/height.js.map +0 -1
  234. package/dist/styles/index.d.ts +0 -2
  235. package/dist/styles/index.js +0 -9
  236. package/dist/styles/index.js.map +0 -1
  237. package/dist/styles/inset.d.ts +0 -52
  238. package/dist/styles/inset.js +0 -150
  239. package/dist/styles/inset.js.map +0 -1
  240. package/dist/styles/justify.d.ts +0 -15
  241. package/dist/styles/justify.js +0 -14
  242. package/dist/styles/justify.js.map +0 -1
  243. package/dist/styles/list.d.ts +0 -16
  244. package/dist/styles/list.js +0 -98
  245. package/dist/styles/list.js.map +0 -1
  246. package/dist/styles/margin.d.ts +0 -24
  247. package/dist/styles/margin.js +0 -104
  248. package/dist/styles/margin.js.map +0 -1
  249. package/dist/styles/outline.d.ts +0 -29
  250. package/dist/styles/outline.js +0 -65
  251. package/dist/styles/outline.js.map +0 -1
  252. package/dist/styles/padding.d.ts +0 -24
  253. package/dist/styles/padding.js +0 -104
  254. package/dist/styles/padding.js.map +0 -1
  255. package/dist/styles/predefined.d.ts +0 -71
  256. package/dist/styles/predefined.js +0 -238
  257. package/dist/styles/predefined.js.map +0 -1
  258. package/dist/styles/preset.d.ts +0 -52
  259. package/dist/styles/preset.js +0 -127
  260. package/dist/styles/preset.js.map +0 -1
  261. package/dist/styles/radius.d.ts +0 -14
  262. package/dist/styles/radius.js +0 -51
  263. package/dist/styles/radius.js.map +0 -1
  264. package/dist/styles/scrollbar.d.ts +0 -25
  265. package/dist/styles/scrollbar.js +0 -48
  266. package/dist/styles/scrollbar.js.map +0 -1
  267. package/dist/styles/shadow.d.ts +0 -14
  268. package/dist/styles/shadow.js +0 -24
  269. package/dist/styles/shadow.js.map +0 -1
  270. package/dist/styles/transition.d.ts +0 -14
  271. package/dist/styles/transition.js +0 -158
  272. package/dist/styles/transition.js.map +0 -1
  273. package/dist/styles/types.d.ts +0 -547
  274. package/dist/styles/width.d.ts +0 -17
  275. package/dist/styles/width.js +0 -20
  276. package/dist/styles/width.js.map +0 -1
  277. package/dist/tasty.d.ts +0 -119
  278. package/dist/tasty.js +0 -232
  279. package/dist/tasty.js.map +0 -1
  280. package/dist/types.d.ts +0 -184
  281. package/dist/utils/cache-wrapper.js +0 -22
  282. package/dist/utils/cache-wrapper.js.map +0 -1
  283. package/dist/utils/case-converter.js +0 -8
  284. package/dist/utils/case-converter.js.map +0 -1
  285. package/dist/utils/color-math.d.ts +0 -46
  286. package/dist/utils/color-math.js +0 -749
  287. package/dist/utils/color-math.js.map +0 -1
  288. package/dist/utils/color-space.d.ts +0 -5
  289. package/dist/utils/color-space.js +0 -229
  290. package/dist/utils/color-space.js.map +0 -1
  291. package/dist/utils/colors.d.ts +0 -5
  292. package/dist/utils/colors.js +0 -11
  293. package/dist/utils/colors.js.map +0 -1
  294. package/dist/utils/css-types.d.ts +0 -7
  295. package/dist/utils/dotize.d.ts +0 -26
  296. package/dist/utils/dotize.js +0 -122
  297. package/dist/utils/dotize.js.map +0 -1
  298. package/dist/utils/filter-base-props.d.ts +0 -15
  299. package/dist/utils/filter-base-props.js +0 -45
  300. package/dist/utils/filter-base-props.js.map +0 -1
  301. package/dist/utils/get-display-name.d.ts +0 -7
  302. package/dist/utils/get-display-name.js +0 -10
  303. package/dist/utils/get-display-name.js.map +0 -1
  304. package/dist/utils/has-keys.js +0 -13
  305. package/dist/utils/has-keys.js.map +0 -1
  306. package/dist/utils/is-dev-env.js +0 -19
  307. package/dist/utils/is-dev-env.js.map +0 -1
  308. package/dist/utils/is-valid-element-type.js +0 -15
  309. package/dist/utils/is-valid-element-type.js.map +0 -1
  310. package/dist/utils/merge-styles.js.map +0 -1
  311. package/dist/utils/mod-attrs.d.ts +0 -8
  312. package/dist/utils/mod-attrs.js +0 -21
  313. package/dist/utils/mod-attrs.js.map +0 -1
  314. package/dist/utils/process-tokens.d.ts +0 -21
  315. package/dist/utils/process-tokens.js +0 -91
  316. package/dist/utils/process-tokens.js.map +0 -1
  317. package/dist/utils/resolve-recipes.d.ts +0 -17
  318. package/dist/utils/resolve-recipes.js.map +0 -1
  319. package/dist/utils/selector-transform.js +0 -32
  320. package/dist/utils/selector-transform.js.map +0 -1
  321. package/dist/utils/string.js +0 -8
  322. package/dist/utils/string.js.map +0 -1
  323. package/dist/utils/styles.d.ts +0 -99
  324. package/dist/utils/styles.js +0 -221
  325. package/dist/utils/styles.js.map +0 -1
  326. package/dist/utils/typography.d.ts +0 -47
  327. package/dist/utils/typography.js +0 -51
  328. package/dist/utils/typography.js.map +0 -1
  329. package/dist/utils/warnings.d.ts +0 -16
  330. package/dist/utils/warnings.js +0 -16
  331. package/dist/utils/warnings.js.map +0 -1
  332. package/dist/zero/css-writer.d.ts +0 -45
  333. package/dist/zero/css-writer.js +0 -74
  334. package/dist/zero/css-writer.js.map +0 -1
  335. package/dist/zero/extractor.d.ts +0 -24
  336. package/dist/zero/extractor.js.map +0 -1
@@ -1,734 +0,0 @@
1
- import { PropertyTypeResolver } from "../properties/property-type-resolver.js";
2
- import { createStyle } from "../styles/createStyle.js";
3
- import { STYLE_HANDLER_MAP } from "../styles/index.js";
4
-
5
- //#region src/injector/sheet-manager.ts
6
- var SheetManager = class {
7
- rootRegistries = /* @__PURE__ */ new WeakMap();
8
- config;
9
- /** Dedicated style elements for raw CSS per root */
10
- rawStyleElements = /* @__PURE__ */ new WeakMap();
11
- /** Tracking for raw CSS blocks per root */
12
- rawCSSBlocks = /* @__PURE__ */ new WeakMap();
13
- /** Counter for generating unique raw CSS IDs */
14
- rawCSSCounter = 0;
15
- constructor(config) {
16
- this.config = config;
17
- }
18
- /**
19
- * Get or create registry for a root (Document or ShadowRoot)
20
- */
21
- getRegistry(root) {
22
- let registry = this.rootRegistries.get(root);
23
- if (!registry) {
24
- const metrics = this.config.devMode ? {
25
- hits: 0,
26
- misses: 0,
27
- bulkCleanups: 0,
28
- totalInsertions: 0,
29
- totalUnused: 0,
30
- stylesCleanedUp: 0,
31
- cleanupHistory: [],
32
- startTime: Date.now()
33
- } : void 0;
34
- registry = {
35
- sheets: [],
36
- refCounts: /* @__PURE__ */ new Map(),
37
- rules: /* @__PURE__ */ new Map(),
38
- cacheKeyToClassName: /* @__PURE__ */ new Map(),
39
- ruleTextSet: /* @__PURE__ */ new Set(),
40
- bulkCleanupTimeout: null,
41
- cleanupCheckTimeout: null,
42
- metrics,
43
- classCounter: 0,
44
- keyframesCache: /* @__PURE__ */ new Map(),
45
- keyframesNameToContent: /* @__PURE__ */ new Map(),
46
- keyframesCounter: 0,
47
- injectedProperties: /* @__PURE__ */ new Map(),
48
- injectedFontFaces: /* @__PURE__ */ new Set(),
49
- injectedCounterStyles: /* @__PURE__ */ new Set(),
50
- globalRules: /* @__PURE__ */ new Map(),
51
- propertyTypeResolver: new PropertyTypeResolver()
52
- };
53
- this.rootRegistries.set(root, registry);
54
- }
55
- return registry;
56
- }
57
- /**
58
- * Create a new stylesheet for the registry
59
- */
60
- createSheet(registry, root) {
61
- const sheetInfo = {
62
- sheet: this.createStyleElement(root),
63
- ruleCount: 0,
64
- holes: []
65
- };
66
- registry.sheets.push(sheetInfo);
67
- return sheetInfo;
68
- }
69
- /**
70
- * Create a style element and append to document
71
- */
72
- createStyleElement(root) {
73
- const style = root.createElement?.("style") || document.createElement("style");
74
- if (this.config.nonce) style.nonce = this.config.nonce;
75
- style.setAttribute("data-tasty", "");
76
- if ("head" in root && root.head) root.head.appendChild(style);
77
- else if ("appendChild" in root) root.appendChild(style);
78
- else document.head.appendChild(style);
79
- if (!style.isConnected && !this.config.forceTextInjection) console.error("SheetManager: Style element failed to connect to DOM!", {
80
- parentNode: style.parentNode?.nodeName,
81
- isConnected: style.isConnected
82
- });
83
- return style;
84
- }
85
- /**
86
- * Insert CSS rules as a single block
87
- */
88
- insertRule(registry, flattenedRules, className, root) {
89
- let targetSheet = this.findAvailableSheet(registry, root);
90
- if (!targetSheet) targetSheet = this.createSheet(registry, root);
91
- const sheetIndex = registry.sheets.indexOf(targetSheet);
92
- try {
93
- const groupedRules = [];
94
- const groupMap = /* @__PURE__ */ new Map();
95
- const atKey = (at) => at && at.length ? at.join("|") : "";
96
- flattenedRules.forEach((r) => {
97
- const key = `${atKey(r.atRules)}||${r.selector}||${r.startingStyle ? "1" : "0"}`;
98
- const existing = groupMap.get(key);
99
- if (existing) existing.declarations = existing.declarations ? `${existing.declarations} ${r.declarations}` : r.declarations;
100
- else {
101
- groupMap.set(key, {
102
- idx: groupedRules.length,
103
- selector: r.selector,
104
- atRules: r.atRules,
105
- startingStyle: r.startingStyle,
106
- declarations: r.declarations
107
- });
108
- groupedRules.push({ ...r });
109
- }
110
- });
111
- groupMap.forEach((val) => {
112
- groupedRules[val.idx] = {
113
- selector: val.selector,
114
- atRules: val.atRules,
115
- startingStyle: val.startingStyle,
116
- declarations: val.declarations
117
- };
118
- });
119
- const insertedRuleTexts = [];
120
- const insertedIndices = [];
121
- let currentRuleIndex = this.findAvailableRuleIndex(targetSheet);
122
- let firstInsertedIndex = null;
123
- let lastInsertedIndex = null;
124
- for (const rule of groupedRules) {
125
- const declarations = rule.declarations;
126
- const innerContent = rule.startingStyle ? `@starting-style { ${declarations} }` : declarations;
127
- const baseRule = `${rule.selector} { ${innerContent} }`;
128
- let fullRule = baseRule;
129
- if (rule.atRules && rule.atRules.length > 0) fullRule = rule.atRules.reduce((css, atRule) => `${atRule} { ${css} }`, baseRule);
130
- const styleElement = targetSheet.sheet;
131
- const styleSheet = styleElement.sheet;
132
- if (styleSheet && !this.config.forceTextInjection) {
133
- const maxIndex = styleSheet.cssRules.length;
134
- const atomicRuleIndex = this.findAvailableRuleIndex(targetSheet);
135
- const safeIndex = Math.min(Math.max(0, atomicRuleIndex), maxIndex);
136
- const splitSelectorsSafely = (selectorList) => {
137
- const parts = [];
138
- let buf = "";
139
- let depthSq = 0;
140
- let depthPar = 0;
141
- let inStr = "";
142
- for (let i = 0; i < selectorList.length; i++) {
143
- const ch = selectorList[i];
144
- if (inStr) {
145
- if (ch === inStr && selectorList[i - 1] !== "\\") inStr = "";
146
- buf += ch;
147
- continue;
148
- }
149
- if (ch === "\"" || ch === "'") {
150
- inStr = ch;
151
- buf += ch;
152
- continue;
153
- }
154
- if (ch === "[") depthSq++;
155
- else if (ch === "]") depthSq = Math.max(0, depthSq - 1);
156
- else if (ch === "(") depthPar++;
157
- else if (ch === ")") depthPar = Math.max(0, depthPar - 1);
158
- if (ch === "," && depthSq === 0 && depthPar === 0) {
159
- const part = buf.trim();
160
- if (part) parts.push(part);
161
- buf = "";
162
- } else buf += ch;
163
- }
164
- const tail = buf.trim();
165
- if (tail) parts.push(tail);
166
- return parts;
167
- };
168
- try {
169
- styleSheet.insertRule(fullRule, safeIndex);
170
- targetSheet.ruleCount++;
171
- insertedIndices.push(safeIndex);
172
- if (firstInsertedIndex == null) firstInsertedIndex = safeIndex;
173
- lastInsertedIndex = safeIndex;
174
- currentRuleIndex = safeIndex + 1;
175
- } catch (e) {
176
- const selectors = splitSelectorsSafely(rule.selector);
177
- if (selectors.length > 1) {
178
- let anyInserted = false;
179
- for (const sel of selectors) {
180
- const singleBase = `${sel} { ${declarations} }`;
181
- let singleRule = singleBase;
182
- if (rule.atRules && rule.atRules.length > 0) singleRule = rule.atRules.reduce((css, atRule) => `${atRule} { ${css} }`, singleBase);
183
- try {
184
- const maxIdx = styleSheet.cssRules.length;
185
- const atomicIdx = this.findAvailableRuleIndex(targetSheet);
186
- const idx = Math.min(Math.max(0, atomicIdx), maxIdx);
187
- styleSheet.insertRule(singleRule, idx);
188
- targetSheet.ruleCount++;
189
- insertedIndices.push(idx);
190
- if (firstInsertedIndex == null) firstInsertedIndex = idx;
191
- lastInsertedIndex = idx;
192
- currentRuleIndex = idx + 1;
193
- anyInserted = true;
194
- } catch (singleErr) {
195
- console.warn("[tasty] Browser rejected CSS rule:", singleRule, singleErr);
196
- }
197
- }
198
- if (!anyInserted) {}
199
- } else console.warn("[tasty] Browser rejected CSS rule:", fullRule, e);
200
- }
201
- } else {
202
- const atomicRuleIndex = this.findAvailableRuleIndex(targetSheet);
203
- styleElement.textContent = (styleElement.textContent || "") + "\n" + fullRule;
204
- targetSheet.ruleCount++;
205
- insertedIndices.push(atomicRuleIndex);
206
- if (firstInsertedIndex == null) firstInsertedIndex = atomicRuleIndex;
207
- lastInsertedIndex = atomicRuleIndex;
208
- currentRuleIndex = atomicRuleIndex + 1;
209
- }
210
- if (!styleElement.parentNode && !this.config.forceTextInjection) console.error("SheetManager: Style element is NOT in DOM! This is the problem!", {
211
- className,
212
- ruleIndex: currentRuleIndex
213
- });
214
- if (this.config.devMode) {
215
- insertedRuleTexts.push(fullRule);
216
- try {
217
- registry.ruleTextSet.add(fullRule);
218
- } catch {}
219
- }
220
- }
221
- if (insertedIndices.length === 0) return null;
222
- return {
223
- className,
224
- ruleIndex: firstInsertedIndex ?? 0,
225
- sheetIndex,
226
- cssText: this.config.devMode ? insertedRuleTexts : void 0,
227
- endRuleIndex: lastInsertedIndex ?? firstInsertedIndex ?? 0,
228
- indices: insertedIndices
229
- };
230
- } catch (error) {
231
- console.warn("Failed to insert CSS rules:", error, {
232
- flattenedRules,
233
- className
234
- });
235
- return null;
236
- }
237
- }
238
- /**
239
- * Insert global CSS rules
240
- */
241
- insertGlobalRule(registry, flattenedRules, globalKey, root) {
242
- const ruleInfo = this.insertRule(registry, flattenedRules, globalKey, root);
243
- if (ruleInfo) registry.globalRules.set(globalKey, ruleInfo);
244
- return ruleInfo;
245
- }
246
- /**
247
- * Delete a global CSS rule by key
248
- */
249
- deleteGlobalRule(registry, globalKey) {
250
- const ruleInfo = registry.globalRules.get(globalKey);
251
- if (!ruleInfo) return;
252
- this.deleteRule(registry, ruleInfo);
253
- registry.globalRules.delete(globalKey);
254
- }
255
- /**
256
- * Adjust rule indices after deletion to account for shifting
257
- */
258
- adjustIndicesAfterDeletion(registry, sheetIndex, startIdx, endIdx, deleteCount, deletedRuleInfo, deletedIndices) {
259
- try {
260
- const sortedDeleted = deletedIndices && deletedIndices.length > 0 ? [...deletedIndices].sort((a, b) => a - b) : null;
261
- const countDeletedBefore = (sorted, idx) => {
262
- let shift = 0;
263
- for (const delIdx of sorted) if (delIdx < idx) shift++;
264
- else break;
265
- return shift;
266
- };
267
- const adjustRuleInfo = (info) => {
268
- if (info === deletedRuleInfo) return;
269
- if (info.sheetIndex !== sheetIndex) return;
270
- if (!info.indices || info.indices.length === 0) return;
271
- if (sortedDeleted) info.indices = info.indices.map((idx) => {
272
- return idx - countDeletedBefore(sortedDeleted, idx);
273
- });
274
- else info.indices = info.indices.map((idx) => idx > endIdx ? Math.max(0, idx - deleteCount) : idx);
275
- if (info.indices.length > 0) {
276
- info.ruleIndex = Math.min(...info.indices);
277
- info.endRuleIndex = Math.max(...info.indices);
278
- }
279
- };
280
- for (const info of registry.rules.values()) adjustRuleInfo(info);
281
- for (const info of registry.globalRules.values()) adjustRuleInfo(info);
282
- for (const entry of registry.keyframesCache.values()) {
283
- const ki = entry.info;
284
- if (ki.sheetIndex !== sheetIndex) continue;
285
- if (sortedDeleted) {
286
- const shift = countDeletedBefore(sortedDeleted, ki.ruleIndex);
287
- if (shift > 0) ki.ruleIndex = Math.max(0, ki.ruleIndex - shift);
288
- } else if (ki.ruleIndex > endIdx) ki.ruleIndex = Math.max(0, ki.ruleIndex - deleteCount);
289
- }
290
- } catch {}
291
- }
292
- /**
293
- * Delete a CSS rule from the sheet
294
- */
295
- deleteRule(registry, ruleInfo) {
296
- const sheet = registry.sheets[ruleInfo.sheetIndex];
297
- if (!sheet) return;
298
- try {
299
- const texts = this.config.devMode && Array.isArray(ruleInfo.cssText) ? ruleInfo.cssText.slice() : [];
300
- const styleSheet = sheet.sheet.sheet;
301
- if (styleSheet) {
302
- const rules = styleSheet.cssRules;
303
- if (ruleInfo.indices && ruleInfo.indices.length > 0) {
304
- const sortedIndices = [...ruleInfo.indices].sort((a, b) => b - a);
305
- const deletedIndices = [];
306
- for (const idx of sortedIndices) if (idx >= 0 && idx < styleSheet.cssRules.length) try {
307
- styleSheet.deleteRule(idx);
308
- deletedIndices.push(idx);
309
- } catch (e) {
310
- console.warn(`Failed to delete rule at index ${idx}:`, e);
311
- }
312
- sheet.ruleCount = Math.max(0, sheet.ruleCount - deletedIndices.length);
313
- if (deletedIndices.length > 0) this.adjustIndicesAfterDeletion(registry, ruleInfo.sheetIndex, Math.min(...deletedIndices), Math.max(...deletedIndices), deletedIndices.length, ruleInfo, deletedIndices);
314
- } else {
315
- const startIdx = Math.max(0, ruleInfo.ruleIndex);
316
- const endIdx = Math.min(rules.length - 1, Number.isFinite(ruleInfo.endRuleIndex) ? ruleInfo.endRuleIndex : startIdx);
317
- if (Number.isFinite(startIdx) && endIdx >= startIdx) {
318
- const deleteCount = endIdx - startIdx + 1;
319
- for (let idx = endIdx; idx >= startIdx; idx--) {
320
- if (idx < 0 || idx >= styleSheet.cssRules.length) continue;
321
- styleSheet.deleteRule(idx);
322
- }
323
- sheet.ruleCount = Math.max(0, sheet.ruleCount - deleteCount);
324
- this.adjustIndicesAfterDeletion(registry, ruleInfo.sheetIndex, startIdx, endIdx, deleteCount, ruleInfo);
325
- }
326
- }
327
- }
328
- if (this.config.devMode && texts.length) try {
329
- for (const text of texts) registry.ruleTextSet.delete(text);
330
- } catch {}
331
- } catch (error) {
332
- console.warn("Failed to delete CSS rule:", error);
333
- }
334
- }
335
- /**
336
- * Find a sheet with available space or return null
337
- */
338
- findAvailableSheet(registry, _root) {
339
- const maxRules = this.config.maxRulesPerSheet;
340
- if (!maxRules) return registry.sheets[registry.sheets.length - 1] || null;
341
- for (const sheet of registry.sheets) if (sheet.ruleCount < maxRules) return sheet;
342
- return null;
343
- }
344
- /**
345
- * Find an available rule index in the sheet
346
- */
347
- findAvailableRuleIndex(sheet) {
348
- return sheet.ruleCount;
349
- }
350
- /**
351
- * Schedule bulk cleanup of all unused styles (non-stacking)
352
- */
353
- scheduleBulkCleanup(registry) {
354
- if (registry.bulkCleanupTimeout) {
355
- if (this.config.idleCleanup && typeof cancelIdleCallback !== "undefined") cancelIdleCallback(registry.bulkCleanupTimeout);
356
- else clearTimeout(registry.bulkCleanupTimeout);
357
- registry.bulkCleanupTimeout = null;
358
- }
359
- const performCleanup = () => {
360
- this.performBulkCleanup(registry);
361
- registry.bulkCleanupTimeout = null;
362
- };
363
- if (this.config.idleCleanup && typeof requestIdleCallback !== "undefined") registry.bulkCleanupTimeout = requestIdleCallback(performCleanup);
364
- else {
365
- const delay = this.config.bulkCleanupDelay || 5e3;
366
- registry.bulkCleanupTimeout = setTimeout(performCleanup, delay);
367
- }
368
- }
369
- /**
370
- * Force cleanup of unused styles
371
- */
372
- forceCleanup(registry) {
373
- this.performBulkCleanup(registry, true);
374
- }
375
- /**
376
- * Perform bulk cleanup of all unused styles
377
- */
378
- performBulkCleanup(registry, cleanupAll = false) {
379
- const cleanupStartTime = Date.now();
380
- const unusedClassNames = Array.from(registry.refCounts.entries()).filter(([, refCount]) => refCount === 0).map(([className]) => className);
381
- if (unusedClassNames.length === 0) return;
382
- const candidates = unusedClassNames.map((className) => {
383
- return {
384
- className,
385
- ruleInfo: registry.rules.get(className)
386
- };
387
- });
388
- if (candidates.length === 0) return;
389
- let selected = candidates;
390
- if (!cleanupAll) {
391
- const ratio = this.config.bulkCleanupBatchRatio ?? .5;
392
- const limit = Math.max(1, Math.floor(candidates.length * Math.min(1, Math.max(0, ratio))));
393
- selected = candidates.slice(0, limit);
394
- }
395
- let cleanedUpCount = 0;
396
- let totalCssSize = 0;
397
- let totalRulesDeleted = 0;
398
- const rulesBySheet = /* @__PURE__ */ new Map();
399
- for (const { className, ruleInfo } of selected) {
400
- const sheetIndex = ruleInfo.sheetIndex;
401
- if (this.config.devMode && Array.isArray(ruleInfo.cssText)) {
402
- const cssSize = ruleInfo.cssText.reduce((total, css) => total + css.length, 0);
403
- totalCssSize += cssSize;
404
- totalRulesDeleted += ruleInfo.cssText.length;
405
- }
406
- if (!rulesBySheet.has(sheetIndex)) rulesBySheet.set(sheetIndex, []);
407
- rulesBySheet.get(sheetIndex).push({
408
- className,
409
- ruleInfo
410
- });
411
- }
412
- for (const [_sheetIndex, rulesInSheet] of rulesBySheet) {
413
- rulesInSheet.sort((a, b) => b.ruleInfo.ruleIndex - a.ruleInfo.ruleIndex);
414
- for (const { className, ruleInfo } of rulesInSheet) {
415
- if ((registry.refCounts.get(className) || 0) > 0) continue;
416
- if (registry.rules.get(className) !== ruleInfo) continue;
417
- const sheetInfo = registry.sheets[ruleInfo.sheetIndex];
418
- if (!sheetInfo || !sheetInfo.sheet) continue;
419
- const styleSheet = sheetInfo.sheet.sheet;
420
- if (!styleSheet) continue;
421
- const maxRuleIndex = styleSheet.cssRules.length - 1;
422
- const startIdx = ruleInfo.ruleIndex;
423
- const endIdx = ruleInfo.endRuleIndex ?? ruleInfo.ruleIndex;
424
- if (startIdx < 0 || endIdx > maxRuleIndex || startIdx > endIdx) continue;
425
- this.deleteRule(registry, ruleInfo);
426
- registry.rules.delete(className);
427
- registry.refCounts.delete(className);
428
- const keysToDelete = [];
429
- for (const [key, mappedClassName] of registry.cacheKeyToClassName.entries()) if (mappedClassName === className) keysToDelete.push(key);
430
- for (const key of keysToDelete) registry.cacheKeyToClassName.delete(key);
431
- cleanedUpCount++;
432
- }
433
- }
434
- if (registry.metrics) {
435
- registry.metrics.bulkCleanups++;
436
- registry.metrics.stylesCleanedUp += cleanedUpCount;
437
- registry.metrics.cleanupHistory.push({
438
- timestamp: cleanupStartTime,
439
- classesDeleted: cleanedUpCount,
440
- cssSize: totalCssSize,
441
- rulesDeleted: totalRulesDeleted
442
- });
443
- }
444
- }
445
- /**
446
- * Get total number of rules across all sheets
447
- */
448
- getTotalRuleCount(registry) {
449
- return registry.sheets.reduce((total, sheet) => total + sheet.ruleCount - sheet.holes.length, 0);
450
- }
451
- /**
452
- * Get CSS text from all sheets (for SSR)
453
- */
454
- getCssText(registry) {
455
- const cssChunks = [];
456
- for (const sheet of registry.sheets) try {
457
- const styleElement = sheet.sheet;
458
- if (styleElement.textContent) cssChunks.push(styleElement.textContent);
459
- else if (styleElement.sheet) {
460
- const rules = Array.from(styleElement.sheet.cssRules);
461
- cssChunks.push(rules.map((rule) => rule.cssText).join("\n"));
462
- }
463
- } catch (error) {
464
- console.warn("Failed to read CSS from sheet:", error);
465
- }
466
- return cssChunks.join("\n");
467
- }
468
- /**
469
- * Get cache performance metrics
470
- */
471
- getMetrics(registry) {
472
- if (!registry.metrics) return null;
473
- const unusedRulesCount = Array.from(registry.refCounts.values()).filter((count) => count === 0).length;
474
- return {
475
- ...registry.metrics,
476
- unusedHits: unusedRulesCount
477
- };
478
- }
479
- /**
480
- * Reset cache performance metrics
481
- */
482
- resetMetrics(registry) {
483
- if (registry.metrics) registry.metrics = {
484
- hits: 0,
485
- misses: 0,
486
- bulkCleanups: 0,
487
- totalInsertions: 0,
488
- totalUnused: 0,
489
- stylesCleanedUp: 0,
490
- cleanupHistory: [],
491
- startTime: Date.now()
492
- };
493
- }
494
- /**
495
- * Convert keyframes steps to CSS string.
496
- * Public so the SSR collector can format keyframes without DOM access.
497
- * Returns both the CSS text and a combined declarations string for property type scanning.
498
- */
499
- stepsToCSS(steps) {
500
- const rules = [];
501
- const allDeclarations = [];
502
- for (const [key, value] of Object.entries(steps)) {
503
- if (typeof value === "string") {
504
- rules.push(`${key} { ${value.trim()} }`);
505
- allDeclarations.push(value.trim());
506
- continue;
507
- }
508
- const styleMap = value || {};
509
- const styleNames = Object.keys(styleMap).sort();
510
- const handlerQueue = [];
511
- const seenHandlers = /* @__PURE__ */ new Set();
512
- styleNames.forEach((styleName) => {
513
- let handlers = STYLE_HANDLER_MAP[styleName];
514
- if (!handlers) handlers = STYLE_HANDLER_MAP[styleName] = [createStyle(styleName)];
515
- handlers.forEach((handler) => {
516
- if (!seenHandlers.has(handler)) {
517
- seenHandlers.add(handler);
518
- handlerQueue.push(handler);
519
- }
520
- });
521
- });
522
- const declarationPairs = [];
523
- handlerQueue.forEach((handler) => {
524
- const result = handler(handler.__lookupStyles.reduce((acc, name) => {
525
- const v = styleMap[name];
526
- if (v !== void 0) acc[name] = v;
527
- return acc;
528
- }, {}));
529
- if (!result) return;
530
- (Array.isArray(result) ? result : [result]).forEach((cssMap) => {
531
- if (!cssMap || typeof cssMap !== "object") return;
532
- const { $: _$, ...props } = cssMap;
533
- Object.entries(props).forEach(([prop, val]) => {
534
- if (val == null || val === "") return;
535
- if (Array.isArray(val)) val.forEach((v) => {
536
- if (v != null && v !== "") declarationPairs.push({
537
- prop,
538
- value: String(v)
539
- });
540
- });
541
- else declarationPairs.push({
542
- prop,
543
- value: String(val)
544
- });
545
- });
546
- });
547
- });
548
- const declarations = declarationPairs.map((d) => `${d.prop}: ${d.value}`).join("; ");
549
- rules.push(`${key} { ${declarations.trim()} }`);
550
- allDeclarations.push(declarations);
551
- }
552
- return {
553
- css: rules.join(" "),
554
- declarations: allDeclarations.join("; ")
555
- };
556
- }
557
- /**
558
- * Insert keyframes rule.
559
- * Returns the KeyframesInfo and the raw declarations string for property type scanning.
560
- */
561
- insertKeyframes(registry, steps, name, root) {
562
- let targetSheet = this.findAvailableSheet(registry, root);
563
- if (!targetSheet) targetSheet = this.createSheet(registry, root);
564
- const ruleIndex = this.findAvailableRuleIndex(targetSheet);
565
- const sheetIndex = registry.sheets.indexOf(targetSheet);
566
- try {
567
- const { css: cssSteps, declarations } = this.stepsToCSS(steps);
568
- const fullRule = `@keyframes ${name} { ${cssSteps} }`;
569
- const styleElement = targetSheet.sheet;
570
- const styleSheet = styleElement.sheet;
571
- if (styleSheet && !this.config.forceTextInjection) {
572
- const safeIndex = Math.min(Math.max(0, ruleIndex), styleSheet.cssRules.length);
573
- styleSheet.insertRule(fullRule, safeIndex);
574
- } else styleElement.textContent = (styleElement.textContent || "") + "\n" + fullRule;
575
- targetSheet.ruleCount++;
576
- return {
577
- info: {
578
- name,
579
- ruleIndex,
580
- sheetIndex,
581
- cssText: this.config.devMode ? fullRule : void 0
582
- },
583
- declarations
584
- };
585
- } catch (error) {
586
- console.warn("Failed to insert keyframes:", error);
587
- return null;
588
- }
589
- }
590
- /**
591
- * Delete keyframes rule
592
- */
593
- deleteKeyframes(registry, info) {
594
- const sheet = registry.sheets[info.sheetIndex];
595
- if (!sheet) return;
596
- try {
597
- const styleSheet = sheet.sheet.sheet;
598
- if (styleSheet) {
599
- if (info.ruleIndex >= 0 && info.ruleIndex < styleSheet.cssRules.length) {
600
- styleSheet.deleteRule(info.ruleIndex);
601
- sheet.ruleCount = Math.max(0, sheet.ruleCount - 1);
602
- this.adjustIndicesAfterDeletion(registry, info.sheetIndex, info.ruleIndex, info.ruleIndex, 1, {
603
- className: "",
604
- ruleIndex: info.ruleIndex,
605
- sheetIndex: info.sheetIndex
606
- }, [info.ruleIndex]);
607
- }
608
- }
609
- } catch (error) {
610
- console.warn("Failed to delete keyframes:", error);
611
- }
612
- }
613
- /**
614
- * Schedule async cleanup check (non-stacking)
615
- */
616
- checkCleanupNeeded(registry) {
617
- if (registry.cleanupCheckTimeout) {
618
- clearTimeout(registry.cleanupCheckTimeout);
619
- registry.cleanupCheckTimeout = null;
620
- }
621
- registry.cleanupCheckTimeout = setTimeout(() => {
622
- this.performCleanupCheck(registry);
623
- registry.cleanupCheckTimeout = null;
624
- }, 0);
625
- }
626
- /**
627
- * Perform the actual cleanup check (called asynchronously)
628
- */
629
- performCleanupCheck(registry) {
630
- if (Array.from(registry.refCounts.values()).filter((count) => count === 0).length >= (this.config.unusedStylesThreshold || 500)) this.scheduleBulkCleanup(registry);
631
- }
632
- /**
633
- * Clean up resources for a root
634
- */
635
- cleanup(root) {
636
- const registry = this.rootRegistries.get(root);
637
- if (!registry) return;
638
- if (registry.bulkCleanupTimeout) {
639
- if (this.config.idleCleanup && typeof cancelIdleCallback !== "undefined") cancelIdleCallback(registry.bulkCleanupTimeout);
640
- else clearTimeout(registry.bulkCleanupTimeout);
641
- registry.bulkCleanupTimeout = null;
642
- }
643
- if (registry.cleanupCheckTimeout) {
644
- clearTimeout(registry.cleanupCheckTimeout);
645
- registry.cleanupCheckTimeout = null;
646
- }
647
- for (const sheet of registry.sheets) try {
648
- const styleElement = sheet.sheet;
649
- if (styleElement.parentNode) styleElement.parentNode.removeChild(styleElement);
650
- } catch (error) {
651
- console.warn("Failed to cleanup sheet:", error);
652
- }
653
- this.rootRegistries.delete(root);
654
- const rawStyleElement = this.rawStyleElements.get(root);
655
- if (rawStyleElement?.parentNode) rawStyleElement.parentNode.removeChild(rawStyleElement);
656
- this.rawStyleElements.delete(root);
657
- this.rawCSSBlocks.delete(root);
658
- }
659
- /**
660
- * Get or create a dedicated style element for raw CSS
661
- * Raw CSS is kept separate from tasty-managed sheets to avoid index conflicts
662
- */
663
- getOrCreateRawStyleElement(root) {
664
- let styleElement = this.rawStyleElements.get(root);
665
- if (!styleElement) {
666
- styleElement = root.createElement?.("style") || document.createElement("style");
667
- if (this.config.nonce) styleElement.nonce = this.config.nonce;
668
- styleElement.setAttribute("data-tasty-raw", "");
669
- if ("head" in root && root.head) root.head.appendChild(styleElement);
670
- else if ("appendChild" in root) root.appendChild(styleElement);
671
- else document.head.appendChild(styleElement);
672
- this.rawStyleElements.set(root, styleElement);
673
- this.rawCSSBlocks.set(root, /* @__PURE__ */ new Map());
674
- }
675
- return styleElement;
676
- }
677
- /**
678
- * Inject raw CSS text directly without parsing
679
- * Returns a dispose function to remove the injected CSS
680
- */
681
- injectRawCSS(css, root) {
682
- if (!css.trim()) return { dispose: () => {} };
683
- const styleElement = this.getOrCreateRawStyleElement(root);
684
- const blocksMap = this.rawCSSBlocks.get(root);
685
- const id = `raw_${this.rawCSSCounter++}`;
686
- const currentContent = styleElement.textContent || "";
687
- const startOffset = currentContent.length;
688
- const cssWithNewline = (currentContent ? "\n" : "") + css;
689
- const endOffset = startOffset + cssWithNewline.length;
690
- styleElement.textContent = currentContent + cssWithNewline;
691
- const info = {
692
- id,
693
- css,
694
- startOffset,
695
- endOffset
696
- };
697
- blocksMap.set(id, info);
698
- return { dispose: () => {
699
- this.disposeRawCSS(id, root);
700
- } };
701
- }
702
- /**
703
- * Remove a raw CSS block by ID
704
- */
705
- disposeRawCSS(id, root) {
706
- const styleElement = this.rawStyleElements.get(root);
707
- const blocksMap = this.rawCSSBlocks.get(root);
708
- if (!styleElement || !blocksMap) return;
709
- if (!blocksMap.get(id)) return;
710
- blocksMap.delete(id);
711
- const remainingBlocks = Array.from(blocksMap.values());
712
- if (remainingBlocks.length === 0) styleElement.textContent = "";
713
- else {
714
- remainingBlocks.sort((a, b) => a.startOffset - b.startOffset);
715
- styleElement.textContent = remainingBlocks.map((block) => block.css).join("\n");
716
- let offset = 0;
717
- for (const block of remainingBlocks) {
718
- block.startOffset = offset;
719
- block.endOffset = offset + block.css.length;
720
- offset = block.endOffset + 1;
721
- }
722
- }
723
- }
724
- /**
725
- * Get the raw CSS content for SSR
726
- */
727
- getRawCSSText(root) {
728
- return this.rawStyleElements.get(root)?.textContent || "";
729
- }
730
- };
731
-
732
- //#endregion
733
- export { SheetManager };
734
- //# sourceMappingURL=sheet-manager.js.map