@typeonce/ritmo-devtools 0.24.0 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.md +197 -1
  3. package/dist/Fuzz.d.ts +38 -6
  4. package/dist/Fuzz.d.ts.map +1 -1
  5. package/dist/Fuzz.js +37 -11
  6. package/dist/Fuzz.js.map +1 -1
  7. package/dist/Replay.d.ts +1 -1
  8. package/dist/cli.js +171 -3
  9. package/dist/cli.js.map +1 -1
  10. package/dist/internal/dependencyScan.js +1 -1
  11. package/dist/internal/dependencyScan.js.map +1 -1
  12. package/dist/internal/fuzzNetwork.d.ts +15 -2
  13. package/dist/internal/fuzzNetwork.d.ts.map +1 -1
  14. package/dist/internal/fuzzNetwork.js +30 -7
  15. package/dist/internal/fuzzNetwork.js.map +1 -1
  16. package/dist/internal/keyboardPage.d.ts +29 -0
  17. package/dist/internal/keyboardPage.d.ts.map +1 -0
  18. package/dist/internal/keyboardPage.js +199 -0
  19. package/dist/internal/keyboardPage.js.map +1 -0
  20. package/dist/internal/keyboardWalk.d.ts +91 -0
  21. package/dist/internal/keyboardWalk.d.ts.map +1 -0
  22. package/dist/internal/keyboardWalk.js +373 -0
  23. package/dist/internal/keyboardWalk.js.map +1 -0
  24. package/dist/internal/layoutPage.d.ts +35 -0
  25. package/dist/internal/layoutPage.d.ts.map +1 -0
  26. package/dist/internal/layoutPage.js +535 -0
  27. package/dist/internal/layoutPage.js.map +1 -0
  28. package/dist/internal/layoutWalk.d.ts +84 -0
  29. package/dist/internal/layoutWalk.d.ts.map +1 -0
  30. package/dist/internal/layoutWalk.js +252 -0
  31. package/dist/internal/layoutWalk.js.map +1 -0
  32. package/dist/internal/pageOpen.d.ts +86 -0
  33. package/dist/internal/pageOpen.d.ts.map +1 -0
  34. package/dist/internal/pageOpen.js +166 -0
  35. package/dist/internal/pageOpen.js.map +1 -0
  36. package/dist/internal/route.d.ts +94 -0
  37. package/dist/internal/route.d.ts.map +1 -0
  38. package/dist/internal/route.js +230 -0
  39. package/dist/internal/route.js.map +1 -0
  40. package/dist/internal/version.d.ts +1 -1
  41. package/dist/internal/version.js +1 -1
  42. package/package.json +3 -3
  43. package/src/Fuzz.ts +62 -11
  44. package/src/cli.ts +165 -3
  45. package/src/internal/dependencyScan.ts +1 -1
  46. package/src/internal/fuzzNetwork.ts +33 -7
  47. package/src/internal/keyboardPage.ts +198 -0
  48. package/src/internal/keyboardWalk.ts +474 -0
  49. package/src/internal/layoutPage.ts +534 -0
  50. package/src/internal/layoutWalk.ts +365 -0
  51. package/src/internal/pageOpen.ts +225 -0
  52. package/src/internal/route.ts +276 -0
  53. package/src/internal/version.ts +1 -1
@@ -0,0 +1,534 @@
1
+ /**
2
+ * The page side of `ritmo-devtools layout`: a script installed before the
3
+ * game's own scripts on every page the audit opens. It runs in the browser,
4
+ * so it is plain JavaScript in a string: Node never runs it, and the package
5
+ * config has no DOM types.
6
+ *
7
+ * It installs `__ritmoLayout` with:
8
+ *
9
+ * - `snapshot()`: every measured element's layout box, ignoring transforms,
10
+ * for the walk to tell when the screen holds still (a transform animation
11
+ * is motion, not layout, so a pulsing button still settles);
12
+ * - `audit(options)`: the findings of each check over what the page shows
13
+ * now (the open modal only, when there is one), and how many items it
14
+ * measured;
15
+ * - `steady()` and `shifts(allow)`: Chromium's own layout-shift entries
16
+ * recorded since `steady()`, with the elements that moved;
17
+ * - `pad()`: makes every text on the page about 40% longer, and keeps it so
18
+ * as the game renders new text, for the longer-text pass;
19
+ * - `mark(boxes)` and `unmark()`: numbered boxes over the findings, in the
20
+ * top layer, for a screenshot.
21
+ *
22
+ * What it measures: each line of visible text (`Range.getClientRects`, the
23
+ * text itself rather than the box around it), each visible control (the
24
+ * elements the keyboard walk calls controls), and each button of a
25
+ * `Touch.actions` layout (read from the element it is attached to). Boxes
26
+ * come in viewport pixels after every transform, so a UI scaled as one piece
27
+ * is measured as it is shown.
28
+ *
29
+ * It changes nothing on the game's page except in `pad()` (a pass of its
30
+ * own, on its own page load), and while `audit` runs, when a style that
31
+ * makes every element answer hit tests lets it see what is drawn on top
32
+ * (removed before it returns).
33
+ */
34
+ export const pageScript = String.raw`(() => {
35
+ if (globalThis.__ritmoLayout) return
36
+ const touchKey = Symbol.for("@typeonce/ritmo-browser/Touch.layout")
37
+ const controlSelector = [
38
+ "button", "a[href]", "input:not([type=hidden])", "select", "textarea", "summary",
39
+ '[role="button"]', '[role="link"]', '[role="tab"]', '[role="switch"]', '[role="checkbox"]', '[role="radio"]',
40
+ '[role="menuitem"]', '[role="menuitemcheckbox"]', '[role="menuitemradio"]', '[role="option"]', '[role="treeitem"]',
41
+ '[role="slider"]', '[role="spinbutton"]', '[role="combobox"]', '[role="textbox"]'
42
+ ].join(", ")
43
+ const marksId = "__ritmo_layout_marks"
44
+
45
+ // Chromium's layout-shift entries, from the first paint on.
46
+ const shiftLog = []
47
+ try {
48
+ new PerformanceObserver((list) => {
49
+ for (const entry of list.getEntries()) {
50
+ if (entry.hadRecentInput) continue
51
+ shiftLog.push({ time: entry.startTime, value: entry.value, nodes: (entry.sources || []).map((source) => source.node).filter((node) => node) })
52
+ }
53
+ }).observe({ type: "layout-shift", buffered: true })
54
+ } catch (error) {}
55
+ let steadyFrom = Infinity
56
+
57
+ const shown = (element) => typeof element.checkVisibility === "function" && element.checkVisibility({ visibilityProperty: true, opacityProperty: true }) === true
58
+ const transparent = (color) => color === "transparent" || /rgba\([^)]*,\s*0\)$/.test(color) || /\/\s*0\)$/.test(color)
59
+
60
+ const selector = (element) => {
61
+ const parts = []
62
+ let at = element
63
+ while (at !== null && at.nodeType === 1 && at !== document.documentElement) {
64
+ if (at.id !== "" && document.querySelectorAll("#" + CSS.escape(at.id)).length === 1) {
65
+ parts.unshift("#" + CSS.escape(at.id))
66
+ break
67
+ }
68
+ let part = at.localName
69
+ const classes = [...at.classList].filter((name) => !name.startsWith("__")).slice(0, 2)
70
+ if (classes.length > 0) part += classes.map((name) => "." + CSS.escape(name)).join("")
71
+ const parent = at.parentElement
72
+ if (parent !== null) {
73
+ const same = [...parent.children].filter((child) => child.localName === at.localName && classes.every((name) => child.classList.contains(name)))
74
+ if (same.length > 1) part += ":nth-of-type(" + ([...parent.children].filter((child) => child.localName === at.localName).indexOf(at) + 1) + ")"
75
+ }
76
+ parts.unshift(part)
77
+ if (parts.length >= 4) break
78
+ at = parent
79
+ }
80
+ return parts.join(" > ")
81
+ }
82
+
83
+ const box = (rect) => ({ l: rect.left, t: rect.top, r: rect.right, b: rect.bottom })
84
+ const meet = (a, b) => ({ l: Math.max(a.l, b.l), t: Math.max(a.t, b.t), r: Math.min(a.r, b.r), b: Math.min(a.b, b.b) })
85
+ const width = (a) => Math.max(0, a.r - a.l)
86
+ const height = (a) => Math.max(0, a.b - a.t)
87
+ const area = (a) => width(a) * height(a)
88
+ // What its clips leave no more than a pixel of, either way, is not shown: a collapsed box, or text kept for
89
+ // screen readers alone (the 1 x 1 "visually hidden" box).
90
+ const unseen = (item) => width(item.visible) <= 1 || height(item.visible) <= 1
91
+ const contains = (outer, inner) => outer.l <= inner.l + 1 && outer.t <= inner.t + 1 && outer.r >= inner.r - 1 && outer.b >= inner.b - 1
92
+ const round = (a) => ({ l: Math.round(a.l), t: Math.round(a.t), r: Math.round(a.r), b: Math.round(a.b) })
93
+
94
+ const clipsOverflow = (style) => style.overflowX !== "visible" || style.overflowY !== "visible"
95
+ const scrolls = (style) => /auto|scroll/.test(style.overflowX) || /auto|scroll/.test(style.overflowY)
96
+ const makesContainingBlock = (style, fixed) =>
97
+ (!fixed && style.position !== "static") || style.transform !== "none" || style.perspective !== "none" ||
98
+ style.filter !== "none" || /paint|layout|strict|content/.test(style.contain) || /transform/.test(style.willChange)
99
+ /** The ancestor whose box a positioned element is placed in (null: the viewport). */
100
+ const containingBlock = (element, fixed) => {
101
+ for (let at = element.parentElement; at !== null && at !== document.documentElement; at = at.parentElement) {
102
+ if (makesContainingBlock(getComputedStyle(at), fixed)) return at
103
+ }
104
+ return null
105
+ }
106
+ /**
107
+ * The ancestors that clip what an element draws (its text, with self), in
108
+ * order: overflow clips a descendant only when the descendant's containing
109
+ * block is inside the clipping element, so an absolutely placed badge
110
+ * escapes a parent that hides overflow, as it does on screen.
111
+ */
112
+ const clippers = (element, self) => {
113
+ const out = []
114
+ let escapeTo
115
+ let at = element
116
+ while (at !== null && at !== document.documentElement) {
117
+ if (escapeTo !== undefined && at === escapeTo) escapeTo = undefined
118
+ const style = getComputedStyle(at)
119
+ // The body's overflow belongs to the viewport when the root's is visible: it clips nothing of its own.
120
+ const propagated = at === document.body && getComputedStyle(document.documentElement).overflow === "visible"
121
+ if (escapeTo === undefined && (at !== element || self) && !propagated && clipsOverflow(style)) {
122
+ out.push({ element: at, box: box(at.getBoundingClientRect()), scroll: scrolls(style) })
123
+ }
124
+ if (style.position === "fixed" || style.position === "absolute") {
125
+ const block = containingBlock(at, style.position === "fixed")
126
+ if (block === null) break
127
+ if (escapeTo === undefined) escapeTo = block
128
+ }
129
+ at = at.parentElement
130
+ }
131
+ return out
132
+ }
133
+ const nearestScroller = (clips) => clips.find((clip) => clip.scroll)
134
+
135
+ const painted = (element) => {
136
+ const style = getComputedStyle(element)
137
+ if (/^(img|svg|canvas|video|input|select|textarea|progress|meter)$/.test(element.localName)) return true
138
+ if (!transparent(style.backgroundColor) || style.backgroundImage !== "none" || style.boxShadow !== "none") return true
139
+ if (style.backdropFilter !== undefined && style.backdropFilter !== "none") return true
140
+ const borders = ["Top", "Right", "Bottom", "Left"].some((side) => parseFloat(style["border" + side + "Width"]) > 0 && !transparent(style["border" + side + "Color"]))
141
+ return borders
142
+ }
143
+
144
+ const scope = () => {
145
+ const modals = [...document.querySelectorAll("dialog:modal")]
146
+ if (modals.length > 0) return modals[modals.length - 1]
147
+ const shownModals = [...document.querySelectorAll('[aria-modal="true"]')].filter(shown)
148
+ return shownModals.length > 0 ? shownModals[shownModals.length - 1] : document.body
149
+ }
150
+
151
+ const textOf = (element) => (element.getAttribute("aria-label") || element.textContent || element.getAttribute("title") || element.getAttribute("placeholder") || "").replace(/\s+/g, " ").trim()
152
+
153
+ /** Everything the audit measures: lines of text, controls, and the buttons of a Touch layout. */
154
+ const items = () => {
155
+ const root = scope()
156
+ const out = []
157
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT)
158
+ for (let node = walker.nextNode(); node !== null; node = walker.nextNode()) {
159
+ if (node.nodeValue.trim() === "") continue
160
+ const parent = node.parentElement
161
+ if (parent === null || parent.closest("script, style, noscript, template, #" + marksId) !== null || !shown(parent)) continue
162
+ if (transparent(getComputedStyle(parent).color)) continue
163
+ const control = parent.closest(controlSelector)
164
+ const range = document.createRange()
165
+ range.selectNodeContents(node)
166
+ for (const rect of range.getClientRects()) {
167
+ if (rect.width < 1 || rect.height < 1) continue
168
+ out.push({ kind: "text", element: parent, owner: control !== null && root.contains(control) ? control : parent, rect: box(rect), text: node.nodeValue.replace(/\s+/g, " ").trim() })
169
+ }
170
+ }
171
+ for (const control of root.querySelectorAll(controlSelector)) {
172
+ if (control.disabled || control.closest("[inert]") !== null || !shown(control)) continue
173
+ const rect = control.getBoundingClientRect()
174
+ if (rect.width < 1 || rect.height < 1) continue
175
+ out.push({ kind: "control", element: control, owner: control, rect: box(rect), text: textOf(control), painted: painted(control) || control.querySelector("img, svg, canvas, video") !== null })
176
+ }
177
+ for (const element of document.querySelectorAll("canvas, [style*='touch-action']")) {
178
+ const read = element[touchKey]
179
+ if (typeof read !== "function" || !shown(element)) continue
180
+ const origin = element.getBoundingClientRect()
181
+ let layout
182
+ try { layout = read() } catch (error) { continue }
183
+ for (const [name, zone] of Object.entries(layout.buttons || {})) {
184
+ if (zone === null || zone === undefined) continue
185
+ const x = origin.left + zone.center.x
186
+ const y = origin.top + zone.center.y
187
+ out.push({ kind: "touch", element, owner: element, rect: { l: x - zone.radius, t: y - zone.radius, r: x + zone.radius, b: y + zone.radius }, text: name, centre: { x, y }, radius: zone.radius })
188
+ }
189
+ }
190
+ return out
191
+ }
192
+
193
+ /** The layout box of an element, transforms left out, in page pixels. */
194
+ const layoutBox = (element) => {
195
+ let left = 0
196
+ let top = 0
197
+ for (let at = element; at !== null && at instanceof HTMLElement; at = at.offsetParent) {
198
+ left += at.offsetLeft
199
+ top += at.offsetTop
200
+ }
201
+ return element instanceof HTMLElement ? [left, top, element.offsetWidth, element.offsetHeight] : [0, 0, 0, 0]
202
+ }
203
+
204
+ const snapshot = () => {
205
+ const seen = new Set()
206
+ const out = []
207
+ for (const item of items()) {
208
+ if (seen.has(item.element)) continue
209
+ seen.add(item.element)
210
+ out.push([selector(item.element), ...layoutBox(item.element), item.kind === "text" ? item.text.length : 0])
211
+ }
212
+ return JSON.stringify(out)
213
+ }
214
+
215
+ const describe = (item) => ({ kind: item.kind, selector: selector(item.element), text: item.text.slice(0, 60) })
216
+ const allowed = (allow, check, elements) =>
217
+ allow.some((entry) => entry.check === check && elements.some((element) => {
218
+ try { return element !== null && element.closest(entry.selector) !== null } catch (error) { return false }
219
+ }))
220
+
221
+ const related = (owner, element) => element === owner || owner.contains(element)
222
+
223
+ /**
224
+ * Whether two owners really show over each other at the points of their
225
+ * shared box: both hit there, and nothing drawn sits above both (a scrim
226
+ * over a hidden button, a panel over the stage). Every element answers hit
227
+ * tests while this runs, so text in a pointer-events: none layer counts.
228
+ */
229
+ const shownTogether = (a, b, boxA, boxB, shared) => {
230
+ const points = []
231
+ for (const fx of [0.2, 0.5, 0.8]) for (const fy of [0.2, 0.5, 0.8]) points.push([shared.l + width(shared) * fx, shared.t + height(shared) * fy])
232
+ let top = null
233
+ let count = 0
234
+ for (const [x, y] of points) {
235
+ const stack = document.elementsFromPoint(x, y)
236
+ const hitA = stack.findIndex((element) => related(a, element))
237
+ const hitB = stack.findIndex((element) => related(b, element))
238
+ if (hitA === -1 || hitB === -1) continue
239
+ const first = Math.min(hitA, hitB)
240
+ const covered = stack.slice(0, first).some((element) => element.id !== marksId && painted(element))
241
+ if (covered) continue
242
+ // A layer the upper one sits on (a scrim, a panel) that hides the whole lower one: the lower is behind it, not under the upper.
243
+ const [upper, lower, lowerBox] = hitA < hitB ? [a, b, boxB] : [b, a, boxA]
244
+ const layered = stack.slice(0, Math.max(hitA, hitB)).some((element) => element !== upper && !related(lower, element) && element.id !== marksId && painted(element) && contains(box(element.getBoundingClientRect()), lowerBox))
245
+ if (layered) continue
246
+ count++
247
+ if (top === null) top = hitA < hitB ? "a" : "b"
248
+ }
249
+ return { count, top }
250
+ }
251
+
252
+ const tapExtent = (item, most) => {
253
+ const x0 = item.rect.l + width(item.rect) / 2
254
+ const y0 = item.rect.t + height(item.rect) / 2
255
+ const hits = (x, y) => {
256
+ if (x < 0 || y < 0 || x >= innerWidth || y >= innerHeight) return false
257
+ const found = document.elementFromPoint(x, y)
258
+ return found !== null && related(item.element, found)
259
+ }
260
+ if (!hits(x0, y0)) return { width: 0, height: 0, box: null }
261
+ const run = (dx, dy) => {
262
+ let steps = 0
263
+ while (steps < most && hits(x0 + dx * (steps + 1), y0 + dy * (steps + 1))) steps++
264
+ return steps
265
+ }
266
+ const left = run(-1, 0), right = run(1, 0), up = run(0, -1), down = run(0, 1)
267
+ return { width: left + right + 1, height: up + down + 1, box: { l: x0 - left, t: y0 - up, r: x0 + right + 1, b: y0 + down + 1 } }
268
+ }
269
+
270
+ const audit = (options) => {
271
+ const insets = options.insets
272
+ const minTarget = options.minTarget
273
+ const allow = options.allow
274
+ const checks = new Set(options.checks)
275
+ // The screen's size, not the page's: on a phone, a page wider than the screen widens its layout and is zoomed out to fit.
276
+ const screenWidth = options.width
277
+ const screenHeight = options.height
278
+ const viewport = { l: 0, t: 0, r: screenWidth, b: screenHeight }
279
+ const safe = { l: insets.left, t: insets.top, r: screenWidth - insets.right, b: screenHeight - insets.bottom }
280
+ const pageScrolls = document.documentElement.scrollHeight > screenHeight + 1
281
+ const all = items()
282
+ const findings = []
283
+ let skipped = 0
284
+ const add = (check, elements, finding) => {
285
+ if (allowed(allow, check, elements)) { skipped++; return }
286
+ findings.push({ check, ...finding })
287
+ }
288
+ for (const item of all) {
289
+ item.clips = item.kind === "touch" ? [] : clippers(item.element, item.kind === "text")
290
+ item.scroller = nearestScroller(item.clips)
291
+ item.visible = item.clips.reduce((rect, clip) => meet(rect, clip.box), item.rect)
292
+ }
293
+
294
+ // overlap: two things drawn over each other, neither part of the other.
295
+ if (checks.has("overlap")) {
296
+ const force = document.createElement("style")
297
+ force.textContent = "*, *::before, *::after { pointer-events: auto !important; }"
298
+ document.documentElement.appendChild(force)
299
+ try {
300
+ const drawn = all.filter((item) => item.kind === "text" || (item.kind === "control" && item.painted))
301
+ const reported = new Set()
302
+ for (let i = 0; i < drawn.length; i++) for (let j = i + 1; j < drawn.length; j++) {
303
+ const a = drawn[i], b = drawn[j]
304
+ if (a.owner === b.owner || a.owner.contains(b.owner) || b.owner.contains(a.owner)) continue
305
+ const shared = meet(a.visible, b.visible)
306
+ if (width(shared) <= 2 || height(shared) <= 2) continue
307
+ const key = [selector(a.owner), selector(b.owner)].sort().join("|")
308
+ if (reported.has(key)) continue
309
+ const seen = shownTogether(a.owner, b.owner, a.visible, b.visible, shared)
310
+ if (seen.count === 0) continue
311
+ reported.add(key)
312
+ const over = seen.top === "a" ? a : b
313
+ const under = seen.top === "a" ? b : a
314
+ // A control under the whole of the other is its backdrop ("tap anywhere", a clickable panel): what is on top takes its own taps.
315
+ if (under.kind === "control" && contains(under.visible, over.visible)) continue
316
+ add("overlap", [a.owner, b.owner], {
317
+ element: describe(under),
318
+ other: describe(over),
319
+ box: round(shared),
320
+ saw: "they overlap by " + Math.round(width(shared)) + " x " + Math.round(height(shared)) + " px, and the " + over.kind + " is drawn over the " + under.kind + (under.kind === "control" ? ", so a tap there reaches the wrong thing" : "")
321
+ })
322
+ }
323
+ } finally {
324
+ force.remove()
325
+ }
326
+ }
327
+
328
+ // Touch buttons, with hit tests as a finger meets them: what takes a tap at each button's centre.
329
+ const touches = all.filter((item) => item.kind === "touch")
330
+ const taken = new Map(touches.map((touch) => {
331
+ const found = document.elementFromPoint(touch.centre.x, touch.centre.y)
332
+ return [touch, found === null || related(touch.element, found) ? null : found]
333
+ }))
334
+ for (const target of new Set(touches.map((touch) => touch.element))) {
335
+ const mine = touches.filter((touch) => touch.element === target)
336
+ // No button of the layout takes a tap: a screen covers the game (a menu, a pause screen), so none is in play.
337
+ if (mine.every((touch) => taken.get(touch) !== null)) {
338
+ for (const touch of mine) all.splice(all.indexOf(touch), 1)
339
+ continue
340
+ }
341
+ if (!checks.has("overlap")) continue
342
+ for (const touch of mine) {
343
+ const found = taken.get(touch)
344
+ if (found === null || found === undefined) continue
345
+ add("overlap", [touch.element, found], { element: describe(touch), other: { kind: "control", selector: selector(found), text: textOf(found).slice(0, 60) }, box: round(touch.rect), saw: "a tap at the centre of the touch button " + JSON.stringify(touch.text) + " reaches " + selector(found) + " instead" })
346
+ }
347
+ for (let i = 0; i < mine.length; i++) for (let j = i + 1; j < mine.length; j++) {
348
+ const a = mine[i], b = mine[j]
349
+ if (Math.hypot(a.centre.x - b.centre.x, a.centre.y - b.centre.y) < a.radius + b.radius - 2) {
350
+ add("overlap", [a.element], { element: describe(a), other: describe(b), box: round(meet(a.rect, b.rect)), saw: "touch buttons " + JSON.stringify(a.text) + " and " + JSON.stringify(b.text) + " share space, so a thumb between them presses either" })
351
+ }
352
+ }
353
+ }
354
+
355
+ // cut-text and truncated-text: text a clipping box hides part of.
356
+ const cut = new Set()
357
+ for (const item of all) {
358
+ if (item.kind !== "text" || cut.has(item.element)) continue
359
+ if (unseen(item)) continue
360
+ for (const clip of item.clips) {
361
+ if (clip.scroll) break
362
+ const inside = meet(item.rect, clip.box)
363
+ if (width(item.rect) - width(inside) <= 1 && height(item.rect) - height(inside) <= 1) continue
364
+ cut.add(item.element)
365
+ const style = getComputedStyle(clip.element)
366
+ const meant = style.textOverflow === "ellipsis" || (style.webkitLineClamp !== undefined && style.webkitLineClamp !== "none")
367
+ if (meant && checks.has("truncated-text")) {
368
+ add("truncated-text", [item.element, clip.element], { element: describe(item), box: round(item.visible), saw: JSON.stringify(item.text.slice(0, 60)) + " is shortened with an ellipsis: " + Math.round(width(inside)) + " of its " + Math.round(width(item.rect)) + " px show" })
369
+ } else if (!meant && checks.has("cut-text")) {
370
+ add("cut-text", [item.element, clip.element], { element: describe(item), box: round(item.visible), saw: JSON.stringify(item.text.slice(0, 60)) + " is cut by the edge of " + selector(clip.element) + " (" + Math.round(width(inside)) + " x " + Math.round(height(inside)) + " of " + Math.round(width(item.rect)) + " x " + Math.round(height(item.rect)) + " px show), with nothing to say it is cut" })
371
+ }
372
+ break
373
+ }
374
+ }
375
+ if (checks.has("truncated-text")) {
376
+ for (const element of scope().querySelectorAll("*")) {
377
+ if (cut.has(element) || !shown(element)) continue
378
+ const style = getComputedStyle(element)
379
+ if (style.webkitLineClamp === undefined || style.webkitLineClamp === "none" || element.scrollHeight <= element.clientHeight + 1) continue
380
+ cut.add(element)
381
+ add("truncated-text", [element], { element: { kind: "text", selector: selector(element), text: textOf(element).slice(0, 60) }, box: round(box(element.getBoundingClientRect())), saw: "the text is clamped to " + style.webkitLineClamp + " lines and the rest is hidden" })
382
+ }
383
+ }
384
+
385
+ // off-screen, safe-area, scroll-edge: where the visible part of each item is.
386
+ const scrollEdge = new Set()
387
+ const edgeOf = (item, saw) => {
388
+ const key = item.scroller.element
389
+ if (scrollEdge.has(key)) return
390
+ scrollEdge.add(key)
391
+ add("scroll-edge", [item.owner, item.scroller.element], { element: describe(item), box: round(item.visible), saw })
392
+ }
393
+ const placed = new Set()
394
+ // A control's own box first: its text is judged only when the box is fine.
395
+ for (const item of [...all].sort((a, b) => (a.kind === "text" ? 1 : 0) - (b.kind === "text" ? 1 : 0))) {
396
+ if (item.kind === "touch") {
397
+ // The button is a circle: how far it reaches past each edge, with the same 2 px allowance as a box.
398
+ const past = (edges) => Math.max(edges.l - (item.centre.x - item.radius), edges.t - (item.centre.y - item.radius), item.centre.x + item.radius - edges.r, item.centre.y + item.radius - edges.b)
399
+ const out = past(viewport) > 2
400
+ if (out && checks.has("off-screen")) add("off-screen", [item.element], { element: describe(item), box: round(item.rect), saw: "the touch button " + JSON.stringify(item.text) + " reaches past the edge of the screen" })
401
+ else if (!out && checks.has("safe-area") && past(safe) > 2) add("safe-area", [item.element], { element: describe(item), box: round(item.rect), saw: "the touch button " + JSON.stringify(item.text) + " reaches " + Math.round(past(safe)) + " px into the space the device keeps (a notch, a home bar)" })
402
+ continue
403
+ }
404
+ if (placed.has(item.owner) || unseen(item)) continue
405
+ // A control that draws nothing (a touch area over the stage) has nothing to cut; its text is measured on its own.
406
+ if (item.kind === "control" && !item.painted) continue
407
+ // A box that spans the whole screen (the game's root, the body) cuts at the screen's edge: that is off-screen, not a cut.
408
+ const shownPart = item.clips.filter((clip) => !contains(clip.box, viewport)).reduce((rect, clip) => meet(rect, clip.box), item.rect)
409
+ if (area(shownPart) > area(item.visible) + 1) item.visible = shownPart
410
+ const onScreen = meet(item.visible, viewport)
411
+ if (area(onScreen) < 1) continue
412
+ if (item.kind === "control" && item.scroller !== undefined && checks.has("scroll-edge")) {
413
+ const shownShare = area(item.visible) / Math.max(1, area(item.rect))
414
+ if (shownShare > 0.1 && shownShare < 0.9) {
415
+ edgeOf(item, "only " + Math.round(shownShare * 100) + "% of it shows at the edge of a scrolling area")
416
+ }
417
+ }
418
+ const past = area(onScreen) < area(item.visible) - 1
419
+ if (past) {
420
+ placed.add(item.owner)
421
+ if (item.scroller !== undefined) {
422
+ if (checks.has("scroll-edge")) edgeOf(item, "its scrolling area runs past the edge of the screen")
423
+ } else if (pageScrolls && item.visible.t >= 0 && item.visible.l >= 0 && item.visible.r <= screenWidth + 1) {
424
+ // Below the fold of a page that scrolls: not cut.
425
+ } else if (checks.has("off-screen")) {
426
+ add("off-screen", [item.owner], { element: describe(item), box: round(item.visible), saw: "it reaches past the " + ["left", "top", "right", "bottom"].filter((side, index) => [item.visible.l < -1, item.visible.t < -1, item.visible.r > screenWidth + 1, item.visible.b > screenHeight + 1][index]).join(" and ") + " edge of the screen" })
427
+ }
428
+ continue
429
+ }
430
+ if (item.scroller !== undefined || !checks.has("safe-area")) continue
431
+ // A control over the whole safe area ("tap anywhere to start", a scrim) is meant to fill the screen; its text is judged on its own.
432
+ if (item.kind === "control" && contains(onScreen, safe)) continue
433
+ const inSafe = meet(onScreen, safe)
434
+ if (area(inSafe) < area(onScreen) - 1) {
435
+ placed.add(item.owner)
436
+ const sides = ["left", "top", "right", "bottom"].filter((side, index) => [onScreen.l < safe.l - 1, onScreen.t < safe.t - 1, onScreen.r > safe.r + 1, onScreen.b > safe.b + 1][index])
437
+ add("safe-area", [item.owner], { element: describe(item), box: round(onScreen), saw: "it reaches into the space the device keeps on the " + sides.join(" and ") + " (" + sides.map((side) => insets[side] + " px").join(", ") + ": a notch or the home bar)" })
438
+ }
439
+ }
440
+
441
+ // small-target: the area that takes a tap, by hit tests through the control's centre.
442
+ if (checks.has("small-target")) {
443
+ for (const item of all) {
444
+ if (item.kind === "touch") {
445
+ if (item.radius * 2 < minTarget) add("small-target", [item.element], { element: describe(item), box: round(item.rect), saw: "the touch button " + JSON.stringify(item.text) + " is " + Math.round(item.radius * 2) + " px across (at least " + minTarget + ")" })
446
+ continue
447
+ }
448
+ if (item.kind !== "control") continue
449
+ if (width(item.rect) >= minTarget && height(item.rect) >= minTarget) continue
450
+ if (area(item.visible) < area(item.rect) - 1 || area(meet(item.visible, viewport)) < area(item.visible) - 1) continue
451
+ const tap = tapExtent(item, minTarget * 2)
452
+ // Something else takes a tap at its centre: it is behind a layer now, not a target (overlap reports a partial cover).
453
+ if (tap.box === null) continue
454
+ if (tap.width >= minTarget && tap.height >= minTarget) continue
455
+ add("small-target", [item.element], {
456
+ element: describe(item),
457
+ box: round(tap.box),
458
+ saw: "a tap reaches it over " + tap.width + " x " + tap.height + " px (at least " + minTarget + " x " + minTarget + ")"
459
+ })
460
+ }
461
+ }
462
+ return { findings, measured: all.length, allowed: skipped }
463
+ }
464
+
465
+ const steady = () => { steadyFrom = performance.now() }
466
+ const shifts = (allow) => {
467
+ const out = []
468
+ let skipped = 0
469
+ const seen = new Set()
470
+ for (const entry of shiftLog) {
471
+ if (entry.time < steadyFrom) continue
472
+ for (const node of entry.nodes) {
473
+ const element = node.nodeType === 1 ? node : node.parentElement
474
+ if (element === null || seen.has(element) || element.id === marksId) continue
475
+ seen.add(element)
476
+ if (allowed(allow, "layout-shift", [element])) { skipped++; continue }
477
+ out.push({ check: "layout-shift", element: { kind: "text", selector: selector(element), text: textOf(element).slice(0, 60) }, box: round(box(element.getBoundingClientRect())), saw: "it moved while nothing was pressed (layout shift " + entry.value.toFixed(4) + ")" })
478
+ }
479
+ }
480
+ return { findings: out, allowed: skipped }
481
+ }
482
+
483
+ // Longer text: about 40% more of every text, kept as the game renders new text.
484
+ const written = new WeakMap()
485
+ const padNode = (node) => {
486
+ if (node.nodeValue.trim() === "" || written.get(node) === node.nodeValue) return
487
+ const parent = node.parentElement
488
+ if (parent === null || parent.closest("script, style, noscript, template, #" + marksId) !== null) return
489
+ // The text's own letters, so the added width is the font's own: about 40% more.
490
+ const longer = node.nodeValue.replace(/\S(?:[\s\S]*\S)?/, (text) => text + " " + text.repeat(2).slice(0, Math.ceil(text.length * 0.4)))
491
+ written.set(node, longer)
492
+ node.nodeValue = longer
493
+ }
494
+ const padAll = (root) => {
495
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT)
496
+ const nodes = []
497
+ for (let node = walker.nextNode(); node !== null; node = walker.nextNode()) nodes.push(node)
498
+ nodes.forEach(padNode)
499
+ }
500
+ const pad = () => {
501
+ padAll(document.body)
502
+ new MutationObserver((records) => {
503
+ for (const record of records) {
504
+ if (record.type === "characterData") padNode(record.target)
505
+ for (const added of record.addedNodes) {
506
+ if (added.nodeType === 3) padNode(added)
507
+ else if (added.nodeType === 1) padAll(added)
508
+ }
509
+ }
510
+ }).observe(document.body, { subtree: true, childList: true, characterData: true })
511
+ }
512
+
513
+ const unmark = () => document.getElementById(marksId)?.remove()
514
+ const mark = (boxes) => {
515
+ unmark()
516
+ const layer = document.createElement("div")
517
+ layer.id = marksId
518
+ layer.setAttribute("popover", "manual")
519
+ layer.style.cssText = "position:fixed;inset:0;margin:0;padding:0;border:0;width:100vw;height:100vh;max-width:none;max-height:none;background:transparent;overflow:visible;pointer-events:none"
520
+ for (const entry of boxes) {
521
+ const outline = document.createElement("div")
522
+ outline.style.cssText = "position:fixed;box-sizing:border-box;left:" + entry.l + "px;top:" + entry.t + "px;width:" + Math.max(2, entry.r - entry.l) + "px;height:" + Math.max(2, entry.b - entry.t) + "px;border:2px solid " + entry.color + ";background:" + entry.color + "22"
523
+ const tag = document.createElement("span")
524
+ tag.textContent = String(entry.number)
525
+ tag.style.cssText = "position:absolute;left:-2px;bottom:100%;font:700 11px/14px system-ui,sans-serif;color:#fff;background:" + entry.color + ";padding:0 4px"
526
+ outline.appendChild(tag)
527
+ layer.appendChild(outline)
528
+ }
529
+ document.body.appendChild(layer)
530
+ if (typeof layer.showPopover === "function") layer.showPopover()
531
+ }
532
+
533
+ globalThis.__ritmoLayout = { snapshot, audit, steady, shifts, pad, mark, unmark }
534
+ })()`