@mizchi/vlmkit-core 0.7.0 → 0.8.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.
- package/dist/a11y-semantic.d.mts +34 -0
- package/dist/a11y-semantic.mjs +261 -0
- package/dist/authored-style-capture.d.mts +42 -0
- package/dist/authored-style-capture.mjs +117 -0
- package/dist/authored-style-diff.d.mts +15 -0
- package/dist/authored-style-diff.mjs +16 -0
- package/dist/cli-args.d.mts +11 -0
- package/dist/cli-args.mjs +21 -0
- package/dist/cli-error.d.mts +4 -0
- package/dist/cli-error.mjs +59 -0
- package/dist/computed-style-capture.d.mts +41 -0
- package/dist/computed-style-capture.mjs +589 -0
- package/dist/computed-style-diff.d.mts +83 -0
- package/dist/computed-style-diff.mjs +123 -0
- package/dist/diff-regions.d.mts +8 -0
- package/dist/diff-regions.mjs +52 -0
- package/dist/dom-equivalence.d.mts +51 -0
- package/dist/dom-equivalence.mjs +91 -0
- package/dist/dom-position-styles.d.mts +166 -0
- package/dist/dom-position-styles.mjs +296 -0
- package/dist/element-compare.d.mts +71 -0
- package/dist/element-compare.mjs +269 -0
- package/dist/heatmap-regions.d.mts +57 -0
- package/dist/heatmap-regions.mjs +158 -0
- package/dist/heatmap.d.mts +86 -0
- package/dist/heatmap.mjs +510 -0
- package/dist/image-resize.d.mts +34 -0
- package/dist/image-resize.mjs +92 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.mjs +14 -0
- package/dist/mask.d.mts +18 -0
- package/dist/mask.mjs +27 -0
- package/dist/png-utils.d.mts +30 -0
- package/dist/png-utils.mjs +80 -0
- package/dist/region-shift.d.mts +20 -0
- package/dist/region-shift.mjs +128 -0
- package/dist/run-ledger.d.mts +14 -0
- package/dist/run-ledger.mjs +31 -0
- package/dist/terminal-colors.d.mts +12 -0
- package/dist/terminal-colors.mjs +14 -0
- package/dist/text-rows.d.mts +121 -0
- package/dist/text-rows.mjs +246 -0
- package/{src/types.ts → dist/types.d.mts} +88 -206
- package/dist/types.mjs +1 -0
- package/package.json +17 -5
- package/src/a11y-semantic.ts +0 -384
- package/src/authored-style-capture.ts +0 -185
- package/src/authored-style-diff.ts +0 -32
- package/src/cli-args.ts +0 -29
- package/src/cli-error.ts +0 -85
- package/src/computed-style-capture.ts +0 -717
- package/src/computed-style-diff.ts +0 -192
- package/src/diff-regions.ts +0 -70
- package/src/dom-equivalence.ts +0 -158
- package/src/dom-position-styles.ts +0 -457
- package/src/element-compare.ts +0 -354
- package/src/heatmap-regions.ts +0 -245
- package/src/heatmap.ts +0 -670
- package/src/image-resize.ts +0 -112
- package/src/index.ts +0 -105
- package/src/mask.ts +0 -41
- package/src/png-utils.ts +0 -86
- package/src/region-shift.ts +0 -173
- package/src/terminal-colors.ts +0 -13
- package/src/text-rows.ts +0 -412
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
//#region src/computed-style-capture.ts
|
|
2
|
+
const FORCED_PSEUDO_REGEX = /:(focus-visible|focus-within|focus|hover|active)\b/g;
|
|
3
|
+
function extractForcedStates(selector) {
|
|
4
|
+
const states = [];
|
|
5
|
+
const seen = /* @__PURE__ */ new Set();
|
|
6
|
+
FORCED_PSEUDO_REGEX.lastIndex = 0;
|
|
7
|
+
let match;
|
|
8
|
+
while ((match = FORCED_PSEUDO_REGEX.exec(selector)) !== null) {
|
|
9
|
+
const state = match[1];
|
|
10
|
+
if (seen.has(state)) continue;
|
|
11
|
+
seen.add(state);
|
|
12
|
+
states.push(state);
|
|
13
|
+
}
|
|
14
|
+
return states;
|
|
15
|
+
}
|
|
16
|
+
function classifyInteractionFromStates(states) {
|
|
17
|
+
if (states.includes("active")) return "active";
|
|
18
|
+
if (states.some((s) => s === "focus" || s === "focus-visible" || s === "focus-within")) return "focus";
|
|
19
|
+
return "hover";
|
|
20
|
+
}
|
|
21
|
+
const TRACKED_PROPERTIES = [
|
|
22
|
+
"display",
|
|
23
|
+
"visibility",
|
|
24
|
+
"opacity",
|
|
25
|
+
"width",
|
|
26
|
+
"height",
|
|
27
|
+
"max-width",
|
|
28
|
+
"max-height",
|
|
29
|
+
"min-width",
|
|
30
|
+
"min-height",
|
|
31
|
+
"margin-top",
|
|
32
|
+
"margin-right",
|
|
33
|
+
"margin-bottom",
|
|
34
|
+
"margin-left",
|
|
35
|
+
"padding-top",
|
|
36
|
+
"padding-right",
|
|
37
|
+
"padding-bottom",
|
|
38
|
+
"padding-left",
|
|
39
|
+
"border-top-width",
|
|
40
|
+
"border-right-width",
|
|
41
|
+
"border-bottom-width",
|
|
42
|
+
"border-left-width",
|
|
43
|
+
"border-top-color",
|
|
44
|
+
"border-right-color",
|
|
45
|
+
"border-bottom-color",
|
|
46
|
+
"border-left-color",
|
|
47
|
+
"border-top-style",
|
|
48
|
+
"border-right-style",
|
|
49
|
+
"border-bottom-style",
|
|
50
|
+
"border-left-style",
|
|
51
|
+
"border-radius",
|
|
52
|
+
"background-color",
|
|
53
|
+
"background-image",
|
|
54
|
+
"color",
|
|
55
|
+
"font-size",
|
|
56
|
+
"font-weight",
|
|
57
|
+
"font-family",
|
|
58
|
+
"font-style",
|
|
59
|
+
"text-decoration",
|
|
60
|
+
"text-align",
|
|
61
|
+
"text-transform",
|
|
62
|
+
"line-height",
|
|
63
|
+
"letter-spacing",
|
|
64
|
+
"word-spacing",
|
|
65
|
+
"white-space",
|
|
66
|
+
"flex-direction",
|
|
67
|
+
"flex-wrap",
|
|
68
|
+
"flex-grow",
|
|
69
|
+
"flex-shrink",
|
|
70
|
+
"align-items",
|
|
71
|
+
"justify-content",
|
|
72
|
+
"gap",
|
|
73
|
+
"position",
|
|
74
|
+
"top",
|
|
75
|
+
"right",
|
|
76
|
+
"bottom",
|
|
77
|
+
"left",
|
|
78
|
+
"overflow",
|
|
79
|
+
"overflow-x",
|
|
80
|
+
"overflow-y",
|
|
81
|
+
"box-shadow",
|
|
82
|
+
"cursor",
|
|
83
|
+
"aspect-ratio"
|
|
84
|
+
];
|
|
85
|
+
const INTERACTION_PSEUDO_PATTERN = /:(focus-visible|focus-within|focus|hover|active)\b/g;
|
|
86
|
+
const INTERACTION_SELECTOR_PATTERN = /:(focus-visible|focus-within|focus|hover|active)\b/;
|
|
87
|
+
function normalizeInteractionSelector(selector) {
|
|
88
|
+
return selector.replace(INTERACTION_PSEUDO_PATTERN, "").replace(/::[\w-]+/g, "").replace(/\s+/g, " ").replace(/\s*([>+~])\s*/g, " $1 ").trim();
|
|
89
|
+
}
|
|
90
|
+
function buildInteractionTargetPlans(selectors) {
|
|
91
|
+
const plans = [];
|
|
92
|
+
const seen = /* @__PURE__ */ new Set();
|
|
93
|
+
for (const selectorText of selectors) for (const rawSelector of selectorText.split(",")) {
|
|
94
|
+
const selector = rawSelector.trim();
|
|
95
|
+
if (!selector || !INTERACTION_SELECTOR_PATTERN.test(selector)) continue;
|
|
96
|
+
const normalizedSelector = normalizeInteractionSelector(selector);
|
|
97
|
+
if (!normalizedSelector) continue;
|
|
98
|
+
const forcedStates = extractForcedStates(selector);
|
|
99
|
+
if (forcedStates.length === 0) continue;
|
|
100
|
+
const interaction = classifyInteractionFromStates(forcedStates);
|
|
101
|
+
const key = `${interaction}\u0000${normalizedSelector}\u0000${forcedStates.join(",")}`;
|
|
102
|
+
if (seen.has(key)) continue;
|
|
103
|
+
seen.add(key);
|
|
104
|
+
plans.push({
|
|
105
|
+
selector,
|
|
106
|
+
normalizedSelector,
|
|
107
|
+
interaction,
|
|
108
|
+
forcedStates
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
return plans;
|
|
112
|
+
}
|
|
113
|
+
function selectInteractionFallbackPlans(plans, emulatedSnapshotMeaningful) {
|
|
114
|
+
const selected = [];
|
|
115
|
+
const seen = /* @__PURE__ */ new Set();
|
|
116
|
+
function add(plan) {
|
|
117
|
+
const key = `${plan.interaction}\u0000${plan.normalizedSelector}\u0000${plan.forcedStates.join(",")}`;
|
|
118
|
+
if (seen.has(key)) return;
|
|
119
|
+
seen.add(key);
|
|
120
|
+
selected.push(plan);
|
|
121
|
+
}
|
|
122
|
+
for (const plan of plans) if (plan.interaction !== "hover" || !emulatedSnapshotMeaningful) add(plan);
|
|
123
|
+
return selected;
|
|
124
|
+
}
|
|
125
|
+
function mergeComputedStyleSnapshots(...snapshots) {
|
|
126
|
+
const merged = {};
|
|
127
|
+
for (const snapshot of snapshots) for (const [selector, props] of Object.entries(snapshot)) merged[selector] = { ...props };
|
|
128
|
+
return merged;
|
|
129
|
+
}
|
|
130
|
+
function captureComputedStyleSnapshotInDom(props) {
|
|
131
|
+
const results = {};
|
|
132
|
+
const semanticTags = new Set([
|
|
133
|
+
"main",
|
|
134
|
+
"nav",
|
|
135
|
+
"header",
|
|
136
|
+
"footer",
|
|
137
|
+
"aside",
|
|
138
|
+
"article",
|
|
139
|
+
"section",
|
|
140
|
+
"table",
|
|
141
|
+
"thead",
|
|
142
|
+
"tbody",
|
|
143
|
+
"tr",
|
|
144
|
+
"th",
|
|
145
|
+
"td",
|
|
146
|
+
"ul",
|
|
147
|
+
"ol",
|
|
148
|
+
"li",
|
|
149
|
+
"h1",
|
|
150
|
+
"h2",
|
|
151
|
+
"h3",
|
|
152
|
+
"h4",
|
|
153
|
+
"h5",
|
|
154
|
+
"h6",
|
|
155
|
+
"p",
|
|
156
|
+
"a",
|
|
157
|
+
"button",
|
|
158
|
+
"input",
|
|
159
|
+
"select",
|
|
160
|
+
"textarea",
|
|
161
|
+
"pre",
|
|
162
|
+
"code",
|
|
163
|
+
"blockquote",
|
|
164
|
+
"img",
|
|
165
|
+
"span",
|
|
166
|
+
"div",
|
|
167
|
+
"form",
|
|
168
|
+
"label"
|
|
169
|
+
]);
|
|
170
|
+
function getClassNames(element) {
|
|
171
|
+
const classList = element.classList;
|
|
172
|
+
if (classList && typeof classList === "object") {
|
|
173
|
+
if (Symbol.iterator in classList) return Array.from(classList).map((value) => String(value)).filter(Boolean);
|
|
174
|
+
const length = Number(classList.length ?? 0);
|
|
175
|
+
const values = [];
|
|
176
|
+
for (let i = 0; i < length; i++) {
|
|
177
|
+
const token = String(classList[i] ?? "");
|
|
178
|
+
if (token) values.push(token);
|
|
179
|
+
}
|
|
180
|
+
if (values.length > 0) return values;
|
|
181
|
+
}
|
|
182
|
+
if (typeof element.className === "string") return element.className.split(/\s+/).filter(Boolean);
|
|
183
|
+
return [];
|
|
184
|
+
}
|
|
185
|
+
const getStyle = typeof getComputedStyle === "function" ? getComputedStyle : typeof window !== "undefined" && typeof window.getComputedStyle === "function" ? window.getComputedStyle.bind(window) : null;
|
|
186
|
+
if (!getStyle) throw new Error("getComputedStyle is not available in this realm");
|
|
187
|
+
const styleGetter = getStyle;
|
|
188
|
+
function shouldTrackElement(element) {
|
|
189
|
+
if (element.id) return true;
|
|
190
|
+
if (getClassNames(element).length > 0) return true;
|
|
191
|
+
return semanticTags.has(element.tagName.toLowerCase());
|
|
192
|
+
}
|
|
193
|
+
function buildElementKey(element, tagCounters) {
|
|
194
|
+
if (element.id) return `#${element.id}`;
|
|
195
|
+
const classNames = getClassNames(element);
|
|
196
|
+
if (classNames.length > 0) return `.${classNames.join(".")}`;
|
|
197
|
+
const tag = element.tagName.toLowerCase();
|
|
198
|
+
const parentClass = element.parentElement ? getClassNames(element.parentElement)[0] : void 0;
|
|
199
|
+
const ctx = parentClass ? `.${parentClass}` : "";
|
|
200
|
+
const counterKey = `${ctx}>${tag}`;
|
|
201
|
+
return `${ctx}>${tag}[${tagCounters[counterKey] = (tagCounters[counterKey] ?? 0) + 1}]`;
|
|
202
|
+
}
|
|
203
|
+
function normalizeCssClassToken(token) {
|
|
204
|
+
return token.replace(/\\([^\n\r\f])/g, "$1");
|
|
205
|
+
}
|
|
206
|
+
function collectDeclaredClassAliases() {
|
|
207
|
+
const aliases = /* @__PURE__ */ new Set();
|
|
208
|
+
const classPattern = /\.((?:\\.|[-_a-zA-Z0-9])+)/g;
|
|
209
|
+
function collectFromSelectorText(selectorText) {
|
|
210
|
+
let match;
|
|
211
|
+
while (match = classPattern.exec(selectorText)) aliases.add(`.${normalizeCssClassToken(match[1])}`);
|
|
212
|
+
}
|
|
213
|
+
function visitRule(rule) {
|
|
214
|
+
if (typeof rule.selectorText === "string") collectFromSelectorText(rule.selectorText);
|
|
215
|
+
const nested = rule.cssRules;
|
|
216
|
+
if (!nested || typeof nested.length !== "number") return;
|
|
217
|
+
const nestedRules = nested;
|
|
218
|
+
for (let i = 0; i < nestedRules.length; i++) {
|
|
219
|
+
const child = nestedRules[i];
|
|
220
|
+
if (child && typeof child === "object") visitRule(child);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
const styleSheets = document.styleSheets;
|
|
224
|
+
if (!styleSheets) return aliases;
|
|
225
|
+
for (const sheet of Array.from(styleSheets)) try {
|
|
226
|
+
const rules = sheet.cssRules;
|
|
227
|
+
for (const rule of Array.from(rules)) visitRule(rule);
|
|
228
|
+
} catch {}
|
|
229
|
+
return aliases;
|
|
230
|
+
}
|
|
231
|
+
function captureElementStyles(key, element) {
|
|
232
|
+
if (!key || results[key]) return;
|
|
233
|
+
const computed = styleGetter(element);
|
|
234
|
+
const styles = {};
|
|
235
|
+
for (const prop of props) styles[prop] = computed.getPropertyValue(prop);
|
|
236
|
+
results[key] = styles;
|
|
237
|
+
for (const pseudo of ["::before", "::after"]) {
|
|
238
|
+
const pseudoComputed = styleGetter(element, pseudo);
|
|
239
|
+
const content = pseudoComputed.getPropertyValue("content");
|
|
240
|
+
if (!content || content === "none" || content === "normal") continue;
|
|
241
|
+
const pseudoStyles = {};
|
|
242
|
+
for (const prop of props) pseudoStyles[prop] = pseudoComputed.getPropertyValue(prop);
|
|
243
|
+
results[`${key}${pseudo}`] = pseudoStyles;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const elements = document.querySelectorAll("*");
|
|
247
|
+
const tagCounters = {};
|
|
248
|
+
const declaredClassAliases = collectDeclaredClassAliases();
|
|
249
|
+
for (const element of elements) {
|
|
250
|
+
const he = element;
|
|
251
|
+
if (!shouldTrackElement(he)) continue;
|
|
252
|
+
captureElementStyles(buildElementKey(he, tagCounters), he);
|
|
253
|
+
for (const className of getClassNames(he)) {
|
|
254
|
+
const alias = `.${className}`;
|
|
255
|
+
if (declaredClassAliases.has(alias)) captureElementStyles(alias, he);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return results;
|
|
259
|
+
}
|
|
260
|
+
function captureComputedStyleSnapshotForTargetSelectorsInDom(input) {
|
|
261
|
+
const { props, selectors } = input;
|
|
262
|
+
const results = {};
|
|
263
|
+
const semanticTags = new Set([
|
|
264
|
+
"main",
|
|
265
|
+
"nav",
|
|
266
|
+
"header",
|
|
267
|
+
"footer",
|
|
268
|
+
"aside",
|
|
269
|
+
"article",
|
|
270
|
+
"section",
|
|
271
|
+
"table",
|
|
272
|
+
"thead",
|
|
273
|
+
"tbody",
|
|
274
|
+
"tr",
|
|
275
|
+
"th",
|
|
276
|
+
"td",
|
|
277
|
+
"ul",
|
|
278
|
+
"ol",
|
|
279
|
+
"li",
|
|
280
|
+
"h1",
|
|
281
|
+
"h2",
|
|
282
|
+
"h3",
|
|
283
|
+
"h4",
|
|
284
|
+
"h5",
|
|
285
|
+
"h6",
|
|
286
|
+
"p",
|
|
287
|
+
"a",
|
|
288
|
+
"button",
|
|
289
|
+
"input",
|
|
290
|
+
"select",
|
|
291
|
+
"textarea",
|
|
292
|
+
"pre",
|
|
293
|
+
"code",
|
|
294
|
+
"blockquote",
|
|
295
|
+
"img",
|
|
296
|
+
"span",
|
|
297
|
+
"div",
|
|
298
|
+
"form",
|
|
299
|
+
"label"
|
|
300
|
+
]);
|
|
301
|
+
function getClassNames(element) {
|
|
302
|
+
const classList = element.classList;
|
|
303
|
+
if (classList && typeof classList === "object") {
|
|
304
|
+
if (Symbol.iterator in classList) return Array.from(classList).map((value) => String(value)).filter(Boolean);
|
|
305
|
+
const length = Number(classList.length ?? 0);
|
|
306
|
+
const values = [];
|
|
307
|
+
for (let i = 0; i < length; i++) {
|
|
308
|
+
const token = String(classList[i] ?? "");
|
|
309
|
+
if (token) values.push(token);
|
|
310
|
+
}
|
|
311
|
+
if (values.length > 0) return values;
|
|
312
|
+
}
|
|
313
|
+
if (typeof element.className === "string") return element.className.split(/\s+/).filter(Boolean);
|
|
314
|
+
return [];
|
|
315
|
+
}
|
|
316
|
+
const getStyle = typeof getComputedStyle === "function" ? getComputedStyle : typeof window !== "undefined" && typeof window.getComputedStyle === "function" ? window.getComputedStyle.bind(window) : null;
|
|
317
|
+
if (!getStyle) throw new Error("getComputedStyle is not available in this realm");
|
|
318
|
+
function shouldTrackElement(element) {
|
|
319
|
+
if (element.id) return true;
|
|
320
|
+
if (getClassNames(element).length > 0) return true;
|
|
321
|
+
return semanticTags.has(element.tagName.toLowerCase());
|
|
322
|
+
}
|
|
323
|
+
function buildElementKey(element, tagCounters) {
|
|
324
|
+
if (element.id) return `#${element.id}`;
|
|
325
|
+
const classNames = getClassNames(element);
|
|
326
|
+
if (classNames.length > 0) return `.${classNames.join(".")}`;
|
|
327
|
+
const tag = element.tagName.toLowerCase();
|
|
328
|
+
const parentClass = element.parentElement ? getClassNames(element.parentElement)[0] : void 0;
|
|
329
|
+
const ctx = parentClass ? `.${parentClass}` : "";
|
|
330
|
+
const counterKey = `${ctx}>${tag}`;
|
|
331
|
+
return `${ctx}>${tag}[${tagCounters[counterKey] = (tagCounters[counterKey] ?? 0) + 1}]`;
|
|
332
|
+
}
|
|
333
|
+
const targets = [];
|
|
334
|
+
for (const selector of selectors) try {
|
|
335
|
+
targets.push(...document.querySelectorAll(selector));
|
|
336
|
+
} catch {}
|
|
337
|
+
const tagCounters = {};
|
|
338
|
+
for (const element of targets) {
|
|
339
|
+
const he = element;
|
|
340
|
+
if (!shouldTrackElement(he)) continue;
|
|
341
|
+
const key = buildElementKey(he, tagCounters);
|
|
342
|
+
if (!key || results[key]) continue;
|
|
343
|
+
const computed = getStyle(he);
|
|
344
|
+
const styles = {};
|
|
345
|
+
for (const prop of props) styles[prop] = computed.getPropertyValue(prop);
|
|
346
|
+
results[key] = styles;
|
|
347
|
+
for (const pseudo of ["::before", "::after"]) {
|
|
348
|
+
const pseudoComputed = getStyle(he, pseudo);
|
|
349
|
+
const content = pseudoComputed.getPropertyValue("content");
|
|
350
|
+
if (!content || content === "none" || content === "normal") continue;
|
|
351
|
+
const pseudoStyles = {};
|
|
352
|
+
for (const prop of props) pseudoStyles[prop] = pseudoComputed.getPropertyValue(prop);
|
|
353
|
+
results[`${key}${pseudo}`] = pseudoStyles;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return results;
|
|
357
|
+
}
|
|
358
|
+
function collectInteractionTargetPlansInDom() {
|
|
359
|
+
const selectorTexts = [];
|
|
360
|
+
for (const sheet of document.styleSheets) try {
|
|
361
|
+
for (const rule of sheet.cssRules) {
|
|
362
|
+
if (!(typeof CSSStyleRule === "undefined" ? typeof rule.selectorText === "string" : rule instanceof CSSStyleRule)) continue;
|
|
363
|
+
const selectorText = rule.selectorText;
|
|
364
|
+
if (!/:(focus-visible|focus-within|focus|hover|active)\b/.test(selectorText)) continue;
|
|
365
|
+
selectorTexts.push(selectorText);
|
|
366
|
+
}
|
|
367
|
+
} catch {}
|
|
368
|
+
const plans = [];
|
|
369
|
+
const seen = /* @__PURE__ */ new Set();
|
|
370
|
+
for (const selectorText of selectorTexts) for (const rawSelector of selectorText.split(",")) {
|
|
371
|
+
const selector = rawSelector.trim();
|
|
372
|
+
if (!selector || !/:(focus-visible|focus-within|focus|hover|active)\b/.test(selector)) continue;
|
|
373
|
+
const normalizedSelector = selector.replace(/:(focus-visible|focus-within|focus|hover|active)\b/g, "").replace(/::[\w-]+/g, "").replace(/\s+/g, " ").replace(/\s*([>+~])\s*/g, " $1 ").trim();
|
|
374
|
+
if (!normalizedSelector) continue;
|
|
375
|
+
const forcedStates = [];
|
|
376
|
+
{
|
|
377
|
+
const seenStates = /* @__PURE__ */ new Set();
|
|
378
|
+
const pattern = /:(focus-visible|focus-within|focus|hover|active)\b/g;
|
|
379
|
+
let m;
|
|
380
|
+
while ((m = pattern.exec(selector)) !== null) {
|
|
381
|
+
const s = m[1];
|
|
382
|
+
if (seenStates.has(s)) continue;
|
|
383
|
+
seenStates.add(s);
|
|
384
|
+
forcedStates.push(s);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
if (forcedStates.length === 0) continue;
|
|
388
|
+
const interaction = forcedStates.includes("active") ? "active" : forcedStates.some((s) => s === "focus" || s === "focus-visible" || s === "focus-within") ? "focus" : "hover";
|
|
389
|
+
const key = `${interaction}\u0000${normalizedSelector}\u0000${forcedStates.join(",")}`;
|
|
390
|
+
if (seen.has(key)) continue;
|
|
391
|
+
seen.add(key);
|
|
392
|
+
plans.push({
|
|
393
|
+
selector,
|
|
394
|
+
normalizedSelector,
|
|
395
|
+
interaction,
|
|
396
|
+
forcedStates
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
return plans;
|
|
400
|
+
}
|
|
401
|
+
async function waitForInteractionStylesInDom() {
|
|
402
|
+
try {
|
|
403
|
+
document.documentElement?.offsetHeight;
|
|
404
|
+
} catch {}
|
|
405
|
+
const waitFrame = () => new Promise((resolve) => {
|
|
406
|
+
if (typeof requestAnimationFrame === "function") {
|
|
407
|
+
requestAnimationFrame(() => resolve());
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
setTimeout(resolve, 0);
|
|
411
|
+
});
|
|
412
|
+
await waitFrame();
|
|
413
|
+
await waitFrame();
|
|
414
|
+
}
|
|
415
|
+
async function captureEmulatedInteractionStyleSnapshotInDom(props) {
|
|
416
|
+
const results = {};
|
|
417
|
+
const semanticTags = new Set([
|
|
418
|
+
"main",
|
|
419
|
+
"nav",
|
|
420
|
+
"header",
|
|
421
|
+
"footer",
|
|
422
|
+
"aside",
|
|
423
|
+
"article",
|
|
424
|
+
"section",
|
|
425
|
+
"table",
|
|
426
|
+
"thead",
|
|
427
|
+
"tbody",
|
|
428
|
+
"tr",
|
|
429
|
+
"th",
|
|
430
|
+
"td",
|
|
431
|
+
"ul",
|
|
432
|
+
"ol",
|
|
433
|
+
"li",
|
|
434
|
+
"h1",
|
|
435
|
+
"h2",
|
|
436
|
+
"h3",
|
|
437
|
+
"h4",
|
|
438
|
+
"h5",
|
|
439
|
+
"h6",
|
|
440
|
+
"p",
|
|
441
|
+
"a",
|
|
442
|
+
"button",
|
|
443
|
+
"input",
|
|
444
|
+
"select",
|
|
445
|
+
"textarea",
|
|
446
|
+
"pre",
|
|
447
|
+
"code",
|
|
448
|
+
"blockquote",
|
|
449
|
+
"img",
|
|
450
|
+
"span",
|
|
451
|
+
"div",
|
|
452
|
+
"form",
|
|
453
|
+
"label"
|
|
454
|
+
]);
|
|
455
|
+
function getClassNames(element) {
|
|
456
|
+
const classList = element.classList;
|
|
457
|
+
if (classList && typeof classList === "object") {
|
|
458
|
+
if (Symbol.iterator in classList) return Array.from(classList).map((value) => String(value)).filter(Boolean);
|
|
459
|
+
const length = Number(classList.length ?? 0);
|
|
460
|
+
const values = [];
|
|
461
|
+
for (let i = 0; i < length; i++) {
|
|
462
|
+
const token = String(classList[i] ?? "");
|
|
463
|
+
if (token) values.push(token);
|
|
464
|
+
}
|
|
465
|
+
if (values.length > 0) return values;
|
|
466
|
+
}
|
|
467
|
+
if (typeof element.className === "string") return element.className.split(/\s+/).filter(Boolean);
|
|
468
|
+
return [];
|
|
469
|
+
}
|
|
470
|
+
const getStyle = typeof getComputedStyle === "function" ? getComputedStyle : typeof window !== "undefined" && typeof window.getComputedStyle === "function" ? window.getComputedStyle.bind(window) : null;
|
|
471
|
+
if (!getStyle) throw new Error("getComputedStyle is not available in this realm");
|
|
472
|
+
function shouldTrackElement(element) {
|
|
473
|
+
if (element.id) return true;
|
|
474
|
+
if (getClassNames(element).length > 0) return true;
|
|
475
|
+
return semanticTags.has(element.tagName.toLowerCase());
|
|
476
|
+
}
|
|
477
|
+
function buildElementKey(element, tagCounters) {
|
|
478
|
+
if (element.id) return `#${element.id}`;
|
|
479
|
+
const classNames = getClassNames(element);
|
|
480
|
+
if (classNames.length > 0) return `.${classNames.join(".")}`;
|
|
481
|
+
const tag = element.tagName.toLowerCase();
|
|
482
|
+
const parentClass = element.parentElement ? getClassNames(element.parentElement)[0] : void 0;
|
|
483
|
+
const ctx = parentClass ? `.${parentClass}` : "";
|
|
484
|
+
const counterKey = `${ctx}>${tag}`;
|
|
485
|
+
return `${ctx}>${tag}[${tagCounters[counterKey] = (tagCounters[counterKey] ?? 0) + 1}]`;
|
|
486
|
+
}
|
|
487
|
+
const emulatedRules = [];
|
|
488
|
+
const targetSelectors = /* @__PURE__ */ new Set();
|
|
489
|
+
for (const sheet of document.styleSheets) try {
|
|
490
|
+
for (const rule of sheet.cssRules) {
|
|
491
|
+
if (!(typeof CSSStyleRule === "undefined" ? typeof rule.selectorText === "string" : rule instanceof CSSStyleRule)) continue;
|
|
492
|
+
const selectorText = rule.selectorText;
|
|
493
|
+
if (!/:(focus-visible|focus-within|focus|hover|active)\b/.test(selectorText)) continue;
|
|
494
|
+
const emulatedSelector = selectorText.replace(/:(focus-visible|focus-within|focus|hover|active)\b/g, "").replace(/\s+/g, " ").replace(/\s*([>+~])\s*/g, " $1 ").trim();
|
|
495
|
+
if (emulatedSelector) emulatedRules.push(`${emulatedSelector} { ${rule.style.cssText} }`);
|
|
496
|
+
for (const rawSelector of selectorText.split(",")) {
|
|
497
|
+
const normalizedSelector = rawSelector.trim().replace(/:(focus-visible|focus-within|focus|hover|active)\b/g, "").replace(/::[\w-]+/g, "").replace(/\s+/g, " ").replace(/\s*([>+~])\s*/g, " $1 ").trim();
|
|
498
|
+
if (normalizedSelector) targetSelectors.add(normalizedSelector);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
} catch {}
|
|
502
|
+
if (emulatedRules.length === 0 || targetSelectors.size === 0) return {};
|
|
503
|
+
const style = document.createElement("style");
|
|
504
|
+
style.id = "__hover_emulation__";
|
|
505
|
+
style.textContent = emulatedRules.join("\n");
|
|
506
|
+
(document.head ?? document.documentElement).appendChild(style);
|
|
507
|
+
try {
|
|
508
|
+
try {
|
|
509
|
+
document.documentElement?.offsetHeight;
|
|
510
|
+
} catch {}
|
|
511
|
+
const waitFrame = () => new Promise((resolve) => {
|
|
512
|
+
if (typeof requestAnimationFrame === "function") {
|
|
513
|
+
requestAnimationFrame(() => resolve());
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
setTimeout(resolve, 0);
|
|
517
|
+
});
|
|
518
|
+
await waitFrame();
|
|
519
|
+
await waitFrame();
|
|
520
|
+
const selectorText = [...targetSelectors].join(", ");
|
|
521
|
+
let targets;
|
|
522
|
+
try {
|
|
523
|
+
targets = document.querySelectorAll(selectorText);
|
|
524
|
+
} catch {
|
|
525
|
+
targets = document.querySelectorAll("*");
|
|
526
|
+
}
|
|
527
|
+
const tagCounters = {};
|
|
528
|
+
for (const element of targets) {
|
|
529
|
+
const he = element;
|
|
530
|
+
if (!shouldTrackElement(he)) continue;
|
|
531
|
+
const key = buildElementKey(he, tagCounters);
|
|
532
|
+
if (!key || results[key]) continue;
|
|
533
|
+
const computed = getStyle(he);
|
|
534
|
+
const styles = {};
|
|
535
|
+
for (const prop of props) styles[prop] = computed.getPropertyValue(prop);
|
|
536
|
+
results[key] = styles;
|
|
537
|
+
for (const pseudo of ["::before", "::after"]) {
|
|
538
|
+
const pseudoComputed = getStyle(he, pseudo);
|
|
539
|
+
const content = pseudoComputed.getPropertyValue("content");
|
|
540
|
+
if (!content || content === "none" || content === "normal") continue;
|
|
541
|
+
const pseudoStyles = {};
|
|
542
|
+
for (const prop of props) pseudoStyles[prop] = pseudoComputed.getPropertyValue(prop);
|
|
543
|
+
results[`${key}${pseudo}`] = pseudoStyles;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
return results;
|
|
547
|
+
} finally {
|
|
548
|
+
style.remove();
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
function computedStyleSnapshotToMap(snapshot) {
|
|
552
|
+
const map = /* @__PURE__ */ new Map();
|
|
553
|
+
for (const [selector, props] of Object.entries(snapshot)) map.set(selector, props);
|
|
554
|
+
return map;
|
|
555
|
+
}
|
|
556
|
+
const ESBUILD_NAME_POLYFILL = "var __name = typeof __name !== 'undefined' ? __name : function(fn) { return fn; };";
|
|
557
|
+
function buildComputedStyleCaptureExpression(props = TRACKED_PROPERTIES) {
|
|
558
|
+
return `(function(){ ${ESBUILD_NAME_POLYFILL} return (${captureComputedStyleSnapshotInDom.toString()})(${JSON.stringify(props)}); })()`;
|
|
559
|
+
}
|
|
560
|
+
function buildComputedStyleCaptureJsonExpression(props = TRACKED_PROPERTIES) {
|
|
561
|
+
return `JSON.stringify(${buildComputedStyleCaptureExpression(props)})`;
|
|
562
|
+
}
|
|
563
|
+
function parseComputedStyleSnapshot(value) {
|
|
564
|
+
const candidate = typeof value === "string" ? safeJsonParse(value) : value;
|
|
565
|
+
if (!isRecord(candidate)) return {};
|
|
566
|
+
const snapshot = {};
|
|
567
|
+
for (const [selector, props] of Object.entries(candidate)) {
|
|
568
|
+
if (!isRecord(props)) continue;
|
|
569
|
+
const normalized = {};
|
|
570
|
+
for (const [prop, propValue] of Object.entries(props)) normalized[prop] = typeof propValue === "string" ? propValue : String(propValue ?? "");
|
|
571
|
+
snapshot[selector] = normalized;
|
|
572
|
+
}
|
|
573
|
+
return snapshot;
|
|
574
|
+
}
|
|
575
|
+
function hasMeaningfulComputedStyleSnapshot(snapshot) {
|
|
576
|
+
return Object.values(snapshot).some((props) => Object.values(props).some((value) => value.trim().length > 0));
|
|
577
|
+
}
|
|
578
|
+
function safeJsonParse(value) {
|
|
579
|
+
try {
|
|
580
|
+
return JSON.parse(value);
|
|
581
|
+
} catch {
|
|
582
|
+
return null;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
function isRecord(value) {
|
|
586
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
587
|
+
}
|
|
588
|
+
//#endregion
|
|
589
|
+
export { ESBUILD_NAME_POLYFILL, TRACKED_PROPERTIES, buildComputedStyleCaptureExpression, buildComputedStyleCaptureJsonExpression, buildInteractionTargetPlans, captureComputedStyleSnapshotForTargetSelectorsInDom, captureComputedStyleSnapshotInDom, captureEmulatedInteractionStyleSnapshotInDom, collectInteractionTargetPlansInDom, computedStyleSnapshotToMap, hasMeaningfulComputedStyleSnapshot, mergeComputedStyleSnapshots, normalizeInteractionSelector, parseComputedStyleSnapshot, selectInteractionFallbackPlans, waitForInteractionStylesInDom };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { ComputedStyleSnapshot } from "./computed-style-capture.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/computed-style-diff.d.ts
|
|
4
|
+
interface CsdEntry {
|
|
5
|
+
selector: string;
|
|
6
|
+
property: string;
|
|
7
|
+
baseline: string;
|
|
8
|
+
variant: string;
|
|
9
|
+
}
|
|
10
|
+
interface CsdResult {
|
|
11
|
+
/** Per-(selector, property) differences. */
|
|
12
|
+
entries: CsdEntry[];
|
|
13
|
+
/** Total differing (selector, property) pairs. */
|
|
14
|
+
totalDiffs: number;
|
|
15
|
+
/** Sorted property → count map (descending). */
|
|
16
|
+
byProperty: Array<{
|
|
17
|
+
property: string;
|
|
18
|
+
count: number;
|
|
19
|
+
}>;
|
|
20
|
+
/** Sorted selector → count map (descending). */
|
|
21
|
+
bySelector: Array<{
|
|
22
|
+
selector: string;
|
|
23
|
+
count: number;
|
|
24
|
+
}>;
|
|
25
|
+
/** Selectors present only in one side. */
|
|
26
|
+
selectorsOnlyInBaseline: string[];
|
|
27
|
+
selectorsOnlyInVariant: string[];
|
|
28
|
+
}
|
|
29
|
+
interface CsdPerViewportSample {
|
|
30
|
+
viewport: string;
|
|
31
|
+
baseline: string;
|
|
32
|
+
variant: string;
|
|
33
|
+
}
|
|
34
|
+
interface CsdPerViewportEntry {
|
|
35
|
+
selector: string;
|
|
36
|
+
property: string;
|
|
37
|
+
/** Viewports on which this (selector, property) pair differs. */
|
|
38
|
+
viewports: string[];
|
|
39
|
+
samples: CsdPerViewportSample[];
|
|
40
|
+
}
|
|
41
|
+
interface CsdPerViewportResult {
|
|
42
|
+
/** Total (selector, property, viewport) tuples — i.e. Σ per-viewport counts. */
|
|
43
|
+
totalDiffs: number;
|
|
44
|
+
byViewport: Array<{
|
|
45
|
+
viewport: string;
|
|
46
|
+
count: number;
|
|
47
|
+
}>;
|
|
48
|
+
/**
|
|
49
|
+
* One row per unique (selector, property) pair, listing which viewports
|
|
50
|
+
* it differs on and the actual baseline/variant samples per viewport.
|
|
51
|
+
*/
|
|
52
|
+
bySelectorProperty: CsdPerViewportEntry[];
|
|
53
|
+
/**
|
|
54
|
+
* "selector|property" tokens for pairs that differ on every viewport.
|
|
55
|
+
* A consumer rendering "fix the base rule" tables wants this subset.
|
|
56
|
+
*/
|
|
57
|
+
universalPairs: string[];
|
|
58
|
+
/**
|
|
59
|
+
* "selector|property" tokens for pairs that differ on a strict subset
|
|
60
|
+
* of viewports. Almost always a `@media` rule is missing or wrong.
|
|
61
|
+
*/
|
|
62
|
+
breakpointGatedPairs: string[];
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Roll up N per-viewport CSD results into a single per-viewport report,
|
|
66
|
+
* surfacing which pairs apply universally vs. which are breakpoint-gated.
|
|
67
|
+
*
|
|
68
|
+
* Stable iteration order: results are taken in the input array's order,
|
|
69
|
+
* which is the same order migration-compare captures viewports (small →
|
|
70
|
+
* large or whatever the caller chose).
|
|
71
|
+
*/
|
|
72
|
+
declare function aggregateCsdByViewport(perViewport: ReadonlyArray<{
|
|
73
|
+
viewport: string;
|
|
74
|
+
result: CsdResult;
|
|
75
|
+
}>): CsdPerViewportResult;
|
|
76
|
+
/**
|
|
77
|
+
* Diff two computed-style snapshots. Both inputs are
|
|
78
|
+
* `Record<selector, Record<property, value>>`; produced by
|
|
79
|
+
* `captureComputedStyleSnapshotInDom`.
|
|
80
|
+
*/
|
|
81
|
+
declare function diffComputedStyles(baseline: ComputedStyleSnapshot, variant: ComputedStyleSnapshot): CsdResult;
|
|
82
|
+
//#endregion
|
|
83
|
+
export { type ComputedStyleSnapshot, CsdEntry, CsdPerViewportEntry, CsdPerViewportResult, CsdPerViewportSample, CsdResult, aggregateCsdByViewport, diffComputedStyles };
|