@monster-orange/den-ui 0.0.0-stage → 0.6.6
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/LICENSE +21 -0
- package/README.md +408 -2
- package/bin/den-ui-parity.mjs +365 -0
- package/inventory/nav-inventory.d.ts +89 -0
- package/inventory/nav-inventory.js +590 -0
- package/inventory/reference/den-chromium-1280.json +5137 -0
- package/inventory/reference/den-chromium-390.json +3653 -0
- package/inventory/reference/den-webkit-1280.json +5137 -0
- package/inventory/reference/den-webkit-390.json +3653 -0
- package/package.json +51 -4
- package/src/base.css +80 -0
- package/src/busy-words.d.ts +2 -0
- package/src/busy-words.js +71 -0
- package/src/busy.css +155 -0
- package/src/busy.d.ts +80 -0
- package/src/busy.js +662 -0
- package/src/dialog.css +103 -0
- package/src/dialog.d.ts +44 -0
- package/src/dialog.js +282 -0
- package/src/fonts/OFL.txt +97 -0
- package/src/fonts/rubik-latin-400-normal.woff2 +0 -0
- package/src/fonts/rubik-latin-500-normal.woff2 +0 -0
- package/src/fonts/rubik-latin-ext-400-normal.woff2 +0 -0
- package/src/fonts/rubik-latin-ext-500-normal.woff2 +0 -0
- package/src/fonts/space-mono-latin-400-normal.woff2 +0 -0
- package/src/fonts/space-mono-latin-ext-400-normal.woff2 +0 -0
- package/src/fonts.css +71 -0
- package/src/icons.d.ts +4 -0
- package/src/icons.js +43 -0
- package/src/kit.d.ts +146 -0
- package/src/kit.js +224 -0
- package/src/report-shot.d.ts +47 -0
- package/src/report-shot.js +255 -0
- package/src/report.css +387 -0
- package/src/report.d.ts +61 -0
- package/src/report.js +909 -0
- package/src/shell-client.d.ts +31 -0
- package/src/shell-client.js +240 -0
- package/src/shell-html.d.ts +91 -0
- package/src/shell-html.js +289 -0
- package/src/shell.css +558 -0
- package/src/strings.d.ts +18 -0
- package/src/strings.js +514 -0
- package/src/time.d.ts +18 -0
- package/src/time.js +254 -0
- package/src/tokens.css +158 -0
- package/src/update-pill.css +32 -0
- package/src/update-pill.d.ts +17 -0
- package/src/update-pill.js +95 -0
- package/src/version.d.ts +7 -0
- package/src/version.js +31 -0
- package/src/whatsnew.css +235 -0
- package/src/whatsnew.d.ts +36 -0
- package/src/whatsnew.js +411 -0
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* den-ui nav inventory: the global nav (the top bar and the rail) as a
|
|
3
|
+
* list of elements, and a comparer that names every difference. One spec
|
|
4
|
+
* every product runs against the same reference, so two products' navs
|
|
5
|
+
* never quietly differ.
|
|
6
|
+
*
|
|
7
|
+
* collectNav(page) → Promise<NavSnapshot>
|
|
8
|
+
* compareNav(actual, reference, { hostSlots, themeTokens, tolerance })
|
|
9
|
+
* → NavDiff[] (empty: the same nav)
|
|
10
|
+
* formatNavDiffs(diffs) → string, one line per difference
|
|
11
|
+
*
|
|
12
|
+
* `page` is a Playwright Page (anything with `evaluate(fn, arg)` works),
|
|
13
|
+
* already on a page that draws the kit's shell.
|
|
14
|
+
*
|
|
15
|
+
* What is collected, in DOM order: header.den-top and nav.den-rail and
|
|
16
|
+
* every element under them that has a box (display: none, `hidden` and
|
|
17
|
+
* what sits inside them are left out; an <svg> is one element, its shapes
|
|
18
|
+
* are not). Each element's slot is "top", "rail" or "foot" (the rail's
|
|
19
|
+
* entries after its spacer, .den-rail-grow: Settings, the collapse
|
|
20
|
+
* toggle). For each: its key (below), its parent's key, slot, role, tag,
|
|
21
|
+
* accessible name, classes, box { x, y, w, h } and computed styles
|
|
22
|
+
* (STYLE_PROPS).
|
|
23
|
+
*
|
|
24
|
+
* The key names an element the same way on every run: `#id` when it has
|
|
25
|
+
* an id; `[data-nav="x"]` for a rail entry; else its parent's key, " > ",
|
|
26
|
+
* its tag and first class, and [n] when siblings share that.
|
|
27
|
+
*
|
|
28
|
+
* Theme tokens: the snapshot also maps each colour and font-family it saw
|
|
29
|
+
* to the :root custom properties (--tokens) that compute to the same
|
|
30
|
+
* value. The method: every custom property declared in a rule whose
|
|
31
|
+
* selector names :root or html (inside @media and @layer too) is read from
|
|
32
|
+
* getComputedStyle(<html>); a value that is a colour (CSS.supports) is set
|
|
33
|
+
* on a probe element's `color`, one that is a font-family list on its
|
|
34
|
+
* `font-family`, and read back, so it is in the same form as any computed
|
|
35
|
+
* value ("rgb(…)", "color(…)", a quoted family list). A value equal to
|
|
36
|
+
* that is said to come from the token. Two tokens of one value both
|
|
37
|
+
* match; the comparer asks only that one listed token match on both
|
|
38
|
+
* sides.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/** The computed styles kept for each element. */
|
|
42
|
+
export const STYLE_PROPS = [
|
|
43
|
+
"display",
|
|
44
|
+
"visibility",
|
|
45
|
+
"height",
|
|
46
|
+
"border-top-left-radius",
|
|
47
|
+
"border-top-right-radius",
|
|
48
|
+
"border-bottom-right-radius",
|
|
49
|
+
"border-bottom-left-radius",
|
|
50
|
+
"background-color",
|
|
51
|
+
"border-top-width",
|
|
52
|
+
"border-top-style",
|
|
53
|
+
"border-top-color",
|
|
54
|
+
"border-right-width",
|
|
55
|
+
"border-right-style",
|
|
56
|
+
"border-right-color",
|
|
57
|
+
"border-bottom-width",
|
|
58
|
+
"border-bottom-style",
|
|
59
|
+
"border-bottom-color",
|
|
60
|
+
"border-left-width",
|
|
61
|
+
"border-left-style",
|
|
62
|
+
"border-left-color",
|
|
63
|
+
"font-size",
|
|
64
|
+
"font-weight",
|
|
65
|
+
"font-family",
|
|
66
|
+
"font-style",
|
|
67
|
+
"line-height",
|
|
68
|
+
"letter-spacing",
|
|
69
|
+
"text-transform",
|
|
70
|
+
"padding-top",
|
|
71
|
+
"padding-right",
|
|
72
|
+
"padding-bottom",
|
|
73
|
+
"padding-left",
|
|
74
|
+
"row-gap",
|
|
75
|
+
"column-gap",
|
|
76
|
+
"color",
|
|
77
|
+
"opacity",
|
|
78
|
+
"box-shadow",
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
/** The styles that hold one colour (a theme token may stand for it). */
|
|
82
|
+
export const COLOR_PROPS = STYLE_PROPS.filter((p) => /color$/.test(p));
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The styles a theme token may stand for: each colour, and font-family
|
|
86
|
+
* (from --sans, --mono or --display, say).
|
|
87
|
+
*/
|
|
88
|
+
export const TOKEN_PROPS = [...COLOR_PROPS, "font-family"];
|
|
89
|
+
|
|
90
|
+
/** The snapshot's format; a reference with another one is refused. */
|
|
91
|
+
export const NAV_INVENTORY_VERSION = 2;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The theme the reference was taken in: Den's "Den" theme, as Den draws
|
|
95
|
+
* its pages today. Every product runs the inventory in this theme, so a
|
|
96
|
+
* colour or font that differs is a real gap.
|
|
97
|
+
*/
|
|
98
|
+
export const NAV_THEME = { id: "den", version: "2.0.1" };
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The one browser build the nav is measured in: Playwright's image, by
|
|
102
|
+
* version and the digest of its linux/amd64 build (its arm64 build ships
|
|
103
|
+
* another Chromium, which lays text out apart; on an arm64 machine run
|
|
104
|
+
* it with --platform linux/amd64). The references were taken there; a host runs its
|
|
105
|
+
* check there too (its run-server, reached through DEN_PW_WS), so both
|
|
106
|
+
* products are measured alike. Text lays out a few px apart on any other
|
|
107
|
+
* build, so boxes from another one are never compared.
|
|
108
|
+
*/
|
|
109
|
+
export const NAV_BROWSER_IMAGE =
|
|
110
|
+
"mcr.microsoft.com/playwright:v1.61.1-noble@sha256:cf0daee9b994042e011bc29f20cdff1a9f682a039b43fcd738f7d8a9d3bcd9d6";
|
|
111
|
+
|
|
112
|
+
/** The fixed line each face is measured with, for the fingerprint. */
|
|
113
|
+
export const FINGERPRINT_TEXT =
|
|
114
|
+
"Search pages, services, actions… Report ⌘K ⌘⇧F v9.9.0 0123456789";
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The global nav of the page, in DOM order. Runs in the page: nothing is
|
|
118
|
+
* read from outside it. `opts.image`: where the browser runs (say
|
|
119
|
+
* NAV_BROWSER_IMAGE), kept in the snapshot's env for the messages.
|
|
120
|
+
* `opts.theme`: { id, version } of the theme the page draws in (the
|
|
121
|
+
* reference's is NAV_THEME); a snapshot on another theme never matches.
|
|
122
|
+
*/
|
|
123
|
+
export async function collectNav(page, opts = {}) {
|
|
124
|
+
const snap = await page.evaluate(
|
|
125
|
+
({ props, colorProps, line }) => {
|
|
126
|
+
const round = (n) => Math.round(n * 100) / 100;
|
|
127
|
+
const ROLES = {
|
|
128
|
+
a: "link",
|
|
129
|
+
button: "button",
|
|
130
|
+
nav: "navigation",
|
|
131
|
+
header: "banner",
|
|
132
|
+
input: "textbox",
|
|
133
|
+
select: "combobox",
|
|
134
|
+
img: "img",
|
|
135
|
+
h1: "heading",
|
|
136
|
+
h2: "heading",
|
|
137
|
+
h3: "heading",
|
|
138
|
+
ul: "list",
|
|
139
|
+
li: "listitem",
|
|
140
|
+
dialog: "dialog",
|
|
141
|
+
};
|
|
142
|
+
const textOf = (el) => (el.textContent || "").replace(/\s+/g, " ").trim();
|
|
143
|
+
const nameOf = (el) => {
|
|
144
|
+
const label = el.getAttribute("aria-label");
|
|
145
|
+
if (label) return label.trim();
|
|
146
|
+
const by = el.getAttribute("aria-labelledby");
|
|
147
|
+
if (by) {
|
|
148
|
+
const t = by
|
|
149
|
+
.split(/\s+/)
|
|
150
|
+
.map((id) => document.getElementById(id))
|
|
151
|
+
.filter(Boolean)
|
|
152
|
+
.map(textOf)
|
|
153
|
+
.join(" ")
|
|
154
|
+
.trim();
|
|
155
|
+
if (t) return t;
|
|
156
|
+
}
|
|
157
|
+
const alt = el.getAttribute("alt");
|
|
158
|
+
if (alt) return alt.trim();
|
|
159
|
+
// Its text names it only when it is a control or a leaf: a
|
|
160
|
+
// container's text is its children's, which are listed themselves.
|
|
161
|
+
const tag = el.tagName.toLowerCase();
|
|
162
|
+
const fromText =
|
|
163
|
+
tag !== "svg" &&
|
|
164
|
+
(/^(a|button|kbd|label|h[1-6])$/.test(tag) ||
|
|
165
|
+
el.children.length === 0);
|
|
166
|
+
const t = fromText ? textOf(el) : "";
|
|
167
|
+
if (t) return t;
|
|
168
|
+
return (el.getAttribute("title") || "").trim();
|
|
169
|
+
};
|
|
170
|
+
const roleOf = (el) => {
|
|
171
|
+
const r = el.getAttribute("role");
|
|
172
|
+
if (r) return r;
|
|
173
|
+
const tag = el.tagName.toLowerCase();
|
|
174
|
+
if (tag === "a" && !el.hasAttribute("href")) return "a";
|
|
175
|
+
return ROLES[tag] || tag;
|
|
176
|
+
};
|
|
177
|
+
const step = (el) => {
|
|
178
|
+
const tag = el.tagName.toLowerCase();
|
|
179
|
+
const c = el.classList[0];
|
|
180
|
+
return c ? `${tag}.${c}` : tag;
|
|
181
|
+
};
|
|
182
|
+
const keyOf = (el, parentKey) => {
|
|
183
|
+
if (el.id) return `#${el.id}`;
|
|
184
|
+
const nav = el.getAttribute("data-nav");
|
|
185
|
+
if (nav) return `[data-nav="${nav}"]`;
|
|
186
|
+
const s = step(el);
|
|
187
|
+
const parent = el.parentElement;
|
|
188
|
+
const same = parent
|
|
189
|
+
? Array.from(parent.children).filter(
|
|
190
|
+
(k) => !k.id && !k.getAttribute("data-nav") && step(k) === s,
|
|
191
|
+
)
|
|
192
|
+
: [el];
|
|
193
|
+
const n = same.indexOf(el);
|
|
194
|
+
const here = same.length > 1 ? `${s}[${n + 1}]` : s;
|
|
195
|
+
return parentKey ? `${parentKey} > ${here}` : here;
|
|
196
|
+
};
|
|
197
|
+
const out = [];
|
|
198
|
+
const used = new Map();
|
|
199
|
+
// `pathKey`: the DOM parent's key (an element with no box of its own,
|
|
200
|
+
// display: contents, still names its children); `boxParent`: the
|
|
201
|
+
// nearest ancestor with a box, the one the snapshot lists.
|
|
202
|
+
const walk = (el, pathKey, boxParent, slot) => {
|
|
203
|
+
if (el.hidden) return slot;
|
|
204
|
+
const cs = getComputedStyle(el);
|
|
205
|
+
if (cs.display === "none") return slot;
|
|
206
|
+
const mySlot = el.classList.contains("den-rail-grow") ? "foot" : slot;
|
|
207
|
+
let key = keyOf(el, pathKey);
|
|
208
|
+
const n = (used.get(key) || 0) + 1;
|
|
209
|
+
used.set(key, n);
|
|
210
|
+
if (n > 1) key += `{${n}}`;
|
|
211
|
+
const boxed = el.getClientRects().length > 0;
|
|
212
|
+
if (boxed) {
|
|
213
|
+
const r = el.getBoundingClientRect();
|
|
214
|
+
const style = {};
|
|
215
|
+
for (const p of props) style[p] = cs.getPropertyValue(p);
|
|
216
|
+
out.push({
|
|
217
|
+
key,
|
|
218
|
+
parent: boxParent,
|
|
219
|
+
slot: mySlot,
|
|
220
|
+
role: roleOf(el),
|
|
221
|
+
tag: el.tagName.toLowerCase(),
|
|
222
|
+
name: nameOf(el),
|
|
223
|
+
classes: Array.from(el.classList),
|
|
224
|
+
box: {
|
|
225
|
+
x: round(r.x),
|
|
226
|
+
y: round(r.y),
|
|
227
|
+
w: round(r.width),
|
|
228
|
+
h: round(r.height),
|
|
229
|
+
},
|
|
230
|
+
style,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
if (el.tagName.toLowerCase() === "svg") return mySlot;
|
|
234
|
+
let kidSlot = mySlot;
|
|
235
|
+
for (const k of Array.from(el.children)) {
|
|
236
|
+
// Everything after the rail's spacer is the rail's foot.
|
|
237
|
+
kidSlot = walk(k, key, boxed ? key : boxParent, kidSlot);
|
|
238
|
+
}
|
|
239
|
+
return kidSlot === "foot" ? "foot" : mySlot;
|
|
240
|
+
};
|
|
241
|
+
const top = document.querySelector("header.den-top");
|
|
242
|
+
const rail = document.querySelector("nav.den-rail, .den-rail");
|
|
243
|
+
if (top) walk(top, null, null, "top");
|
|
244
|
+
if (rail) walk(rail, null, null, "rail");
|
|
245
|
+
|
|
246
|
+
// The theme tokens: each colour seen → the --tokens that make it.
|
|
247
|
+
const names = new Set();
|
|
248
|
+
const scan = (rules) => {
|
|
249
|
+
for (const r of Array.from(rules || [])) {
|
|
250
|
+
if (r.cssRules && !r.selectorText) scan(r.cssRules);
|
|
251
|
+
if (!r.selectorText || !r.style) continue;
|
|
252
|
+
if (!/:root|\bhtml\b/.test(r.selectorText)) continue;
|
|
253
|
+
for (const p of Array.from(r.style))
|
|
254
|
+
if (p.startsWith("--")) names.add(p);
|
|
255
|
+
if (r.cssRules) scan(r.cssRules);
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
for (const sh of Array.from(document.styleSheets)) {
|
|
259
|
+
try {
|
|
260
|
+
scan(sh.cssRules);
|
|
261
|
+
} catch {
|
|
262
|
+
/* another origin's sheet: not readable */
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const rootCs = getComputedStyle(document.documentElement);
|
|
266
|
+
const probe = document.createElement("span");
|
|
267
|
+
probe.setAttribute("aria-hidden", "true");
|
|
268
|
+
document.documentElement.appendChild(probe);
|
|
269
|
+
// A token's value, set on the probe and read back: the form an
|
|
270
|
+
// element's computed colour or font-family takes.
|
|
271
|
+
const byValue = {};
|
|
272
|
+
const note = (v, name) => (byValue[v] = byValue[v] || []).push(name);
|
|
273
|
+
for (const name of [...names].sort()) {
|
|
274
|
+
const v = rootCs.getPropertyValue(name).trim();
|
|
275
|
+
if (!v) continue;
|
|
276
|
+
if (CSS.supports("color", v)) {
|
|
277
|
+
probe.style.color = "";
|
|
278
|
+
probe.style.color = v;
|
|
279
|
+
note(getComputedStyle(probe).color, name);
|
|
280
|
+
} else if (CSS.supports("font-family", v)) {
|
|
281
|
+
probe.style.fontFamily = "";
|
|
282
|
+
probe.style.fontFamily = v;
|
|
283
|
+
note(getComputedStyle(probe).fontFamily, name);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
probe.remove();
|
|
287
|
+
const seen = new Set();
|
|
288
|
+
for (const e of out) for (const p of colorProps) seen.add(e.style[p]);
|
|
289
|
+
const tokens = {};
|
|
290
|
+
for (const c of [...seen].sort()) if (byValue[c]) tokens[c] = byValue[c];
|
|
291
|
+
// The fingerprint: the browser, and the width of one fixed line in
|
|
292
|
+
// each face (weight, size, family) the nav's text uses, laid out in
|
|
293
|
+
// the page as its text is. Boxes match only where it matches.
|
|
294
|
+
const faces = {};
|
|
295
|
+
const span = document.createElement("span");
|
|
296
|
+
span.setAttribute("aria-hidden", "true");
|
|
297
|
+
for (const [k, v] of Object.entries({
|
|
298
|
+
position: "absolute",
|
|
299
|
+
left: "-10000px",
|
|
300
|
+
top: "0",
|
|
301
|
+
"white-space": "pre",
|
|
302
|
+
"letter-spacing": "normal",
|
|
303
|
+
visibility: "hidden",
|
|
304
|
+
}))
|
|
305
|
+
span.style.setProperty(k, v);
|
|
306
|
+
span.textContent = line;
|
|
307
|
+
document.body.appendChild(span);
|
|
308
|
+
// Fixed faces, never the page's own (a page that changes a font
|
|
309
|
+
// must fail on that font, not on its fingerprint): the generic
|
|
310
|
+
// families as this machine draws them, and the page's font tokens.
|
|
311
|
+
const faceKeys = new Set();
|
|
312
|
+
for (const fam of [
|
|
313
|
+
"sans-serif",
|
|
314
|
+
"serif",
|
|
315
|
+
"monospace",
|
|
316
|
+
rootCs.getPropertyValue("--sans").trim(),
|
|
317
|
+
rootCs.getPropertyValue("--mono").trim(),
|
|
318
|
+
])
|
|
319
|
+
if (fam)
|
|
320
|
+
for (const wt of ["400", "500"]) faceKeys.add(`${wt} 14px ${fam}`);
|
|
321
|
+
for (const f of [...faceKeys].sort()) {
|
|
322
|
+
const [weight, size, ...family] = f.split(" ");
|
|
323
|
+
span.style.setProperty("font-weight", weight);
|
|
324
|
+
span.style.setProperty("font-size", size);
|
|
325
|
+
span.style.setProperty("font-family", family.join(" "));
|
|
326
|
+
faces[f] = Math.round(span.getBoundingClientRect().width * 1000) / 1000;
|
|
327
|
+
}
|
|
328
|
+
span.remove();
|
|
329
|
+
return {
|
|
330
|
+
viewport: { w: innerWidth, h: innerHeight },
|
|
331
|
+
env: { userAgent: navigator.userAgent, faces },
|
|
332
|
+
elements: out,
|
|
333
|
+
tokens,
|
|
334
|
+
};
|
|
335
|
+
},
|
|
336
|
+
{ props: STYLE_PROPS, colorProps: TOKEN_PROPS, line: FINGERPRINT_TEXT },
|
|
337
|
+
);
|
|
338
|
+
if (opts.image) snap.env.image = opts.image;
|
|
339
|
+
if (opts.theme)
|
|
340
|
+
snap.env.theme = { id: opts.theme.id, version: opts.theme.version };
|
|
341
|
+
return { version: NAV_INVENTORY_VERSION, ...snap };
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* A snapshot's fingerprint: its browser (user agent) and the widths of its
|
|
346
|
+
* faces' fixed line. Boxes compare only between equal fingerprints: on
|
|
347
|
+
* another browser build or font renderer text lays out a few px apart,
|
|
348
|
+
* and the comparer says so instead of comparing boxes that can't match.
|
|
349
|
+
*/
|
|
350
|
+
export function fingerprint(snap) {
|
|
351
|
+
const env = snap.env || {};
|
|
352
|
+
const faces = env.faces || {};
|
|
353
|
+
return JSON.stringify([
|
|
354
|
+
env.userAgent || "",
|
|
355
|
+
Object.keys(faces)
|
|
356
|
+
.sort()
|
|
357
|
+
.map((k) => [k, faces[k]]),
|
|
358
|
+
]);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** Where a snapshot was taken, in words: its image (if named) and browser. */
|
|
362
|
+
function envWords(snap) {
|
|
363
|
+
const env = snap.env || {};
|
|
364
|
+
return `${env.image || "an unnamed browser"} (${env.userAgent || "?"})`;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** Does host slot `s` cover element `e` (or one of its ancestors)? */
|
|
368
|
+
function slotMatches(s, e) {
|
|
369
|
+
if (s.startsWith(".") && !s.includes(" "))
|
|
370
|
+
return e.classes.includes(s.slice(1));
|
|
371
|
+
return e.key === s;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** The elements not inside a host slot, in order. */
|
|
375
|
+
function kept(snap, hostSlots) {
|
|
376
|
+
const byKey = new Map(snap.elements.map((e) => [e.key, e]));
|
|
377
|
+
const inSlot = (e) => {
|
|
378
|
+
for (let x = e; x; x = x.parent ? byKey.get(x.parent) : undefined)
|
|
379
|
+
if (hostSlots.some((s) => slotMatches(s, x))) return true;
|
|
380
|
+
return false;
|
|
381
|
+
};
|
|
382
|
+
return snap.elements.filter((e) => !inSlot(e));
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* The horizontal moves a host slot may cause in its own slot. A host slot
|
|
387
|
+
* that is wider or narrower (or there on one side only, with its flex gap)
|
|
388
|
+
* moves or stretches its neighbours by just that much: each host slot's
|
|
389
|
+
* change in width, per nav slot, and every sum of them.
|
|
390
|
+
*/
|
|
391
|
+
function slotShifts(actual, reference, hostSlots) {
|
|
392
|
+
const roots = (snap) => {
|
|
393
|
+
const byKey = new Map(snap.elements.map((e) => [e.key, e]));
|
|
394
|
+
const out = new Map();
|
|
395
|
+
for (const e of snap.elements) {
|
|
396
|
+
if (!hostSlots.some((s) => slotMatches(s, e))) continue;
|
|
397
|
+
let inner = false;
|
|
398
|
+
for (
|
|
399
|
+
let x = e.parent && byKey.get(e.parent);
|
|
400
|
+
x;
|
|
401
|
+
x = x.parent && byKey.get(x.parent)
|
|
402
|
+
)
|
|
403
|
+
if (hostSlots.some((s) => slotMatches(s, x))) inner = true;
|
|
404
|
+
if (inner) continue;
|
|
405
|
+
const parent = e.parent && byKey.get(e.parent);
|
|
406
|
+
const gap = parseFloat(parent ? parent.style["column-gap"] : "") || 0;
|
|
407
|
+
out.set(e.key, { slot: e.slot, w: e.box.w, gap });
|
|
408
|
+
}
|
|
409
|
+
return out;
|
|
410
|
+
};
|
|
411
|
+
const a = roots(actual);
|
|
412
|
+
const r = roots(reference);
|
|
413
|
+
const deltas = {};
|
|
414
|
+
for (const k of new Set([...a.keys(), ...r.keys()])) {
|
|
415
|
+
const x = a.get(k);
|
|
416
|
+
const y = r.get(k);
|
|
417
|
+
const slot = (x || y).slot;
|
|
418
|
+
const d = x && y ? x.w - y.w : x ? x.w + x.gap : -(y.w + y.gap);
|
|
419
|
+
if (d) (deltas[slot] = deltas[slot] || []).push(d);
|
|
420
|
+
}
|
|
421
|
+
const sums = {};
|
|
422
|
+
for (const [slot, ds] of Object.entries(deltas)) {
|
|
423
|
+
let set = [0];
|
|
424
|
+
for (const d of ds.slice(0, 8)) set = [...set, ...set.map((v) => v + d)];
|
|
425
|
+
sums[slot] = set.flatMap((v) => [v, -v]);
|
|
426
|
+
}
|
|
427
|
+
return sums;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** The longest common subsequence of two key lists, as a Set of keys. */
|
|
431
|
+
function lcs(a, b) {
|
|
432
|
+
const n = a.length;
|
|
433
|
+
const m = b.length;
|
|
434
|
+
const t = Array.from({ length: n + 1 }, () => new Uint16Array(m + 1));
|
|
435
|
+
for (let i = n - 1; i >= 0; i--)
|
|
436
|
+
for (let j = m - 1; j >= 0; j--)
|
|
437
|
+
t[i][j] =
|
|
438
|
+
a[i] === b[j]
|
|
439
|
+
? t[i + 1][j + 1] + 1
|
|
440
|
+
: Math.max(t[i + 1][j], t[i][j + 1]);
|
|
441
|
+
const keep = new Set();
|
|
442
|
+
for (let i = 0, j = 0; i < n && j < m; ) {
|
|
443
|
+
if (a[i] === b[j]) {
|
|
444
|
+
keep.add(a[i]);
|
|
445
|
+
i++;
|
|
446
|
+
j++;
|
|
447
|
+
} else if (t[i + 1][j] >= t[i][j + 1]) i++;
|
|
448
|
+
else j++;
|
|
449
|
+
}
|
|
450
|
+
return keep;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Every difference between `actual` and `reference`. Both must come from
|
|
455
|
+
* the same browser build (equal `fingerprint`): else the one difference is
|
|
456
|
+
* `env`, "reference taken on X, running on Y". Fails on: an element
|
|
457
|
+
* missing, extra or out of order; a slot, role, tag, name or class list
|
|
458
|
+
* that differs; a box edge moved more than `tolerance` px (default 2); a
|
|
459
|
+
* computed style that differs. Not compared: anything inside `hostSlots`
|
|
460
|
+
* (keys such as "#den-status", or one class such as ".den-scope": the
|
|
461
|
+
* element and all under it, on both sides); a colour or font-family that
|
|
462
|
+
* differs where both values come from one token named in `themeTokens`
|
|
463
|
+
* ("--accent", "--sans"). `themeTokens` is empty by default and is never
|
|
464
|
+
* for a parity run: both products run in NAV_THEME, where every colour
|
|
465
|
+
* and font must match. It is only for comparing two runs on purpose in
|
|
466
|
+
* different themes. Both snapshots must name the same theme (env.theme):
|
|
467
|
+
* else the one difference is `theme`.
|
|
468
|
+
* A host slot may be wider or narrower than in the reference (or there on
|
|
469
|
+
* one side only): the other elements in its nav slot may then move or
|
|
470
|
+
* stretch sideways by exactly that change (or a sum of such changes, with
|
|
471
|
+
* the flex gap of a slot there on one side only), never by anything else.
|
|
472
|
+
*/
|
|
473
|
+
export function compareNav(actual, reference, opts = {}) {
|
|
474
|
+
const hostSlots = opts.hostSlots || [];
|
|
475
|
+
const themeTokens = new Set(opts.themeTokens || []);
|
|
476
|
+
const tol = opts.tolerance == null ? 2 : opts.tolerance;
|
|
477
|
+
const diffs = [];
|
|
478
|
+
const add = (e, property, ref, act) =>
|
|
479
|
+
diffs.push({
|
|
480
|
+
key: e.key,
|
|
481
|
+
slot: e.slot,
|
|
482
|
+
property,
|
|
483
|
+
reference: ref,
|
|
484
|
+
actual: act,
|
|
485
|
+
});
|
|
486
|
+
if (reference.version !== NAV_INVENTORY_VERSION)
|
|
487
|
+
return [
|
|
488
|
+
{
|
|
489
|
+
key: "(snapshot)",
|
|
490
|
+
slot: "",
|
|
491
|
+
property: "version",
|
|
492
|
+
reference: String(reference.version),
|
|
493
|
+
actual: String(NAV_INVENTORY_VERSION),
|
|
494
|
+
},
|
|
495
|
+
];
|
|
496
|
+
// Another browser build: no box comparison, one clear difference.
|
|
497
|
+
if (fingerprint(actual) !== fingerprint(reference))
|
|
498
|
+
return [
|
|
499
|
+
{
|
|
500
|
+
key: "(snapshot)",
|
|
501
|
+
slot: "",
|
|
502
|
+
property: "env",
|
|
503
|
+
reference: `reference taken on ${envWords(reference)}`,
|
|
504
|
+
actual: `running on ${envWords(actual)}`,
|
|
505
|
+
},
|
|
506
|
+
];
|
|
507
|
+
// Another theme: its colours and fonts can't match.
|
|
508
|
+
const themeWords = (snap) => {
|
|
509
|
+
const t = (snap.env || {}).theme;
|
|
510
|
+
return t ? `${t.id} ${t.version}` : "no named theme";
|
|
511
|
+
};
|
|
512
|
+
if (themeWords(actual) !== themeWords(reference))
|
|
513
|
+
return [
|
|
514
|
+
{
|
|
515
|
+
key: "(snapshot)",
|
|
516
|
+
slot: "",
|
|
517
|
+
property: "theme",
|
|
518
|
+
reference: `reference taken in theme ${themeWords(reference)}`,
|
|
519
|
+
actual: `running in theme ${themeWords(actual)}`,
|
|
520
|
+
},
|
|
521
|
+
];
|
|
522
|
+
const want = kept(reference, hostSlots);
|
|
523
|
+
const got = kept(actual, hostSlots);
|
|
524
|
+
const wantBy = new Map(want.map((e) => [e.key, e]));
|
|
525
|
+
const gotBy = new Map(got.map((e) => [e.key, e]));
|
|
526
|
+
for (const e of want)
|
|
527
|
+
if (!gotBy.has(e.key)) add(e, "element", "present", "missing");
|
|
528
|
+
for (const e of got)
|
|
529
|
+
if (!wantBy.has(e.key)) add(e, "element", "absent", "extra");
|
|
530
|
+
const a = want.filter((e) => gotBy.has(e.key)).map((e) => e.key);
|
|
531
|
+
const b = got.filter((e) => wantBy.has(e.key)).map((e) => e.key);
|
|
532
|
+
const inOrder = lcs(a, b);
|
|
533
|
+
for (const k of a)
|
|
534
|
+
if (!inOrder.has(k))
|
|
535
|
+
add(
|
|
536
|
+
wantBy.get(k),
|
|
537
|
+
"order",
|
|
538
|
+
`#${a.indexOf(k) + 1} of ${a.length}`,
|
|
539
|
+
`#${b.indexOf(k) + 1} of ${b.length}`,
|
|
540
|
+
);
|
|
541
|
+
const tokenOf = (snap, c) => (snap.tokens && snap.tokens[c]) || [];
|
|
542
|
+
const shifts = slotShifts(actual, reference, hostSlots);
|
|
543
|
+
for (const w of want) {
|
|
544
|
+
const g = gotBy.get(w.key);
|
|
545
|
+
if (!g) continue;
|
|
546
|
+
for (const p of ["slot", "role", "tag", "name"])
|
|
547
|
+
if (w[p] !== g[p]) add(w, p, w[p], g[p]);
|
|
548
|
+
const wc = w.classes.join(" ");
|
|
549
|
+
const gc = g.classes.join(" ");
|
|
550
|
+
if (wc !== gc) add(w, "classes", wc, gc);
|
|
551
|
+
const edges = (x) => ({
|
|
552
|
+
left: x.box.x,
|
|
553
|
+
top: x.box.y,
|
|
554
|
+
right: Math.round((x.box.x + x.box.w) * 100) / 100,
|
|
555
|
+
bottom: Math.round((x.box.y + x.box.h) * 100) / 100,
|
|
556
|
+
});
|
|
557
|
+
const we = edges(w);
|
|
558
|
+
const ge = edges(g);
|
|
559
|
+
const moves = shifts[w.slot] || [0];
|
|
560
|
+
for (const edge of ["left", "top", "right", "bottom"]) {
|
|
561
|
+
const d = ge[edge] - we[edge];
|
|
562
|
+
const ok =
|
|
563
|
+
edge === "left" || edge === "right"
|
|
564
|
+
? moves.some((m) => Math.abs(d - m) <= tol)
|
|
565
|
+
: Math.abs(d) <= tol;
|
|
566
|
+
if (!ok) add(w, `box.${edge}`, we[edge], ge[edge]);
|
|
567
|
+
}
|
|
568
|
+
for (const p of STYLE_PROPS) {
|
|
569
|
+
const rv = w.style[p];
|
|
570
|
+
const av = g.style[p];
|
|
571
|
+
if (rv === av) continue;
|
|
572
|
+
if (TOKEN_PROPS.includes(p) && themeTokens.size) {
|
|
573
|
+
const rt = tokenOf(reference, rv);
|
|
574
|
+
const at = tokenOf(actual, av);
|
|
575
|
+
if (rt.some((t) => themeTokens.has(t) && at.includes(t))) continue;
|
|
576
|
+
}
|
|
577
|
+
add(w, `style.${p}`, rv, av);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
return diffs;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/** The differences, one per line: "top #den-search style.font-weight: 500 → 700". */
|
|
584
|
+
export function formatNavDiffs(diffs) {
|
|
585
|
+
return diffs
|
|
586
|
+
.map(
|
|
587
|
+
(d) => `${d.slot} ${d.key} ${d.property}: ${d.reference} → ${d.actual}`,
|
|
588
|
+
)
|
|
589
|
+
.join("\n");
|
|
590
|
+
}
|