@uselazuli/cli 0.0.0-stage → 1.0.2
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 +120 -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 +747 -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 +19 -0
- package/rules/content/_area.yaml +8 -0
- package/rules/content/review.yaml +378 -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 +18 -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 +271 -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 +246 -0
- package/rules/ui/grid.yaml +75 -0
- package/rules/ui/review.yaml +400 -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 +183 -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 +36 -0
- package/skills/lazuli/procedures/show-options.md +33 -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 +40 -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 +45 -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 +162 -0
- package/src/commands/capture.mjs +313 -0
- package/src/commands/catalog.mjs +37 -0
- package/src/commands/check-design.mjs +463 -0
- package/src/commands/check-goals.mjs +57 -0
- package/src/commands/compare-designs.mjs +160 -0
- package/src/commands/draft-memo.mjs +162 -0
- package/src/commands/hooks.mjs +345 -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 +325 -0
- package/src/commands/record-task.mjs +250 -0
- package/src/commands/render-options.mjs +227 -0
- package/src/commands/start-project.mjs +32 -0
- package/src/measure/a11y.mjs +391 -0
- package/src/measure/asset.mjs +206 -0
- package/src/measure/at-rest.mjs +49 -0
- package/src/measure/cases.mjs +566 -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 +86 -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 +522 -0
- package/src/measure/in-page/color.js +348 -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 +377 -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 +171 -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 +269 -0
- package/src/measure/in-page/weight.js +207 -0
- package/src/measure/layout.mjs +100 -0
- package/src/measure/motion.mjs +509 -0
- package/src/measure/piece.mjs +195 -0
- package/src/measure/registry.mjs +253 -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 +168 -0
- package/src/project/lazuli-md.mjs +521 -0
- package/src/project/log.mjs +335 -0
- package/src/project/match.mjs +200 -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 +227 -0
- package/src/report/evaluators/chart.mjs +151 -0
- package/src/report/evaluators/coherence.mjs +103 -0
- package/src/report/evaluators/color.mjs +133 -0
- package/src/report/evaluators/content.mjs +112 -0
- package/src/report/evaluators/extreme.mjs +87 -0
- package/src/report/evaluators/forms.mjs +116 -0
- package/src/report/evaluators/layout.mjs +103 -0
- package/src/report/evaluators/motion.mjs +280 -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 +166 -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 +274 -0
- package/src/rules/public-text.mjs +56 -0
- package/src/rules/schema.mjs +220 -0
- package/templates/LAZULI.md +88 -0
|
@@ -0,0 +1,566 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import { restOn, restStill, restOff } from "./at-rest.mjs";
|
|
3
|
+
import { bounded } from "./states.mjs";
|
|
4
|
+
import { flowStopUrl } from "../project/tasks.mjs";
|
|
5
|
+
|
|
6
|
+
// Extreme content cases: a script-free snapshot of the rendered page, case pages that load it with page scripts
|
|
7
|
+
// off, the guard that keeps every case page read only, the in-page library (in-page/extreme.js) and the pure
|
|
8
|
+
// comparison of a case with its base.
|
|
9
|
+
//
|
|
10
|
+
// Case page mechanism: the case page's context
|
|
11
|
+
// has JavaScript disabled and the guard below; the page navigates to the source page's final URL and the guard
|
|
12
|
+
// fulfills that one main-frame document request with the snapshot (http, https and file alike), so the page
|
|
13
|
+
// keeps the target's URL and relative addresses while its own document never reaches the network or the disk.
|
|
14
|
+
|
|
15
|
+
// Definitions, not cutoffs (fixed before any page was measured, never edited after a reading).
|
|
16
|
+
// A spill, cut or overlap must exceed this many px (subpixel rounding).
|
|
17
|
+
export const SLACK_PX = 1;
|
|
18
|
+
// A box bleeds when it crosses the left or right edge of the viewport by more than this many px: the
|
|
19
|
+
// composition tolerance (the same value as ALIGN_PX in in-page/composition.js, which its bleeds use).
|
|
20
|
+
export const ALIGN_PX = 2;
|
|
21
|
+
// "centenas de itens": the many case fills each list to this many items.
|
|
22
|
+
export const MANY_ITEMS = 300;
|
|
23
|
+
// Lists by markup; an element with REPEATED_MIN or more visible element children sharing tag and class list
|
|
24
|
+
// is a list too.
|
|
25
|
+
export const LIST_SELECTOR = "ul, ol, tbody, [role=list], [role=feed]";
|
|
26
|
+
export const REPEATED_MIN = 3;
|
|
27
|
+
// The navigation regions of the content reader (the same text as NAV_REGIONS in in-page/composition.js).
|
|
28
|
+
export const NAV_REGIONS = "nav, header, footer, [role=navigation], [role=banner], [role=contentinfo]";
|
|
29
|
+
// Lists inside these are not lists.
|
|
30
|
+
export const NOT_LIST = `${NAV_REGIONS}, [role=menu], [role=menubar], [role=tablist], select, form`;
|
|
31
|
+
export const LABEL_SCOPE = "a, button, label, summary, th, [role=button], [role=tab], [role=menuitem], [role=link]";
|
|
32
|
+
export const HEADING_SCOPE = "h1, h2, h3, h4, h5, h6, [role=heading]";
|
|
33
|
+
// Decorative layers: subtrees hidden from assistive technology are left out of every case, neither changed nor
|
|
34
|
+
// read.
|
|
35
|
+
export const DECORATIVE = "[aria-hidden=true]";
|
|
36
|
+
// A list item is block-level: a child whose computed display is one of these is never an item, so the word or
|
|
37
|
+
// letter spans of a heading are not a list.
|
|
38
|
+
export const INLINE_ITEMS = ["inline", "inline-block", "inline-flex", "inline-grid"];
|
|
39
|
+
// Case samples: our own sentences, case data (Portuguese with accents and one very long dictionary word).
|
|
40
|
+
export const LONG_LABEL = "Solicitar orçamento personalizado para minha empresa";
|
|
41
|
+
export const LONG_HEADING = "Informações completas sobre atendimento, prazos de entrega e condições especiais de pagamento parcelado";
|
|
42
|
+
export const LONG_TEXT =
|
|
43
|
+
"A palavra pneumoultramicroscopicossilicovulcanoconiótico aparece aqui para testar a quebra de linha. Os prazos de entrega, as condições de pagamento e as informações de atendimento mudam conforme a região, a disponibilidade do estoque e a forma de envio escolhida no momento da compra.";
|
|
44
|
+
// The words of LONG_TEXT, cycled from the first word.
|
|
45
|
+
export const GROWTH_FILL = LONG_TEXT.split(/\s+/);
|
|
46
|
+
|
|
47
|
+
// Sampling limits, not rule cutoffs; each is noted when it cuts.
|
|
48
|
+
export const MAX_LISTS = 6;
|
|
49
|
+
export const MAX_CLONE_ELEMENTS = 50000;
|
|
50
|
+
export const MAX_RECTS = 5000;
|
|
51
|
+
export const MAX_SNAPSHOT_BYTES = 10_000_000;
|
|
52
|
+
export const MAX_LISTED = 10;
|
|
53
|
+
// Styles, fonts and images kept in memory per width for the case pages.
|
|
54
|
+
export const MAX_MEMORY_BYTES = 100_000_000;
|
|
55
|
+
|
|
56
|
+
export const EXTREME_DEFS = {
|
|
57
|
+
SLACK_PX, ALIGN_PX, MANY_ITEMS, LIST_SELECTOR, REPEATED_MIN, NOT_LIST, LABEL_SCOPE, HEADING_SCOPE, DECORATIVE, INLINE_ITEMS,
|
|
58
|
+
LONG_LABEL, LONG_HEADING, LONG_TEXT, GROWTH_FILL, MAX_LISTS, MAX_CLONE_ELEMENTS, MAX_RECTS,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// The only requests a case page may send: GET for these resource types (plus its own document, served by
|
|
62
|
+
// the guard).
|
|
63
|
+
const ALLOWED = new Set(["stylesheet", "font", "image"]);
|
|
64
|
+
const MB = (bytes) => `${bytes / 1_000_000} MB`;
|
|
65
|
+
const plural = (n, one, many) => (n === 1 ? one : many);
|
|
66
|
+
|
|
67
|
+
let sources;
|
|
68
|
+
async function source() {
|
|
69
|
+
sources ??= Promise.all([
|
|
70
|
+
fs.readFile(new URL("./in-page/composition.js", import.meta.url), "utf8"),
|
|
71
|
+
fs.readFile(new URL("./in-page/extreme.js", import.meta.url), "utf8"),
|
|
72
|
+
]).then(([composition, extreme]) => ({ composition, extreme }));
|
|
73
|
+
return sources;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// The library as a page expression: { tag, inject, read }.
|
|
77
|
+
export async function extremeLibrary() {
|
|
78
|
+
const { composition, extreme } = await source();
|
|
79
|
+
return `(${extreme})(${composition}, ${JSON.stringify(EXTREME_DEFS)})`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// In the page, at rest: the rendered document cloned without scripts, refresh, inline handlers or the at-rest
|
|
83
|
+
// style; CSSOM rules written into their style elements and adopted sheets appended; lazy images and
|
|
84
|
+
// frames made eager. Counts of kept shadow hosts, canvases and frames, and of loaded fonts added by script
|
|
85
|
+
// (a face the FontFaceSet lets delete is not tied to an @font-face rule; it is put back at once).
|
|
86
|
+
function snapshotInPage(composition, restId) {
|
|
87
|
+
const kept = composition({ read: "seen", keepPinned: true });
|
|
88
|
+
const keptSet = new Set(kept);
|
|
89
|
+
const shadows = kept.filter((el) => el.shadowRoot).length;
|
|
90
|
+
const canvases = kept.filter((el) => el.tagName === "CANVAS").length;
|
|
91
|
+
const frames = [...document.querySelectorAll("iframe, frame")].filter((f) => {
|
|
92
|
+
const r = f.getBoundingClientRect();
|
|
93
|
+
return keptSet.has(f.parentElement) && r.width > 0 && r.height > 0;
|
|
94
|
+
}).length;
|
|
95
|
+
let scriptFonts = 0;
|
|
96
|
+
for (const face of [...document.fonts]) {
|
|
97
|
+
if (face.status !== "loaded") continue;
|
|
98
|
+
if (document.fonts.delete(face)) {
|
|
99
|
+
scriptFonts += 1;
|
|
100
|
+
document.fonts.add(face);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// Cloned into an inert document: nothing in the clone loads (an image or a frame cloned in the page would
|
|
104
|
+
// be requested again).
|
|
105
|
+
const inert = document.implementation.createHTMLDocument("");
|
|
106
|
+
const clone = inert.importNode(document.documentElement, true);
|
|
107
|
+
const styles = [...document.querySelectorAll("style")];
|
|
108
|
+
const copies = [...clone.querySelectorAll("style")];
|
|
109
|
+
styles.forEach((s, i) => {
|
|
110
|
+
try {
|
|
111
|
+
const rules = s.sheet?.cssRules;
|
|
112
|
+
if (rules && copies[i]) copies[i].textContent = [...rules].map((r) => r.cssText).join("\n");
|
|
113
|
+
} catch {
|
|
114
|
+
// a sheet that cannot be read keeps its text
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
const adopted = [...(document.adoptedStyleSheets ?? [])].flatMap((sheet) => {
|
|
118
|
+
try {
|
|
119
|
+
return [...sheet.cssRules].map((r) => r.cssText);
|
|
120
|
+
} catch {
|
|
121
|
+
return [];
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
if (adopted.length) {
|
|
125
|
+
const style = inert.createElement("style");
|
|
126
|
+
style.textContent = adopted.join("\n");
|
|
127
|
+
(clone.querySelector("head") ?? clone).appendChild(style);
|
|
128
|
+
}
|
|
129
|
+
clone.querySelector(`#${CSS.escape(restId)}`)?.remove();
|
|
130
|
+
for (const el of clone.querySelectorAll("script, noscript")) el.remove();
|
|
131
|
+
// Connection hints open connections to other hosts without a request the guard sees.
|
|
132
|
+
for (const l of clone.querySelectorAll("link[rel]")) {
|
|
133
|
+
if (/(^|\s)(preconnect|dns-prefetch)(\s|$)/i.test(l.getAttribute("rel"))) l.remove();
|
|
134
|
+
}
|
|
135
|
+
for (const m of clone.querySelectorAll("meta")) {
|
|
136
|
+
if ((m.getAttribute("http-equiv") || "").trim().toLowerCase() === "refresh") m.remove();
|
|
137
|
+
}
|
|
138
|
+
for (const el of [clone, ...clone.querySelectorAll("*")]) {
|
|
139
|
+
for (const a of [...el.attributes]) if (/^on/i.test(a.name)) el.removeAttribute(a.name);
|
|
140
|
+
}
|
|
141
|
+
for (const el of clone.querySelectorAll("img, iframe")) el.setAttribute("loading", "eager");
|
|
142
|
+
const doctype = document.doctype ? new XMLSerializer().serializeToString(document.doctype) : "";
|
|
143
|
+
return { html: doctype + clone.outerHTML, shadows, canvases, frames, scriptFonts };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// The script-free snapshot of a page opened normally (read at rest; the at-rest style is removed in finally).
|
|
147
|
+
export async function snapshotOf(page) {
|
|
148
|
+
const { composition } = await source();
|
|
149
|
+
let out;
|
|
150
|
+
try {
|
|
151
|
+
await page.evaluate(restOn());
|
|
152
|
+
out = await page.evaluate(`(${snapshotInPage})(${composition}, "lazuli-at-rest")`);
|
|
153
|
+
} finally {
|
|
154
|
+
await page.evaluate(restOff()).catch(() => {});
|
|
155
|
+
}
|
|
156
|
+
if (Buffer.byteLength(out.html, "utf8") > MAX_SNAPSHOT_BYTES) {
|
|
157
|
+
throw new Error(`The page is larger than ${MB(MAX_SNAPSHOT_BYTES)}; extreme content was not checked.`);
|
|
158
|
+
}
|
|
159
|
+
const notes = [];
|
|
160
|
+
if (out.shadows) notes.push(`${out.shadows} ${plural(out.shadows, "shadow root: its content is not in the snapshot.", "shadow roots: their content is not in the snapshot.")}`);
|
|
161
|
+
if (out.canvases) notes.push(`${out.canvases} ${plural(out.canvases, "canvas is blank in the case pages.", "canvases are blank in the case pages.")}`);
|
|
162
|
+
if (out.frames) notes.push(`${out.frames} ${plural(out.frames, "frame is empty in the case pages.", "frames are empty in the case pages.")}`);
|
|
163
|
+
if (out.scriptFonts) notes.push(`${out.scriptFonts} ${plural(out.scriptFonts, "font added by script is not in the case pages.", "fonts added by script are not in the case pages.")}`);
|
|
164
|
+
return { html: out.html, notes };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/// Styles, fonts and images by address, shared by the source open and the case pages of one width:
|
|
168
|
+
// entries resolve to { status, contentType, body, hops } or null when the body could not be kept. hops: every
|
|
169
|
+
// address of the redirect chain, the first one asked for first; the final response is kept under each of them.
|
|
170
|
+
// limit: MAX_MEMORY_BYTES (a test may lower it).
|
|
171
|
+
export function resourceMemory(limit = MAX_MEMORY_BYTES) {
|
|
172
|
+
return { entries: new Map(), bytes: 0, cut: false, limit };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// The final response of a chain, kept within the memory limit; null when it does not fit.
|
|
176
|
+
const keepBody = (memory, { status, contentType, body, hops }) => {
|
|
177
|
+
if (memory.bytes + body.length > memory.limit) {
|
|
178
|
+
memory.cut = true;
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
memory.bytes += body.length;
|
|
182
|
+
return { status, contentType, body, hops };
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// The redirect chain of a request, the first address asked for first.
|
|
186
|
+
const chainOf = (req) => {
|
|
187
|
+
const hops = [];
|
|
188
|
+
for (let r = req; r; r = r.redirectedFrom()) hops.unshift(r.url());
|
|
189
|
+
return hops;
|
|
190
|
+
};
|
|
191
|
+
const isRedirect = (status, location) => status >= 300 && status < 400 && !!location;
|
|
192
|
+
|
|
193
|
+
// The note when the memory limit cut, else null.
|
|
194
|
+
export function memoryNote(memory) {
|
|
195
|
+
return memory.cut ? `Styles, fonts and images past ${MB(memory.limit)} were not kept for the case pages; some may be missing there.` : null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// The prepare of the source open: it only listens, and keeps every GET style, font and image response. A
|
|
199
|
+
// redirect is kept once its chain ends: the final response under every address of the chain.
|
|
200
|
+
export function recorder(memory = resourceMemory()) {
|
|
201
|
+
return {
|
|
202
|
+
memory,
|
|
203
|
+
prepare: async (context) => {
|
|
204
|
+
context.on("response", (res) => {
|
|
205
|
+
const req = res.request();
|
|
206
|
+
if (req.method() !== "GET" || !ALLOWED.has(req.resourceType())) return;
|
|
207
|
+
if (isRedirect(res.status(), res.headers().location)) return; // the chain goes on
|
|
208
|
+
const hops = chainOf(req);
|
|
209
|
+
if (hops.every((u) => memory.entries.has(u))) return;
|
|
210
|
+
const kept = res.body().then(
|
|
211
|
+
(body) => keepBody(memory, { status: res.status(), contentType: res.headers()["content-type"] ?? "", body, hops }),
|
|
212
|
+
() => null,
|
|
213
|
+
);
|
|
214
|
+
for (const u of hops) if (!memory.entries.has(u)) memory.entries.set(u, kept);
|
|
215
|
+
});
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Guard state by context, so openCase and loadCase find the guard of the context they open in.
|
|
221
|
+
const guards = new WeakMap();
|
|
222
|
+
const noHash = (url) => url.replace(/#.*$/, "");
|
|
223
|
+
// Implementation limit, not a rule cutoff: redirects followed for one style, font or image (Chrome's own limit).
|
|
224
|
+
const MAX_REDIRECTS = 20;
|
|
225
|
+
|
|
226
|
+
// The guard of the case pages' context (install it in prepare, before any page opens). In every case page, on
|
|
227
|
+
// every host: the page's own document is served from the snapshot; any other main-frame navigation is aborted
|
|
228
|
+
// (a refresh, a form, a link: none ever leaves); a request whose address has a stop word (flowStopUrl: logout,
|
|
229
|
+
// delete, unsubscribe, checkout) is aborted and counted as a stop; a GET for a stylesheet, font or image
|
|
230
|
+
// is served from memory (blocked when a hop of its kept redirect chain has a stop word), or passes once per
|
|
231
|
+
// width and is kept; everything else is aborted and counted in sent and byType. A file passes with
|
|
232
|
+
// route.fallback (routes registered before it still see it). An http or https address passes hop by hop
|
|
233
|
+
// through route.fetch with redirects off: the browser follows a redirect outside every route, so each hop is
|
|
234
|
+
// checked here (stop word, once per width) and the final response is fulfilled.
|
|
235
|
+
// WebSockets are closed before they connect, dialogs dismissed, and a page that is not a case page is closed
|
|
236
|
+
// and counted (windows). unserved(page) lists the stylesheets a case page asked for and did not get.
|
|
237
|
+
export function caseGuard(memory = resourceMemory()) {
|
|
238
|
+
const state = { sent: 0, byType: {}, stops: 0, windows: 0, sockets: 0, passed: 0, fromMemory: 0 };
|
|
239
|
+
const own = { pages: new Set(), docs: new Map(), expecting: true, tried: new Set(), memory, unserved: new Map() };
|
|
240
|
+
const count = (type) => {
|
|
241
|
+
state.sent += 1;
|
|
242
|
+
state.byType[type] = (state.byType[type] ?? 0) + 1;
|
|
243
|
+
};
|
|
244
|
+
const missed = (page, type, url) => {
|
|
245
|
+
if (type !== "stylesheet" || !page) return;
|
|
246
|
+
if (!own.unserved.has(page)) own.unserved.set(page, new Set());
|
|
247
|
+
own.unserved.get(page).add(url);
|
|
248
|
+
};
|
|
249
|
+
// One pass of an http(s) resource: each hop checked, redirects followed here; the final response, or null
|
|
250
|
+
// when a hop is stopped, was already tried this width, or the chain is too long.
|
|
251
|
+
const passHops = async (route, url) => {
|
|
252
|
+
let current = url;
|
|
253
|
+
const hops = [url];
|
|
254
|
+
for (let i = 0; i <= MAX_REDIRECTS; i++) {
|
|
255
|
+
state.passed += 1;
|
|
256
|
+
const res = await route.fetch({ url: current, method: "GET", maxRedirects: 0 });
|
|
257
|
+
const location = res.headers().location;
|
|
258
|
+
if (!isRedirect(res.status(), location)) {
|
|
259
|
+
return { status: res.status(), contentType: res.headers()["content-type"] ?? "", body: await res.body(), hops };
|
|
260
|
+
}
|
|
261
|
+
const next = new URL(location, current).href;
|
|
262
|
+
if (flowStopUrl(next)) {
|
|
263
|
+
state.stops += 1;
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
if (own.tried.has(next)) return null;
|
|
267
|
+
own.tried.add(next);
|
|
268
|
+
hops.push(next);
|
|
269
|
+
current = next;
|
|
270
|
+
}
|
|
271
|
+
return null;
|
|
272
|
+
};
|
|
273
|
+
const install = async (context) => {
|
|
274
|
+
guards.set(context, own);
|
|
275
|
+
context.on("page", (p) => {
|
|
276
|
+
// The first page of the context (the open's own page) and each page openCase creates are case pages.
|
|
277
|
+
if (own.expecting) {
|
|
278
|
+
own.expecting = false;
|
|
279
|
+
own.pages.add(p);
|
|
280
|
+
p.on("dialog", (d) => d.dismiss().catch(() => {}));
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (own.pages.has(p)) return;
|
|
284
|
+
state.windows += 1;
|
|
285
|
+
p.close().catch(() => {});
|
|
286
|
+
});
|
|
287
|
+
await context.routeWebSocket(() => true, (ws) => {
|
|
288
|
+
state.sockets += 1;
|
|
289
|
+
ws.close();
|
|
290
|
+
});
|
|
291
|
+
await context.route("**/*", async (route) => {
|
|
292
|
+
const req = route.request();
|
|
293
|
+
const url = req.url();
|
|
294
|
+
const type = req.resourceType();
|
|
295
|
+
let frame = null;
|
|
296
|
+
let page = null;
|
|
297
|
+
try {
|
|
298
|
+
frame = req.frame();
|
|
299
|
+
page = frame.page();
|
|
300
|
+
} catch {
|
|
301
|
+
frame = null; // a window the page opens: fail closed below
|
|
302
|
+
page = null;
|
|
303
|
+
}
|
|
304
|
+
try {
|
|
305
|
+
if (req.isNavigationRequest() && (!frame || frame.parentFrame() === null)) {
|
|
306
|
+
const doc = page ? own.docs.get(page) : undefined;
|
|
307
|
+
if (doc && !doc.served && req.method() === "GET" && noHash(url) === doc.url) {
|
|
308
|
+
doc.served = true;
|
|
309
|
+
return await route.fulfill({ status: 200, contentType: "text/html; charset=utf-8", body: doc.html });
|
|
310
|
+
}
|
|
311
|
+
count(type);
|
|
312
|
+
return await route.abort("aborted");
|
|
313
|
+
}
|
|
314
|
+
if (flowStopUrl(url)) {
|
|
315
|
+
state.stops += 1;
|
|
316
|
+
missed(page, type, url);
|
|
317
|
+
return await route.abort("aborted");
|
|
318
|
+
}
|
|
319
|
+
if (req.method() === "GET" && ALLOWED.has(type)) {
|
|
320
|
+
const entry = memory.entries.get(url);
|
|
321
|
+
const got = entry ? await entry : null;
|
|
322
|
+
if (got) {
|
|
323
|
+
if (got.hops.some((u) => flowStopUrl(u))) {
|
|
324
|
+
state.stops += 1;
|
|
325
|
+
missed(page, type, url);
|
|
326
|
+
return await route.abort("aborted");
|
|
327
|
+
}
|
|
328
|
+
state.fromMemory += 1;
|
|
329
|
+
return await route.fulfill({ status: got.status, contentType: got.contentType, body: got.body });
|
|
330
|
+
}
|
|
331
|
+
if (own.tried.has(url)) {
|
|
332
|
+
missed(page, type, url);
|
|
333
|
+
return await route.abort("aborted");
|
|
334
|
+
}
|
|
335
|
+
own.tried.add(url);
|
|
336
|
+
let settle;
|
|
337
|
+
memory.entries.set(url, new Promise((resolve) => (settle = resolve)));
|
|
338
|
+
if (/^https?:/i.test(url)) {
|
|
339
|
+
const out = await passHops(route, url).catch(() => null);
|
|
340
|
+
if (!out) {
|
|
341
|
+
settle(null);
|
|
342
|
+
missed(page, type, url);
|
|
343
|
+
return await route.abort("aborted");
|
|
344
|
+
}
|
|
345
|
+
const kept = keepBody(memory, out);
|
|
346
|
+
for (const u of out.hops) if (u !== url && !memory.entries.has(u)) memory.entries.set(u, Promise.resolve(kept));
|
|
347
|
+
settle(kept);
|
|
348
|
+
return await route.fulfill({ status: out.status, contentType: out.contentType, body: out.body });
|
|
349
|
+
}
|
|
350
|
+
const done = (r) => r === req;
|
|
351
|
+
const finished = (r) => {
|
|
352
|
+
if (!done(r)) return;
|
|
353
|
+
context.off("requestfinished", finished);
|
|
354
|
+
context.off("requestfailed", failed);
|
|
355
|
+
r.response()
|
|
356
|
+
.then((res) => (res ? res.body().then((body) => keepBody(memory, { status: res.status(), contentType: res.headers()["content-type"] ?? "", body, hops: [url] })) : null))
|
|
357
|
+
.catch(() => null)
|
|
358
|
+
.then(settle);
|
|
359
|
+
};
|
|
360
|
+
const failed = (r) => {
|
|
361
|
+
if (!done(r)) return;
|
|
362
|
+
context.off("requestfinished", finished);
|
|
363
|
+
context.off("requestfailed", failed);
|
|
364
|
+
missed(page, type, url);
|
|
365
|
+
settle(null);
|
|
366
|
+
};
|
|
367
|
+
context.on("requestfinished", finished);
|
|
368
|
+
context.on("requestfailed", failed);
|
|
369
|
+
state.passed += 1;
|
|
370
|
+
return await route.fallback();
|
|
371
|
+
}
|
|
372
|
+
count(type);
|
|
373
|
+
return await route.abort("aborted");
|
|
374
|
+
} catch {
|
|
375
|
+
// The page closed while the request was handled, or the route failed: fail closed.
|
|
376
|
+
await route.abort("aborted").catch(() => {});
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
};
|
|
380
|
+
// The stylesheets a case page asked for and did not get (blocked, or not kept for this width), sorted.
|
|
381
|
+
const unserved = (page) => [...(own.unserved.get(page) ?? [])].sort();
|
|
382
|
+
return { state, install, unserved };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// Loads a case page of a guarded context: its main document is html at url (the source page's final URL),
|
|
386
|
+
// then load, then document.fonts.ready, then the at-rest style. Registered with the guard before navigation.
|
|
387
|
+
// The whole load is bounded by capMs.
|
|
388
|
+
export async function loadCase(page, url, html, capMs) {
|
|
389
|
+
const own = guards.get(page.context());
|
|
390
|
+
if (!own) throw new Error("a case page needs a context with the case guard installed");
|
|
391
|
+
if (!/^(https?|file):/i.test(url)) throw new Error(`A case page cannot be opened at ${url}.`);
|
|
392
|
+
own.pages.add(page);
|
|
393
|
+
own.docs.set(page, { url: noHash(url), html, served: false });
|
|
394
|
+
const work = (async () => {
|
|
395
|
+
await page.goto(url, { waitUntil: "load" });
|
|
396
|
+
if (!own.docs.get(page).served) throw new Error("the case page was not served from the snapshot");
|
|
397
|
+
await page.evaluate(() => document.fonts.ready.then(() => true));
|
|
398
|
+
await page.evaluate(restOn());
|
|
399
|
+
return page;
|
|
400
|
+
})();
|
|
401
|
+
work.catch(() => {});
|
|
402
|
+
return bounded(work, capMs, "the case page");
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// A new case page in the guarded context (see loadCase); closed when it fails or runs past capMs.
|
|
406
|
+
export async function openCase(context, url, html, capMs) {
|
|
407
|
+
const own = guards.get(context);
|
|
408
|
+
if (!own) throw new Error("a case page needs a context with the case guard installed");
|
|
409
|
+
own.expecting = true;
|
|
410
|
+
let page;
|
|
411
|
+
try {
|
|
412
|
+
page = await context.newPage();
|
|
413
|
+
} finally {
|
|
414
|
+
own.expecting = false;
|
|
415
|
+
}
|
|
416
|
+
own.pages.add(page);
|
|
417
|
+
try {
|
|
418
|
+
return await loadCase(page, url, html, capMs);
|
|
419
|
+
} catch (err) {
|
|
420
|
+
await page.close().catch(() => {});
|
|
421
|
+
throw err;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// One case on a loaded case page: tag, inject, the at-rest walk again, read.
|
|
426
|
+
export async function runCase(page, kind, params = {}) {
|
|
427
|
+
await page.evaluate(`window.__lazuliExtreme = ${await extremeLibrary()}; window.__lazuliExtreme.tag()`);
|
|
428
|
+
const injected = await page.evaluate(([k, p]) => window.__lazuliExtreme.inject(k, p), [kind, params]);
|
|
429
|
+
await page.evaluate(restStill());
|
|
430
|
+
const breaks = await page.evaluate(() => window.__lazuliExtreme.read());
|
|
431
|
+
return { injected, breaks };
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// ---- comparison (pure) ----
|
|
435
|
+
const originOf = (id) => id.split("~")[0];
|
|
436
|
+
const isCopy = (id) => id !== originOf(id);
|
|
437
|
+
|
|
438
|
+
// True when a case page loaded fonts, images or stylesheets differently from the base: a face loaded in one and loading or failed in the other, a stylesheet present in both that
|
|
439
|
+
// holds a different number of rules (a sheet that failed to load holds none), or an image present in both (by original) that failed in only one.
|
|
440
|
+
export function loadsDiffer(base, kase) {
|
|
441
|
+
const faces = (b) => {
|
|
442
|
+
const m = new Map();
|
|
443
|
+
for (const f of b.fonts ?? []) {
|
|
444
|
+
const at = f.lastIndexOf(": ");
|
|
445
|
+
m.set(f.slice(0, at), f.slice(at + 2));
|
|
446
|
+
}
|
|
447
|
+
return m;
|
|
448
|
+
};
|
|
449
|
+
const fb = faces(base);
|
|
450
|
+
const fk = faces(kase);
|
|
451
|
+
for (const [face, status] of fb) {
|
|
452
|
+
const other = fk.get(face);
|
|
453
|
+
if (other !== undefined && (status === "loaded") !== (other === "loaded")) return true;
|
|
454
|
+
}
|
|
455
|
+
const sheets = (b) => new Map((b.sheets ?? []).map((s) => [s.href, s.rules]));
|
|
456
|
+
const sb = sheets(base);
|
|
457
|
+
const sk = sheets(kase);
|
|
458
|
+
for (const [href, rules] of sb) if (sk.has(href) && sk.get(href) !== rules) return true;
|
|
459
|
+
const present = new Set((kase.kept ?? []).map(originOf));
|
|
460
|
+
const baseKept = new Set(base.kept ?? []);
|
|
461
|
+
const bb = new Set((base.brokenImages ?? []).map(originOf));
|
|
462
|
+
const bk = new Set((kase.brokenImages ?? []).map(originOf));
|
|
463
|
+
for (const id of new Set([...bb, ...bk])) {
|
|
464
|
+
if (baseKept.has(id) && present.has(id) && bb.has(id) !== bk.has(id)) return true;
|
|
465
|
+
}
|
|
466
|
+
return false;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// What a case adds to its base (both Breaks read at the same width). A copy (x~c) is compared through its
|
|
470
|
+
// original x; a flag counts only when the base element did not have it; lost: text elements kept in the base,
|
|
471
|
+
// still in the case DOM (not removed), not kept in the case and cut away entirely by a hidden or clip box;
|
|
472
|
+
// an overlap counts only when the base had none between the same originals and both members had every
|
|
473
|
+
// line box read in the base and in the case. Copies of one original are reported once, with
|
|
474
|
+
// "(<n> copies)" after the text; texts carry the base text of the original and bleedsAtRest, true when the
|
|
475
|
+
// original's box crossed the viewport's left or right edge in the base. overflowX: the sideways scroll the case
|
|
476
|
+
// adds (null on a page that hides its sideways overflow); clippedX: on such a page, how much further the content
|
|
477
|
+
// runs past the clipped right edge. wide: the texts whose right edge passes the viewport in the case and did not
|
|
478
|
+
// at rest, or passes it further than at rest, listed only when the case adds one of the two. restBleeds: how many texts of the
|
|
479
|
+
// base bleed at rest (null when the base reading has no bleed fields).
|
|
480
|
+
export function compareBreaks(base, kase, injected = { removed: [] }) {
|
|
481
|
+
const baseText = new Map(base.texts.map((t) => [t.id, t]));
|
|
482
|
+
const baseKept = new Set(base.kept);
|
|
483
|
+
const removed = new Set(injected.removed ?? []);
|
|
484
|
+
const caseKept = new Set(kase.kept);
|
|
485
|
+
const caseText = new Map(kase.texts.map((t) => [t.id, t]));
|
|
486
|
+
|
|
487
|
+
// Group flagged ids by original, counting the copies among them.
|
|
488
|
+
const group = (ids, extra = () => ({})) => {
|
|
489
|
+
const byOrigin = new Map();
|
|
490
|
+
for (const id of ids) {
|
|
491
|
+
const o = originOf(id);
|
|
492
|
+
const g = byOrigin.get(o) ?? { id: o, copies: 0, first: id };
|
|
493
|
+
if (isCopy(id)) g.copies += 1;
|
|
494
|
+
byOrigin.set(o, g);
|
|
495
|
+
}
|
|
496
|
+
return [...byOrigin.values()].map((g) => describe(g.id, g.copies, caseText.get(g.first), extra(g.first)));
|
|
497
|
+
};
|
|
498
|
+
const describe = (o, copies, fallback, extra = {}) => {
|
|
499
|
+
const b = baseText.get(o) ?? fallback;
|
|
500
|
+
const suffix = copies > 0 ? ` (${copies} ${plural(copies, "copy", "copies")})` : "";
|
|
501
|
+
return { id: o, selector: b?.selector ?? "", text: `${b?.text ?? ""}${suffix}`, bleedsAtRest: baseText.get(o)?.bleeds === true, ...extra };
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
const spills = group(kase.texts.filter((t) => t.spills && !baseText.get(originOf(t.id))?.spills).map((t) => t.id));
|
|
505
|
+
const cut = group(
|
|
506
|
+
kase.texts.filter((t) => t.cut && !baseText.get(originOf(t.id))?.cut).map((t) => t.id),
|
|
507
|
+
(id) => ({ cut: caseText.get(id).cut }),
|
|
508
|
+
);
|
|
509
|
+
const lost = group(
|
|
510
|
+
(kase.away ?? []).filter((id) => {
|
|
511
|
+
const o = originOf(id);
|
|
512
|
+
return baseKept.has(o) && baseText.has(o) && !removed.has(id) && !caseKept.has(id);
|
|
513
|
+
}),
|
|
514
|
+
);
|
|
515
|
+
|
|
516
|
+
const whole = (b, id) => b.texts.find((t) => t.id === id)?.whole !== false;
|
|
517
|
+
const baseWhole = new Set(base.texts.filter((t) => t.whole !== false).map((t) => t.id));
|
|
518
|
+
const pairKey = (a, b) => [a, b].sort((x, y) => x.localeCompare(y, "en", { numeric: true }));
|
|
519
|
+
const basePairs = new Set(base.overlaps.map(([a, b]) => pairKey(originOf(a), originOf(b)).join("|")));
|
|
520
|
+
const newPairs = new Map();
|
|
521
|
+
for (const [a, b] of kase.overlaps) {
|
|
522
|
+
const [oa, ob] = pairKey(originOf(a), originOf(b));
|
|
523
|
+
const key = `${oa}|${ob}`;
|
|
524
|
+
if (basePairs.has(key)) continue;
|
|
525
|
+
if (!baseWhole.has(oa) || !baseWhole.has(ob) || !whole(kase, a) || !whole(kase, b)) continue;
|
|
526
|
+
const g = newPairs.get(key) ?? { oa, ob, ca: new Set(), cb: new Set() };
|
|
527
|
+
for (const id of [a, b]) {
|
|
528
|
+
if (!isCopy(id)) continue;
|
|
529
|
+
if (originOf(id) === oa) g.ca.add(id);
|
|
530
|
+
else g.cb.add(id);
|
|
531
|
+
}
|
|
532
|
+
newPairs.set(key, g);
|
|
533
|
+
}
|
|
534
|
+
const overlaps = [...newPairs.values()].map((g) => [
|
|
535
|
+
describe(g.oa, g.ca.size, caseText.get(g.oa)),
|
|
536
|
+
describe(g.ob, g.cb.size, caseText.get(g.ob)),
|
|
537
|
+
]);
|
|
538
|
+
|
|
539
|
+
const grew = (k, b) => (Number.isFinite(k) && Number.isFinite(b) && k - b > SLACK_PX ? k - b : null);
|
|
540
|
+
const overflowX = grew(kase.overflowX, base.overflowX);
|
|
541
|
+
const clippedX = grew(kase.clippedX, base.clippedX);
|
|
542
|
+
// Past the right edge in the case and not at rest, or further past it than at rest.
|
|
543
|
+
const newlyPast = (t) => {
|
|
544
|
+
const b = baseText.get(originOf(t.id));
|
|
545
|
+
return !b?.pastRight || grew(t.right, b.right) !== null;
|
|
546
|
+
};
|
|
547
|
+
const wide = overflowX === null && clippedX === null ? [] : group(kase.texts.filter((t) => t.pastRight && newlyPast(t)).map((t) => t.id));
|
|
548
|
+
const restBleeds = base.texts.some((t) => "bleeds" in t) ? base.texts.filter((t) => t.bleeds).length : null;
|
|
549
|
+
return { overflowX, clippedX, spills, cut, lost, overlaps, wide, restBleeds };
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// The verdict of one case: breaks are reported only when every stylesheet was
|
|
553
|
+
// served to the base and the case page (missing: the guard's unserved lists of both pages) and the two pages
|
|
554
|
+
// loaded fonts, images and stylesheets alike; otherwise breaks is null and the note says why. caseWords names
|
|
555
|
+
// the case in the note.
|
|
556
|
+
export function judgeCase(base, kase, injected, { caseWords, missing = [] }) {
|
|
557
|
+
const sheets = [...new Set(missing)].sort();
|
|
558
|
+
if (sheets.length) {
|
|
559
|
+
const what = sheets.length === 1 ? `the stylesheet ${sheets[0]}` : `${sheets.length} stylesheets (${sheets.join(", ")})`;
|
|
560
|
+
return { breaks: null, note: `The ${caseWords} case could not be checked: ${what} could not be loaded in its case pages, so its breaks are not reported.` };
|
|
561
|
+
}
|
|
562
|
+
if (loadsDiffer(base, kase)) {
|
|
563
|
+
return { breaks: null, note: `The ${caseWords} case loaded fonts, images or stylesheets differently from the base copy, so its breaks are not reported.` };
|
|
564
|
+
}
|
|
565
|
+
return { breaks: compareBreaks(base, kase, injected), note: null };
|
|
566
|
+
}
|