@uselazuli/cli 0.0.0-stage → 1.0.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/CHANGELOG.md +98 -0
- package/LICENSE +21 -0
- package/README.md +202 -2
- package/agents/lazuli-design-reviewer.md +30 -0
- package/agents/lazuli-first-look.md +22 -0
- package/agents/lazuli-task-tester.md +25 -0
- package/bin/lazuli.mjs +699 -0
- package/package.json +11 -4
- package/rules/accessibility/HYPOTHESES.md +8 -0
- package/rules/accessibility/_area.yaml +7 -0
- package/rules/accessibility/operable.yaml +100 -0
- package/rules/accessibility/perceivable.yaml +153 -0
- package/rules/accessibility/robust.yaml +24 -0
- package/rules/accessibility/understandable.yaml +72 -0
- package/rules/color/HYPOTHESES.md +27 -0
- package/rules/color/_area.yaml +10 -0
- package/rules/color/adapt.yaml +183 -0
- package/rules/color/create.yaml +259 -0
- package/rules/color/review.yaml +215 -0
- package/rules/color/signal.yaml +196 -0
- package/rules/content/HYPOTHESES.md +18 -0
- package/rules/content/_area.yaml +8 -0
- package/rules/content/review.yaml +347 -0
- package/rules/content/voice.yaml +144 -0
- package/rules/content/writing.yaml +202 -0
- package/rules/data/HYPOTHESES.md +11 -0
- package/rules/data/_area.yaml +7 -0
- package/rules/data/charts.yaml +189 -0
- package/rules/data/dashboards.yaml +141 -0
- package/rules/data/review.yaml +159 -0
- package/rules/graphic/HYPOTHESES.md +17 -0
- package/rules/graphic/_area.yaml +8 -0
- package/rules/graphic/identity.yaml +141 -0
- package/rules/graphic/pieces.yaml +164 -0
- package/rules/graphic/review.yaml +242 -0
- package/rules/graphic/symbol.yaml +141 -0
- package/rules/motion/HYPOTHESES.md +19 -0
- package/rules/motion/_area.yaml +7 -0
- package/rules/motion/micro.yaml +143 -0
- package/rules/motion/review.yaml +347 -0
- package/rules/motion/transitions.yaml +169 -0
- package/rules/typography/HYPOTHESES.md +12 -0
- package/rules/typography/_area.yaml +8 -0
- package/rules/typography/adapt.yaml +45 -0
- package/rules/typography/create.yaml +126 -0
- package/rules/typography/review.yaml +127 -0
- package/rules/ui/HYPOTHESES.md +23 -0
- package/rules/ui/_area.yaml +9 -0
- package/rules/ui/compose.yaml +219 -0
- package/rules/ui/grid.yaml +75 -0
- package/rules/ui/review.yaml +399 -0
- package/rules/ui/states.yaml +257 -0
- package/rules/ux/HYPOTHESES.md +13 -0
- package/rules/ux/_area.yaml +8 -0
- package/rules/ux/flows.yaml +164 -0
- package/rules/ux/forms.yaml +142 -0
- package/rules/ux/review.yaml +186 -0
- package/skills/lazuli/SKILL.md +180 -0
- package/skills/lazuli/procedures/create.md +29 -0
- package/skills/lazuli/procedures/explain-decisions.md +22 -0
- package/skills/lazuli/procedures/improve.md +30 -0
- package/skills/lazuli/procedures/remember-this.md +41 -0
- package/skills/lazuli/procedures/review-design.md +35 -0
- package/skills/lazuli/procedures/show-options.md +32 -0
- package/skills/lazuli-accessibility-check/SKILL.md +68 -0
- package/skills/lazuli-color-context-and-culture/SKILL.md +53 -0
- package/skills/lazuli-color-context-and-culture/references/sectors.md +26 -0
- package/skills/lazuli-color-palette-by-intent/SKILL.md +54 -0
- package/skills/lazuli-color-palette-by-intent/references/intent-chords.md +51 -0
- package/skills/lazuli-color-screen-review/SKILL.md +51 -0
- package/skills/lazuli-color-states-and-signals/SKILL.md +53 -0
- package/skills/lazuli-content-interface-writing/SKILL.md +53 -0
- package/skills/lazuli-content-voice/SKILL.md +39 -0
- package/skills/lazuli-data-charts-and-dashboards/SKILL.md +59 -0
- package/skills/lazuli-graphic-derived-pieces/SKILL.md +40 -0
- package/skills/lazuli-graphic-derived-pieces/references/piece-template.html +24 -0
- package/skills/lazuli-graphic-identity/SKILL.md +43 -0
- package/skills/lazuli-motion-micro-and-transitions/SKILL.md +49 -0
- package/skills/lazuli-typography-choose-and-pair/SKILL.md +50 -0
- package/skills/lazuli-typography-screen-review/SKILL.md +51 -0
- package/skills/lazuli-ui-composition/SKILL.md +42 -0
- package/skills/lazuli-ui-composition/references/option-template.html +34 -0
- package/skills/lazuli-ui-layout-review/SKILL.md +47 -0
- package/skills/lazuli-ui-states-and-system/SKILL.md +54 -0
- package/skills/lazuli-ux-flows-and-forms/SKILL.md +51 -0
- package/skills/lazuli-ux-task-test/SKILL.md +44 -0
- package/src/browser.mjs +161 -0
- package/src/commands/capture.mjs +313 -0
- package/src/commands/catalog.mjs +37 -0
- package/src/commands/check-design.mjs +428 -0
- package/src/commands/check-goals.mjs +57 -0
- package/src/commands/compare-designs.mjs +97 -0
- package/src/commands/draft-memo.mjs +161 -0
- package/src/commands/hooks.mjs +343 -0
- package/src/commands/install-hooks.mjs +120 -0
- package/src/commands/install.mjs +132 -0
- package/src/commands/list-rules.mjs +49 -0
- package/src/commands/map-page.mjs +594 -0
- package/src/commands/record-answers.mjs +79 -0
- package/src/commands/record-log.mjs +296 -0
- package/src/commands/record-task.mjs +250 -0
- package/src/commands/render-options.mjs +164 -0
- package/src/commands/start-project.mjs +32 -0
- package/src/measure/a11y.mjs +383 -0
- package/src/measure/asset.mjs +206 -0
- package/src/measure/at-rest.mjs +49 -0
- package/src/measure/cases.mjs +549 -0
- package/src/measure/chart-read.mjs +401 -0
- package/src/measure/chart.mjs +264 -0
- package/src/measure/coherence.mjs +145 -0
- package/src/measure/color.mjs +79 -0
- package/src/measure/content.mjs +82 -0
- package/src/measure/cvd.mjs +24 -0
- package/src/measure/extreme.mjs +241 -0
- package/src/measure/forms.mjs +150 -0
- package/src/measure/in-page/a11y.js +489 -0
- package/src/measure/in-page/color.js +346 -0
- package/src/measure/in-page/composition.js +1496 -0
- package/src/measure/in-page/contrast.js +15 -0
- package/src/measure/in-page/cvd.js +173 -0
- package/src/measure/in-page/extreme.js +355 -0
- package/src/measure/in-page/frames.js +781 -0
- package/src/measure/in-page/look.js +28 -0
- package/src/measure/in-page/motion-probe.js +169 -0
- package/src/measure/in-page/style.js +107 -0
- package/src/measure/in-page/svg-strokes.js +112 -0
- package/src/measure/in-page/svg-uses.js +50 -0
- package/src/measure/in-page/symbol.js +123 -0
- package/src/measure/in-page/type.js +266 -0
- package/src/measure/in-page/weight.js +207 -0
- package/src/measure/layout.mjs +91 -0
- package/src/measure/motion.mjs +508 -0
- package/src/measure/piece.mjs +195 -0
- package/src/measure/registry.mjs +252 -0
- package/src/measure/rhythm.mjs +81 -0
- package/src/measure/signature.mjs +65 -0
- package/src/measure/states.mjs +240 -0
- package/src/measure/style.mjs +13 -0
- package/src/measure/symbol.mjs +268 -0
- package/src/measure/terms.mjs +153 -0
- package/src/measure/tiles.mjs +20 -0
- package/src/measure/timeline.mjs +621 -0
- package/src/measure/type.mjs +167 -0
- package/src/measure/weight.mjs +18 -0
- package/src/private-strings.mjs +81 -0
- package/src/project/blind.mjs +70 -0
- package/src/project/edits.mjs +154 -0
- package/src/project/gate.mjs +159 -0
- package/src/project/lazuli-md.mjs +497 -0
- package/src/project/log.mjs +329 -0
- package/src/project/match.mjs +112 -0
- package/src/project/records.mjs +380 -0
- package/src/project/tasks.mjs +153 -0
- package/src/report/evaluate.mjs +27 -0
- package/src/report/evaluators/a11y.mjs +206 -0
- package/src/report/evaluators/chart.mjs +151 -0
- package/src/report/evaluators/coherence.mjs +103 -0
- package/src/report/evaluators/color.mjs +86 -0
- package/src/report/evaluators/content.mjs +112 -0
- package/src/report/evaluators/extreme.mjs +53 -0
- package/src/report/evaluators/forms.mjs +116 -0
- package/src/report/evaluators/layout.mjs +94 -0
- package/src/report/evaluators/motion.mjs +273 -0
- package/src/report/evaluators/piece.mjs +69 -0
- package/src/report/evaluators/rhythm.mjs +99 -0
- package/src/report/evaluators/states.mjs +39 -0
- package/src/report/evaluators/style.mjs +35 -0
- package/src/report/evaluators/symbol.mjs +110 -0
- package/src/report/evaluators/terms.mjs +49 -0
- package/src/report/evaluators/type.mjs +163 -0
- package/src/report/evaluators/weight.mjs +51 -0
- package/src/report/evidence.mjs +4 -0
- package/src/report/finding.mjs +11 -0
- package/src/report/format.mjs +215 -0
- package/src/rules/hypotheses.mjs +28 -0
- package/src/rules/load.mjs +256 -0
- package/src/rules/public-text.mjs +56 -0
- package/src/rules/schema.mjs +212 -0
- package/templates/LAZULI.md +86 -0
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { targetKey } from "../project/match.mjs";
|
|
5
|
+
import { bounded } from "./states.mjs";
|
|
6
|
+
import {
|
|
7
|
+
ANALYSIS_CAP_MS, BAND_LEVELS, CELL_PX, FILM_OFFSETS_MS, LAYOUT_PROPS, MAX_SHIFT_PX, MIN_MOVING_SECONDS, MOTION_CAP_MS, MOVE_PROPS,
|
|
8
|
+
PASS_CAP_MS, PAUSE_WORDS, PIXEL_NOISE, QUIET_MS, SCROLL_SETTLE_MS, SHIFT_GAP_MS, SHIFT_WINDOW_MS, STEP_WINDOW_MS, recordPass,
|
|
9
|
+
} from "./timeline.mjs";
|
|
10
|
+
|
|
11
|
+
// The motion measurer. It never touches the page it is given: at the first width of a run it records its
|
|
12
|
+
// own passes through recordPass (which opens, guards and closes its own pages). Pass A records the
|
|
13
|
+
// default motion (animation inventory, long frames, layout shifts); pass B records frames over the same
|
|
14
|
+
// window and steps, and a lossless clip of each gradient at rest; pass C repeats pass A with reduced motion
|
|
15
|
+
// asked for, only when something moved in pass A, and its inventory is compared exactly with pass A's.
|
|
16
|
+
// Pass B's frames and clips are analyzed in a blank page of their own context (every request aborted):
|
|
17
|
+
// settle and moving content over the load window, flashes over the whole pass, bands per gradient, and a
|
|
18
|
+
// filmstrip for the reviewer.
|
|
19
|
+
// Readings are split into exact (the same on every run of a page: the inventory without times, the reduced
|
|
20
|
+
// comparison, banding and pause controls) and timed (start times, long frames, layout shift, frames,
|
|
21
|
+
// settle, flashes, moving content: machine and timing dependent).
|
|
22
|
+
|
|
23
|
+
const contrastSource = readFileSync(new URL("./in-page/contrast.js", import.meta.url), "utf8");
|
|
24
|
+
const framesSource = readFileSync(new URL("./in-page/frames.js", import.meta.url), "utf8");
|
|
25
|
+
// Filmstrip label strip above each tile, in px (layout of the review image, not a reading).
|
|
26
|
+
const LABEL_PX = 20;
|
|
27
|
+
|
|
28
|
+
// Sampling limit, noted when it cuts: the moved elements listed for the layout shift.
|
|
29
|
+
const MAX_LISTED = 10;
|
|
30
|
+
|
|
31
|
+
// The rules each pass reads, named in a note when the pass fails.
|
|
32
|
+
const DEFAULT_IDS = [
|
|
33
|
+
"motion.micro-duration", "motion.transition-duration", "motion.easing", "motion.properties",
|
|
34
|
+
"motion.sameness", "motion.reduced", "motion.long-frames", "motion.layout-shift",
|
|
35
|
+
];
|
|
36
|
+
const REDUCED_IDS = ["motion.reduced"];
|
|
37
|
+
const FRAMES_IDS = ["motion.settle", "motion.banding", "motion.flashes", "motion.moving"];
|
|
38
|
+
|
|
39
|
+
const plural = (n, one, many) => (n === 1 ? one : many);
|
|
40
|
+
const joined = (list) => (list.length < 2 ? list.join("") : `${list.slice(0, -1).join(", ")} and ${list[list.length - 1]}`);
|
|
41
|
+
const firstLine = (err) => String(err?.message ?? err).split("\n")[0];
|
|
42
|
+
const sortedKeys = (o) => Object.fromEntries(Object.keys(o ?? {}).sort().map((k) => [k, o[k]]));
|
|
43
|
+
|
|
44
|
+
// The rule names (else the measure ids) of the selected rules among ids.
|
|
45
|
+
function names(ids, rules) {
|
|
46
|
+
const list = (rules ?? []).filter((r) => ids.includes(r.measure)).map((r) => r.name);
|
|
47
|
+
return list.length > 0 ? list : ids;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// "<names> was not checked" or "were not checked", by how many names there are.
|
|
51
|
+
const notChecked = (ids, rules) => {
|
|
52
|
+
const list = names(ids, rules);
|
|
53
|
+
return `${joined(list)} ${plural(list.length, "was", "were")} not checked`;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
function failure(which, err, ids, rules) {
|
|
57
|
+
return `The ${which} failed: ${firstLine(err)}; ${notChecked(ids, rules)}.`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// The whole measure's cap (MOTION_CAP_MS): each pass and the analysis get what is left
|
|
61
|
+
// of it, at most their own cap. A step skipped or stopped by it leaves this note and null readings.
|
|
62
|
+
const leftOf = (budget) => budget.capMs - (Date.now() - budget.started);
|
|
63
|
+
function capNote(budget, which, ids, rules) {
|
|
64
|
+
return `The ${which} did not finish within the ${budget.capMs} ms cap of the whole motion measure; ${notChecked(ids, rules)}.`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Runs work(capMs) within what is left of the budget; null and a note when it is past the cap or fails.
|
|
68
|
+
async function withinCap(budget, which, ids, rules, notes, ownCapMs, work) {
|
|
69
|
+
const left = leftOf(budget);
|
|
70
|
+
if (left <= 0) {
|
|
71
|
+
notes.push(capNote(budget, which, ids, rules));
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
return await work(Math.min(ownCapMs, left));
|
|
76
|
+
} catch (err) {
|
|
77
|
+
notes.push(leftOf(budget) <= 0 ? capNote(budget, which, ids, rules) : failure(which, err, ids, rules));
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// A move property ends at rest: none, an identity matrix or only zeros (translateY(0px), 0px).
|
|
83
|
+
function atRest(value) {
|
|
84
|
+
const v = String(value).trim();
|
|
85
|
+
if (v === "none" || v === "matrix(1, 0, 0, 1, 0, 0)" || v === "matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)") return true;
|
|
86
|
+
const numbers = v.match(/-?\d*\.?\d+(?:e-?\d+)?/g);
|
|
87
|
+
return !!numbers && numbers.every((n) => Number(n) === 0);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const STEP_KIND = new Set(["hover", "focus", "click"]);
|
|
91
|
+
|
|
92
|
+
// Pass records -> the inventory of animations on kept elements in view at the end of their phase.
|
|
93
|
+
// Each target's kept and inView are read once, when the phase it first appeared in ends (the recorder
|
|
94
|
+
// reads new targets after the load window and after each step).
|
|
95
|
+
function inventory(pass) {
|
|
96
|
+
const targets = new Map(pass.targets.map((t) => [t.index, t]));
|
|
97
|
+
const steps = new Map(pass.steps.map((s) => [`${s.kind} ${s.id}`, s]));
|
|
98
|
+
const order = new Map([["load", 0], ...pass.steps.map((s, i) => [`${s.kind} ${s.id}`, i + 1])]);
|
|
99
|
+
const records = [];
|
|
100
|
+
const seen = new Set();
|
|
101
|
+
let unread = 0;
|
|
102
|
+
let timed = 0;
|
|
103
|
+
for (const a of pass.animations) {
|
|
104
|
+
const t = a.target === null || a.target === undefined ? null : targets.get(a.target);
|
|
105
|
+
if (!t || !t.kept || !t.inView) {
|
|
106
|
+
unread += 1;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const selector = `${t.selector}${a.pseudo ?? ""}`;
|
|
110
|
+
const properties = [...a.properties].sort();
|
|
111
|
+
// The reverse transition after the pointer leaves or the element blurs is recorded under the same
|
|
112
|
+
// step: the first of a target, kind, name and properties in a step counts, later ones do not.
|
|
113
|
+
const key = JSON.stringify([a.step, a.target, a.pseudo ?? null, a.kind, a.name, properties]);
|
|
114
|
+
if (seen.has(key)) continue;
|
|
115
|
+
seen.add(key);
|
|
116
|
+
// Trigger: load for the load window; a step's kind only when the animation
|
|
117
|
+
// starts within STEP_WINDOW_MS of the step and, for a hover, focus or click, runs on the element the
|
|
118
|
+
// step acted on, holds it or sits inside it; anything else is "time", judged by no duration cutoff.
|
|
119
|
+
let trigger = "load";
|
|
120
|
+
if (a.step !== "load") {
|
|
121
|
+
const step = steps.get(a.step);
|
|
122
|
+
const soon = !!step && a.startMs - step.startMs <= STEP_WINDOW_MS;
|
|
123
|
+
if (step && soon && step.kind === "scroll") trigger = "scroll";
|
|
124
|
+
else if (step && soon && STEP_KIND.has(step.kind) && a.related === true) trigger = step.kind;
|
|
125
|
+
else trigger = "time";
|
|
126
|
+
}
|
|
127
|
+
if (trigger === "time") timed += 1;
|
|
128
|
+
const from = sortedKeys(a.from);
|
|
129
|
+
const to = sortedKeys(a.to);
|
|
130
|
+
const endMs = a.iterations === "infinite" ? null : Math.round(a.delayMs + a.durationMs * a.iterations);
|
|
131
|
+
const moves = MOVE_PROPS.some((p) => (p in from || p in to) && from[p] !== to[p]);
|
|
132
|
+
const opacityRises = "opacity" in from && Number(from.opacity) < 1 && Number(to.opacity ?? 1) > Number(from.opacity);
|
|
133
|
+
const returns = MOVE_PROPS.some((p) => p in to && from[p] !== to[p] && atRest(to[p]));
|
|
134
|
+
records.push({
|
|
135
|
+
order: order.get(a.step) ?? order.size,
|
|
136
|
+
startMs: a.startMs,
|
|
137
|
+
record: {
|
|
138
|
+
selector,
|
|
139
|
+
name: a.name,
|
|
140
|
+
kind: a.kind,
|
|
141
|
+
trigger,
|
|
142
|
+
step: a.step,
|
|
143
|
+
endMs,
|
|
144
|
+
delayMs: Math.round(a.delayMs),
|
|
145
|
+
durationMs: Math.round(a.durationMs),
|
|
146
|
+
easing: a.easing,
|
|
147
|
+
properties,
|
|
148
|
+
from,
|
|
149
|
+
to,
|
|
150
|
+
moves,
|
|
151
|
+
entrance: (trigger === "load" || trigger === "scroll") && endMs !== null && (opacityRises || returns),
|
|
152
|
+
layoutProps: properties.filter((p) => LAYOUT_PROPS.includes(p)),
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
const cmp = (x, y) => (x < y ? -1 : x > y ? 1 : 0);
|
|
157
|
+
records.sort(
|
|
158
|
+
(x, y) =>
|
|
159
|
+
x.order - y.order || cmp(x.record.selector, y.record.selector) || cmp(x.record.name, y.record.name) || cmp(x.record.kind, y.record.kind) || cmp(x.record.trigger, y.record.trigger),
|
|
160
|
+
);
|
|
161
|
+
return {
|
|
162
|
+
animations: records.map((r) => r.record),
|
|
163
|
+
starts: records.map((r) => ({ selector: r.record.selector, name: r.record.name, step: r.record.step, startMs: Math.round(r.startMs - pass.loadMs) })),
|
|
164
|
+
unread,
|
|
165
|
+
timed,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// The step label a page time falls in: the last step started at or before it, else the load window.
|
|
170
|
+
function stepAt(pass, ms) {
|
|
171
|
+
let label = "load";
|
|
172
|
+
for (const s of pass.steps) if (s.startMs <= ms) label = `${s.kind} ${s.id}`;
|
|
173
|
+
return label;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Layout shift: the largest session window (shifts less than SHIFT_GAP_MS apart, at most SHIFT_WINDOW_MS
|
|
177
|
+
// from its first) over shifts without recent input. Each shift's value is credited to every source of it
|
|
178
|
+
// that the walk keeps (hidden or clipped sources are left out).
|
|
179
|
+
function shiftOf(pass, notes) {
|
|
180
|
+
const targets = new Map(pass.targets.map((t) => [t.index, t]));
|
|
181
|
+
const windows = [];
|
|
182
|
+
let current = null;
|
|
183
|
+
for (const s of [...pass.shifts].filter((x) => !x.hadRecentInput).sort((a, b) => a.startMs - b.startMs)) {
|
|
184
|
+
if (!current || s.startMs - current.last >= SHIFT_GAP_MS || s.startMs - current.first >= SHIFT_WINDOW_MS) {
|
|
185
|
+
current = { first: s.startMs, last: s.startMs, score: 0, shifts: [] };
|
|
186
|
+
windows.push(current);
|
|
187
|
+
}
|
|
188
|
+
current.score += s.value;
|
|
189
|
+
current.last = s.startMs;
|
|
190
|
+
current.shifts.push(s);
|
|
191
|
+
}
|
|
192
|
+
const best = windows.reduce((b, w) => (b === null || w.score > b.score ? w : b), null);
|
|
193
|
+
if (!best) return { score: 0, top: [] };
|
|
194
|
+
const by = new Map();
|
|
195
|
+
for (const s of best.shifts) {
|
|
196
|
+
for (const i of s.sources) {
|
|
197
|
+
const t = targets.get(i);
|
|
198
|
+
if (!t || !t.kept) continue;
|
|
199
|
+
by.set(t.selector, (by.get(t.selector) ?? 0) + s.value);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
const top = [...by].map(([selector, value]) => ({ selector, value })).sort((a, b) => b.value - a.value || (a.selector < b.selector ? -1 : 1));
|
|
203
|
+
if (top.length > MAX_LISTED) notes.push(`The ${MAX_LISTED} elements that moved most of ${top.length} are listed.`);
|
|
204
|
+
return { score: best.score, top: top.slice(0, MAX_LISTED) };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function pushNew(notes, list) {
|
|
208
|
+
for (const n of list) if (!notes.includes(n)) notes.push(n);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Whether an accessible name holds one of PAUSE_WORDS as a whole word.
|
|
212
|
+
const pauseWord = (name) => String(name ?? "").toLowerCase().split(/[^\p{L}\p{N}]+/u).some((w) => PAUSE_WORDS.includes(w));
|
|
213
|
+
|
|
214
|
+
// The kept element at the center of a box (CSS px), from the snapshot grid of pass B, or null.
|
|
215
|
+
function elementAt(points, box) {
|
|
216
|
+
if (!points) return null;
|
|
217
|
+
const i = Math.min(points.cols - 1, Math.max(0, Math.round((box.x + box.width / 2) / points.step)));
|
|
218
|
+
const j = Math.min(points.rows - 1, Math.max(0, Math.round((box.y + box.height / 2) / points.step)));
|
|
219
|
+
const k = points.at[j * points.cols + i];
|
|
220
|
+
return k >= 0 ? points.selectors[k] : null;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// The last frame at or before each offset from a phase start: [{ offset, col, index }], col being the
|
|
224
|
+
// offset's place in FILM_OFFSETS_MS (an offset with no frame yet leaves its column empty).
|
|
225
|
+
function filmPicks(frames, start) {
|
|
226
|
+
const picks = [];
|
|
227
|
+
FILM_OFFSETS_MS.forEach((offset, col) => {
|
|
228
|
+
let index = -1;
|
|
229
|
+
for (let i = 0; i < frames.length && frames[i].t <= start + offset; i++) index = i;
|
|
230
|
+
if (index >= 0) picks.push({ offset, col, index });
|
|
231
|
+
});
|
|
232
|
+
return picks;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// In the analysis page: decodes a PNG (base64) losslessly to an ImageData.
|
|
236
|
+
export const DECODE = `async (b64) => {
|
|
237
|
+
const blob = await (await fetch("data:image/png;base64," + b64)).blob();
|
|
238
|
+
const bmp = await createImageBitmap(blob, { colorSpaceConversion: "none", premultiplyAlpha: "none" });
|
|
239
|
+
const cv = new OffscreenCanvas(bmp.width, bmp.height);
|
|
240
|
+
const g = cv.getContext("2d", { willReadFrequently: true });
|
|
241
|
+
g.drawImage(bmp, 0, 0);
|
|
242
|
+
bmp.close();
|
|
243
|
+
return g.getImageData(0, 0, cv.width, cv.height);
|
|
244
|
+
}`;
|
|
245
|
+
|
|
246
|
+
// Pass B's frames and gradient clips, analyzed in a blank page of the given context (its own, every request
|
|
247
|
+
// aborted; the caller closes it). Frames go in one by one, decoded in the page; the analyzer's result is
|
|
248
|
+
// taken at the last frame of the load window (settle, moving content) and at the end (flashes, with the
|
|
249
|
+
// movers).
|
|
250
|
+
async function analyzePass(context, b, o) {
|
|
251
|
+
await context.route("**/*", (route) => route.abort());
|
|
252
|
+
const page = await context.newPage();
|
|
253
|
+
await page.evaluate(`window.__lazuliFrames = (${framesSource})(${contrastSource}); window.__lazuliDecode = ${DECODE};`);
|
|
254
|
+
const frames = b.frames?.frames ?? [];
|
|
255
|
+
let load = null;
|
|
256
|
+
let all = null;
|
|
257
|
+
if (frames.length > 0) {
|
|
258
|
+
// The load window's reading: settle leaves out the areas that keep moving (its own moving content and
|
|
259
|
+
// the endless animations), which moving content judges instead.
|
|
260
|
+
await page.evaluate(([options, endless]) => {
|
|
261
|
+
window.__lazuliRun = { options, analyzer: null, load: null };
|
|
262
|
+
window.__lazuliLoad = (analyzer) => {
|
|
263
|
+
const first = analyzer.result();
|
|
264
|
+
return analyzer.result({ settleIgnore: [...endless, ...first.moving.map((m) => m.box)] });
|
|
265
|
+
};
|
|
266
|
+
}, [o.analyzer, o.endless]);
|
|
267
|
+
for (const f of frames) {
|
|
268
|
+
await page.evaluate(async ([b64, t]) => {
|
|
269
|
+
const run = window.__lazuliRun;
|
|
270
|
+
const img = await window.__lazuliDecode(b64);
|
|
271
|
+
// Frames are at a share of the viewport: the scale is read from the first frame.
|
|
272
|
+
run.analyzer ??= window.__lazuliFrames.createAnalyzer({ ...run.options, scale: img.width / run.options.viewportWidth });
|
|
273
|
+
if (run.load === null && t > run.options.loadEndMs) run.load = window.__lazuliLoad(run.analyzer);
|
|
274
|
+
run.analyzer.push(img, t);
|
|
275
|
+
}, [f.png.toString("base64"), f.t]);
|
|
276
|
+
}
|
|
277
|
+
// Flashes over the whole pass, with only real movement left out: cells
|
|
278
|
+
// where a shift was found and the boxes of animations that change a move property.
|
|
279
|
+
({ load, all } = await page.evaluate((movers) => {
|
|
280
|
+
const run = window.__lazuliRun;
|
|
281
|
+
const plain = (r) => ({ count: r.count, changeIntervalMs: r.changeIntervalMs, changedAt: r.changedAt, settle: r.settle, moving: r.moving, flashes: r.flashes, notes: r.notes });
|
|
282
|
+
const load = run.load ?? window.__lazuliLoad(run.analyzer);
|
|
283
|
+
const all = run.analyzer.result({ movers });
|
|
284
|
+
return { load: plain(load), all: plain(all) };
|
|
285
|
+
}, o.movers));
|
|
286
|
+
}
|
|
287
|
+
let film = null;
|
|
288
|
+
if (frames.length > 0) {
|
|
289
|
+
const rows = [{ phase: "load", picks: filmPicks(frames, b.loadMs) }];
|
|
290
|
+
for (const s of b.steps) {
|
|
291
|
+
if (!all.changedAt.some((t) => t > s.startMs && t <= s.endMs)) continue;
|
|
292
|
+
rows.push({ phase: `${s.kind} ${s.id}`, picks: filmPicks(frames, s.startMs) });
|
|
293
|
+
}
|
|
294
|
+
const tiles = rows.flatMap((r, row) => r.picks.map((p) => ({ row, col: p.col, label: `${r.phase} +${p.offset} ms`, b64: frames[p.index].png.toString("base64") })));
|
|
295
|
+
if (tiles.length > 0) {
|
|
296
|
+
const b64 = await page.evaluate(async ([tiles, rowCount, label]) => {
|
|
297
|
+
const imgs = [];
|
|
298
|
+
for (const t of tiles) imgs.push(await window.__lazuliDecode(t.b64));
|
|
299
|
+
const w = imgs[0].width;
|
|
300
|
+
const h = imgs[0].height;
|
|
301
|
+
const cols = Math.max(...tiles.map((t) => t.col)) + 1;
|
|
302
|
+
const cv = new OffscreenCanvas(cols * w, rowCount * (h + label));
|
|
303
|
+
const g = cv.getContext("2d");
|
|
304
|
+
g.fillStyle = "#ffffff";
|
|
305
|
+
g.fillRect(0, 0, cv.width, cv.height);
|
|
306
|
+
g.font = `${label - 6}px sans-serif`;
|
|
307
|
+
g.textBaseline = "middle";
|
|
308
|
+
tiles.forEach((t, k) => {
|
|
309
|
+
const x = t.col * w;
|
|
310
|
+
const y = t.row * (h + label);
|
|
311
|
+
g.fillStyle = "#000000";
|
|
312
|
+
g.fillText(t.label, x + 4, y + label / 2);
|
|
313
|
+
g.putImageData(imgs[k], x, y + label);
|
|
314
|
+
});
|
|
315
|
+
const bytes = new Uint8Array(await (await cv.convertToBlob({ type: "image/png" })).arrayBuffer());
|
|
316
|
+
let s = "";
|
|
317
|
+
for (let i = 0; i < bytes.length; i += 0x8000) s += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
|
318
|
+
return btoa(s);
|
|
319
|
+
}, [tiles, rows.length, LABEL_PX]);
|
|
320
|
+
film = Buffer.from(b64, "base64");
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
const banding = [];
|
|
324
|
+
for (const shot of b.gradientShots ?? []) {
|
|
325
|
+
const r = await page.evaluate(async ([b64, bandLevels]) => window.__lazuliFrames.bandsOf(await window.__lazuliDecode(b64), { bandLevels }), [shot.png.toString("base64"), BAND_LEVELS]);
|
|
326
|
+
banding.push({ selector: shot.selector, axis: r?.axis ?? null, steps: r?.steps ?? 0, widestBandPx: r?.widestBandPx ?? 0, levelStep: r?.levelStep ?? 0 });
|
|
327
|
+
}
|
|
328
|
+
return { load, all, film, banding };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Pass B: frames over the same window and steps, the gradients and the snapshot; its readings go into the
|
|
332
|
+
// report. A failed recording or analysis leaves its readings null and a note naming the unchecked rules.
|
|
333
|
+
async function framesPass(page, ctx, report, notes, inventoryA, budget) {
|
|
334
|
+
const rules = ctx.rules ?? [];
|
|
335
|
+
const threshold = (id) => rules.find((r) => r.measure === id)?.threshold ?? null;
|
|
336
|
+
const full = rules.some((r) => r.measure === "motion.moving" || r.measure === "motion.flashes");
|
|
337
|
+
const b = await withinCap(budget, "frames recording", FRAMES_IDS, ctx.rules, notes, PASS_CAP_MS, (capMs) =>
|
|
338
|
+
recordPass(ctx, { frames: true, fullWindow: full, gradients: true, snapshot: true, capMs }),
|
|
339
|
+
);
|
|
340
|
+
if (!b) return;
|
|
341
|
+
// Pass B's own "Recording ended" note is left out: its window is reported in timed.frames.
|
|
342
|
+
pushNew(notes, b.notes.filter((n) => !n.startsWith("Recording ended ")));
|
|
343
|
+
report.method = b.frames?.method ?? null;
|
|
344
|
+
report.exact.pauseControls = [
|
|
345
|
+
...b.actions.filter((x) => pauseWord(x.name)).map((x) => ({ name: x.name, selector: x.selector })),
|
|
346
|
+
...(b.snapshot?.media ?? []).map((m) => ({ name: `${m.kind} controls`, selector: m.selector })),
|
|
347
|
+
];
|
|
348
|
+
// Boxes, read after the window, of the elements of pass B's inventory: movers (an animation changes a move
|
|
349
|
+
// property) for the flashes reading, endless ones (infinite iterations) for settle.
|
|
350
|
+
const invB = inventory(b).animations;
|
|
351
|
+
const boxesOf = (list) => {
|
|
352
|
+
const selectors = new Set(list.map((x) => x.selector.replace(/::.*$/, "")));
|
|
353
|
+
return (b.snapshot?.targets ?? []).filter((t) => selectors.has(t.selector) && t.box && t.box.width > 0 && t.box.height > 0).map((t) => t.box);
|
|
354
|
+
};
|
|
355
|
+
const movers = boxesOf(invB.filter((x) => x.moves));
|
|
356
|
+
const endless = boxesOf(invB.filter((x) => x.endMs === null));
|
|
357
|
+
const flashRule = threshold("motion.flashes");
|
|
358
|
+
let flash = null;
|
|
359
|
+
if (flashRule) {
|
|
360
|
+
const { maxFlashes: _max, ...keys } = flashRule;
|
|
361
|
+
flash = keys;
|
|
362
|
+
}
|
|
363
|
+
const maxMovingMs = threshold("motion.moving")?.maxMovingMs;
|
|
364
|
+
const scrolls = b.steps.filter((s) => s.kind === "scroll").map((s) => ({ fromMs: s.startMs, toMs: s.scrollEndMs ?? s.endMs }));
|
|
365
|
+
const analyzer = {
|
|
366
|
+
viewportWidth: ctx.width,
|
|
367
|
+
loadMs: b.loadMs,
|
|
368
|
+
loadEndMs: b.loadMs + b.windowMs,
|
|
369
|
+
quietMs: QUIET_MS,
|
|
370
|
+
cellPx: CELL_PX,
|
|
371
|
+
pixelNoise: PIXEL_NOISE,
|
|
372
|
+
minMovingSeconds: MIN_MOVING_SECONDS,
|
|
373
|
+
maxMovingMs: Number.isFinite(maxMovingMs) ? maxMovingMs : Infinity,
|
|
374
|
+
flash,
|
|
375
|
+
filmOffsets: FILM_OFFSETS_MS,
|
|
376
|
+
maxShiftPx: MAX_SHIFT_PX,
|
|
377
|
+
scrolls,
|
|
378
|
+
scrollSettleMs: SCROLL_SETTLE_MS,
|
|
379
|
+
};
|
|
380
|
+
// The analysis context opens in the browser of the given page; that page itself is never used.
|
|
381
|
+
const browser = page.context().browser();
|
|
382
|
+
let context = null;
|
|
383
|
+
const out = await withinCap(budget, "frame analysis", FRAMES_IDS, ctx.rules, notes, ANALYSIS_CAP_MS, async (capMs) => {
|
|
384
|
+
try {
|
|
385
|
+
if (!browser) throw new Error("no browser was found to open the analysis page");
|
|
386
|
+
context = await browser.newContext();
|
|
387
|
+
const work = analyzePass(context, b, { analyzer, movers, endless });
|
|
388
|
+
// Past the cap the work runs on against a closed context; its rejection is expected.
|
|
389
|
+
work.catch(() => {});
|
|
390
|
+
return await bounded(work, capMs, "the frame analysis");
|
|
391
|
+
} finally {
|
|
392
|
+
if (context) await context.close().catch(() => {});
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
if (!out) return;
|
|
396
|
+
report.exact.banding = out.banding;
|
|
397
|
+
const kept = b.frames?.frames.length ?? 0;
|
|
398
|
+
report.timed.frames = { method: report.method, count: kept, cut: b.frames?.cut ?? false, changeIntervalMs: out.all?.changeIntervalMs ?? null, windowMs: b.windowMs };
|
|
399
|
+
if (b.frames?.cut) notes.push(`${kept} frames were kept of ${b.frames.seen}; later frames were not read.`);
|
|
400
|
+
if (!out.load) {
|
|
401
|
+
notes.push(`No frame was captured; ${notChecked(FRAMES_IDS.filter((id) => id !== "motion.banding"), ctx.rules)}.`);
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
pushNew(notes, out.all.notes);
|
|
405
|
+
const rel = (t) => (t === null ? null : Math.round(t - b.loadMs));
|
|
406
|
+
const ignored = out.load.settle.ignoredAreas ?? 0;
|
|
407
|
+
if (out.load.settle.allLeftOut) {
|
|
408
|
+
// Every cell that changed after load is an area that keeps moving: nothing was compared, so no reading.
|
|
409
|
+
notes.push("Every area that changed keeps moving; settle was not judged.");
|
|
410
|
+
} else {
|
|
411
|
+
const lastChangeMs = Math.round(out.load.settle.lastChangeMs);
|
|
412
|
+
// Unsettled against the end of the load window, not the last frame: the capture sends no frame while
|
|
413
|
+
// nothing changes.
|
|
414
|
+
report.timed.settle = { lastChangeMs, unsettled: lastChangeMs > b.windowMs - QUIET_MS, ignoredAreas: ignored };
|
|
415
|
+
}
|
|
416
|
+
if (ignored > 0) notes.push(`${ignored} ${plural(ignored, "area that keeps moving was", "areas that keep moving were")} left out of settle; ${plural(ignored, "it is", "they are")} judged by moving content (WCAG 2.2.2).`);
|
|
417
|
+
report.timed.moving = out.load.moving.map((m) => ({ selector: elementAt(b.snapshot?.points, m.box), box: m.box, firstMs: rel(m.firstMs), lastMs: rel(m.lastMs), seconds: m.seconds }));
|
|
418
|
+
if (out.all.flashes) {
|
|
419
|
+
const f = out.all.flashes;
|
|
420
|
+
report.timed.flashes = { maxPerSecond: f.maxPerSecond, at: rel(f.at), inPlace: { maxPerSecond: f.inPlace.maxPerSecond, at: rel(f.inPlace.at) } };
|
|
421
|
+
}
|
|
422
|
+
const changedAfterLoad = out.load.changedAt.some((t) => t > b.loadMs);
|
|
423
|
+
if (changedAfterLoad && (inventoryA ?? inventory(b).animations).length === 0) {
|
|
424
|
+
notes.push("The screen changed after load with no CSS or Web Animation running: motion driven by script is read from frames only; its durations and curves are not read.");
|
|
425
|
+
}
|
|
426
|
+
if (out.film) {
|
|
427
|
+
const file = path.join(ctx.cwd, ".lazuli", "motion", targetKey(ctx.target, ctx.cwd), "filmstrip.png");
|
|
428
|
+
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
429
|
+
await fs.writeFile(file, out.film);
|
|
430
|
+
report.written.push(file);
|
|
431
|
+
notes.push(`Frames for review: ${file}`);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// Passes A, B and C of the target at the first width of the run; null at any other width.
|
|
436
|
+
// capMs: the whole measure's cap (MOTION_CAP_MS; tests pass a smaller one).
|
|
437
|
+
export async function measureMotion(page, ctx, { capMs = MOTION_CAP_MS } = {}) {
|
|
438
|
+
const started = Date.now();
|
|
439
|
+
const budget = { capMs, started };
|
|
440
|
+
const first = await ctx.once("motion.first-width", () => ctx.width);
|
|
441
|
+
const held = await ctx.once("motion.report", () => ({ report: null }));
|
|
442
|
+
if (ctx.width !== first) {
|
|
443
|
+
const note = `Motion was recorded at ${first} px only (the first width of the run).`;
|
|
444
|
+
if (held.report && !held.report.notes.includes(note)) held.report.notes.push(note);
|
|
445
|
+
return null;
|
|
446
|
+
}
|
|
447
|
+
const notes = [];
|
|
448
|
+
const report = {
|
|
449
|
+
width: ctx.width,
|
|
450
|
+
local: null,
|
|
451
|
+
method: null,
|
|
452
|
+
steps: null,
|
|
453
|
+
exact: { animations: null, reduced: null, banding: null, pauseControls: null },
|
|
454
|
+
timed: { starts: null, longFrames: null, longFrameApi: null, shift: null, frames: null, settle: null, flashes: null, moving: null },
|
|
455
|
+
written: [],
|
|
456
|
+
notes,
|
|
457
|
+
elapsedMs: null,
|
|
458
|
+
};
|
|
459
|
+
held.report = report;
|
|
460
|
+
|
|
461
|
+
const a = await withinCap(budget, "default recording", DEFAULT_IDS, ctx.rules, notes, PASS_CAP_MS, (cap) => recordPass(ctx, { capMs: cap }));
|
|
462
|
+
if (a) {
|
|
463
|
+
pushNew(notes, a.notes);
|
|
464
|
+
report.local = a.local;
|
|
465
|
+
report.steps = a.steps.map((s) => ({ id: s.id, kind: s.kind, selector: s.selector, name: s.name, startMs: Math.round(s.startMs - a.loadMs), endMs: Math.round(s.endMs - a.loadMs) }));
|
|
466
|
+
const inv = inventory(a);
|
|
467
|
+
report.exact.animations = inv.animations;
|
|
468
|
+
report.timed.starts = inv.starts;
|
|
469
|
+
if (inv.unread > 0) {
|
|
470
|
+
notes.push(`${inv.unread} ${plural(inv.unread, "animation ran on an element hidden, clipped or outside the screen; it was", "animations ran on elements hidden, clipped or outside the screen; they were")} not read.`);
|
|
471
|
+
}
|
|
472
|
+
if (inv.timed > 0) {
|
|
473
|
+
notes.push(
|
|
474
|
+
`${inv.timed} ${plural(inv.timed, "animation", "animations")} started during a step but not on the element it acted on, or more than ${STEP_WINDOW_MS} ms after it; ${plural(inv.timed, "its duration was", "their durations were")} not judged.`,
|
|
475
|
+
);
|
|
476
|
+
}
|
|
477
|
+
report.timed.longFrameApi = a.longFrameApi;
|
|
478
|
+
report.timed.longFrames = a.longFrameApi === null
|
|
479
|
+
? null
|
|
480
|
+
: a.longFrames
|
|
481
|
+
.filter((f) => f.startMs >= a.loadMs)
|
|
482
|
+
.map((f) => ({ startMs: Math.round(f.startMs - a.loadMs), durationMs: f.durationMs, step: stepAt(a, f.startMs) }));
|
|
483
|
+
report.timed.shift = shiftOf(a, notes);
|
|
484
|
+
|
|
485
|
+
const moving = inv.animations.filter((x) => x.moves);
|
|
486
|
+
if (moving.length === 0) {
|
|
487
|
+
notes.push("Nothing moved in the default run, so reduced motion was not compared.");
|
|
488
|
+
} else {
|
|
489
|
+
const c = await withinCap(budget, "reduced recording", REDUCED_IDS, ctx.rules, notes, PASS_CAP_MS, (cap) => recordPass(ctx, { reduced: true, capMs: cap }));
|
|
490
|
+
if (c) {
|
|
491
|
+
pushNew(notes, c.notes);
|
|
492
|
+
// Paired by selector and name, each reduced record used once (selectors can repeat).
|
|
493
|
+
const pool = inventory(c).animations.filter((x) => x.moves && x.durationMs > 0);
|
|
494
|
+
const kept = [];
|
|
495
|
+
for (const d of moving) {
|
|
496
|
+
const i = pool.findIndex((x) => x.selector === d.selector && x.name === d.name);
|
|
497
|
+
if (i < 0) continue;
|
|
498
|
+
pool.splice(i, 1);
|
|
499
|
+
kept.push({ selector: d.selector, name: d.name, properties: d.properties });
|
|
500
|
+
}
|
|
501
|
+
report.exact.reduced = { moving: moving.length, kept };
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
await framesPass(page, ctx, report, notes, report.exact.animations, budget);
|
|
506
|
+
report.elapsedMs = Date.now() - started;
|
|
507
|
+
return report;
|
|
508
|
+
}
|