@mizchi/vlmkit-core 0.8.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/run-ledger.ts +0 -39
- package/src/terminal-colors.ts +0 -13
- package/src/text-rows.ts +0 -412
|
@@ -1,717 +0,0 @@
|
|
|
1
|
-
/// <reference lib="dom" />
|
|
2
|
-
export type ComputedStyleSnapshot = Record<string, Record<string, string>>;
|
|
3
|
-
export type InteractionType = "hover" | "focus" | "active";
|
|
4
|
-
|
|
5
|
-
/** Pseudo-class state names that CDP `CSS.forcePseudoState` accepts. */
|
|
6
|
-
export type ForcedPseudoState =
|
|
7
|
-
| "hover"
|
|
8
|
-
| "focus"
|
|
9
|
-
| "focus-visible"
|
|
10
|
-
| "focus-within"
|
|
11
|
-
| "active";
|
|
12
|
-
|
|
13
|
-
export interface InteractionTargetPlan {
|
|
14
|
-
selector: string;
|
|
15
|
-
normalizedSelector: string;
|
|
16
|
-
/**
|
|
17
|
-
* Primary interaction kind, kept for legacy fallback ordering:
|
|
18
|
-
* `:active` collapses to "active", any `:focus*` to "focus", else "hover".
|
|
19
|
-
*/
|
|
20
|
-
interaction: InteractionType;
|
|
21
|
-
/**
|
|
22
|
-
* The actual pseudo-class names extracted from the selector — preserves
|
|
23
|
-
* the full set (e.g. `:hover:active` → ["hover", "active"]) so Crater and
|
|
24
|
-
* the CDP-driven Playwright fallback can force identical states.
|
|
25
|
-
*/
|
|
26
|
-
forcedStates: ForcedPseudoState[];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const FORCED_PSEUDO_REGEX = /:(focus-visible|focus-within|focus|hover|active)\b/g;
|
|
30
|
-
|
|
31
|
-
function extractForcedStates(selector: string): ForcedPseudoState[] {
|
|
32
|
-
const states: ForcedPseudoState[] = [];
|
|
33
|
-
const seen = new Set<string>();
|
|
34
|
-
FORCED_PSEUDO_REGEX.lastIndex = 0;
|
|
35
|
-
let match: RegExpExecArray | null;
|
|
36
|
-
while ((match = FORCED_PSEUDO_REGEX.exec(selector)) !== null) {
|
|
37
|
-
const state = match[1] as ForcedPseudoState;
|
|
38
|
-
if (seen.has(state)) continue;
|
|
39
|
-
seen.add(state);
|
|
40
|
-
states.push(state);
|
|
41
|
-
}
|
|
42
|
-
return states;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function classifyInteractionFromStates(states: ForcedPseudoState[]): InteractionType {
|
|
46
|
-
if (states.includes("active")) return "active";
|
|
47
|
-
if (states.some((s) => s === "focus" || s === "focus-visible" || s === "focus-within")) return "focus";
|
|
48
|
-
return "hover";
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export const TRACKED_PROPERTIES = [
|
|
52
|
-
"display", "visibility", "opacity",
|
|
53
|
-
"width", "height", "max-width", "max-height", "min-width", "min-height",
|
|
54
|
-
"margin-top", "margin-right", "margin-bottom", "margin-left",
|
|
55
|
-
"padding-top", "padding-right", "padding-bottom", "padding-left",
|
|
56
|
-
"border-top-width", "border-right-width", "border-bottom-width", "border-left-width",
|
|
57
|
-
"border-top-color", "border-right-color", "border-bottom-color", "border-left-color",
|
|
58
|
-
"border-top-style", "border-right-style", "border-bottom-style", "border-left-style",
|
|
59
|
-
"border-radius",
|
|
60
|
-
"background-color", "background-image",
|
|
61
|
-
"color", "font-size", "font-weight", "font-family", "font-style",
|
|
62
|
-
"text-decoration", "text-align", "text-transform",
|
|
63
|
-
"line-height", "letter-spacing", "word-spacing", "white-space",
|
|
64
|
-
"flex-direction", "flex-wrap", "flex-grow", "flex-shrink",
|
|
65
|
-
"align-items", "justify-content", "gap",
|
|
66
|
-
"position", "top", "right", "bottom", "left",
|
|
67
|
-
"overflow", "overflow-x", "overflow-y", "box-shadow", "cursor",
|
|
68
|
-
// Note: grid-template-columns / rows etc. are NOT tracked here because
|
|
69
|
-
// `getComputedStyle()` resolves `minmax(0, 1fr)` to `0px`, which looks
|
|
70
|
-
// like an authored value but would break layout if written back as CSS.
|
|
71
|
-
// The LLM still has the authored values via the full CSS embedded in
|
|
72
|
-
// the prompt — those just aren't surfaced as report deltas.
|
|
73
|
-
"aspect-ratio",
|
|
74
|
-
];
|
|
75
|
-
|
|
76
|
-
const INTERACTION_PSEUDO_PATTERN = /:(focus-visible|focus-within|focus|hover|active)\b/g;
|
|
77
|
-
const INTERACTION_SELECTOR_PATTERN = /:(focus-visible|focus-within|focus|hover|active)\b/;
|
|
78
|
-
|
|
79
|
-
export function normalizeInteractionSelector(selector: string): string {
|
|
80
|
-
return selector
|
|
81
|
-
.replace(INTERACTION_PSEUDO_PATTERN, "")
|
|
82
|
-
.replace(/::[\w-]+/g, "")
|
|
83
|
-
.replace(/\s+/g, " ")
|
|
84
|
-
.replace(/\s*([>+~])\s*/g, " $1 ")
|
|
85
|
-
.trim();
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export function buildInteractionTargetPlans(
|
|
89
|
-
selectors: string[],
|
|
90
|
-
): InteractionTargetPlan[] {
|
|
91
|
-
const plans: InteractionTargetPlan[] = [];
|
|
92
|
-
const seen = new Set<string>();
|
|
93
|
-
|
|
94
|
-
for (const selectorText of selectors) {
|
|
95
|
-
for (const rawSelector of selectorText.split(",")) {
|
|
96
|
-
const selector = rawSelector.trim();
|
|
97
|
-
if (!selector || !INTERACTION_SELECTOR_PATTERN.test(selector)) continue;
|
|
98
|
-
const normalizedSelector = normalizeInteractionSelector(selector);
|
|
99
|
-
if (!normalizedSelector) continue;
|
|
100
|
-
const forcedStates = extractForcedStates(selector);
|
|
101
|
-
if (forcedStates.length === 0) continue;
|
|
102
|
-
const interaction = classifyInteractionFromStates(forcedStates);
|
|
103
|
-
const key = `${interaction}\u0000${normalizedSelector}\u0000${forcedStates.join(",")}`;
|
|
104
|
-
if (seen.has(key)) continue;
|
|
105
|
-
seen.add(key);
|
|
106
|
-
plans.push({ selector, normalizedSelector, interaction, forcedStates });
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return plans;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function selectInteractionFallbackPlans(
|
|
114
|
-
plans: InteractionTargetPlan[],
|
|
115
|
-
emulatedSnapshotMeaningful: boolean,
|
|
116
|
-
): InteractionTargetPlan[] {
|
|
117
|
-
const selected: InteractionTargetPlan[] = [];
|
|
118
|
-
const seen = new Set<string>();
|
|
119
|
-
|
|
120
|
-
function add(plan: InteractionTargetPlan) {
|
|
121
|
-
const key = `${plan.interaction}\u0000${plan.normalizedSelector}\u0000${plan.forcedStates.join(",")}`;
|
|
122
|
-
if (seen.has(key)) return;
|
|
123
|
-
seen.add(key);
|
|
124
|
-
selected.push(plan);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
for (const plan of plans) {
|
|
128
|
-
// `:active` and any `:focus*` cannot be triggered by CSS-rule rewriting
|
|
129
|
-
// alone — they need real DOM activation or a forced-state override.
|
|
130
|
-
// Pure `:hover` plans only fall back when the in-page rewrite produced
|
|
131
|
-
// nothing meaningful.
|
|
132
|
-
if (plan.interaction !== "hover" || !emulatedSnapshotMeaningful) add(plan);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return selected;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function mergeComputedStyleSnapshots(
|
|
139
|
-
...snapshots: ComputedStyleSnapshot[]
|
|
140
|
-
): ComputedStyleSnapshot {
|
|
141
|
-
const merged: ComputedStyleSnapshot = {};
|
|
142
|
-
for (const snapshot of snapshots) {
|
|
143
|
-
for (const [selector, props] of Object.entries(snapshot)) {
|
|
144
|
-
merged[selector] = { ...props };
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return merged;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export function captureComputedStyleSnapshotInDom(props: string[]): ComputedStyleSnapshot {
|
|
151
|
-
const results: ComputedStyleSnapshot = {};
|
|
152
|
-
const semanticTags = new Set([
|
|
153
|
-
"main", "nav", "header", "footer", "aside", "article", "section",
|
|
154
|
-
"table", "thead", "tbody", "tr", "th", "td", "ul", "ol", "li",
|
|
155
|
-
"h1", "h2", "h3", "h4", "h5", "h6", "p", "a", "button", "input", "select", "textarea",
|
|
156
|
-
"pre", "code", "blockquote", "img", "span", "div", "form", "label",
|
|
157
|
-
]);
|
|
158
|
-
|
|
159
|
-
function getClassNames(element: {
|
|
160
|
-
classList?: unknown;
|
|
161
|
-
className?: unknown;
|
|
162
|
-
}): string[] {
|
|
163
|
-
const classList = element.classList;
|
|
164
|
-
if (classList && typeof classList === "object") {
|
|
165
|
-
if (Symbol.iterator in classList) {
|
|
166
|
-
return Array.from(classList as Iterable<unknown>)
|
|
167
|
-
.map((value) => String(value))
|
|
168
|
-
.filter(Boolean);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
const length = Number((classList as { length?: number }).length ?? 0);
|
|
172
|
-
const values: string[] = [];
|
|
173
|
-
for (let i = 0; i < length; i++) {
|
|
174
|
-
const token = String((classList as Record<number, unknown>)[i] ?? "");
|
|
175
|
-
if (token) values.push(token);
|
|
176
|
-
}
|
|
177
|
-
if (values.length > 0) return values;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (typeof element.className === "string") {
|
|
181
|
-
return element.className.split(/\s+/).filter(Boolean);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
return [];
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const getStyle = typeof getComputedStyle === "function"
|
|
188
|
-
? getComputedStyle
|
|
189
|
-
: typeof window !== "undefined" && typeof window.getComputedStyle === "function"
|
|
190
|
-
? window.getComputedStyle.bind(window)
|
|
191
|
-
: null;
|
|
192
|
-
if (!getStyle) {
|
|
193
|
-
throw new Error("getComputedStyle is not available in this realm");
|
|
194
|
-
}
|
|
195
|
-
const styleGetter = getStyle;
|
|
196
|
-
|
|
197
|
-
function shouldTrackElement(element: HTMLElement): boolean {
|
|
198
|
-
if (element.id) return true;
|
|
199
|
-
if (getClassNames(element).length > 0) return true;
|
|
200
|
-
return semanticTags.has(element.tagName.toLowerCase());
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function buildElementKey(
|
|
204
|
-
element: HTMLElement,
|
|
205
|
-
tagCounters: Record<string, number>,
|
|
206
|
-
): string {
|
|
207
|
-
if (element.id) return `#${element.id}`;
|
|
208
|
-
const classNames = getClassNames(element);
|
|
209
|
-
if (classNames.length > 0) return `.${classNames.join(".")}`;
|
|
210
|
-
|
|
211
|
-
const tag = element.tagName.toLowerCase();
|
|
212
|
-
const parentClass = element.parentElement ? getClassNames(element.parentElement)[0] : undefined;
|
|
213
|
-
const ctx = parentClass ? `.${parentClass}` : "";
|
|
214
|
-
const counterKey = `${ctx}>${tag}`;
|
|
215
|
-
const count = tagCounters[counterKey] = (tagCounters[counterKey] ?? 0) + 1;
|
|
216
|
-
return `${ctx}>${tag}[${count}]`;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
function normalizeCssClassToken(token: string): string {
|
|
220
|
-
return token.replace(/\\([^\n\r\f])/g, "$1");
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
function collectDeclaredClassAliases(): Set<string> {
|
|
224
|
-
const aliases = new Set<string>();
|
|
225
|
-
const classPattern = /\.((?:\\.|[-_a-zA-Z0-9])+)/g;
|
|
226
|
-
|
|
227
|
-
function collectFromSelectorText(selectorText: string) {
|
|
228
|
-
let match: RegExpExecArray | null;
|
|
229
|
-
while ((match = classPattern.exec(selectorText))) {
|
|
230
|
-
aliases.add(`.${normalizeCssClassToken(match[1])}`);
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function visitRule(rule: { selectorText?: unknown; cssRules?: unknown }) {
|
|
235
|
-
if (typeof rule.selectorText === "string") {
|
|
236
|
-
collectFromSelectorText(rule.selectorText);
|
|
237
|
-
}
|
|
238
|
-
const nested = rule.cssRules;
|
|
239
|
-
if (!nested || typeof (nested as { length?: unknown }).length !== "number") return;
|
|
240
|
-
const nestedRules = nested as { length: number; [index: number]: unknown };
|
|
241
|
-
for (let i = 0; i < nestedRules.length; i++) {
|
|
242
|
-
const child = nestedRules[i];
|
|
243
|
-
if (child && typeof child === "object") {
|
|
244
|
-
visitRule(child as { selectorText?: unknown; cssRules?: unknown });
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
const styleSheets = (document as Document & { styleSheets?: StyleSheetList }).styleSheets;
|
|
250
|
-
if (!styleSheets) return aliases;
|
|
251
|
-
for (const sheet of Array.from(styleSheets)) {
|
|
252
|
-
try {
|
|
253
|
-
const rules = (sheet as CSSStyleSheet).cssRules;
|
|
254
|
-
for (const rule of Array.from(rules)) {
|
|
255
|
-
visitRule(rule as { selectorText?: unknown; cssRules?: unknown });
|
|
256
|
-
}
|
|
257
|
-
} catch { /* cross-origin stylesheet */ }
|
|
258
|
-
}
|
|
259
|
-
return aliases;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
function captureElementStyles(key: string, element: HTMLElement) {
|
|
263
|
-
if (!key || results[key]) return;
|
|
264
|
-
|
|
265
|
-
const computed = styleGetter(element);
|
|
266
|
-
const styles: Record<string, string> = {};
|
|
267
|
-
for (const prop of props) {
|
|
268
|
-
styles[prop] = computed.getPropertyValue(prop);
|
|
269
|
-
}
|
|
270
|
-
results[key] = styles;
|
|
271
|
-
|
|
272
|
-
for (const pseudo of ["::before", "::after"] as const) {
|
|
273
|
-
const pseudoComputed = styleGetter(element, pseudo);
|
|
274
|
-
const content = pseudoComputed.getPropertyValue("content");
|
|
275
|
-
if (!content || content === "none" || content === "normal") continue;
|
|
276
|
-
|
|
277
|
-
const pseudoStyles: Record<string, string> = {};
|
|
278
|
-
for (const prop of props) {
|
|
279
|
-
pseudoStyles[prop] = pseudoComputed.getPropertyValue(prop);
|
|
280
|
-
}
|
|
281
|
-
results[`${key}${pseudo}`] = pseudoStyles;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
const elements = document.querySelectorAll("*");
|
|
286
|
-
const tagCounters: Record<string, number> = {};
|
|
287
|
-
const declaredClassAliases = collectDeclaredClassAliases();
|
|
288
|
-
|
|
289
|
-
for (const element of elements) {
|
|
290
|
-
const he = element as HTMLElement;
|
|
291
|
-
if (!shouldTrackElement(he)) continue;
|
|
292
|
-
const key = buildElementKey(he, tagCounters);
|
|
293
|
-
captureElementStyles(key, he);
|
|
294
|
-
for (const className of getClassNames(he)) {
|
|
295
|
-
const alias = `.${className}`;
|
|
296
|
-
if (declaredClassAliases.has(alias)) {
|
|
297
|
-
captureElementStyles(alias, he);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
return results;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
export function captureComputedStyleSnapshotForTargetSelectorsInDom(input: {
|
|
306
|
-
props: string[];
|
|
307
|
-
selectors: string[];
|
|
308
|
-
}): ComputedStyleSnapshot {
|
|
309
|
-
const { props, selectors } = input;
|
|
310
|
-
const results: ComputedStyleSnapshot = {};
|
|
311
|
-
const semanticTags = new Set([
|
|
312
|
-
"main", "nav", "header", "footer", "aside", "article", "section",
|
|
313
|
-
"table", "thead", "tbody", "tr", "th", "td", "ul", "ol", "li",
|
|
314
|
-
"h1", "h2", "h3", "h4", "h5", "h6", "p", "a", "button", "input", "select", "textarea",
|
|
315
|
-
"pre", "code", "blockquote", "img", "span", "div", "form", "label",
|
|
316
|
-
]);
|
|
317
|
-
|
|
318
|
-
function getClassNames(element: {
|
|
319
|
-
classList?: unknown;
|
|
320
|
-
className?: unknown;
|
|
321
|
-
}): string[] {
|
|
322
|
-
const classList = element.classList;
|
|
323
|
-
if (classList && typeof classList === "object") {
|
|
324
|
-
if (Symbol.iterator in classList) {
|
|
325
|
-
return Array.from(classList as Iterable<unknown>)
|
|
326
|
-
.map((value) => String(value))
|
|
327
|
-
.filter(Boolean);
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
const length = Number((classList as { length?: number }).length ?? 0);
|
|
331
|
-
const values: string[] = [];
|
|
332
|
-
for (let i = 0; i < length; i++) {
|
|
333
|
-
const token = String((classList as Record<number, unknown>)[i] ?? "");
|
|
334
|
-
if (token) values.push(token);
|
|
335
|
-
}
|
|
336
|
-
if (values.length > 0) return values;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
if (typeof element.className === "string") {
|
|
340
|
-
return element.className.split(/\s+/).filter(Boolean);
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
return [];
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
const getStyle = typeof getComputedStyle === "function"
|
|
347
|
-
? getComputedStyle
|
|
348
|
-
: typeof window !== "undefined" && typeof window.getComputedStyle === "function"
|
|
349
|
-
? window.getComputedStyle.bind(window)
|
|
350
|
-
: null;
|
|
351
|
-
if (!getStyle) {
|
|
352
|
-
throw new Error("getComputedStyle is not available in this realm");
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
function shouldTrackElement(element: HTMLElement): boolean {
|
|
356
|
-
if (element.id) return true;
|
|
357
|
-
if (getClassNames(element).length > 0) return true;
|
|
358
|
-
return semanticTags.has(element.tagName.toLowerCase());
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
function buildElementKey(
|
|
362
|
-
element: HTMLElement,
|
|
363
|
-
tagCounters: Record<string, number>,
|
|
364
|
-
): string {
|
|
365
|
-
if (element.id) return `#${element.id}`;
|
|
366
|
-
const classNames = getClassNames(element);
|
|
367
|
-
if (classNames.length > 0) return `.${classNames.join(".")}`;
|
|
368
|
-
|
|
369
|
-
const tag = element.tagName.toLowerCase();
|
|
370
|
-
const parentClass = element.parentElement ? getClassNames(element.parentElement)[0] : undefined;
|
|
371
|
-
const ctx = parentClass ? `.${parentClass}` : "";
|
|
372
|
-
const counterKey = `${ctx}>${tag}`;
|
|
373
|
-
const count = tagCounters[counterKey] = (tagCounters[counterKey] ?? 0) + 1;
|
|
374
|
-
return `${ctx}>${tag}[${count}]`;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
const targets: Element[] = [];
|
|
378
|
-
for (const selector of selectors) {
|
|
379
|
-
try {
|
|
380
|
-
targets.push(...document.querySelectorAll(selector));
|
|
381
|
-
} catch { /* invalid selector */ }
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
const tagCounters: Record<string, number> = {};
|
|
385
|
-
for (const element of targets) {
|
|
386
|
-
const he = element as HTMLElement;
|
|
387
|
-
if (!shouldTrackElement(he)) continue;
|
|
388
|
-
const key = buildElementKey(he, tagCounters);
|
|
389
|
-
if (!key || results[key]) continue;
|
|
390
|
-
|
|
391
|
-
const computed = getStyle(he);
|
|
392
|
-
const styles: Record<string, string> = {};
|
|
393
|
-
for (const prop of props) {
|
|
394
|
-
styles[prop] = computed.getPropertyValue(prop);
|
|
395
|
-
}
|
|
396
|
-
results[key] = styles;
|
|
397
|
-
|
|
398
|
-
for (const pseudo of ["::before", "::after"] as const) {
|
|
399
|
-
const pseudoComputed = getStyle(he, pseudo);
|
|
400
|
-
const content = pseudoComputed.getPropertyValue("content");
|
|
401
|
-
if (!content || content === "none" || content === "normal") continue;
|
|
402
|
-
|
|
403
|
-
const pseudoStyles: Record<string, string> = {};
|
|
404
|
-
for (const prop of props) {
|
|
405
|
-
pseudoStyles[prop] = pseudoComputed.getPropertyValue(prop);
|
|
406
|
-
}
|
|
407
|
-
results[`${key}${pseudo}`] = pseudoStyles;
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
return results;
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
export function collectInteractionTargetPlansInDom(): InteractionTargetPlan[] {
|
|
415
|
-
const selectorTexts: string[] = [];
|
|
416
|
-
|
|
417
|
-
for (const sheet of document.styleSheets) {
|
|
418
|
-
try {
|
|
419
|
-
for (const rule of sheet.cssRules) {
|
|
420
|
-
const isStyleRule = typeof CSSStyleRule === "undefined"
|
|
421
|
-
? typeof (rule as { selectorText?: unknown }).selectorText === "string"
|
|
422
|
-
: rule instanceof CSSStyleRule;
|
|
423
|
-
if (!isStyleRule) continue;
|
|
424
|
-
const selectorText = (rule as CSSStyleRule).selectorText;
|
|
425
|
-
if (!/:(focus-visible|focus-within|focus|hover|active)\b/.test(selectorText)) continue;
|
|
426
|
-
selectorTexts.push(selectorText);
|
|
427
|
-
}
|
|
428
|
-
} catch { /* cross-origin */ }
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
const plans: InteractionTargetPlan[] = [];
|
|
432
|
-
const seen = new Set<string>();
|
|
433
|
-
for (const selectorText of selectorTexts) {
|
|
434
|
-
for (const rawSelector of selectorText.split(",")) {
|
|
435
|
-
const selector = rawSelector.trim();
|
|
436
|
-
if (!selector || !/:(focus-visible|focus-within|focus|hover|active)\b/.test(selector)) continue;
|
|
437
|
-
const normalizedSelector = selector
|
|
438
|
-
.replace(/:(focus-visible|focus-within|focus|hover|active)\b/g, "")
|
|
439
|
-
.replace(/::[\w-]+/g, "")
|
|
440
|
-
.replace(/\s+/g, " ")
|
|
441
|
-
.replace(/\s*([>+~])\s*/g, " $1 ")
|
|
442
|
-
.trim();
|
|
443
|
-
if (!normalizedSelector) continue;
|
|
444
|
-
const forcedStates: ForcedPseudoState[] = [];
|
|
445
|
-
{
|
|
446
|
-
const seenStates = new Set<string>();
|
|
447
|
-
const pattern = /:(focus-visible|focus-within|focus|hover|active)\b/g;
|
|
448
|
-
let m: RegExpExecArray | null;
|
|
449
|
-
while ((m = pattern.exec(selector)) !== null) {
|
|
450
|
-
const s = m[1] as ForcedPseudoState;
|
|
451
|
-
if (seenStates.has(s)) continue;
|
|
452
|
-
seenStates.add(s);
|
|
453
|
-
forcedStates.push(s);
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
if (forcedStates.length === 0) continue;
|
|
457
|
-
const interaction: InteractionType = forcedStates.includes("active")
|
|
458
|
-
? "active"
|
|
459
|
-
: forcedStates.some((s) => s === "focus" || s === "focus-visible" || s === "focus-within")
|
|
460
|
-
? "focus"
|
|
461
|
-
: "hover";
|
|
462
|
-
const key = `${interaction}\u0000${normalizedSelector}\u0000${forcedStates.join(",")}`;
|
|
463
|
-
if (seen.has(key)) continue;
|
|
464
|
-
seen.add(key);
|
|
465
|
-
plans.push({ selector, normalizedSelector, interaction, forcedStates });
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
return plans;
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
export async function waitForInteractionStylesInDom(): Promise<void> {
|
|
473
|
-
try {
|
|
474
|
-
void (document.documentElement as HTMLElement | undefined)?.offsetHeight;
|
|
475
|
-
} catch { /* ignore */ }
|
|
476
|
-
|
|
477
|
-
const waitFrame = () => new Promise<void>((resolve) => {
|
|
478
|
-
if (typeof requestAnimationFrame === "function") {
|
|
479
|
-
requestAnimationFrame(() => resolve());
|
|
480
|
-
return;
|
|
481
|
-
}
|
|
482
|
-
setTimeout(resolve, 0);
|
|
483
|
-
});
|
|
484
|
-
|
|
485
|
-
await waitFrame();
|
|
486
|
-
await waitFrame();
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
export async function captureEmulatedInteractionStyleSnapshotInDom(
|
|
490
|
-
props: string[],
|
|
491
|
-
): Promise<ComputedStyleSnapshot> {
|
|
492
|
-
const results: ComputedStyleSnapshot = {};
|
|
493
|
-
const semanticTags = new Set([
|
|
494
|
-
"main", "nav", "header", "footer", "aside", "article", "section",
|
|
495
|
-
"table", "thead", "tbody", "tr", "th", "td", "ul", "ol", "li",
|
|
496
|
-
"h1", "h2", "h3", "h4", "h5", "h6", "p", "a", "button", "input", "select", "textarea",
|
|
497
|
-
"pre", "code", "blockquote", "img", "span", "div", "form", "label",
|
|
498
|
-
]);
|
|
499
|
-
|
|
500
|
-
function getClassNames(element: {
|
|
501
|
-
classList?: unknown;
|
|
502
|
-
className?: unknown;
|
|
503
|
-
}): string[] {
|
|
504
|
-
const classList = element.classList;
|
|
505
|
-
if (classList && typeof classList === "object") {
|
|
506
|
-
if (Symbol.iterator in classList) {
|
|
507
|
-
return Array.from(classList as Iterable<unknown>)
|
|
508
|
-
.map((value) => String(value))
|
|
509
|
-
.filter(Boolean);
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
const length = Number((classList as { length?: number }).length ?? 0);
|
|
513
|
-
const values: string[] = [];
|
|
514
|
-
for (let i = 0; i < length; i++) {
|
|
515
|
-
const token = String((classList as Record<number, unknown>)[i] ?? "");
|
|
516
|
-
if (token) values.push(token);
|
|
517
|
-
}
|
|
518
|
-
if (values.length > 0) return values;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
if (typeof element.className === "string") {
|
|
522
|
-
return element.className.split(/\s+/).filter(Boolean);
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
return [];
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
const getStyle = typeof getComputedStyle === "function"
|
|
529
|
-
? getComputedStyle
|
|
530
|
-
: typeof window !== "undefined" && typeof window.getComputedStyle === "function"
|
|
531
|
-
? window.getComputedStyle.bind(window)
|
|
532
|
-
: null;
|
|
533
|
-
if (!getStyle) {
|
|
534
|
-
throw new Error("getComputedStyle is not available in this realm");
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
function shouldTrackElement(element: HTMLElement): boolean {
|
|
538
|
-
if (element.id) return true;
|
|
539
|
-
if (getClassNames(element).length > 0) return true;
|
|
540
|
-
return semanticTags.has(element.tagName.toLowerCase());
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
function buildElementKey(
|
|
544
|
-
element: HTMLElement,
|
|
545
|
-
tagCounters: Record<string, number>,
|
|
546
|
-
): string {
|
|
547
|
-
if (element.id) return `#${element.id}`;
|
|
548
|
-
const classNames = getClassNames(element);
|
|
549
|
-
if (classNames.length > 0) return `.${classNames.join(".")}`;
|
|
550
|
-
|
|
551
|
-
const tag = element.tagName.toLowerCase();
|
|
552
|
-
const parentClass = element.parentElement ? getClassNames(element.parentElement)[0] : undefined;
|
|
553
|
-
const ctx = parentClass ? `.${parentClass}` : "";
|
|
554
|
-
const counterKey = `${ctx}>${tag}`;
|
|
555
|
-
const count = tagCounters[counterKey] = (tagCounters[counterKey] ?? 0) + 1;
|
|
556
|
-
return `${ctx}>${tag}[${count}]`;
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
const emulatedRules: string[] = [];
|
|
560
|
-
const targetSelectors = new Set<string>();
|
|
561
|
-
|
|
562
|
-
for (const sheet of document.styleSheets) {
|
|
563
|
-
try {
|
|
564
|
-
for (const rule of sheet.cssRules) {
|
|
565
|
-
const isStyleRule = typeof CSSStyleRule === "undefined"
|
|
566
|
-
? typeof (rule as { selectorText?: unknown }).selectorText === "string"
|
|
567
|
-
: rule instanceof CSSStyleRule;
|
|
568
|
-
if (!isStyleRule) continue;
|
|
569
|
-
const selectorText = (rule as CSSStyleRule).selectorText;
|
|
570
|
-
if (!/:(focus-visible|focus-within|focus|hover|active)\b/.test(selectorText)) continue;
|
|
571
|
-
|
|
572
|
-
const emulatedSelector = selectorText
|
|
573
|
-
.replace(/:(focus-visible|focus-within|focus|hover|active)\b/g, "")
|
|
574
|
-
.replace(/\s+/g, " ")
|
|
575
|
-
.replace(/\s*([>+~])\s*/g, " $1 ")
|
|
576
|
-
.trim();
|
|
577
|
-
if (emulatedSelector) {
|
|
578
|
-
emulatedRules.push(`${emulatedSelector} { ${(rule as CSSStyleRule).style.cssText} }`);
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
for (const rawSelector of selectorText.split(",")) {
|
|
582
|
-
const normalizedSelector = rawSelector
|
|
583
|
-
.trim()
|
|
584
|
-
.replace(/:(focus-visible|focus-within|focus|hover|active)\b/g, "")
|
|
585
|
-
.replace(/::[\w-]+/g, "")
|
|
586
|
-
.replace(/\s+/g, " ")
|
|
587
|
-
.replace(/\s*([>+~])\s*/g, " $1 ")
|
|
588
|
-
.trim();
|
|
589
|
-
if (normalizedSelector) targetSelectors.add(normalizedSelector);
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
} catch { /* cross-origin */ }
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
if (emulatedRules.length === 0 || targetSelectors.size === 0) return {};
|
|
596
|
-
|
|
597
|
-
const style = document.createElement("style");
|
|
598
|
-
style.id = "__hover_emulation__";
|
|
599
|
-
style.textContent = emulatedRules.join("\n");
|
|
600
|
-
(document.head ?? document.documentElement).appendChild(style);
|
|
601
|
-
|
|
602
|
-
try {
|
|
603
|
-
try {
|
|
604
|
-
void (document.documentElement as HTMLElement | undefined)?.offsetHeight;
|
|
605
|
-
} catch { /* ignore */ }
|
|
606
|
-
|
|
607
|
-
const waitFrame = () => new Promise<void>((resolve) => {
|
|
608
|
-
if (typeof requestAnimationFrame === "function") {
|
|
609
|
-
requestAnimationFrame(() => resolve());
|
|
610
|
-
return;
|
|
611
|
-
}
|
|
612
|
-
setTimeout(resolve, 0);
|
|
613
|
-
});
|
|
614
|
-
|
|
615
|
-
await waitFrame();
|
|
616
|
-
await waitFrame();
|
|
617
|
-
|
|
618
|
-
const selectorText = [...targetSelectors].join(", ");
|
|
619
|
-
let targets: NodeListOf<Element>;
|
|
620
|
-
try {
|
|
621
|
-
targets = document.querySelectorAll(selectorText);
|
|
622
|
-
} catch {
|
|
623
|
-
targets = document.querySelectorAll("*");
|
|
624
|
-
}
|
|
625
|
-
const tagCounters: Record<string, number> = {};
|
|
626
|
-
|
|
627
|
-
for (const element of targets) {
|
|
628
|
-
const he = element as HTMLElement;
|
|
629
|
-
if (!shouldTrackElement(he)) continue;
|
|
630
|
-
const key = buildElementKey(he, tagCounters);
|
|
631
|
-
if (!key || results[key]) continue;
|
|
632
|
-
|
|
633
|
-
const computed = getStyle(he);
|
|
634
|
-
const styles: Record<string, string> = {};
|
|
635
|
-
for (const prop of props) {
|
|
636
|
-
styles[prop] = computed.getPropertyValue(prop);
|
|
637
|
-
}
|
|
638
|
-
results[key] = styles;
|
|
639
|
-
|
|
640
|
-
for (const pseudo of ["::before", "::after"] as const) {
|
|
641
|
-
const pseudoComputed = getStyle(he, pseudo);
|
|
642
|
-
const content = pseudoComputed.getPropertyValue("content");
|
|
643
|
-
if (!content || content === "none" || content === "normal") continue;
|
|
644
|
-
|
|
645
|
-
const pseudoStyles: Record<string, string> = {};
|
|
646
|
-
for (const prop of props) {
|
|
647
|
-
pseudoStyles[prop] = pseudoComputed.getPropertyValue(prop);
|
|
648
|
-
}
|
|
649
|
-
results[`${key}${pseudo}`] = pseudoStyles;
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
return results;
|
|
654
|
-
} finally {
|
|
655
|
-
style.remove();
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
export function computedStyleSnapshotToMap(
|
|
660
|
-
snapshot: ComputedStyleSnapshot,
|
|
661
|
-
): Map<string, Record<string, string>> {
|
|
662
|
-
const map = new Map<string, Record<string, string>>();
|
|
663
|
-
for (const [selector, props] of Object.entries(snapshot)) {
|
|
664
|
-
map.set(selector, props);
|
|
665
|
-
}
|
|
666
|
-
return map;
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
// esbuild injects __name(fn, "name") calls into Function.toString() output.
|
|
670
|
-
// Polyfill it as a no-op for page.evaluate contexts.
|
|
671
|
-
export const ESBUILD_NAME_POLYFILL = "var __name = typeof __name !== 'undefined' ? __name : function(fn) { return fn; };";
|
|
672
|
-
|
|
673
|
-
export function buildComputedStyleCaptureExpression(
|
|
674
|
-
props: string[] = TRACKED_PROPERTIES,
|
|
675
|
-
): string {
|
|
676
|
-
return `(function(){ ${ESBUILD_NAME_POLYFILL} return (${captureComputedStyleSnapshotInDom.toString()})(${JSON.stringify(props)}); })()`;
|
|
677
|
-
}
|
|
678
|
-
|
|
679
|
-
export function buildComputedStyleCaptureJsonExpression(
|
|
680
|
-
props: string[] = TRACKED_PROPERTIES,
|
|
681
|
-
): string {
|
|
682
|
-
return `JSON.stringify(${buildComputedStyleCaptureExpression(props)})`;
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
export function parseComputedStyleSnapshot(value: unknown): ComputedStyleSnapshot {
|
|
686
|
-
const candidate = typeof value === "string" ? safeJsonParse(value) : value;
|
|
687
|
-
if (!isRecord(candidate)) return {};
|
|
688
|
-
|
|
689
|
-
const snapshot: ComputedStyleSnapshot = {};
|
|
690
|
-
for (const [selector, props] of Object.entries(candidate)) {
|
|
691
|
-
if (!isRecord(props)) continue;
|
|
692
|
-
const normalized: Record<string, string> = {};
|
|
693
|
-
for (const [prop, propValue] of Object.entries(props)) {
|
|
694
|
-
normalized[prop] = typeof propValue === "string" ? propValue : String(propValue ?? "");
|
|
695
|
-
}
|
|
696
|
-
snapshot[selector] = normalized;
|
|
697
|
-
}
|
|
698
|
-
return snapshot;
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
export function hasMeaningfulComputedStyleSnapshot(snapshot: ComputedStyleSnapshot): boolean {
|
|
702
|
-
return Object.values(snapshot).some((props) =>
|
|
703
|
-
Object.values(props).some((value) => value.trim().length > 0)
|
|
704
|
-
);
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
function safeJsonParse(value: string): unknown {
|
|
708
|
-
try {
|
|
709
|
-
return JSON.parse(value);
|
|
710
|
-
} catch {
|
|
711
|
-
return null;
|
|
712
|
-
}
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
716
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
717
|
-
}
|