@tenphi/tasty 0.13.1 → 0.14.1

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 (68) hide show
  1. package/README.md +155 -51
  2. package/dist/config.d.ts +13 -1
  3. package/dist/config.js +5 -1
  4. package/dist/config.js.map +1 -1
  5. package/dist/core/index.d.ts +5 -3
  6. package/dist/core/index.js +4 -3
  7. package/dist/debug.d.ts +26 -141
  8. package/dist/debug.js +356 -635
  9. package/dist/debug.js.map +1 -1
  10. package/dist/index.d.ts +5 -3
  11. package/dist/index.js +4 -3
  12. package/dist/parser/classify.js +2 -1
  13. package/dist/parser/classify.js.map +1 -1
  14. package/dist/parser/parser.js +1 -1
  15. package/dist/plugins/okhsl-plugin.js +2 -275
  16. package/dist/plugins/okhsl-plugin.js.map +1 -1
  17. package/dist/plugins/types.d.ts +1 -1
  18. package/dist/properties/index.js +2 -15
  19. package/dist/properties/index.js.map +1 -1
  20. package/dist/ssr/format-property.js +9 -7
  21. package/dist/ssr/format-property.js.map +1 -1
  22. package/dist/styles/color.js +9 -5
  23. package/dist/styles/color.js.map +1 -1
  24. package/dist/styles/createStyle.js +24 -21
  25. package/dist/styles/createStyle.js.map +1 -1
  26. package/dist/styles/index.js +1 -1
  27. package/dist/styles/predefined.js +1 -1
  28. package/dist/styles/predefined.js.map +1 -1
  29. package/dist/styles/types.d.ts +19 -4
  30. package/dist/tasty.d.ts +9 -9
  31. package/dist/tasty.js +1 -1
  32. package/dist/types.d.ts +1 -1
  33. package/dist/utils/color-math.d.ts +46 -0
  34. package/dist/utils/color-math.js +749 -0
  35. package/dist/utils/color-math.js.map +1 -0
  36. package/dist/utils/color-space.d.ts +5 -0
  37. package/dist/utils/color-space.js +229 -0
  38. package/dist/utils/color-space.js.map +1 -0
  39. package/dist/utils/colors.js +3 -1
  40. package/dist/utils/colors.js.map +1 -1
  41. package/dist/utils/mod-attrs.js +1 -1
  42. package/dist/utils/mod-attrs.js.map +1 -1
  43. package/dist/utils/process-tokens.d.ts +3 -13
  44. package/dist/utils/process-tokens.js +18 -98
  45. package/dist/utils/process-tokens.js.map +1 -1
  46. package/dist/utils/styles.d.ts +2 -15
  47. package/dist/utils/styles.js +22 -217
  48. package/dist/utils/styles.js.map +1 -1
  49. package/docs/PIPELINE.md +519 -0
  50. package/docs/README.md +31 -0
  51. package/docs/adoption.md +16 -6
  52. package/docs/comparison.md +16 -9
  53. package/docs/configuration.md +26 -3
  54. package/docs/debug.md +152 -339
  55. package/docs/design-system.md +1 -1
  56. package/docs/dsl.md +3 -1
  57. package/docs/getting-started.md +29 -13
  58. package/docs/injector.md +2 -2
  59. package/docs/methodology.md +3 -1
  60. package/docs/runtime.md +59 -9
  61. package/docs/ssr.md +3 -3
  62. package/docs/styles.md +1 -1
  63. package/docs/tasty-static.md +13 -2
  64. package/package.json +4 -3
  65. package/dist/utils/hsl-to-rgb.js +0 -38
  66. package/dist/utils/hsl-to-rgb.js.map +0 -1
  67. package/dist/utils/okhsl-to-rgb.js +0 -296
  68. package/dist/utils/okhsl-to-rgb.js.map +0 -1
package/dist/debug.js CHANGED
@@ -3,729 +3,450 @@ import { CHUNK_NAMES } from "./chunks/definitions.js";
3
3
  import { getCssTextForNode, injector } from "./injector/index.js";
4
4
 
5
5
  //#region src/debug.ts
6
- /**
7
- * Debug utilities for inspecting tasty-generated CSS at runtime
8
- */
9
- /**
10
- * Pretty-print CSS with proper indentation and formatting
11
- */
12
- function prettifyCSS(css) {
13
- if (!css || css.trim() === "") return "";
14
- let formatted = css.replace(/\s+/g, " ").trim();
15
- formatted = formatted.replace(/\s*\{\s*/g, " {\n");
16
- formatted = formatted.replace(/;(?![^"']*["'][^"']*$)(?![^()]*\))/g, ";\n");
17
- formatted = formatted.replace(/\s*\}\s*/g, "\n}\n");
18
- formatted = formatted.replace(/,(?![^"']*["'][^"']*$)(?![^()]*\))(?=.*:.*\{|.*\{)/g, ",\n");
19
- const lines = formatted.split("\n");
20
- let indentLevel = 0;
21
- const indentSize = 2;
22
- let result = lines.map((line) => {
23
- const trimmed = line.trim();
24
- if (!trimmed) return "";
25
- if (trimmed === "}") {
26
- indentLevel = Math.max(0, indentLevel - 1);
27
- return " ".repeat(indentLevel * indentSize) + trimmed;
28
- }
29
- const result = " ".repeat(indentLevel * indentSize) + trimmed;
30
- if (trimmed.endsWith("{")) indentLevel++;
31
- return result;
32
- }).filter((line) => line.trim()).join("\n").replace(/\n{3,}/g, "\n\n").trim();
33
- result = result.replace(/,\s+/g, ", ");
34
- return result;
6
+ function fmtSize(bytes) {
7
+ return bytes > 1024 ? `${(bytes / 1024).toFixed(1)}KB` : `${bytes}B`;
35
8
  }
36
- function findAllTastyClasses(root = document) {
37
- const classes = /* @__PURE__ */ new Set();
38
- (root.querySelectorAll?.("[class]") || []).forEach((element) => {
39
- const classList = element.getAttribute("class");
40
- if (classList) classList.split(/\s+/).filter((cls) => /^t\d+$/.test(cls)).forEach((cls) => classes.add(cls));
41
- });
42
- return Array.from(classes).sort((a, b) => {
43
- return parseInt(a.slice(1)) - parseInt(b.slice(1));
44
- });
9
+ function countRules(css) {
10
+ return (css.match(/\{[^}]*\}/g) || []).length;
45
11
  }
46
- function findAllStyledClasses(root = document) {
47
- const allCSS = injector.instance.getCssText({ root });
48
- const classes = /* @__PURE__ */ new Set();
49
- const classRegex = /\.t(\d+)/g;
50
- let match;
51
- while ((match = classRegex.exec(allCSS)) !== null) classes.add(`t${match[1]}`);
52
- return Array.from(classes).sort((a, b) => {
53
- return parseInt(a.slice(1)) - parseInt(b.slice(1));
54
- });
12
+ function sortTastyClasses(classes) {
13
+ return Array.from(classes).sort((a, b) => parseInt(a.slice(1)) - parseInt(b.slice(1)));
55
14
  }
56
- function extractCSSRules(css) {
57
- const rules = [];
58
- const cleanCSS = css.replace(/\/\*[\s\S]*?\*\//g, "");
59
- let i = 0;
60
- while (i < cleanCSS.length) {
61
- while (i < cleanCSS.length && /\s/.test(cleanCSS[i])) i++;
62
- if (i >= cleanCSS.length) break;
63
- const selectorStart = i;
64
- let braceDepth = 0;
65
- let inString = false;
66
- let stringChar = "";
67
- while (i < cleanCSS.length) {
68
- const char = cleanCSS[i];
69
- if (inString) {
70
- if (char === stringChar && cleanCSS[i - 1] !== "\\") inString = false;
71
- } else if (char === "\"" || char === "'") {
72
- inString = true;
73
- stringChar = char;
74
- } else if (char === "{") {
75
- braceDepth++;
76
- if (braceDepth === 1) break;
77
- }
78
- i++;
79
- }
80
- if (i >= cleanCSS.length) break;
81
- const selector = cleanCSS.substring(selectorStart, i).trim();
82
- i++;
83
- const contentStart = i;
84
- braceDepth = 1;
85
- inString = false;
86
- while (i < cleanCSS.length && braceDepth > 0) {
87
- const char = cleanCSS[i];
88
- if (inString) {
89
- if (char === stringChar && cleanCSS[i - 1] !== "\\") inString = false;
90
- } else if (char === "\"" || char === "'") {
91
- inString = true;
92
- stringChar = char;
93
- } else if (char === "{") braceDepth++;
94
- else if (char === "}") braceDepth--;
95
- i++;
96
- }
97
- const content = cleanCSS.substring(contentStart, i - 1).trim();
98
- if (content && selector) rules.push({
99
- selector,
100
- declarations: content
101
- });
102
- }
103
- return rules;
15
+ function getRegistry(root = document) {
16
+ return injector.instance._sheetManager?.getRegistry(root);
104
17
  }
105
- function getGlobalCSS(root = document) {
106
- return prettifyCSS(extractCSSRules(injector.instance.getCssText({ root })).filter((rule) => {
107
- return !rule.selector.split(",").map((s) => s.trim()).every((selector) => {
108
- const parts = selector.replace(/[.#:\s>+~[\]()]/g, " ").split(/\s+/).filter(Boolean);
109
- return parts.length > 0 && parts.every((part) => /^t\d+$/.test(part));
110
- });
111
- }).map((rule) => `${rule.selector} { ${rule.declarations} }`).join("\n"));
18
+ function getUnusedClasses(root = document) {
19
+ const registry = getRegistry(root);
20
+ if (!registry) return [];
21
+ const result = [];
22
+ for (const [cls, rc] of registry.refCounts) if (rc === 0) result.push(cls);
23
+ return sortTastyClasses(result);
112
24
  }
113
- function getPageCSS(options) {
114
- const root = options?.root || document;
115
- const includeCrossOrigin = options?.includeCrossOrigin ?? false;
116
- const cssChunks = [];
117
- try {
118
- if ("styleSheets" in root) {
119
- const styleSheets = Array.from(root.styleSheets);
120
- for (const sheet of styleSheets) try {
121
- if (sheet.cssRules) {
122
- const rules = Array.from(sheet.cssRules);
123
- cssChunks.push(rules.map((rule) => rule.cssText).join("\n"));
124
- }
125
- } catch {
126
- if (includeCrossOrigin) cssChunks.push(`/* Cross-origin stylesheet: ${sheet.href || "inline"} */`);
127
- }
25
+ function findDomTastyClasses(root = document) {
26
+ const classes = /* @__PURE__ */ new Set();
27
+ (root.querySelectorAll?.("[class]") || []).forEach((el) => {
28
+ const attr = el.getAttribute("class");
29
+ if (attr) {
30
+ for (const cls of attr.split(/\s+/)) if (/^t\d+$/.test(cls)) classes.add(cls);
128
31
  }
129
- } catch {}
130
- return cssChunks.join("\n");
32
+ });
33
+ return sortTastyClasses(classes);
131
34
  }
132
- function getPageStats(options) {
133
- const root = options?.root || document;
134
- options?.includeCrossOrigin;
135
- let cssSize = 0;
136
- let ruleCount = 0;
137
- let stylesheetCount = 0;
138
- let skippedStylesheets = 0;
139
- try {
140
- if ("styleSheets" in root) {
141
- const styleSheets = Array.from(root.styleSheets);
142
- stylesheetCount = styleSheets.length;
143
- for (const sheet of styleSheets) try {
144
- if (sheet.cssRules) {
145
- const rules = Array.from(sheet.cssRules);
146
- ruleCount += rules.length;
147
- cssSize += rules.reduce((sum, rule) => sum + rule.cssText.length, 0);
148
- }
149
- } catch {
150
- skippedStylesheets++;
151
- }
35
+ function prettifyCSS(css) {
36
+ if (!css || !css.trim()) return "";
37
+ const out = [];
38
+ let depth = 0;
39
+ const indent = () => " ".repeat(depth);
40
+ let normalized = css.replace(/\s+/g, " ").trim();
41
+ normalized = normalized.replace(/\s*\{\s*/g, " { ");
42
+ normalized = normalized.replace(/\s*\}\s*/g, " } ");
43
+ normalized = normalized.replace(/;\s*/g, "; ");
44
+ const tokens = normalized.split(/\s+/);
45
+ let buf = "";
46
+ for (const t of tokens) if (t === "{") {
47
+ const header = buf.trim();
48
+ if (header) {
49
+ const parts = splitOutsideParens(header, ",");
50
+ if (parts.length > 1) out.push(parts.map((p, idx) => idx === 0 ? `${indent()}${p.trim()},` : `${indent()}${p.trim()}${idx < parts.length - 1 ? "," : ""}`).join("\n") + " {");
51
+ else out.push(`${indent()}${header} {`);
52
+ } else out.push(`${indent()}{`);
53
+ depth++;
54
+ buf = "";
55
+ } else if (t === "}") {
56
+ if (buf.trim()) {
57
+ for (const decl of buf.split(";").filter((s) => s.trim())) out.push(`${indent()}${decl.trim()};`);
58
+ buf = "";
152
59
  }
153
- } catch {}
154
- return {
155
- cssSize,
156
- ruleCount,
157
- stylesheetCount,
158
- skippedStylesheets
159
- };
60
+ depth = Math.max(0, depth - 1);
61
+ out.push(`${indent()}}`);
62
+ } else if (t.endsWith(";")) {
63
+ buf += ` ${t}`;
64
+ const full = buf.trim();
65
+ if (full) out.push(`${indent()}${full}`);
66
+ buf = "";
67
+ } else buf += ` ${t}`;
68
+ if (buf.trim()) out.push(buf.trim());
69
+ return out.filter((l) => l.trim()).join("\n").replace(/\n{3,}/g, "\n\n").trim();
70
+ }
71
+ /** Split `str` by `sep` only when not inside parentheses */
72
+ function splitOutsideParens(str, sep) {
73
+ const parts = [];
74
+ let depth = 0;
75
+ let start = 0;
76
+ for (let i = 0; i < str.length; i++) {
77
+ const ch = str[i];
78
+ if (ch === "(") depth++;
79
+ else if (ch === ")") depth--;
80
+ else if (depth === 0 && str.startsWith(sep, i)) {
81
+ parts.push(str.slice(start, i));
82
+ start = i + sep.length;
83
+ }
84
+ }
85
+ parts.push(str.slice(start));
86
+ return parts;
160
87
  }
161
- /**
162
- * Extract chunk name from a cache key.
163
- *
164
- * Cache keys have the format: "chunkName\0key:value\0key:value..."
165
- * or "[states:...]\0chunkName\0..." for predefined states.
166
- *
167
- * @param cacheKey - The cache key to parse
168
- * @returns The chunk name, or null if not found
169
- */
170
- function extractChunkNameFromCacheKey(cacheKey) {
171
- const parts = cacheKey.split("\0");
172
- for (const part of parts) {
88
+ function extractChunkName(cacheKey) {
89
+ for (const part of cacheKey.split("\0")) {
173
90
  if (part.startsWith("[states:")) continue;
174
91
  if (!part.includes(":") && part.length > 0) return part;
175
92
  }
176
93
  return null;
177
94
  }
178
- /**
179
- * Get chunk info for a className by reverse-looking up its cache key.
180
- *
181
- * @param className - The tasty class name (e.g., "t0", "t123")
182
- * @param root - The document or shadow root to search in
183
- * @returns Object with chunk name and cache key, or nulls if not found
184
- */
185
- function getChunkForClassName(className, root = document) {
186
- const registry = injector.instance._sheetManager?.getRegistry(root);
187
- if (!registry) return {
188
- chunkName: null,
189
- cacheKey: null
190
- };
191
- for (const [cacheKey, cn] of registry.cacheKeyToClassName) if (cn === className) return {
192
- chunkName: extractChunkNameFromCacheKey(cacheKey),
193
- cacheKey
194
- };
195
- return {
196
- chunkName: null,
197
- cacheKey: null
198
- };
95
+ function getChunkForClass(className, root = document) {
96
+ const registry = getRegistry(root);
97
+ if (!registry) return null;
98
+ for (const [key, cn] of registry.cacheKeyToClassName) if (cn === className) return extractChunkName(key);
99
+ return null;
199
100
  }
200
- /**
201
- * Get chunk breakdown statistics for all styles.
202
- *
203
- * @param root - The document or shadow root to search in
204
- * @returns Object with breakdown by chunk type and totals
205
- */
206
- function getChunkBreakdown(root = document) {
207
- const registry = injector.instance._sheetManager?.getRegistry(root);
101
+ function buildChunkBreakdown(root = document) {
102
+ const registry = getRegistry(root);
208
103
  if (!registry) return {
209
104
  byChunk: {},
210
- totalChunkTypes: 0
105
+ totalChunkTypes: 0,
106
+ totalClasses: 0
211
107
  };
212
108
  const byChunk = {};
213
109
  for (const [cacheKey, className] of registry.cacheKeyToClassName) {
214
- const chunkName = extractChunkNameFromCacheKey(cacheKey) || "unknown";
215
- if (!byChunk[chunkName]) byChunk[chunkName] = {
110
+ const chunk = extractChunkName(cacheKey) || "unknown";
111
+ if (!byChunk[chunk]) byChunk[chunk] = {
216
112
  classes: [],
217
113
  cssSize: 0,
218
114
  ruleCount: 0
219
115
  };
220
- byChunk[chunkName].classes.push(className);
116
+ byChunk[chunk].classes.push(className);
221
117
  const css = injector.instance.getCssTextForClasses([className], { root });
222
- byChunk[chunkName].cssSize += css.length;
223
- byChunk[chunkName].ruleCount += (css.match(/\{[^}]*\}/g) || []).length;
118
+ byChunk[chunk].cssSize += css.length;
119
+ byChunk[chunk].ruleCount += countRules(css);
224
120
  }
225
- for (const entry of Object.values(byChunk)) entry.classes.sort((a, b) => {
226
- return parseInt(a.slice(1)) - parseInt(b.slice(1));
227
- });
121
+ for (const entry of Object.values(byChunk)) entry.classes = sortTastyClasses(entry.classes);
122
+ const totalClasses = Object.values(byChunk).reduce((s, e) => s + e.classes.length, 0);
228
123
  return {
229
124
  byChunk,
230
- totalChunkTypes: Object.keys(byChunk).length
125
+ totalChunkTypes: Object.keys(byChunk).length,
126
+ totalClasses
231
127
  };
232
128
  }
233
- /**
234
- * Concise tastyDebug API for inspecting styles at runtime
235
- */
129
+ function getGlobalTypeCSS(type, root = document) {
130
+ const registry = getRegistry(root);
131
+ if (!registry) return {
132
+ css: "",
133
+ ruleCount: 0,
134
+ size: 0
135
+ };
136
+ const chunks = [];
137
+ let rc = 0;
138
+ if (type === "keyframes") for (const [, entry] of registry.keyframesCache) {
139
+ const info = entry.info;
140
+ const ss = registry.sheets[info.sheetIndex]?.sheet?.sheet;
141
+ if (ss && info.ruleIndex < ss.cssRules.length) {
142
+ const rule = ss.cssRules[info.ruleIndex];
143
+ if (rule) {
144
+ chunks.push(rule.cssText);
145
+ rc++;
146
+ }
147
+ } else if (info.cssText) {
148
+ chunks.push(info.cssText);
149
+ rc++;
150
+ }
151
+ }
152
+ else {
153
+ const prefix = type === "global" ? "global:" : type === "raw" ? "raw:" : "property:";
154
+ for (const [key, ri] of registry.globalRules) {
155
+ if (!key.startsWith(prefix)) continue;
156
+ const ss = registry.sheets[ri.sheetIndex]?.sheet?.sheet;
157
+ if (ss) {
158
+ const start = Math.max(0, ri.ruleIndex);
159
+ const end = Math.min(ss.cssRules.length - 1, ri.endRuleIndex ?? ri.ruleIndex);
160
+ if (start >= 0 && end >= start && start < ss.cssRules.length) for (let i = start; i <= end; i++) {
161
+ const rule = ss.cssRules[i];
162
+ if (rule) {
163
+ chunks.push(rule.cssText);
164
+ rc++;
165
+ }
166
+ }
167
+ } else if (ri.cssText?.length) {
168
+ chunks.push(...ri.cssText);
169
+ rc += ri.cssText.length;
170
+ }
171
+ }
172
+ }
173
+ const raw = chunks.join("\n");
174
+ return {
175
+ css: prettifyCSS(raw),
176
+ ruleCount: rc,
177
+ size: raw.length
178
+ };
179
+ }
180
+ function getSourceCssForClasses(classNames, root = document) {
181
+ const registry = getRegistry(root);
182
+ if (!registry) return null;
183
+ const chunks = [];
184
+ let found = false;
185
+ for (const cls of classNames) {
186
+ const info = registry.rules.get(cls);
187
+ if (info?.cssText?.length) {
188
+ chunks.push(...info.cssText);
189
+ found = true;
190
+ }
191
+ }
192
+ return found ? chunks.join("\n") : null;
193
+ }
194
+ function getDefs(root = document) {
195
+ const registry = getRegistry(root);
196
+ let properties = [];
197
+ if (registry?.injectedProperties) properties = Array.from(registry.injectedProperties.keys()).sort();
198
+ const keyframes = [];
199
+ if (registry) {
200
+ for (const entry of registry.keyframesCache.values()) keyframes.push({
201
+ name: entry.name,
202
+ refCount: entry.refCount
203
+ });
204
+ keyframes.sort((a, b) => a.name.localeCompare(b.name));
205
+ }
206
+ return {
207
+ properties,
208
+ keyframes
209
+ };
210
+ }
211
+ const CHUNK_ORDER = [
212
+ CHUNK_NAMES.COMBINED,
213
+ CHUNK_NAMES.APPEARANCE,
214
+ CHUNK_NAMES.FONT,
215
+ CHUNK_NAMES.DIMENSION,
216
+ CHUNK_NAMES.DISPLAY,
217
+ CHUNK_NAMES.LAYOUT,
218
+ CHUNK_NAMES.POSITION,
219
+ CHUNK_NAMES.MISC,
220
+ CHUNK_NAMES.SUBCOMPONENTS
221
+ ];
236
222
  const tastyDebug = {
237
223
  css(target, opts) {
238
- const { root = document, prettify = true, log = false } = opts || {};
224
+ const { root = document, prettify = true, raw = false, source = false } = opts || {};
239
225
  let css = "";
240
- if (typeof target === "string") if (target === "all") css = injector.instance.getCssText({ root });
241
- else if (target === "global") css = getGlobalCSS(root);
242
- else if (target === "active") {
243
- const activeClasses = findAllTastyClasses(root);
244
- css = injector.instance.getCssTextForClasses(activeClasses, { root });
226
+ if (source && typeof target === "string" && /^t\d+$/.test(target)) {
227
+ const src = getSourceCssForClasses([target], root);
228
+ if (src) css = src;
229
+ else {
230
+ if (!raw) console.warn("tastyDebug: source CSS not available (requires dev mode or TASTY_DEBUG=true). Falling back to live CSSOM.");
231
+ css = injector.instance.getCssTextForClasses([target], { root });
232
+ }
233
+ } else if (source && Array.isArray(target)) {
234
+ const src = getSourceCssForClasses(target, root);
235
+ if (src) css = src;
236
+ else {
237
+ if (!raw) console.warn("tastyDebug: source CSS not available. Falling back to live CSSOM.");
238
+ css = injector.instance.getCssTextForClasses(target, { root });
239
+ }
240
+ } else if (typeof target === "string") if (target === "all") css = injector.instance.getCssText({ root });
241
+ else if (target === "global") {
242
+ css = getGlobalTypeCSS("global", root).css;
243
+ return css;
244
+ } else if (target === "active") {
245
+ const active = findDomTastyClasses(root);
246
+ css = injector.instance.getCssTextForClasses(active, { root });
245
247
  } else if (target === "unused") {
246
- const registry = injector.instance._sheetManager?.getRegistry(root);
247
- const unusedClasses = registry ? Array.from(registry.refCounts.entries()).filter(([, refCount]) => refCount === 0).map(([className]) => className) : [];
248
- css = injector.instance.getCssTextForClasses(unusedClasses, { root });
249
- } else if (target === "page") css = getPageCSS({
250
- root,
251
- includeCrossOrigin: true
252
- });
248
+ const unused = getUnusedClasses(root);
249
+ css = injector.instance.getCssTextForClasses(unused, { root });
250
+ } else if (target === "page") css = getPageCSS(root);
253
251
  else if (/^t\d+$/.test(target)) css = injector.instance.getCssTextForClasses([target], { root });
254
252
  else {
255
- const element = root.querySelector?.(target);
256
- if (element) css = getCssTextForNode(element, { root });
253
+ const el = root.querySelector?.(target);
254
+ if (el) css = getCssTextForNode(el, { root });
257
255
  }
258
256
  else if (Array.isArray(target)) css = injector.instance.getCssTextForClasses(target, { root });
259
257
  else if (target instanceof Element) css = getCssTextForNode(target, { root });
260
258
  const result = prettify ? prettifyCSS(css) : css;
261
- if (log) {
262
- console.group(`🎨 CSS for ${Array.isArray(target) ? `[${target.join(", ")}]` : target}`);
259
+ if (!raw) {
260
+ const label = Array.isArray(target) ? `[${target.join(", ")}]` : target;
261
+ const rc = countRules(css);
262
+ console.group(`CSS for ${label} (${rc} rules, ${fmtSize(css.length)})`);
263
263
  console.log(result || "(empty)");
264
264
  console.groupEnd();
265
265
  }
266
266
  return result;
267
267
  },
268
268
  inspect(target, opts) {
269
- const { root = document } = opts || {};
269
+ const { root = document, raw = false } = opts || {};
270
270
  const element = typeof target === "string" ? root.querySelector?.(target) : target;
271
- if (!element) return {
272
- element: null,
273
- classes: [],
274
- chunks: [],
275
- css: "",
276
- size: 0,
277
- rules: 0
278
- };
271
+ if (!element) {
272
+ const empty = {
273
+ element: null,
274
+ classes: [],
275
+ chunks: [],
276
+ css: "",
277
+ size: 0,
278
+ rules: 0
279
+ };
280
+ if (!raw) console.warn("tastyDebug.inspect: element not found");
281
+ return empty;
282
+ }
279
283
  const tastyClasses = (element.getAttribute("class") || "").split(/\s+/).filter((cls) => /^t\d+$/.test(cls));
280
284
  const chunks = tastyClasses.map((className) => ({
281
285
  className,
282
- chunkName: getChunkForClassName(className, root).chunkName
286
+ chunkName: getChunkForClass(className, root)
283
287
  }));
284
288
  const css = getCssTextForNode(element, { root });
285
- const prettifiedCSS = prettifyCSS(css);
286
- const ruleCount = (css.match(/\{[^}]*\}/g) || []).length;
287
- return {
289
+ const rules = countRules(css);
290
+ const result = {
288
291
  element,
289
292
  classes: tastyClasses,
290
293
  chunks,
291
- css: prettifiedCSS,
294
+ css: prettifyCSS(css),
292
295
  size: css.length,
293
- rules: ruleCount
296
+ rules
294
297
  };
295
- },
296
- cache(opts) {
297
- const { root = document } = opts || {};
298
- const activeClasses = findAllTastyClasses(root);
299
- findAllStyledClasses(root);
300
- const registry = injector.instance._sheetManager?.getRegistry(root);
301
- const unusedClasses = registry ? Array.from(registry.refCounts.entries()).filter(([, refCount]) => refCount === 0).map(([className]) => className) : [];
302
- return {
303
- classes: {
304
- active: activeClasses,
305
- unused: unusedClasses,
306
- all: [...activeClasses, ...unusedClasses]
307
- },
308
- metrics: injector.instance.getMetrics({ root })
309
- };
310
- },
311
- cleanup(opts) {
312
- const { root } = opts || {};
313
- injector.instance.cleanup(root);
314
- },
315
- metrics(opts) {
316
- const { root } = opts || {};
317
- return injector.instance.getMetrics({ root });
318
- },
319
- resetMetrics(opts) {
320
- const { root } = opts || {};
321
- injector.instance.resetMetrics({ root });
322
- },
323
- chunks(opts) {
324
- const { root = document, log = false } = opts || {};
325
- const breakdown = getChunkBreakdown(root);
326
- const totalClasses = Object.values(breakdown.byChunk).reduce((sum, entry) => sum + entry.classes.length, 0);
327
- if (log) {
328
- console.group("🧩 Style Chunk Breakdown");
329
- const displayOrder = [
330
- CHUNK_NAMES.COMBINED,
331
- CHUNK_NAMES.APPEARANCE,
332
- CHUNK_NAMES.FONT,
333
- CHUNK_NAMES.DIMENSION,
334
- CHUNK_NAMES.DISPLAY,
335
- CHUNK_NAMES.LAYOUT,
336
- CHUNK_NAMES.POSITION,
337
- CHUNK_NAMES.MISC,
338
- CHUNK_NAMES.SUBCOMPONENTS
339
- ];
340
- for (const chunkName of displayOrder) {
341
- const data = breakdown.byChunk[chunkName];
342
- if (data) {
343
- const sizeStr = data.cssSize > 1024 ? `${(data.cssSize / 1024).toFixed(1)}KB` : `${data.cssSize}B`;
344
- console.log(` • ${chunkName}: ${data.classes.length} classes, ${sizeStr}, ${data.ruleCount} rules`);
345
- }
346
- }
347
- for (const [chunkName, data] of Object.entries(breakdown.byChunk)) if (!displayOrder.includes(chunkName)) {
348
- const sizeStr = data.cssSize > 1024 ? `${(data.cssSize / 1024).toFixed(1)}KB` : `${data.cssSize}B`;
349
- console.log(` • ${chunkName}: ${data.classes.length} classes, ${sizeStr}, ${data.ruleCount} rules`);
350
- }
351
- console.log(`📊 Total: ${totalClasses} classes across ${breakdown.totalChunkTypes} chunk types`);
298
+ if (!raw) {
299
+ const tag = element.tagName.toLowerCase();
300
+ const id = element.id ? `#${element.id}` : "";
301
+ console.group(`inspect ${tag}${id} ${tastyClasses.length} classes, ${rules} rules, ${fmtSize(css.length)}`);
302
+ if (chunks.length) console.log("Chunks:", chunks.map((c) => `${c.className}→${c.chunkName || "?"}`).join(", "));
303
+ console.groupCollapsed("CSS");
304
+ console.log(result.css || "(empty)");
305
+ console.groupEnd();
352
306
  console.groupEnd();
353
307
  }
354
- return {
355
- ...breakdown,
356
- totalClasses
357
- };
358
- },
359
- getGlobalTypeCSS(type, opts) {
360
- const { root = document } = opts || {};
361
- const registry = injector.instance._sheetManager?.getRegistry(root);
362
- if (!registry) return {
363
- css: "",
364
- ruleCount: 0,
365
- size: 0
366
- };
367
- const cssChunks = [];
368
- let ruleCount = 0;
369
- if (type === "keyframes") for (const [, entry] of registry.keyframesCache) {
370
- const info = entry.info;
371
- const styleSheet = registry.sheets[info.sheetIndex]?.sheet?.sheet;
372
- if (styleSheet && info.ruleIndex < styleSheet.cssRules.length) {
373
- const rule = styleSheet.cssRules[info.ruleIndex];
374
- if (rule) {
375
- cssChunks.push(rule.cssText);
376
- ruleCount++;
377
- }
378
- } else if (info.cssText) {
379
- cssChunks.push(info.cssText);
380
- ruleCount++;
381
- }
382
- }
383
- else {
384
- const prefix = type === "global" ? "global:" : type === "raw" ? "raw:" : "property:";
385
- for (const [key, ruleInfo] of registry.globalRules) if (key.startsWith(prefix)) {
386
- const styleSheet = registry.sheets[ruleInfo.sheetIndex]?.sheet?.sheet;
387
- if (styleSheet) {
388
- const start = Math.max(0, ruleInfo.ruleIndex);
389
- const end = Math.min(styleSheet.cssRules.length - 1, ruleInfo.endRuleIndex ?? ruleInfo.ruleIndex);
390
- if (start >= 0 && end >= start && start < styleSheet.cssRules.length) for (let i = start; i <= end; i++) {
391
- const rule = styleSheet.cssRules[i];
392
- if (rule) {
393
- cssChunks.push(rule.cssText);
394
- ruleCount++;
395
- }
396
- }
397
- } else if (ruleInfo.cssText && ruleInfo.cssText.length) {
398
- cssChunks.push(...ruleInfo.cssText);
399
- ruleCount += ruleInfo.cssText.length;
400
- }
401
- }
402
- }
403
- const rawCSS = cssChunks.join("\n");
404
- return {
405
- css: prettifyCSS(rawCSS),
406
- ruleCount,
407
- size: rawCSS.length
408
- };
409
- },
410
- defs(opts) {
411
- const { root = document } = opts || {};
412
- let properties = [];
413
- try {
414
- const registry = injector.instance._sheetManager?.getRegistry(root);
415
- if (registry?.injectedProperties) properties = Array.from(registry.injectedProperties.keys()).sort();
416
- } catch {
417
- const allCSS = injector.instance.getCssText({ root });
418
- const propRegex = /@property\s+(--[a-z0-9-]+)/gi;
419
- const propSet = /* @__PURE__ */ new Set();
420
- let match;
421
- while ((match = propRegex.exec(allCSS)) !== null) propSet.add(match[1]);
422
- properties = Array.from(propSet).sort();
423
- }
424
- let keyframes = [];
425
- try {
426
- const registry = injector.instance._sheetManager?.getRegistry(root);
427
- if (registry) {
428
- for (const entry of registry.keyframesCache.values()) keyframes.push({
429
- name: entry.name,
430
- refCount: entry.refCount
431
- });
432
- keyframes.sort((a, b) => a.name.localeCompare(b.name));
433
- }
434
- } catch {
435
- const allCSS = injector.instance.getCssText({ root });
436
- const keyframesRegex = /@keyframes\s+([a-zA-Z0-9_-]+)/gi;
437
- const keyframesSet = /* @__PURE__ */ new Set();
438
- let match;
439
- while ((match = keyframesRegex.exec(allCSS)) !== null) keyframesSet.add(match[1]);
440
- keyframes = Array.from(keyframesSet).sort().map((name) => ({
441
- name,
442
- refCount: 1
443
- }));
444
- }
445
- return {
446
- properties,
447
- keyframes
448
- };
308
+ return result;
449
309
  },
450
310
  summary(opts) {
451
- const { root = document, log = false, includePageCSS = false } = opts || {};
452
- const cacheStatus = this.cache({ root });
453
- const definitions = this.defs({ root });
454
- const metrics = this.metrics({ root });
455
- const activeCSS = this.css("active", {
456
- root,
457
- prettify: false
458
- });
459
- const unusedCSS = this.css("unused", {
460
- root,
461
- prettify: false
462
- });
463
- const allCSS = this.css("all", {
464
- root,
465
- prettify: false
466
- });
467
- const classCSSSize = activeCSS.length + unusedCSS.length;
468
- const totalGlobalCSSSize = allCSS.length - classCSSSize;
469
- const globalData = this.getGlobalTypeCSS("global", { root });
470
- const rawData = this.getGlobalTypeCSS("raw", { root });
471
- const keyframesData = this.getGlobalTypeCSS("keyframes", { root });
472
- const propertyData = this.getGlobalTypeCSS("property", { root });
473
- const globalTypesTotalSize = globalData.size + rawData.size + keyframesData.size + propertyData.size;
474
- const cleanupSummary = {
475
- enabled: !!metrics,
476
- totalCleanups: metrics?.cleanupHistory?.length || 0,
477
- totalClassesDeleted: metrics?.cleanupHistory?.reduce((sum, c) => sum + c.classesDeleted, 0) || 0,
478
- totalCssDeleted: metrics?.cleanupHistory?.reduce((sum, c) => sum + c.cssSize, 0) || 0,
479
- totalRulesDeleted: metrics?.cleanupHistory?.reduce((sum, c) => sum + c.rulesDeleted, 0) || 0,
480
- averageClassesPerCleanup: 0,
481
- averageCssPerCleanup: 0,
482
- averageRulesPerCleanup: 0,
483
- lastCleanup: void 0
484
- };
485
- if (cleanupSummary.totalCleanups > 0) {
486
- cleanupSummary.averageClassesPerCleanup = cleanupSummary.totalClassesDeleted / cleanupSummary.totalCleanups;
487
- cleanupSummary.averageCssPerCleanup = cleanupSummary.totalCssDeleted / cleanupSummary.totalCleanups;
488
- cleanupSummary.averageRulesPerCleanup = cleanupSummary.totalRulesDeleted / cleanupSummary.totalCleanups;
489
- const lastCleanup = metrics?.cleanupHistory?.[metrics.cleanupHistory.length - 1];
490
- if (lastCleanup) cleanupSummary.lastCleanup = {
491
- ...lastCleanup,
492
- date: new Date(lastCleanup.timestamp).toISOString()
493
- };
494
- }
495
- let page;
496
- if (includePageCSS) page = {
497
- ...getPageStats({
498
- root,
499
- includeCrossOrigin: true
500
- }),
501
- css: includePageCSS === "all" ? getPageCSS({
502
- root,
503
- includeCrossOrigin: true
504
- }) : void 0
505
- };
506
- const useIndividualSizes = Math.abs(globalTypesTotalSize - totalGlobalCSSSize) < 100;
507
- let adjustedGlobalSizes;
508
- if (useIndividualSizes) adjustedGlobalSizes = {
509
- globalCSSSize: globalData.size,
510
- rawCSSSize: rawData.size,
511
- keyframesCSSSize: keyframesData.size,
512
- propertyCSSSize: propertyData.size
513
- };
514
- else {
515
- const scaleFactor = totalGlobalCSSSize / globalTypesTotalSize;
516
- adjustedGlobalSizes = {
517
- globalCSSSize: Math.round(globalData.size * scaleFactor),
518
- rawCSSSize: Math.round(rawData.size * scaleFactor),
519
- keyframesCSSSize: Math.round(keyframesData.size * scaleFactor),
520
- propertyCSSSize: Math.round(propertyData.size * scaleFactor)
521
- };
522
- }
523
- const chunkBreakdown = getChunkBreakdown(root);
311
+ const { root = document, raw = false } = opts || {};
312
+ const activeClasses = findDomTastyClasses(root);
313
+ const unusedClasses = getUnusedClasses(root);
314
+ const totalStyledClasses = [...activeClasses, ...unusedClasses];
315
+ const activeCSS = injector.instance.getCssTextForClasses(activeClasses, { root });
316
+ const unusedCSS = injector.instance.getCssTextForClasses(unusedClasses, { root });
317
+ const allCSS = injector.instance.getCssText({ root });
318
+ const activeRuleCount = countRules(activeCSS);
319
+ const unusedRuleCount = countRules(unusedCSS);
320
+ const globalData = getGlobalTypeCSS("global", root);
321
+ const rawData = getGlobalTypeCSS("raw", root);
322
+ const kfData = getGlobalTypeCSS("keyframes", root);
323
+ const propData = getGlobalTypeCSS("property", root);
324
+ const totalRuleCount = activeRuleCount + unusedRuleCount + globalData.ruleCount + rawData.ruleCount + kfData.ruleCount + propData.ruleCount;
325
+ const metrics = injector.instance.getMetrics({ root });
326
+ const defs = getDefs(root);
327
+ const chunkBreakdown = buildChunkBreakdown(root);
524
328
  const summary = {
525
- activeClasses: cacheStatus.classes.active,
526
- unusedClasses: cacheStatus.classes.unused,
527
- totalStyledClasses: cacheStatus.classes.all,
329
+ activeClasses,
330
+ unusedClasses,
331
+ totalStyledClasses,
528
332
  activeCSSSize: activeCSS.length,
529
333
  unusedCSSSize: unusedCSS.length,
530
- ...adjustedGlobalSizes,
334
+ globalCSSSize: globalData.size,
335
+ rawCSSSize: rawData.size,
336
+ keyframesCSSSize: kfData.size,
337
+ propertyCSSSize: propData.size,
531
338
  totalCSSSize: allCSS.length,
532
- activeCSS: prettifyCSS(activeCSS),
533
- unusedCSS: prettifyCSS(unusedCSS),
534
- globalCSS: globalData.css,
535
- rawCSS: rawData.css,
536
- keyframesCSS: keyframesData.css,
537
- propertyCSS: propertyData.css,
538
- allCSS: prettifyCSS(allCSS),
339
+ activeRuleCount,
340
+ unusedRuleCount,
539
341
  globalRuleCount: globalData.ruleCount,
540
342
  rawRuleCount: rawData.ruleCount,
541
- keyframesRuleCount: keyframesData.ruleCount,
542
- propertyRuleCount: propertyData.ruleCount,
543
- page,
343
+ keyframesRuleCount: kfData.ruleCount,
344
+ propertyRuleCount: propData.ruleCount,
345
+ totalRuleCount,
544
346
  metrics,
545
- definedProperties: definitions.properties,
546
- definedKeyframes: definitions.keyframes,
547
- propertyCount: definitions.properties.length,
548
- keyframeCount: definitions.keyframes.length,
549
- cleanupSummary,
347
+ definedProperties: defs.properties,
348
+ definedKeyframes: defs.keyframes,
550
349
  chunkBreakdown
551
350
  };
552
- if (log) {
553
- console.group("🎨 Comprehensive Tasty Debug Summary");
554
- console.log(`📊 Style Cache Status:`);
555
- console.log(` Active classes (in DOM): ${summary.activeClasses.length}`);
556
- console.log(` Unused classes (refCount = 0): ${summary.unusedClasses.length}`);
557
- console.log(` Total styled classes: ${summary.totalStyledClasses.length}`);
558
- console.log(`💾 CSS Size:`);
559
- console.log(` • Active CSS: ${summary.activeCSSSize} characters`);
560
- console.log(` Unused CSS: ${summary.unusedCSSSize} characters`);
561
- console.log(` • Global CSS (injectGlobal): ${summary.globalCSSSize} characters (${summary.globalRuleCount} rules)`);
562
- console.log(` • Raw CSS (injectRawCSS/useRawCSS): ${summary.rawCSSSize} characters (${summary.rawRuleCount} rules)`);
563
- console.log(` • Keyframes CSS: ${summary.keyframesCSSSize} characters (${summary.keyframesRuleCount} rules)`);
564
- console.log(` • Property CSS: ${summary.propertyCSSSize} characters (${summary.propertyRuleCount} rules)`);
565
- const calculatedTotal = summary.activeCSSSize + summary.unusedCSSSize + summary.globalCSSSize + summary.rawCSSSize + summary.keyframesCSSSize + summary.propertyCSSSize;
566
- console.log(` • Calculated Total: ${calculatedTotal} characters`);
567
- console.log(` • Actual Total: ${summary.totalCSSSize} characters`);
568
- const difference = Math.abs(calculatedTotal - summary.totalCSSSize);
569
- if (difference > 100) {
570
- console.warn(` ⚠️ Size mismatch: ${difference} characters difference`);
571
- console.group("🔍 Debugging size mismatch:");
572
- console.log(`Active + Unused = ${summary.activeCSSSize + summary.unusedCSSSize}`);
573
- console.log(`All Global Types = ${summary.globalCSSSize + summary.rawCSSSize + summary.keyframesCSSSize + summary.propertyCSSSize}`);
574
- console.log(`Class-based vs Total difference = ${summary.totalCSSSize - (summary.activeCSSSize + summary.unusedCSSSize)}`);
575
- console.log(`Raw global extraction total: ${globalTypesTotalSize}`);
576
- console.log(`Calculated global size: ${totalGlobalCSSSize}`);
577
- console.log(`Used individual sizes: ${useIndividualSizes}`);
578
- if (!useIndividualSizes) console.log(`Scale factor applied: ${(totalGlobalCSSSize / globalTypesTotalSize).toFixed(3)}`);
579
- console.groupEnd();
580
- }
581
- if (page) {
582
- console.log(`📄 Page CSS:`);
583
- console.log(` • Total page CSS: ${page.cssSize} characters`);
584
- console.log(` • Total page rules: ${page.ruleCount}`);
585
- console.log(` • Stylesheets: ${page.stylesheetCount} (${page.skippedStylesheets} skipped)`);
586
- }
587
- console.log("🏷️ Properties & Keyframes:");
588
- console.log(` • Defined @property: ${summary.propertyCount}`);
589
- console.log(` • Defined @keyframes: ${summary.keyframeCount}`);
351
+ if (!raw) {
352
+ console.group("Tasty Summary");
353
+ console.log(`Active: ${activeClasses.length} classes, ${activeRuleCount} rules, ${fmtSize(activeCSS.length)}`);
354
+ console.log(`Unused: ${unusedClasses.length} classes, ${unusedRuleCount} rules, ${fmtSize(unusedCSS.length)}`);
355
+ console.log(`Global: ${globalData.ruleCount} rules, ${fmtSize(globalData.size)}`);
356
+ if (rawData.ruleCount) console.log(`Raw: ${rawData.ruleCount} rules, ${fmtSize(rawData.size)}`);
357
+ if (kfData.ruleCount) console.log(`Keyframes: ${kfData.ruleCount} rules, ${fmtSize(kfData.size)}`);
358
+ if (propData.ruleCount) console.log(`@property: ${propData.ruleCount} rules, ${fmtSize(propData.size)}`);
359
+ console.log(`Total: ${totalStyledClasses.length} classes, ${totalRuleCount} rules, ${fmtSize(allCSS.length)}`);
590
360
  if (metrics) {
591
- console.log(`⚡ Performance Metrics:`);
592
- console.log(` • Cache hits: ${metrics.hits}`);
593
- console.log(`Cache misses: ${metrics.misses}`);
594
- console.log(` • Cached style reuses: ${metrics.unusedHits}`);
595
- const hitRate = metrics.hits + metrics.misses > 0 ? ((metrics.hits + (metrics.unusedHits || 0)) / (metrics.hits + metrics.misses) * 100).toFixed(1) : "0";
596
- console.log(` • Overall cache hit rate: ${hitRate}%`);
361
+ const total = metrics.hits + metrics.misses;
362
+ const rate = total > 0 ? (metrics.hits / total * 100).toFixed(1) : 0;
363
+ console.log(`Cache: ${rate}% hit rate (${total} lookups)`);
597
364
  }
598
- if (summary.chunkBreakdown.totalChunkTypes > 0) {
599
- console.log("🧩 Style Chunk Breakdown:");
600
- const displayOrder = [
601
- CHUNK_NAMES.COMBINED,
602
- CHUNK_NAMES.APPEARANCE,
603
- CHUNK_NAMES.FONT,
604
- CHUNK_NAMES.DIMENSION,
605
- CHUNK_NAMES.DISPLAY,
606
- CHUNK_NAMES.LAYOUT,
607
- CHUNK_NAMES.POSITION,
608
- CHUNK_NAMES.MISC,
609
- CHUNK_NAMES.SUBCOMPONENTS
610
- ];
611
- for (const chunkName of displayOrder) {
612
- const data = summary.chunkBreakdown.byChunk[chunkName];
613
- if (data) {
614
- const sizeStr = data.cssSize > 1024 ? `${(data.cssSize / 1024).toFixed(1)}KB` : `${data.cssSize}B`;
615
- console.log(` • ${chunkName}: ${data.classes.length} classes, ${sizeStr}, ${data.ruleCount} rules`);
616
- }
365
+ if (chunkBreakdown.totalChunkTypes > 0) {
366
+ console.groupCollapsed(`Chunks (${chunkBreakdown.totalChunkTypes} types, ${chunkBreakdown.totalClasses} classes)`);
367
+ for (const name of CHUNK_ORDER) {
368
+ const d = chunkBreakdown.byChunk[name];
369
+ if (d) console.log(` ${name}: ${d.classes.length} cls, ${d.ruleCount} rules, ${fmtSize(d.cssSize)}`);
617
370
  }
371
+ for (const [name, d] of Object.entries(chunkBreakdown.byChunk)) if (!CHUNK_ORDER.includes(name)) console.log(` ${name}: ${d.classes.length} cls, ${d.ruleCount} rules, ${fmtSize(d.cssSize)}`);
372
+ console.groupEnd();
618
373
  }
619
- console.log("🔍 Details:");
620
- console.log(" • Active classes:", summary.activeClasses);
621
- console.log(" • Unused classes:", summary.unusedClasses);
374
+ if (defs.properties.length || defs.keyframes.length) console.log(`Defs: ${defs.properties.length} @property, ${defs.keyframes.length} @keyframes`);
622
375
  console.groupEnd();
623
376
  }
624
377
  return summary;
625
378
  },
626
- pageCSS(opts) {
627
- const { root = document, prettify = true, log = false, includeCrossOrigin = true } = opts || {};
628
- const css = getPageCSS({
629
- root,
630
- includeCrossOrigin
631
- });
632
- const result = prettify ? prettifyCSS(css) : css;
633
- if (log) {
634
- console.group("📄 Page CSS (All Stylesheets)");
635
- console.log(result || "(empty)");
379
+ chunks(opts) {
380
+ const { root = document, raw = false } = opts || {};
381
+ const breakdown = buildChunkBreakdown(root);
382
+ if (!raw) {
383
+ console.group(`Chunks (${breakdown.totalChunkTypes} types, ${breakdown.totalClasses} classes)`);
384
+ for (const name of CHUNK_ORDER) {
385
+ const d = breakdown.byChunk[name];
386
+ if (d) console.log(` ${name}: ${d.classes.length} cls, ${d.ruleCount} rules, ${fmtSize(d.cssSize)}`);
387
+ }
388
+ for (const [name, d] of Object.entries(breakdown.byChunk)) if (!CHUNK_ORDER.includes(name)) console.log(` ${name}: ${d.classes.length} cls, ${d.ruleCount} rules, ${fmtSize(d.cssSize)}`);
636
389
  console.groupEnd();
637
390
  }
638
- return result;
391
+ return breakdown;
639
392
  },
640
- pageStats(opts) {
641
- const { root = document, includeCrossOrigin = true } = opts || {};
642
- return getPageStats({
643
- root,
644
- includeCrossOrigin
645
- });
393
+ cache(opts) {
394
+ const { root = document, raw = false } = opts || {};
395
+ const active = findDomTastyClasses(root);
396
+ const unused = getUnusedClasses(root);
397
+ const metrics = injector.instance.getMetrics({ root });
398
+ const status = {
399
+ classes: {
400
+ active,
401
+ unused,
402
+ all: [...active, ...unused]
403
+ },
404
+ metrics
405
+ };
406
+ if (!raw) {
407
+ console.group("Cache");
408
+ console.log(`Active: ${active.length}, Unused: ${unused.length}`);
409
+ if (metrics) {
410
+ const total = metrics.hits + metrics.misses;
411
+ const rate = total > 0 ? (metrics.hits / total * 100).toFixed(1) : 0;
412
+ console.log(`Hits: ${metrics.hits}, Misses: ${metrics.misses}, Rate: ${rate}%`);
413
+ }
414
+ console.groupEnd();
415
+ }
416
+ return status;
417
+ },
418
+ cleanup(opts) {
419
+ injector.instance.cleanup(opts?.root);
420
+ },
421
+ help() {
422
+ console.log(`tastyDebug API:
423
+ .summary() — overview (classes, rules, sizes)
424
+ .css("active") — CSS for classes in DOM
425
+ .css("t42") — CSS for a specific class
426
+ .css("t42",{source:1})— original CSS before browser parsing (dev only)
427
+ .css(".selector") — CSS for a DOM element
428
+ .inspect(".selector") — element details (classes, chunks, rules)
429
+ .chunks() — style chunk breakdown
430
+ .cache() — cache status and metrics
431
+ .cleanup() — force unused style cleanup
432
+ Options: { raw: true } suppresses logging, { root: shadowRoot } targets Shadow DOM`);
646
433
  },
647
434
  install() {
648
435
  if (typeof window !== "undefined" && window.tastyDebug !== tastyDebug) {
649
436
  window.tastyDebug = tastyDebug;
650
- console.log("🎨 tastyDebug installed on window. Run tastyDebug.help() for quick start guide.");
437
+ console.log("tastyDebug installed. Run tastyDebug.help() for commands.");
651
438
  }
652
- },
653
- log(target, opts) {
654
- const { title, ...cssOpts } = opts || {};
655
- const css = tastyDebug.css(target, cssOpts);
656
- if (!css.trim()) {
657
- console.warn(`🎨 No CSS found for target: ${String(target)}`);
658
- return;
659
- }
660
- const targetStr = Array.isArray(target) ? target.join(", ") : String(target);
661
- const displayTitle = title || `CSS for "${targetStr}"`;
662
- const lines = css.split("\n").length;
663
- const size = new Blob([css]).size;
664
- const sizeStr = size > 1024 ? `${(size / 1024).toFixed(1)}KB` : `${size}B`;
665
- const ruleCount = (css.match(/\{/g) || []).length;
666
- console.group(`🎨 ${displayTitle} (${ruleCount} rules, ${lines} lines, ${sizeStr})`);
667
- const subElementMatches = css.match(/\[data-element="([^"]+)"\]/g) || [];
668
- const subElements = [...new Set(subElementMatches.map((match) => match.match(/\[data-element="([^"]+)"\]/)?.[1]).filter((v) => !!v))];
669
- if (subElements.length > 0) {
670
- console.log(`🧩 Sub-elements found: ${subElements.join(", ")}`);
671
- subElements.forEach((element) => {
672
- `${element}`;
673
- const elementRegex = new RegExp(`[^}]*\\[data-element="${element.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}"\\][^{]*\\{[^}]*\\}`, "gm");
674
- const elementCSS = (css.match(elementRegex) || []).join("\n");
675
- if (elementCSS) {
676
- const elementRules = (elementCSS.match(/\{/g) || []).length;
677
- const elementLines = elementCSS.split("\n").length;
678
- const elementSize = new Blob([elementCSS]).size;
679
- const elementSizeStr = elementSize > 1024 ? `${(elementSize / 1024).toFixed(1)}KB` : `${elementSize}B`;
680
- console.groupCollapsed(`🧩 ${element} (${elementRules} rules, ${elementLines} lines, ${elementSizeStr})`);
681
- console.log(`%c${elementCSS}`, "color: #666; font-family: monospace; font-size: 12px; white-space: pre;");
682
- console.groupEnd();
683
- }
684
- });
685
- }
686
- console.groupCollapsed("📄 Full CSS (click to expand)");
687
- console.log(`%c${css}`, "color: #666; font-family: monospace; font-size: 12px; white-space: pre;");
688
- console.groupEnd();
689
- console.groupEnd();
690
- },
691
- help() {
692
- console.group("🎨 tastyDebug - Quick Start Guide");
693
- console.log("💡 Essential commands:");
694
- console.log(" • tastyDebug.summary({ log: true }) - comprehensive overview");
695
- console.log(" • tastyDebug.chunks({ log: true }) - style chunk breakdown");
696
- console.log(" • tastyDebug.log(\"active\") - beautiful CSS display");
697
- console.log(" • tastyDebug.css(\"active\") - get active CSS");
698
- console.log(" • tastyDebug.inspect(\".my-element\") - element inspection with chunk info");
699
- console.log(" • tastyDebug.cache() - cache status");
700
- console.log(" • tastyDebug.defs() - defined properties & keyframes");
701
- console.log(" • tastyDebug.pageCSS({ log: true }) - all page CSS");
702
- console.log("");
703
- console.log("📖 Common targets for css()/log():");
704
- console.log(" • \"all\" - all tasty CSS + global CSS");
705
- console.log(" • \"active\" - CSS for classes in DOM");
706
- console.log(" • \"unused\" - CSS for classes with refCount = 0");
707
- console.log(" • \"global\" - only global CSS (injectGlobal)");
708
- console.log(" • \"page\" - ALL page CSS (including non-tasty)");
709
- console.log(" • \"t123\" - specific tasty class");
710
- console.log(" • [\".my-selector\"] - CSS selector");
711
- console.log("");
712
- console.log("🔧 Available options:");
713
- console.log(" • { log: true } - auto-log results to console");
714
- console.log(" • { title: \"Custom\" } - custom title for log()");
715
- console.log(" • { root: shadowRoot } - target Shadow DOM");
716
- console.log(" • { prettify: false } - skip CSS formatting");
717
- console.log("");
718
- console.log("🧩 Style Chunking:");
719
- console.log(" Elements have multiple classes (one per chunk: appearance, font, dimension, etc.)");
720
- console.log(" • tastyDebug.chunks({ log: true }) - breakdown by chunk type");
721
- console.log(" • tastyDebug.inspect() - shows which chunk each class belongs to");
722
- console.log(" Chunk types: combined (non-chunked), appearance, font, dimension, container, scrollbar, position, misc, subcomponents");
723
- console.groupEnd();
724
439
  }
725
440
  };
726
- /**
727
- * Auto-install in development
728
- */
441
+ function getPageCSS(root = document) {
442
+ const chunks = [];
443
+ try {
444
+ if ("styleSheets" in root) for (const sheet of Array.from(root.styleSheets)) try {
445
+ if (sheet.cssRules) chunks.push(Array.from(sheet.cssRules).map((r) => r.cssText).join("\n"));
446
+ } catch {}
447
+ } catch {}
448
+ return chunks.join("\n");
449
+ }
729
450
  if (typeof window !== "undefined" && isDevEnv()) tastyDebug.install();
730
451
 
731
452
  //#endregion