@mizchi/vlmkit-core 0.6.0 → 0.8.0
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/README.md +9 -6
- package/package.json +3 -3
- package/src/authored-style-capture.ts +185 -0
- package/src/authored-style-diff.ts +32 -0
- package/src/computed-style-capture.ts +143 -22
- package/src/element-compare.ts +25 -10
- package/src/heatmap-regions.ts +5 -6
- package/src/heatmap.ts +155 -1
- package/src/image-resize.ts +1 -1
- package/src/index.ts +5 -8
- package/src/png-utils.ts +30 -0
- package/src/region-shift.ts +173 -0
- package/src/run-ledger.ts +39 -0
- package/src/text-rows.ts +2 -0
- package/src/types.ts +65 -0
- package/src/a11y-contrast.ts +0 -387
- package/src/a11y-focus-order.ts +0 -397
- package/src/a11y-touch.ts +0 -340
- package/src/grid-ratio.ts +0 -251
- package/src/png-diff.ts +0 -165
- package/src/quality.ts +0 -209
- package/src/region-classify.ts +0 -189
- package/src/shift-origin.ts +0 -204
- package/src/visual-semantic.ts +0 -185
package/README.md
CHANGED
|
@@ -19,20 +19,23 @@ The package ships both a curated barrel and deep per-module exports.
|
|
|
19
19
|
// Curated barrel (excludes CLI / Playwright modules)
|
|
20
20
|
import { diffPng, type VrtDiff } from "@mizchi/vlmkit-core";
|
|
21
21
|
|
|
22
|
-
// Deep import (use for
|
|
23
|
-
import {
|
|
22
|
+
// Deep import (use for low-level modules not in the barrel)
|
|
23
|
+
import { decodePng } from "@mizchi/vlmkit-core/png-utils.ts";
|
|
24
24
|
```
|
|
25
25
|
|
|
26
26
|
## What's included
|
|
27
27
|
|
|
28
28
|
| Layer | Module | Purpose |
|
|
29
29
|
|---|---|---|
|
|
30
|
-
| Image diff | `
|
|
31
|
-
| DOM | `dom-equivalence`, `dom-position-styles`, `computed-style-diff`, `computed-style-capture` | DOM structural / style comparison. |
|
|
32
|
-
| Semantic | `a11y-semantic
|
|
33
|
-
| CLI tools (deep import) | `a11y-contrast`, `a11y-touch`, `a11y-focus-order`, `element-compare`, `mask` | Run via the `vrt` CLI or import the exported `runXxx` functions. |
|
|
30
|
+
| Image diff | `heatmap`, `heatmap-regions`, `diff-regions`, `text-rows`, `image-resize` | Pixel-level diff + region clustering + shift compensation. |
|
|
31
|
+
| DOM | `dom-equivalence`, `dom-position-styles`, `computed-style-diff`, `computed-style-capture`, `authored-style-capture`, `authored-style-diff` | DOM structural / style comparison. |
|
|
32
|
+
| Semantic | `a11y-semantic` | Accessibility-tree diff. |
|
|
34
33
|
| Shared | `types`, `terminal-colors`, `cli-args`, `cli-error`, `png-utils` | Cross-package types and utilities. |
|
|
35
34
|
|
|
35
|
+
Playwright-driven CLIs (`a11y-contrast`, `a11y-touch`, `a11y-focus-order`,
|
|
36
|
+
`png-diff`, `region-classify`, `visual-semantic`, `quality`, etc.) and
|
|
37
|
+
their MoonBit-backed policy live in `@mizchi/vlmkit-markup`.
|
|
38
|
+
|
|
36
39
|
## License
|
|
37
40
|
|
|
38
41
|
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mizchi/vlmkit-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "VRT diff engine + shared types — pure image / DOM / a11y / visual comparison primitives.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "mizchi",
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"pixelmatch": "^7.1.0",
|
|
29
|
-
"
|
|
30
|
-
"
|
|
29
|
+
"playwright": "^1.61.1",
|
|
30
|
+
"pngjs": "^7.0.0"
|
|
31
31
|
},
|
|
32
32
|
"scripts": {
|
|
33
33
|
"test": "node --test 'src/**/*.test.ts'"
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authored CSS capture for properties whose computed form lies about the
|
|
3
|
+
* authored shape.
|
|
4
|
+
*
|
|
5
|
+
* Motivating case (2026-05-23 dogfood, app-shell `.shell`):
|
|
6
|
+
* `getComputedStyle().getPropertyValue("grid-template-columns")` resolves
|
|
7
|
+
* `minmax(0, 1fr) 4px minmax(0, 1fr)` to a three px-tuple — but writing
|
|
8
|
+
* those numbers back as CSS would corrupt the responsive layout. The fix is
|
|
9
|
+
* not to track grid-template-* in computed-style at all; instead, walk
|
|
10
|
+
* `document.styleSheets.cssRules` to emit the AUTHORED strings and diff
|
|
11
|
+
* THEM. Same approach scales to `flex` shorthand, `transform`, and any
|
|
12
|
+
* other property whose computed form diverges from authored.
|
|
13
|
+
*
|
|
14
|
+
* Surface mirrors `computed-style-capture` so consumers can reuse
|
|
15
|
+
* `diffComputedStyles` (it's just `Record<selector, Record<prop, value>>`
|
|
16
|
+
* → `Record<selector, Record<prop, value>>` either way).
|
|
17
|
+
*/
|
|
18
|
+
/// <reference lib="dom" />
|
|
19
|
+
|
|
20
|
+
export type AuthoredStyleSnapshot = Record<string, Record<string, string>>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Properties tracked by default. These are the ones whose computed
|
|
24
|
+
* representation differs structurally from the authored source — anything
|
|
25
|
+
* already faithful in `getComputedStyle` does not need this channel.
|
|
26
|
+
*/
|
|
27
|
+
export const AUTHORED_PROPERTIES = [
|
|
28
|
+
"grid-template",
|
|
29
|
+
"grid-template-columns",
|
|
30
|
+
"grid-template-rows",
|
|
31
|
+
"grid-template-areas",
|
|
32
|
+
"grid-auto-columns",
|
|
33
|
+
"grid-auto-rows",
|
|
34
|
+
"grid-auto-flow",
|
|
35
|
+
"grid-column",
|
|
36
|
+
"grid-row",
|
|
37
|
+
"grid",
|
|
38
|
+
"flex",
|
|
39
|
+
"transform",
|
|
40
|
+
"clip-path",
|
|
41
|
+
"mask",
|
|
42
|
+
"mask-image",
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
const MEDIA_SEPARATOR = " :: ";
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Format a "scoped" selector key. When `mediaCondition` is empty, returns
|
|
49
|
+
* the bare selector text. When non-empty, the key reads
|
|
50
|
+
* `@media (min-width: 768px) :: .shell` so the diff treats the same
|
|
51
|
+
* selector inside a different `@media` block as a distinct row.
|
|
52
|
+
*/
|
|
53
|
+
function buildScopedSelectorKey(selector: string, mediaCondition: string): string {
|
|
54
|
+
const trimmedMedia = mediaCondition.trim();
|
|
55
|
+
if (!trimmedMedia) return selector;
|
|
56
|
+
return `@media ${trimmedMedia}${MEDIA_SEPARATOR}${selector}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Browser-context capture. Walks all same-origin stylesheets and nested
|
|
61
|
+
* `@media` / `@supports` rules, collecting authored `value` strings for
|
|
62
|
+
* each (scoped-selector, property) pair listed in `props`. Last
|
|
63
|
+
* declaration wins per pair (mirrors authoring order; sufficient for
|
|
64
|
+
* static diff use cases).
|
|
65
|
+
*
|
|
66
|
+
* Selectors are comma-split so `.a, .b { grid-template-columns: ... }`
|
|
67
|
+
* fans out into both `.a` and `.b` entries.
|
|
68
|
+
*/
|
|
69
|
+
export function captureAuthoredStyleSnapshotInDom(props: string[]): AuthoredStyleSnapshot {
|
|
70
|
+
const trackedProps = new Set(props);
|
|
71
|
+
const results: AuthoredStyleSnapshot = {};
|
|
72
|
+
|
|
73
|
+
function visit(rule: unknown, mediaCondition: string) {
|
|
74
|
+
if (!rule || typeof rule !== "object") return;
|
|
75
|
+
const ruleObj = rule as {
|
|
76
|
+
selectorText?: unknown;
|
|
77
|
+
style?: { getPropertyValue?: (name: string) => string } | null;
|
|
78
|
+
conditionText?: unknown;
|
|
79
|
+
media?: { mediaText?: unknown } | null;
|
|
80
|
+
cssRules?: ArrayLike<unknown>;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// @media / @supports / @container — descend with the media condition extended.
|
|
84
|
+
const conditionFromMedia = typeof ruleObj.media?.mediaText === "string"
|
|
85
|
+
? ruleObj.media!.mediaText
|
|
86
|
+
: "";
|
|
87
|
+
const conditionFromAt = typeof ruleObj.conditionText === "string"
|
|
88
|
+
? ruleObj.conditionText
|
|
89
|
+
: "";
|
|
90
|
+
const additionalCondition = conditionFromMedia || conditionFromAt;
|
|
91
|
+
const nestedMedia = additionalCondition
|
|
92
|
+
? mediaCondition
|
|
93
|
+
? `${mediaCondition} and ${additionalCondition}`
|
|
94
|
+
: additionalCondition
|
|
95
|
+
: mediaCondition;
|
|
96
|
+
|
|
97
|
+
if (ruleObj.cssRules && typeof (ruleObj.cssRules as { length?: unknown }).length === "number") {
|
|
98
|
+
const nested = ruleObj.cssRules as ArrayLike<unknown>;
|
|
99
|
+
for (let i = 0; i < nested.length; i++) {
|
|
100
|
+
visit(nested[i], nestedMedia);
|
|
101
|
+
}
|
|
102
|
+
// Containers like @media/@supports never carry selectorText themselves.
|
|
103
|
+
if (!ruleObj.selectorText) return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (typeof ruleObj.selectorText !== "string") return;
|
|
107
|
+
const style = ruleObj.style;
|
|
108
|
+
if (!style || typeof style.getPropertyValue !== "function") return;
|
|
109
|
+
|
|
110
|
+
for (const rawSelector of ruleObj.selectorText.split(",")) {
|
|
111
|
+
const selector = rawSelector.trim();
|
|
112
|
+
if (!selector) continue;
|
|
113
|
+
const key = buildScopedSelectorKey(selector, mediaCondition);
|
|
114
|
+
for (const prop of trackedProps) {
|
|
115
|
+
const value = style.getPropertyValue(prop);
|
|
116
|
+
if (!value) continue;
|
|
117
|
+
const bucket = results[key] ?? (results[key] = {});
|
|
118
|
+
bucket[prop] = value.trim();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const styleSheets = (document as Document & { styleSheets?: StyleSheetList }).styleSheets;
|
|
124
|
+
if (!styleSheets) return results;
|
|
125
|
+
for (const sheet of Array.from(styleSheets)) {
|
|
126
|
+
try {
|
|
127
|
+
const rules = (sheet as CSSStyleSheet).cssRules;
|
|
128
|
+
for (const rule of Array.from(rules)) {
|
|
129
|
+
visit(rule, "");
|
|
130
|
+
}
|
|
131
|
+
} catch { /* cross-origin stylesheet */ }
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return results;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// esbuild injects __name(fn, "name") calls into Function.toString() output.
|
|
138
|
+
const ESBUILD_NAME_POLYFILL =
|
|
139
|
+
"var __name = typeof __name !== 'undefined' ? __name : function(fn) { return fn; };";
|
|
140
|
+
|
|
141
|
+
export function buildAuthoredStyleCaptureExpression(
|
|
142
|
+
props: string[] = AUTHORED_PROPERTIES,
|
|
143
|
+
): string {
|
|
144
|
+
return `(function(){ ${ESBUILD_NAME_POLYFILL} return (${captureAuthoredStyleSnapshotInDom.toString()})(${JSON.stringify(props)}); })()`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function buildAuthoredStyleCaptureJsonExpression(
|
|
148
|
+
props: string[] = AUTHORED_PROPERTIES,
|
|
149
|
+
): string {
|
|
150
|
+
return `JSON.stringify(${buildAuthoredStyleCaptureExpression(props)})`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function parseAuthoredStyleSnapshot(value: unknown): AuthoredStyleSnapshot {
|
|
154
|
+
const candidate = typeof value === "string" ? safeJsonParse(value) : value;
|
|
155
|
+
if (!isRecord(candidate)) return {};
|
|
156
|
+
|
|
157
|
+
const snapshot: AuthoredStyleSnapshot = {};
|
|
158
|
+
for (const [selector, props] of Object.entries(candidate)) {
|
|
159
|
+
if (!isRecord(props)) continue;
|
|
160
|
+
const normalized: Record<string, string> = {};
|
|
161
|
+
for (const [prop, propValue] of Object.entries(props)) {
|
|
162
|
+
normalized[prop] = typeof propValue === "string" ? propValue : String(propValue ?? "");
|
|
163
|
+
}
|
|
164
|
+
snapshot[selector] = normalized;
|
|
165
|
+
}
|
|
166
|
+
return snapshot;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function hasMeaningfulAuthoredStyleSnapshot(snapshot: AuthoredStyleSnapshot): boolean {
|
|
170
|
+
return Object.values(snapshot).some((props) =>
|
|
171
|
+
Object.values(props).some((value) => value.trim().length > 0)
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function safeJsonParse(value: string): unknown {
|
|
176
|
+
try {
|
|
177
|
+
return JSON.parse(value);
|
|
178
|
+
} catch {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
184
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
185
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authored-style diff. Same shape as `computed-style-diff` (a snapshot is
|
|
3
|
+
* `Record<scopedSelector, Record<property, value>>` in both cases), so the
|
|
4
|
+
* heavy lifting reuses `diffComputedStyles` — only the snapshot type
|
|
5
|
+
* label differs.
|
|
6
|
+
*/
|
|
7
|
+
import { diffComputedStyles, aggregateCsdByViewport } from "./computed-style-diff.ts";
|
|
8
|
+
import type {
|
|
9
|
+
CsdEntry,
|
|
10
|
+
CsdResult,
|
|
11
|
+
CsdPerViewportResult,
|
|
12
|
+
CsdPerViewportEntry,
|
|
13
|
+
} from "./computed-style-diff.ts";
|
|
14
|
+
import type { AuthoredStyleSnapshot } from "./authored-style-capture.ts";
|
|
15
|
+
|
|
16
|
+
export type AuthoredStyleEntry = CsdEntry;
|
|
17
|
+
export type AuthoredStyleDiffResult = CsdResult;
|
|
18
|
+
export type AuthoredStylePerViewportEntry = CsdPerViewportEntry;
|
|
19
|
+
export type AuthoredStylePerViewportResult = CsdPerViewportResult;
|
|
20
|
+
|
|
21
|
+
export function diffAuthoredStyles(
|
|
22
|
+
baseline: AuthoredStyleSnapshot,
|
|
23
|
+
variant: AuthoredStyleSnapshot,
|
|
24
|
+
): AuthoredStyleDiffResult {
|
|
25
|
+
return diffComputedStyles(baseline, variant);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function aggregateAuthoredStyleByViewport(
|
|
29
|
+
perViewport: ReadonlyArray<{ viewport: string; result: AuthoredStyleDiffResult }>,
|
|
30
|
+
): AuthoredStylePerViewportResult {
|
|
31
|
+
return aggregateCsdByViewport(perViewport);
|
|
32
|
+
}
|
|
@@ -1,11 +1,51 @@
|
|
|
1
1
|
/// <reference lib="dom" />
|
|
2
2
|
export type ComputedStyleSnapshot = Record<string, Record<string, string>>;
|
|
3
|
-
export type InteractionType = "hover" | "focus";
|
|
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";
|
|
4
12
|
|
|
5
13
|
export interface InteractionTargetPlan {
|
|
6
14
|
selector: string;
|
|
7
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
|
+
*/
|
|
8
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";
|
|
9
49
|
}
|
|
10
50
|
|
|
11
51
|
export const TRACKED_PROPERTIES = [
|
|
@@ -24,11 +64,16 @@ export const TRACKED_PROPERTIES = [
|
|
|
24
64
|
"flex-direction", "flex-wrap", "flex-grow", "flex-shrink",
|
|
25
65
|
"align-items", "justify-content", "gap",
|
|
26
66
|
"position", "top", "right", "bottom", "left",
|
|
27
|
-
"overflow", "box-shadow", "cursor",
|
|
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",
|
|
28
74
|
];
|
|
29
75
|
|
|
30
76
|
const INTERACTION_PSEUDO_PATTERN = /:(focus-visible|focus-within|focus|hover|active)\b/g;
|
|
31
|
-
const FOCUS_PATTERN = /:(focus-visible|focus-within|focus)\b/;
|
|
32
77
|
const INTERACTION_SELECTOR_PATTERN = /:(focus-visible|focus-within|focus|hover|active)\b/;
|
|
33
78
|
|
|
34
79
|
export function normalizeInteractionSelector(selector: string): string {
|
|
@@ -52,11 +97,13 @@ export function buildInteractionTargetPlans(
|
|
|
52
97
|
if (!selector || !INTERACTION_SELECTOR_PATTERN.test(selector)) continue;
|
|
53
98
|
const normalizedSelector = normalizeInteractionSelector(selector);
|
|
54
99
|
if (!normalizedSelector) continue;
|
|
55
|
-
const
|
|
56
|
-
|
|
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(",")}`;
|
|
57
104
|
if (seen.has(key)) continue;
|
|
58
105
|
seen.add(key);
|
|
59
|
-
plans.push({ selector, normalizedSelector, interaction });
|
|
106
|
+
plans.push({ selector, normalizedSelector, interaction, forcedStates });
|
|
60
107
|
}
|
|
61
108
|
}
|
|
62
109
|
|
|
@@ -71,14 +118,18 @@ export function selectInteractionFallbackPlans(
|
|
|
71
118
|
const seen = new Set<string>();
|
|
72
119
|
|
|
73
120
|
function add(plan: InteractionTargetPlan) {
|
|
74
|
-
const key = `${plan.interaction}\u0000${plan.normalizedSelector}`;
|
|
121
|
+
const key = `${plan.interaction}\u0000${plan.normalizedSelector}\u0000${plan.forcedStates.join(",")}`;
|
|
75
122
|
if (seen.has(key)) return;
|
|
76
123
|
seen.add(key);
|
|
77
124
|
selected.push(plan);
|
|
78
125
|
}
|
|
79
126
|
|
|
80
127
|
for (const plan of plans) {
|
|
81
|
-
|
|
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);
|
|
82
133
|
}
|
|
83
134
|
|
|
84
135
|
return selected;
|
|
@@ -141,6 +192,7 @@ export function captureComputedStyleSnapshotInDom(props: string[]): ComputedStyl
|
|
|
141
192
|
if (!getStyle) {
|
|
142
193
|
throw new Error("getComputedStyle is not available in this realm");
|
|
143
194
|
}
|
|
195
|
+
const styleGetter = getStyle;
|
|
144
196
|
|
|
145
197
|
function shouldTrackElement(element: HTMLElement): boolean {
|
|
146
198
|
if (element.id) return true;
|
|
@@ -164,16 +216,53 @@ export function captureComputedStyleSnapshotInDom(props: string[]): ComputedStyl
|
|
|
164
216
|
return `${ctx}>${tag}[${count}]`;
|
|
165
217
|
}
|
|
166
218
|
|
|
167
|
-
|
|
168
|
-
|
|
219
|
+
function normalizeCssClassToken(token: string): string {
|
|
220
|
+
return token.replace(/\\([^\n\r\f])/g, "$1");
|
|
221
|
+
}
|
|
169
222
|
|
|
170
|
-
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
const key = buildElementKey(he, tagCounters);
|
|
174
|
-
if (!key || results[key]) continue;
|
|
223
|
+
function collectDeclaredClassAliases(): Set<string> {
|
|
224
|
+
const aliases = new Set<string>();
|
|
225
|
+
const classPattern = /\.((?:\\.|[-_a-zA-Z0-9])+)/g;
|
|
175
226
|
|
|
176
|
-
|
|
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);
|
|
177
266
|
const styles: Record<string, string> = {};
|
|
178
267
|
for (const prop of props) {
|
|
179
268
|
styles[prop] = computed.getPropertyValue(prop);
|
|
@@ -181,7 +270,7 @@ export function captureComputedStyleSnapshotInDom(props: string[]): ComputedStyl
|
|
|
181
270
|
results[key] = styles;
|
|
182
271
|
|
|
183
272
|
for (const pseudo of ["::before", "::after"] as const) {
|
|
184
|
-
const pseudoComputed =
|
|
273
|
+
const pseudoComputed = styleGetter(element, pseudo);
|
|
185
274
|
const content = pseudoComputed.getPropertyValue("content");
|
|
186
275
|
if (!content || content === "none" || content === "normal") continue;
|
|
187
276
|
|
|
@@ -193,6 +282,23 @@ export function captureComputedStyleSnapshotInDom(props: string[]): ComputedStyl
|
|
|
193
282
|
}
|
|
194
283
|
}
|
|
195
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
|
+
|
|
196
302
|
return results;
|
|
197
303
|
}
|
|
198
304
|
|
|
@@ -335,13 +441,28 @@ export function collectInteractionTargetPlansInDom(): InteractionTargetPlan[] {
|
|
|
335
441
|
.replace(/\s*([>+~])\s*/g, " $1 ")
|
|
336
442
|
.trim();
|
|
337
443
|
if (!normalizedSelector) continue;
|
|
338
|
-
const
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
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(",")}`;
|
|
342
463
|
if (seen.has(key)) continue;
|
|
343
464
|
seen.add(key);
|
|
344
|
-
plans.push({ selector, normalizedSelector, interaction });
|
|
465
|
+
plans.push({ selector, normalizedSelector, interaction, forcedStates });
|
|
345
466
|
}
|
|
346
467
|
}
|
|
347
468
|
|
package/src/element-compare.ts
CHANGED
|
@@ -260,21 +260,36 @@ function parseArgs(args: string[]): ElementCompareOptions {
|
|
|
260
260
|
};
|
|
261
261
|
}
|
|
262
262
|
|
|
263
|
+
function formatUsage(): string {
|
|
264
|
+
return [
|
|
265
|
+
"Usage:",
|
|
266
|
+
" vrt diff component --url <baseline> --current-url <current> --selectors \"header,main,footer\"",
|
|
267
|
+
" vrt diff component before.html after.html --selectors \"header,.content\"",
|
|
268
|
+
"",
|
|
269
|
+
"Aliases:",
|
|
270
|
+
" vrt diff elements",
|
|
271
|
+
" vrt elements",
|
|
272
|
+
"",
|
|
273
|
+
"Options:",
|
|
274
|
+
" --selectors <sel1,sel2,...> CSS selectors to compare (required)",
|
|
275
|
+
" --viewport <WxH> Viewport size (default: 1280x900)",
|
|
276
|
+
" --output <dir> Output directory",
|
|
277
|
+
" --mask <selectors> Mask dynamic content",
|
|
278
|
+
" --threshold <0-1> pixelmatch threshold (default: 0.1)",
|
|
279
|
+
].join("\n");
|
|
280
|
+
}
|
|
281
|
+
|
|
263
282
|
async function main() {
|
|
264
283
|
const cliArgs = process.argv.slice(2);
|
|
284
|
+
if (cliArgs.includes("--help") || cliArgs.includes("-h") || cliArgs.includes("help")) {
|
|
285
|
+
console.log(formatUsage());
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
|
|
265
289
|
const options = parseArgs(cliArgs);
|
|
266
290
|
|
|
267
291
|
if (!options.baselineUrl && !options.baselineFile) {
|
|
268
|
-
console.log(
|
|
269
|
-
console.log(` vrt elements --url <baseline> --current-url <current> --selectors "header,main,footer"`);
|
|
270
|
-
console.log(` vrt elements before.html after.html --selectors "header,.content"`);
|
|
271
|
-
console.log();
|
|
272
|
-
console.log(`Options:`);
|
|
273
|
-
console.log(` --selectors <sel1,sel2,...> CSS selectors to compare (required)`);
|
|
274
|
-
console.log(` --viewport <WxH> Viewport size (default: 1280x900)`);
|
|
275
|
-
console.log(` --output <dir> Output directory`);
|
|
276
|
-
console.log(` --mask <selectors> Mask dynamic content`);
|
|
277
|
-
console.log(` --threshold <0-1> pixelmatch threshold (default: 0.1)`);
|
|
292
|
+
console.log(formatUsage());
|
|
278
293
|
process.exit(1);
|
|
279
294
|
}
|
|
280
295
|
|
package/src/heatmap-regions.ts
CHANGED
|
@@ -18,7 +18,6 @@
|
|
|
18
18
|
*/
|
|
19
19
|
import { PNG } from "pngjs";
|
|
20
20
|
import { readFile } from "node:fs/promises";
|
|
21
|
-
import { classifyRegion } from "./region-classify.ts";
|
|
22
21
|
|
|
23
22
|
export interface HeatmapRegion {
|
|
24
23
|
top: number;
|
|
@@ -39,8 +38,11 @@ export interface HeatmapRegion {
|
|
|
39
38
|
* Content classification of the region inside the source image —
|
|
40
39
|
* `text` / `filled-rect` / `icon` / `image` / `unknown`. Helps the
|
|
41
40
|
* agent decide whether to paint a flat background, write a text
|
|
42
|
-
* run, or place an icon. Only populated when
|
|
43
|
-
*
|
|
41
|
+
* run, or place an icon. Only populated when callers run an
|
|
42
|
+
* explicit annotation pass (see `annotateHeatmapRegionKinds` in
|
|
43
|
+
* `@mizchi/vlmkit-markup/heatmap-region-kinds.ts`); the bare
|
|
44
|
+
* `findHeatmapRegionsFromFile` no longer fills it in to keep
|
|
45
|
+
* vlmkit-core free of MoonBit policy.
|
|
44
46
|
*/
|
|
45
47
|
kind?: "text" | "filled-rect" | "icon" | "image" | "unknown";
|
|
46
48
|
/** Classifier confidence ∈ [0, 1]. */
|
|
@@ -234,9 +236,6 @@ export async function findHeatmapRegionsFromFile(
|
|
|
234
236
|
for (const region of regions) {
|
|
235
237
|
const color = sampleRegionColor(sourcePng.data, sourcePng.width, sourcePng.height, region);
|
|
236
238
|
if (color) region.dominantColor = color;
|
|
237
|
-
const cls = classifyRegion(sourcePng.data, sourcePng.width, sourcePng.height, region);
|
|
238
|
-
region.kind = cls.kind;
|
|
239
|
-
region.kindConfidence = Number(cls.confidence.toFixed(2));
|
|
240
239
|
}
|
|
241
240
|
} catch {
|
|
242
241
|
// Source image unavailable — leave regions without annotations.
|