@k2b/ui 0.65.0 → 0.66.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -0
- package/dist/browser/v2/button.js +180 -0
- package/dist/browser/v2/button.js.map +1 -0
- package/dist/browser/v2/components.js +763 -0
- package/dist/browser/v2/components.js.map +1 -0
- package/dist/browser/v2/content.js +1107 -0
- package/dist/browser/v2/content.js.map +1 -0
- package/dist/browser/v2/controls.js +568 -0
- package/dist/browser/v2/controls.js.map +1 -0
- package/dist/browser/v2/describe.js +397 -0
- package/dist/browser/v2/describe.js.map +1 -0
- package/dist/browser/v2/index.js +25 -0
- package/dist/browser/v2/index.js.map +1 -0
- package/dist/browser/v2/layout.js +578 -0
- package/dist/browser/v2/layout.js.map +1 -0
- package/dist/browser/v2/link.js +9 -0
- package/dist/browser/v2/link.js.map +1 -0
- package/dist/browser/v2/marks.js +211 -0
- package/dist/browser/v2/marks.js.map +1 -0
- package/dist/browser/v2/states.js +180 -0
- package/dist/browser/v2/states.js.map +1 -0
- package/dist/browser/v2/structure.js +328 -0
- package/dist/browser/v2/structure.js.map +1 -0
- package/dist/browser/v2/testing/index.js +23 -0
- package/dist/browser/v2/testing/index.js.map +1 -0
- package/dist/browser/v2/testing/measure-v2-layout.js +762 -0
- package/dist/browser/v2/testing/measure-v2-layout.js.map +1 -0
- package/dist/browser/v2/testing/settled.js +52 -0
- package/dist/browser/v2/testing/settled.js.map +1 -0
- package/dist/browser/v2/tokens.js +83 -0
- package/dist/browser/v2/tokens.js.map +1 -0
- package/dist/browser/v2/types.js +24 -0
- package/dist/browser/v2/types.js.map +1 -0
- package/dist/ssr/v2/button.js +177 -0
- package/dist/ssr/v2/button.js.map +1 -0
- package/dist/ssr/v2/components.js +518 -0
- package/dist/ssr/v2/components.js.map +1 -0
- package/dist/ssr/v2/content.js +727 -0
- package/dist/ssr/v2/content.js.map +1 -0
- package/dist/ssr/v2/controls.js +268 -0
- package/dist/ssr/v2/controls.js.map +1 -0
- package/dist/ssr/v2/describe.js +397 -0
- package/dist/ssr/v2/describe.js.map +1 -0
- package/dist/ssr/v2/index.js +25 -0
- package/dist/ssr/v2/index.js.map +1 -0
- package/dist/ssr/v2/layout.js +377 -0
- package/dist/ssr/v2/layout.js.map +1 -0
- package/dist/ssr/v2/link.js +9 -0
- package/dist/ssr/v2/link.js.map +1 -0
- package/dist/ssr/v2/marks.js +126 -0
- package/dist/ssr/v2/marks.js.map +1 -0
- package/dist/ssr/v2/states.js +128 -0
- package/dist/ssr/v2/states.js.map +1 -0
- package/dist/ssr/v2/structure.js +229 -0
- package/dist/ssr/v2/structure.js.map +1 -0
- package/dist/ssr/v2/testing/index.js +23 -0
- package/dist/ssr/v2/testing/index.js.map +1 -0
- package/dist/ssr/v2/testing/measure-v2-layout.js +762 -0
- package/dist/ssr/v2/testing/measure-v2-layout.js.map +1 -0
- package/dist/ssr/v2/testing/settled.js +52 -0
- package/dist/ssr/v2/testing/settled.js.map +1 -0
- package/dist/ssr/v2/tokens.js +83 -0
- package/dist/ssr/v2/tokens.js.map +1 -0
- package/dist/ssr/v2/types.js +24 -0
- package/dist/ssr/v2/types.js.map +1 -0
- package/dist/types/v2/button.d.ts +54 -0
- package/dist/types/v2/components.d.ts +173 -0
- package/dist/types/v2/content.d.ts +233 -0
- package/dist/types/v2/controls.d.ts +118 -0
- package/dist/types/v2/describe.d.ts +18 -0
- package/dist/types/v2/index.d.ts +22 -0
- package/dist/types/v2/layout.d.ts +124 -0
- package/dist/types/v2/link.d.ts +7 -0
- package/dist/types/v2/marks.d.ts +90 -0
- package/dist/types/v2/states.d.ts +45 -0
- package/dist/types/v2/structure.d.ts +47 -0
- package/dist/types/v2/testing/index.d.ts +16 -0
- package/dist/types/v2/testing/measure-v2-layout.d.ts +99 -0
- package/dist/types/v2/testing/settled.d.ts +13 -0
- package/dist/types/v2/tokens.d.ts +77 -0
- package/dist/types/v2/types.d.ts +44 -0
- package/dist/v2.css +3072 -0
- package/package.json +23 -4
- package/skills/k2b-ui/SKILL.md +201 -0
- package/skills/k2b-ui/images/form-1440.png +0 -0
- package/skills/k2b-ui/images/form-390.png +0 -0
- package/skills/k2b-ui/images/list-detail-1440.png +0 -0
- package/skills/k2b-ui/images/list-detail-390.png +0 -0
- package/skills/k2b-ui/images/mail-1440.png +0 -0
- package/skills/k2b-ui/images/mail-390.png +0 -0
- package/skills/k2b-ui/images/record-1440.png +0 -0
- package/skills/k2b-ui/images/record-390.png +0 -0
- package/skills/k2b-ui/images/record-groups-1440.png +0 -0
- package/skills/k2b-ui/images/record-groups-390.png +0 -0
- package/skills/k2b-ui/images/table-1440.png +0 -0
- package/skills/k2b-ui/images/table-390.png +0 -0
- package/skills/k2b-ui/recipes/form.tsx +54 -0
- package/skills/k2b-ui/recipes/list-detail.tsx +105 -0
- package/skills/k2b-ui/recipes/mail.tsx +193 -0
- package/skills/k2b-ui/recipes/record-groups.tsx +115 -0
- package/skills/k2b-ui/recipes/record.tsx +163 -0
- package/skills/k2b-ui/recipes/table.tsx +68 -0
- package/skills/k2b-ui/references/mistakes.md +162 -0
- package/skills/k2b-ui/references/primitives.md +310 -0
- package/skills/k2b-ui/references/recipes.md +101 -0
- package/skills/k2b-ui/references/seeing.md +94 -0
|
@@ -0,0 +1,762 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A test helper, shipped as `@k2b/ui/v2/testing`: measures the layout rules of @k2b/ui v2 on a Playwright page, as numbers
|
|
3
|
+
* against the mockup instead of a count of error classes (the lesson of the flat audit, 22.11.).
|
|
4
|
+
*
|
|
5
|
+
* - **edge:** a horizontal hairline or a tinted band reaches both edges of its column (within 1.5 px). A column edge
|
|
6
|
+
* is a vertical hairline, an element matched by `columns`, or the edge of `root`.
|
|
7
|
+
* - **double:** no two parallel lines lie at most 1 px apart over more than 1 px of length, also across components.
|
|
8
|
+
* - **type:** every run of text is set in a type role: 12, 13, 14 or 18 px on a 16, 20 or 24 px line, in 400, 500 or
|
|
9
|
+
* 600. Reading text (`prose`) may also use 16 px and a 22 px line.
|
|
10
|
+
* - **grip:** the pill of every grip is centred on its 1 px line within ±0.25 CSS px, measured in the DOM and again
|
|
11
|
+
* in the screenshot at the page's device pixel ratio, inside the grip only.
|
|
12
|
+
*
|
|
13
|
+
* A line is any way to draw one: a border side, a `box-shadow` without blur (offset or spread), a box of at most
|
|
14
|
+
* 1.5 px, and the same on `::before` and `::after`, also under a `display: contents` parent whose own box has no
|
|
15
|
+
* size. To measure a pseudo-element, the helper puts a stand-in element with the pseudo's computed style in its place
|
|
16
|
+
* for the moment of the measurement and restores the page afterwards.
|
|
17
|
+
*
|
|
18
|
+
* Only what a person sees counts: a line or band is cut to the viewport and to every ancestor that clips it (a
|
|
19
|
+
* scrolled column), so content scrolled out of view is measured after scrolling, not before. A rounded box (a
|
|
20
|
+
* control, a mark, an overlay) floats on its own: its outline is no divider and makes no double line.
|
|
21
|
+
*
|
|
22
|
+
* The DOM contract of v2 marks what the rules need: `data-k2-column` (a column), `data-k2-grip` (a grip whose box is
|
|
23
|
+
* its pill), `data-k2-prose` (reading text), `data-k2-figure` (a drawn mark such as the track of steps; its lines are
|
|
24
|
+
* no dividers), `data-k2-canvas` (free drawing: never measured inside, only the line it draws itself) and
|
|
25
|
+
* `data-k2-avatar` (initials, no type role). The options take other selectors, for the mockup's classes for example.
|
|
26
|
+
*
|
|
27
|
+
* - **inside:** a band or a group is one thing (rule 9): a horizontal hairline inside it separates its parts and is a
|
|
28
|
+
* finding at any distance from other lines. A frame, a control's outline and a figure's track are no such line.
|
|
29
|
+
* - **cut:** a control's label never leaves its box and is never clipped, unless it ends in "…" in its own box (the
|
|
30
|
+
* way back to a list with a long name); a one-line text (the lines of a row, the
|
|
31
|
+
* cells of a table with column widths) never wraps, it ends with "…"; a box that clips its text shows every line
|
|
32
|
+
* whole and cuts a line at its end only with "…".
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { measureV2Layout } from "@k2b/ui/v2/testing";
|
|
36
|
+
*
|
|
37
|
+
* for (const deviceScaleFactor of [1, 2])
|
|
38
|
+
* test(`DPR ${deviceScaleFactor}: the layout keeps the v2 rules`, async () => {
|
|
39
|
+
* const page = await browser.newPage({ viewport: { width: 1440, height: 900 }, deviceScaleFactor });
|
|
40
|
+
* await page.setContent(html);
|
|
41
|
+
* expect((await measureV2Layout(page)).problems).toEqual([]);
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
import { fontWeights, gripTolerance, hairline, typeRoles } from "../tokens";
|
|
47
|
+
import { settled } from "./settled";
|
|
48
|
+
/** Runs in the page: closes over nothing. */
|
|
49
|
+
function measureInPage(options) {
|
|
50
|
+
const empty = {
|
|
51
|
+
edge: [],
|
|
52
|
+
double: [],
|
|
53
|
+
inside: [],
|
|
54
|
+
cut: [],
|
|
55
|
+
type: [],
|
|
56
|
+
grip: [],
|
|
57
|
+
grips: [],
|
|
58
|
+
pseudo: [],
|
|
59
|
+
measured: {
|
|
60
|
+
lines: 0,
|
|
61
|
+
bands: 0,
|
|
62
|
+
texts: 0
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
const root = document.querySelector(options.root);
|
|
66
|
+
if (!root) return {
|
|
67
|
+
...empty,
|
|
68
|
+
edge: [`no element matches ${options.root}`]
|
|
69
|
+
};
|
|
70
|
+
const round = value => Math.round(value * 100) / 100;
|
|
71
|
+
const clear = color => !color || color === "transparent" || /rgba\(.*,\s*0\)$/.test(color) || /\/\s*0\)$/.test(color);
|
|
72
|
+
const name = element => {
|
|
73
|
+
const marker = element.getAttribute("data-probe");
|
|
74
|
+
if (marker) return `[data-probe="${marker}"]`;
|
|
75
|
+
const pseudo = element.getAttribute("data-k2-stand-in");
|
|
76
|
+
if (pseudo) return pseudo;
|
|
77
|
+
const classes = typeof element.className === "string" ? element.className.trim().split(/\s+/).filter(Boolean).slice(0, 2) : [];
|
|
78
|
+
return `${element.tagName.toLowerCase()}${classes.map(c => `.${c}`).join("")}`;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// 1. Stand-ins for pseudo-elements that draw: read every computed style first, then write, so reading never sees a stand-in.
|
|
82
|
+
const draws = style => !clear(style.backgroundColor) || style.backgroundImage !== "none" || style.boxShadow !== "none" || ["top", "right", "bottom", "left"].some(side => Number.parseFloat(style.getPropertyValue(`border-${side}-width`)) > 0 && !["none", "hidden"].includes(style.getPropertyValue(`border-${side}-style`)) && !clear(style.getPropertyValue(`border-${side}-color`)));
|
|
83
|
+
const planned = [];
|
|
84
|
+
// A quoted string such as `"… "` becomes the stand-in's text; anything else (a counter, attr()) changes its size, and
|
|
85
|
+
// the comparison below then reports the pseudo-element as not measurable.
|
|
86
|
+
const textOf = content => {
|
|
87
|
+
try {
|
|
88
|
+
return /^".*"$/.test(content) ? String(JSON.parse(content)) : "";
|
|
89
|
+
} catch {
|
|
90
|
+
return "";
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
for (const owner of [root, ...Array.from(root.querySelectorAll("*"))]) {
|
|
94
|
+
if (owner.parentElement?.closest(options.skip)) continue;
|
|
95
|
+
for (const which of ["before", "after"]) {
|
|
96
|
+
const style = getComputedStyle(owner, `::${which}`);
|
|
97
|
+
if (style.content === "none" || style.content === "normal" || style.display === "none" || !draws(style)) continue;
|
|
98
|
+
const values = [];
|
|
99
|
+
for (let index = 0; index < style.length; index++) {
|
|
100
|
+
const property = style.item(index);
|
|
101
|
+
if (property !== "content") values.push([property, style.getPropertyValue(property)]);
|
|
102
|
+
}
|
|
103
|
+
planned.push({
|
|
104
|
+
owner,
|
|
105
|
+
which,
|
|
106
|
+
values,
|
|
107
|
+
text: textOf(style.content)
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const hide = document.createElement("style");
|
|
112
|
+
hide.textContent = "[data-k2-hide-before]::before{content:none!important}[data-k2-hide-after]::after{content:none!important}";
|
|
113
|
+
const standIns = new Map();
|
|
114
|
+
const pseudo = [];
|
|
115
|
+
// A stand-in is a real element: it can change what a sibling selector matches (`.row:last-child`, `:empty`, `+`,
|
|
116
|
+
// `:has()`) and so the look of its neighbours. Every box and line of the page is compared before and after; a
|
|
117
|
+
// stand-in that changes anything is taken out again and its pseudo-element reported as not measurable.
|
|
118
|
+
const look = () => {
|
|
119
|
+
const seen = new Map();
|
|
120
|
+
for (const element of [root, ...Array.from(root.querySelectorAll("*"))]) {
|
|
121
|
+
if (element.localName === "k2-stand-in") continue;
|
|
122
|
+
const r = element.getBoundingClientRect();
|
|
123
|
+
const s = getComputedStyle(element);
|
|
124
|
+
seen.set(element, [r.left, r.top, r.width, r.height].map(n => Math.round(n * 100)).join(",") + ["top", "right", "bottom", "left"].map(side => s.getPropertyValue(`border-${side}-width`) + s.getPropertyValue(`border-${side}-color`)).join() + s.boxShadow + s.backgroundColor + s.display);
|
|
125
|
+
}
|
|
126
|
+
return seen;
|
|
127
|
+
};
|
|
128
|
+
const differences = before => {
|
|
129
|
+
const after = look();
|
|
130
|
+
return [...before].filter(([element, value]) => after.get(element) !== value).map(([element]) => element);
|
|
131
|
+
};
|
|
132
|
+
const insert = plan => {
|
|
133
|
+
const {
|
|
134
|
+
owner,
|
|
135
|
+
which,
|
|
136
|
+
values,
|
|
137
|
+
text
|
|
138
|
+
} = plan;
|
|
139
|
+
const standIn = document.createElement("k2-stand-in");
|
|
140
|
+
for (const [property, value] of values) standIn.style.setProperty(property, value);
|
|
141
|
+
standIn.textContent = text;
|
|
142
|
+
standIn.setAttribute("data-k2-stand-in", `${name(owner)}::${which}`);
|
|
143
|
+
owner.setAttribute(`data-k2-hide-${which}`, "");
|
|
144
|
+
if (which === "before") owner.prepend(standIn);else owner.append(standIn);
|
|
145
|
+
standIns.set(plan, standIn);
|
|
146
|
+
};
|
|
147
|
+
const remove = plan => {
|
|
148
|
+
standIns.get(plan)?.remove();
|
|
149
|
+
standIns.delete(plan);
|
|
150
|
+
plan.owner.removeAttribute(`data-k2-hide-${plan.which}`);
|
|
151
|
+
};
|
|
152
|
+
try {
|
|
153
|
+
if (planned.length) {
|
|
154
|
+
const before = look();
|
|
155
|
+
document.head.append(hide);
|
|
156
|
+
for (const plan of planned) insert(plan);
|
|
157
|
+
if (differences(before).length) {
|
|
158
|
+
// Find the stand-ins that change the page, one at a time, and keep only the others.
|
|
159
|
+
for (const plan of planned) remove(plan);
|
|
160
|
+
const clean = planned.filter(plan => {
|
|
161
|
+
insert(plan);
|
|
162
|
+
const changed = differences(before);
|
|
163
|
+
remove(plan);
|
|
164
|
+
if (changed.length) pseudo.push(`pseudo-element ${name(plan.owner)}::${plan.which} cannot be measured: in its place a real element changes ${changed.slice(0, 3).map(name).join(", ")} (a sibling selector such as :last-child depends on it)`);
|
|
165
|
+
return !changed.length;
|
|
166
|
+
});
|
|
167
|
+
for (const plan of clean) insert(plan);
|
|
168
|
+
if (differences(before).length) {
|
|
169
|
+
for (const plan of clean) remove(plan);
|
|
170
|
+
pseudo.push("pseudo-elements cannot be measured together: their stand-ins change the page only in combination");
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const visible = element => {
|
|
175
|
+
const rect = element.getBoundingClientRect();
|
|
176
|
+
return rect.width > 0 && rect.height > 0 && element.checkVisibility({
|
|
177
|
+
visibilityProperty: true,
|
|
178
|
+
opacityProperty: true
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
const elements = [root, ...Array.from(root.querySelectorAll("*"))].filter(
|
|
182
|
+
// A skipped surface (a Canvas) is free inside, but the line it draws itself is a divider like any other, also
|
|
183
|
+
// when a pseudo-element draws it (its stand-in stands inside the surface and counts as the surface).
|
|
184
|
+
element => {
|
|
185
|
+
const host = element.localName === "k2-stand-in" ? element.parentElement : element;
|
|
186
|
+
return !host?.parentElement?.closest(options.skip) && visible(element);
|
|
187
|
+
});
|
|
188
|
+
// What a person sees of an element: the viewport, cut by every ancestor that clips (a scrolled column, the card).
|
|
189
|
+
const clips = new Map();
|
|
190
|
+
const clipOf = element => {
|
|
191
|
+
const known = clips.get(element);
|
|
192
|
+
if (known) return known;
|
|
193
|
+
const parent = element.parentElement;
|
|
194
|
+
let clip = {
|
|
195
|
+
left: 0,
|
|
196
|
+
right: window.innerWidth,
|
|
197
|
+
top: 0,
|
|
198
|
+
bottom: window.innerHeight
|
|
199
|
+
};
|
|
200
|
+
if (parent) {
|
|
201
|
+
clip = {
|
|
202
|
+
...clipOf(parent)
|
|
203
|
+
};
|
|
204
|
+
const style = getComputedStyle(parent);
|
|
205
|
+
// Each axis on its own: `overflow: clip visible` cuts sideways only, and a 2 px tab mark below the box stays whole.
|
|
206
|
+
if (style.display !== "contents") {
|
|
207
|
+
const r = parent.getBoundingClientRect();
|
|
208
|
+
if (style.overflowX !== "visible") clip = {
|
|
209
|
+
...clip,
|
|
210
|
+
left: Math.max(clip.left, r.left),
|
|
211
|
+
right: Math.min(clip.right, r.right)
|
|
212
|
+
};
|
|
213
|
+
if (style.overflowY !== "visible") clip = {
|
|
214
|
+
...clip,
|
|
215
|
+
top: Math.max(clip.top, r.top),
|
|
216
|
+
bottom: Math.min(clip.bottom, r.bottom)
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
clips.set(element, clip);
|
|
221
|
+
return clip;
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
// 2. Every line: border sides, blur-free shadows, and boxes of at most 1.5 px, as far as it can be seen.
|
|
225
|
+
// `frame`: one side of a box framed on several sides, or of a rounded one; it is no divider, so the edge rule skips it.
|
|
226
|
+
// `round`: drawn by a rounded box, a control, mark or overlay; it floats on its own, so the double-line rule skips it.
|
|
227
|
+
|
|
228
|
+
const horizontal = [];
|
|
229
|
+
const vertical = [];
|
|
230
|
+
const add = (element, how, x0, x1, y0, y1, frame, round, boxed = frame) => {
|
|
231
|
+
// Lines inside a figure (the track of steps, a progress bar) are marks, not dividers.
|
|
232
|
+
if (element.closest(options.figure)) return;
|
|
233
|
+
const clip = clipOf(element);
|
|
234
|
+
const [cx0, cx1, cy0, cy1] = [Math.max(x0, clip.left), Math.min(x1, clip.right), Math.max(y0, clip.top), Math.min(y1, clip.bottom)];
|
|
235
|
+
if (cx1 - cx0 <= 0.25 || cy1 - cy0 <= 0.25) return;
|
|
236
|
+
const line = {
|
|
237
|
+
element,
|
|
238
|
+
how,
|
|
239
|
+
frame: frame || round,
|
|
240
|
+
round,
|
|
241
|
+
boxed: boxed || round
|
|
242
|
+
};
|
|
243
|
+
if (x1 - x0 >= y1 - y0) horizontal.push({
|
|
244
|
+
...line,
|
|
245
|
+
across0: cy0,
|
|
246
|
+
across1: cy1,
|
|
247
|
+
along0: cx0,
|
|
248
|
+
along1: cx1
|
|
249
|
+
});else vertical.push({
|
|
250
|
+
...line,
|
|
251
|
+
across0: cx0,
|
|
252
|
+
across1: cx1,
|
|
253
|
+
along0: cy0,
|
|
254
|
+
along1: cy1
|
|
255
|
+
});
|
|
256
|
+
};
|
|
257
|
+
const shadowPattern = /(rgba?\([^)]*\)|#[0-9a-f]{3,8})\s+(-?[\d.]+)px\s+(-?[\d.]+)px\s+([\d.]+)px\s+(-?[\d.]+)px(\s+inset)?/gi;
|
|
258
|
+
for (const element of elements) {
|
|
259
|
+
const style = getComputedStyle(element);
|
|
260
|
+
const r = element.getBoundingClientRect();
|
|
261
|
+
const sides = ["top", "right", "bottom", "left"].filter(side => Number.parseFloat(style.getPropertyValue(`border-${side}-width`)) > 0 && !["none", "hidden"].includes(style.getPropertyValue(`border-${side}-style`)) && !clear(style.getPropertyValue(`border-${side}-color`)));
|
|
262
|
+
const round = Number.parseFloat(style.borderTopLeftRadius) > 0;
|
|
263
|
+
const framed = sides.length > 1;
|
|
264
|
+
// `border-block` draws two lines, both between parts; a box with a side left or right frames something.
|
|
265
|
+
const boxed = sides.includes("left") || sides.includes("right");
|
|
266
|
+
for (const side of sides) {
|
|
267
|
+
const width = Number.parseFloat(style.getPropertyValue(`border-${side}-width`));
|
|
268
|
+
if (side === "top") add(element, "border-top", r.left, r.right, r.top, r.top + width, framed, round, boxed);
|
|
269
|
+
if (side === "bottom") add(element, "border-bottom", r.left, r.right, r.bottom - width, r.bottom, framed, round, boxed);
|
|
270
|
+
if (side === "left") add(element, "border-left", r.left, r.left + width, r.top, r.bottom, framed, round);
|
|
271
|
+
if (side === "right") add(element, "border-right", r.right - width, r.right, r.top, r.bottom, framed, round);
|
|
272
|
+
}
|
|
273
|
+
if (style.boxShadow !== "none") for (const match of style.boxShadow.matchAll(shadowPattern)) {
|
|
274
|
+
const [, color, ox, oy, blur, spread, inset] = match;
|
|
275
|
+
if (clear(color) || Number(blur) > 0) continue;
|
|
276
|
+
const x = Number(ox);
|
|
277
|
+
const y = Number(oy);
|
|
278
|
+
const s = Number(spread);
|
|
279
|
+
if (s > 0 && x === 0 && y === 0) {
|
|
280
|
+
// A spread without offset draws a frame: inside the box when inset, around it otherwise.
|
|
281
|
+
const [l, rt, tp, b] = inset ? [r.left, r.right, r.top, r.bottom] : [r.left - s, r.right + s, r.top - s, r.bottom + s];
|
|
282
|
+
add(element, "shadow-frame", l, rt, tp, tp + s, true, round);
|
|
283
|
+
add(element, "shadow-frame", l, rt, b - s, b, true, round);
|
|
284
|
+
add(element, "shadow-frame", l, l + s, tp, b, true, round);
|
|
285
|
+
add(element, "shadow-frame", rt - s, rt, tp, b, true, round);
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
if (s !== 0) continue;
|
|
289
|
+
if (y > 0) add(element, "shadow", r.left, r.right, inset ? r.top : r.bottom, (inset ? r.top : r.bottom) + y, false, round);
|
|
290
|
+
if (y < 0) add(element, "shadow", r.left, r.right, (inset ? r.bottom : r.top) + y, inset ? r.bottom : r.top, false, round);
|
|
291
|
+
if (x > 0) add(element, "shadow", inset ? r.left : r.right, (inset ? r.left : r.right) + x, r.top, r.bottom, false, round);
|
|
292
|
+
if (x < 0) add(element, "shadow", (inset ? r.right : r.left) + x, inset ? r.right : r.left, r.top, r.bottom, false, round);
|
|
293
|
+
}
|
|
294
|
+
const thin = Math.min(r.width, r.height);
|
|
295
|
+
if (thin <= options.hairline + 0.5 && Math.max(r.width, r.height) > 4 * thin && !clear(style.backgroundColor)) add(element, "box", r.left, r.right, r.top, r.bottom, false, round);
|
|
296
|
+
}
|
|
297
|
+
const hairlines = lines => lines.filter(line => line.across1 - line.across0 <= options.hairline + 0.5);
|
|
298
|
+
|
|
299
|
+
// 3. Column edges at a height: vertical hairlines, the columns and the root.
|
|
300
|
+
const rootRect = root.getBoundingClientRect();
|
|
301
|
+
const columns = Array.from(root.querySelectorAll(options.columns)).filter(visible).map(column => column.getBoundingClientRect());
|
|
302
|
+
const edgesAt = y => {
|
|
303
|
+
const lefts = [rootRect.left];
|
|
304
|
+
const rights = [rootRect.right];
|
|
305
|
+
for (const line of hairlines(vertical)) if (line.along1 - line.along0 > 40 && line.along0 - 1 <= y && line.along1 + 1 >= y) {
|
|
306
|
+
lefts.push(line.across1);
|
|
307
|
+
rights.push(line.across0);
|
|
308
|
+
}
|
|
309
|
+
for (const column of columns) if (column.top - 1 <= y && column.bottom + 1 >= y) {
|
|
310
|
+
lefts.push(column.left);
|
|
311
|
+
rights.push(column.right);
|
|
312
|
+
}
|
|
313
|
+
return {
|
|
314
|
+
lefts,
|
|
315
|
+
rights
|
|
316
|
+
};
|
|
317
|
+
};
|
|
318
|
+
const gaps = (x0, x1, y) => {
|
|
319
|
+
const {
|
|
320
|
+
lefts,
|
|
321
|
+
rights
|
|
322
|
+
} = edgesAt(y);
|
|
323
|
+
const left = Math.max(...lefts.filter(x => x <= x0 + 1.5));
|
|
324
|
+
const right = Math.min(...rights.filter(x => x >= x1 - 1.5));
|
|
325
|
+
return {
|
|
326
|
+
left: x0 - left,
|
|
327
|
+
right: right - x1,
|
|
328
|
+
edgeLeft: left,
|
|
329
|
+
edgeRight: right
|
|
330
|
+
};
|
|
331
|
+
};
|
|
332
|
+
const edge = [];
|
|
333
|
+
// Collinear pieces of one line (the cells of a row) merge first.
|
|
334
|
+
const dividers = hairlines(horizontal).filter(line => !line.frame).sort((a, b) => a.across0 - b.across0 || a.along0 - b.along0);
|
|
335
|
+
const merged = [];
|
|
336
|
+
for (const line of dividers) {
|
|
337
|
+
const same = merged.find(m => Math.abs(m.across0 - line.across0) <= 0.6 && line.along0 <= m.along1 + 1.5 && line.along1 >= m.along0 - 1.5);
|
|
338
|
+
if (same) {
|
|
339
|
+
same.along0 = Math.min(same.along0, line.along0);
|
|
340
|
+
same.along1 = Math.max(same.along1, line.along1);
|
|
341
|
+
} else merged.push({
|
|
342
|
+
...line
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
for (const line of merged) {
|
|
346
|
+
if (line.along1 - line.along0 < 60) continue;
|
|
347
|
+
const gap = gaps(line.along0, line.along1, line.across0);
|
|
348
|
+
if (gap.left > 1.5 || gap.right > 1.5) edge.push(`line ${name(line.element)} ${line.how} at y ${round(line.across0)} stops ${round(gap.left)} px short of the left and ${round(gap.right)} px short of the right edge of its column (${round(gap.edgeLeft)}–${round(gap.edgeRight)})`);
|
|
349
|
+
}
|
|
350
|
+
// Tinted bands: a fill of its own, not a control, not rounded, at least 160 x 36.
|
|
351
|
+
const fillBelow = element => {
|
|
352
|
+
for (let parent = element.parentElement; parent; parent = parent.parentElement) {
|
|
353
|
+
const color = getComputedStyle(parent).backgroundColor;
|
|
354
|
+
if (!clear(color)) return color;
|
|
355
|
+
}
|
|
356
|
+
return "rgb(255, 255, 255)";
|
|
357
|
+
};
|
|
358
|
+
const control = element => !!element.closest("button, a, input, textarea, select, [role=button], [role=tab], [role=option], [role=menuitem], [role=separator], [contenteditable=true], [data-k2-grip]");
|
|
359
|
+
let bands = 0;
|
|
360
|
+
for (const element of elements) {
|
|
361
|
+
const style = getComputedStyle(element);
|
|
362
|
+
const color = style.backgroundColor;
|
|
363
|
+
if (clear(color) || color === fillBelow(element) || control(element) || Number.parseFloat(style.borderTopLeftRadius) > 0) continue;
|
|
364
|
+
let box = element.getBoundingClientRect();
|
|
365
|
+
// A table row is tinted cell by cell (a sticky cell must be opaque), so a tinted row is measured as the band of
|
|
366
|
+
// all its cells, once; a single tinted cell (an error, the cell under edit) is a state of that cell, no band.
|
|
367
|
+
if (element.tagName === "TD" || element.tagName === "TH") {
|
|
368
|
+
const cells = Array.from(element.parentElement?.children ?? []);
|
|
369
|
+
if (cells[0] !== element || !cells.every(cell => getComputedStyle(cell).backgroundColor === color)) continue;
|
|
370
|
+
const last = cells[cells.length - 1].getBoundingClientRect();
|
|
371
|
+
box = new DOMRect(box.left, box.top, last.right - box.left, box.height);
|
|
372
|
+
}
|
|
373
|
+
const clip = clipOf(element);
|
|
374
|
+
const r = {
|
|
375
|
+
left: Math.max(box.left, clip.left),
|
|
376
|
+
right: Math.min(box.right, clip.right),
|
|
377
|
+
top: Math.max(box.top, clip.top),
|
|
378
|
+
bottom: Math.min(box.bottom, clip.bottom)
|
|
379
|
+
};
|
|
380
|
+
if (r.right - r.left < 160 || r.bottom - r.top < 36) continue;
|
|
381
|
+
bands += 1;
|
|
382
|
+
const gap = gaps(r.left, r.right, (r.top + r.bottom) / 2);
|
|
383
|
+
if (gap.left > 1.5 || gap.right > 1.5) edge.push(`band ${name(element)} at y ${round(r.top)} stops ${round(gap.left)} px short of the left and ${round(gap.right)} px short of the right edge of its column`);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// 4. Double lines: parallel, at most 1 px apart, sharing more than 1 px of length, of different elements.
|
|
387
|
+
const collapsedPair = (a, b) => {
|
|
388
|
+
const table = a.closest("table");
|
|
389
|
+
return !!table && table === b.closest("table") && getComputedStyle(table).borderCollapse === "collapse";
|
|
390
|
+
};
|
|
391
|
+
const double = [];
|
|
392
|
+
for (const list of [hairlines(horizontal), hairlines(vertical)]) {
|
|
393
|
+
list.sort((a, b) => a.across0 - b.across0);
|
|
394
|
+
for (const [index, a] of list.entries()) for (const b of list.slice(index + 1)) {
|
|
395
|
+
if (b.across0 - a.across1 > 1) break;
|
|
396
|
+
if (a.element === b.element || a.round || b.round || collapsedPair(a.element, b.element)) continue;
|
|
397
|
+
const shared = Math.min(a.along1, b.along1) - Math.max(a.along0, b.along0);
|
|
398
|
+
if (shared > 1) double.push(`double line: ${name(a.element)} ${a.how} and ${name(b.element)} ${b.how} at ${round(a.across0)} px over ${round(shared)} px`);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// 4b. Lines inside one thing: a hairline drawn inside a band or a group, not on its own edge (rule 9).
|
|
403
|
+
const inside = [];
|
|
404
|
+
for (const line of hairlines(horizontal)) {
|
|
405
|
+
if (line.boxed || line.round) continue;
|
|
406
|
+
const owner = line.element.parentElement?.closest(options.unbroken);
|
|
407
|
+
if (!owner || !root.contains(owner)) continue;
|
|
408
|
+
// Inside the owner's own border: a line on its edge lies on the owner's line, which the double rule reports.
|
|
409
|
+
const box = owner.getBoundingClientRect();
|
|
410
|
+
const ownerStyle = getComputedStyle(owner);
|
|
411
|
+
const top = box.top + Number.parseFloat(ownerStyle.borderTopWidth);
|
|
412
|
+
const bottom = box.bottom - Number.parseFloat(ownerStyle.borderBottomWidth);
|
|
413
|
+
if (line.across0 < top + 0.5 || line.across1 > bottom - 0.5) continue;
|
|
414
|
+
inside.push(`line inside ${name(owner)}: ${name(line.element)} ${line.how} at y ${round(line.across0)} separates parts of one thing; spacing alone separates them`);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// 4c. Cut text: a control's label that leaves its box or is clipped, a one-line text that wraps.
|
|
418
|
+
const cut = [];
|
|
419
|
+
const labelOf = element => (element.textContent ?? "").replace(/\s+/g, " ").trim().slice(0, 40);
|
|
420
|
+
// The boxes of the visible text in an element, one per line box of each text node.
|
|
421
|
+
// Text kept only as a name (a visually hidden label of 1 px, such as the words of a button that gave way to its
|
|
422
|
+
// symbol in a narrow place) shows nothing to cut.
|
|
423
|
+
const hiddenName = element => {
|
|
424
|
+
for (let node = element; node; node = node.parentElement) {
|
|
425
|
+
// A box of `display: contents` has no size of its own and hides nothing.
|
|
426
|
+
if (getComputedStyle(node).display === "contents") continue;
|
|
427
|
+
const r = node.getBoundingClientRect();
|
|
428
|
+
if (r.width <= 1 || r.height <= 1) return true;
|
|
429
|
+
if (node === root) break;
|
|
430
|
+
}
|
|
431
|
+
return false;
|
|
432
|
+
};
|
|
433
|
+
/** The boxes of the text in `element`, without text that `skip` names. */
|
|
434
|
+
const textBoxes = (element, skip) => {
|
|
435
|
+
const boxes = [];
|
|
436
|
+
const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT);
|
|
437
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
438
|
+
if (!node.textContent?.trim() || hiddenName(node.parentElement) || skip?.(node)) continue;
|
|
439
|
+
const range = document.createRange();
|
|
440
|
+
range.selectNodeContents(node);
|
|
441
|
+
for (const box of Array.from(range.getClientRects())) if (box.width > 0 && box.height > 0) boxes.push(box);
|
|
442
|
+
}
|
|
443
|
+
return boxes;
|
|
444
|
+
};
|
|
445
|
+
// What cuts sideways for good: the element and every ancestor with overflow hidden or clip. A scroller (auto,
|
|
446
|
+
// scroll) is left out: what it hides, a person scrolls to, such as tabs in a scrolling bar.
|
|
447
|
+
const hardClipX = element => {
|
|
448
|
+
let left = Number.NEGATIVE_INFINITY;
|
|
449
|
+
let right = Number.POSITIVE_INFINITY;
|
|
450
|
+
for (let node = element; node; node = node.parentElement) {
|
|
451
|
+
const nodeStyle = getComputedStyle(node);
|
|
452
|
+
// A box of `display: contents` has no box of its own, so its overflow cuts nothing.
|
|
453
|
+
if (nodeStyle.display === "contents") {
|
|
454
|
+
if (node === root) break;
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
const overflow = nodeStyle.overflowX;
|
|
458
|
+
// Inside a scroller everything can be scrolled into view; what lies beyond it is no cut of this control.
|
|
459
|
+
if (node !== element && ["auto", "scroll"].includes(overflow)) break;
|
|
460
|
+
if (["hidden", "clip"].includes(overflow)) {
|
|
461
|
+
const r = node.getBoundingClientRect();
|
|
462
|
+
left = Math.max(left, r.left);
|
|
463
|
+
right = Math.min(right, r.right);
|
|
464
|
+
}
|
|
465
|
+
if (node === root) break;
|
|
466
|
+
}
|
|
467
|
+
return {
|
|
468
|
+
left,
|
|
469
|
+
right
|
|
470
|
+
};
|
|
471
|
+
};
|
|
472
|
+
// A label that ends in "…" in its own box (one line that clips with `text-overflow: ellipsis`) is cut on purpose,
|
|
473
|
+
// as the way back to a list with a long name: it is no finding, while any other clipped label is.
|
|
474
|
+
const endsInEllipsis = element => {
|
|
475
|
+
const style = getComputedStyle(element);
|
|
476
|
+
return style.textOverflow === "ellipsis" && style.overflowX !== "visible" && style.whiteSpace.startsWith("nowrap");
|
|
477
|
+
};
|
|
478
|
+
for (const control of Array.from(root.querySelectorAll(options.controls))) {
|
|
479
|
+
if (!visible(control) || control.parentElement?.closest(options.skip)) continue;
|
|
480
|
+
const box = control.getBoundingClientRect();
|
|
481
|
+
let clipped = false;
|
|
482
|
+
const shortened = [control, ...Array.from(control.querySelectorAll("*"))].filter(endsInEllipsis);
|
|
483
|
+
for (const part of [control, ...Array.from(control.querySelectorAll("*"))]) {
|
|
484
|
+
if (part !== control && hiddenName(part)) continue;
|
|
485
|
+
const style = getComputedStyle(part);
|
|
486
|
+
if (style.overflowX !== "visible" && part.scrollWidth > part.clientWidth + 1 && !endsInEllipsis(part)) clipped = true;
|
|
487
|
+
}
|
|
488
|
+
const clip = hardClipX(control);
|
|
489
|
+
for (const r of textBoxes(control, node => shortened.some(part => part.contains(node)))) if (r.left < Math.max(box.left, clip.left) - 0.5 || r.right > Math.min(box.right, clip.right) + 0.5) clipped = true;
|
|
490
|
+
if (clipped) cut.push(`control ${name(control)} cuts its label "${labelOf(control)}"; it needs the room of its whole label`);
|
|
491
|
+
}
|
|
492
|
+
// A box of text that clips (overflow hidden or clip, not a scroller) shows every line of it whole and cuts a line at
|
|
493
|
+
// its end only with "…". Only boxes of text count: text of their own or inline children. A control's label is
|
|
494
|
+
// judged above, more strictly.
|
|
495
|
+
for (const element of elements) {
|
|
496
|
+
if (!(element instanceof HTMLElement) || !element.textContent?.trim() || element.closest(options.controls)) continue;
|
|
497
|
+
// Text kept only as a name (a visually hidden label of 1 px) shows nothing to cut.
|
|
498
|
+
const box = element.getBoundingClientRect();
|
|
499
|
+
if (box.width <= 1 || box.height <= 1) continue;
|
|
500
|
+
const style = getComputedStyle(element);
|
|
501
|
+
// A clamp to a number of lines ends its last line with "…" by itself.
|
|
502
|
+
if (style.webkitLineClamp !== "none" && style.webkitLineClamp !== "") continue;
|
|
503
|
+
const clipsX = style.overflowX === "hidden" || style.overflowX === "clip";
|
|
504
|
+
const clipsY = style.overflowY === "hidden" || style.overflowY === "clip";
|
|
505
|
+
if (!clipsX && !clipsY) continue;
|
|
506
|
+
if (!Array.from(element.children).every(child => getComputedStyle(child).display.startsWith("inline"))) continue;
|
|
507
|
+
if (clipsY && element.scrollHeight > element.clientHeight + 1) cut.push(`${name(element)} "${labelOf(element)}" hides ${element.scrollHeight - element.clientHeight} px of its text above or below; every line shows whole`);
|
|
508
|
+
// A control that sticks out sideways is reported once, at the control; the box above it says nothing new.
|
|
509
|
+
const holdsControl = Array.from(element.children).some(child => child.matches(options.controls));
|
|
510
|
+
if (clipsX && !holdsControl && element.scrollWidth > element.clientWidth + 1 && style.textOverflow !== "ellipsis") cut.push(`${name(element)} "${labelOf(element)}" cuts its line without "…"`);
|
|
511
|
+
}
|
|
512
|
+
for (const element of Array.from(root.querySelectorAll(options.oneLine))) {
|
|
513
|
+
if (!visible(element) || element.parentElement?.closest(options.skip)) continue;
|
|
514
|
+
// Count line boxes, not the element's height: a control in the line makes it taller without a second line.
|
|
515
|
+
const boxes = textBoxes(element).sort((a, b) => a.top - b.top);
|
|
516
|
+
let lines = 0;
|
|
517
|
+
let bottom = Number.NEGATIVE_INFINITY;
|
|
518
|
+
for (const box of boxes) {
|
|
519
|
+
if (box.top >= bottom - 1) lines += 1;
|
|
520
|
+
bottom = Math.max(bottom, box.bottom);
|
|
521
|
+
}
|
|
522
|
+
if (lines > 1) cut.push(`${name(element)} "${labelOf(element)}" wraps to ${lines} lines; it stays on one line and ends with "…"`);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// 5. Type roles: every run of text that a person sees.
|
|
526
|
+
const type = new Map();
|
|
527
|
+
let texts = 0;
|
|
528
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
529
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
530
|
+
const parent = node.parentElement;
|
|
531
|
+
if (!parent || !node.textContent?.trim() || parent.closest(options.skip) || parent.closest(options.untyped) || !visible(parent)) continue;
|
|
532
|
+
if (parent.closest("script, style, template")) continue;
|
|
533
|
+
texts += 1;
|
|
534
|
+
const style = getComputedStyle(parent);
|
|
535
|
+
const size = Number.parseFloat(style.fontSize);
|
|
536
|
+
const lineHeight = Number.parseFloat(style.lineHeight);
|
|
537
|
+
const weight = Number(style.fontWeight);
|
|
538
|
+
const prose = !!parent.closest(options.prose);
|
|
539
|
+
const sizeOk = options.sizes.includes(size) || prose && size === 16;
|
|
540
|
+
const lineOk = options.lineHeights.includes(lineHeight) || prose && lineHeight === 22;
|
|
541
|
+
if (sizeOk && lineOk && options.weights.includes(weight)) continue;
|
|
542
|
+
const key = `${style.fontSize}/${style.lineHeight} ${weight} in ${name(parent)}`;
|
|
543
|
+
type.set(key, (type.get(key) ?? 0) + 1);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// 6. Grips: the pill centre against the centre of the vertical hairline that runs through it, above the pill.
|
|
547
|
+
const grip = [];
|
|
548
|
+
const grips = [];
|
|
549
|
+
const standInsOf = element => Array.from(element.querySelectorAll("k2-stand-in"));
|
|
550
|
+
for (const element of Array.from(root.querySelectorAll(options.grips)).filter(visible)) {
|
|
551
|
+
const r = element.getBoundingClientRect();
|
|
552
|
+
const centre = r.left + r.width / 2;
|
|
553
|
+
const own = new Set([element, ...standInsOf(element)]);
|
|
554
|
+
const y = r.top - 4;
|
|
555
|
+
const candidates = hairlines(vertical).filter(line => !own.has(line.element) && line.along0 <= y && line.along1 >= y && Math.abs((line.across0 + line.across1) / 2 - centre) <= r.width).sort((a, b) => Math.abs((a.across0 + a.across1) / 2 - centre) - Math.abs((b.across0 + b.across1) / 2 - centre));
|
|
556
|
+
const line = candidates[0];
|
|
557
|
+
const label = name(element);
|
|
558
|
+
if (!line) {
|
|
559
|
+
grip.push(`grip ${label} has no vertical line through it`);
|
|
560
|
+
grips.push({
|
|
561
|
+
grip: label,
|
|
562
|
+
dom: Number.NaN,
|
|
563
|
+
rect: {
|
|
564
|
+
left: r.left,
|
|
565
|
+
top: r.top,
|
|
566
|
+
width: r.width,
|
|
567
|
+
height: r.height
|
|
568
|
+
},
|
|
569
|
+
line: null
|
|
570
|
+
});
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
const offset = centre - (line.across0 + line.across1) / 2;
|
|
574
|
+
if (Math.abs(offset) > options.tolerance) grip.push(`grip ${label}: pill centre ${round(offset)} px off its line (DOM)`);
|
|
575
|
+
if (Math.abs(line.across1 - line.across0 - options.hairline) > 0.01) grip.push(`grip ${label}: its line is ${round(line.across1 - line.across0)} px, not ${options.hairline} px`);
|
|
576
|
+
grips.push({
|
|
577
|
+
grip: label,
|
|
578
|
+
dom: round(offset),
|
|
579
|
+
rect: {
|
|
580
|
+
left: r.left,
|
|
581
|
+
top: r.top,
|
|
582
|
+
width: r.width,
|
|
583
|
+
height: r.height
|
|
584
|
+
},
|
|
585
|
+
line: {
|
|
586
|
+
x0: line.across0,
|
|
587
|
+
x1: line.across1
|
|
588
|
+
}
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
return {
|
|
592
|
+
edge,
|
|
593
|
+
double,
|
|
594
|
+
inside,
|
|
595
|
+
cut,
|
|
596
|
+
type: [...type].map(([key, count]) => `text outside the type roles: ${key}${count > 1 ? ` (${count}×)` : ""}`),
|
|
597
|
+
grip,
|
|
598
|
+
grips,
|
|
599
|
+
pseudo,
|
|
600
|
+
measured: {
|
|
601
|
+
lines: horizontal.length + vertical.length,
|
|
602
|
+
bands,
|
|
603
|
+
texts
|
|
604
|
+
}
|
|
605
|
+
};
|
|
606
|
+
} finally {
|
|
607
|
+
for (const plan of planned) remove(plan);
|
|
608
|
+
hide.remove();
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/** Runs in the page: finds the pill and the line in a screenshot of one grip. */
|
|
613
|
+
async function gripPixelsInPage(input) {
|
|
614
|
+
const image = new Image();
|
|
615
|
+
image.src = `data:image/png;base64,${input.png}`;
|
|
616
|
+
await image.decode();
|
|
617
|
+
const canvas = document.createElement("canvas");
|
|
618
|
+
canvas.width = image.naturalWidth;
|
|
619
|
+
canvas.height = image.naturalHeight;
|
|
620
|
+
const context = canvas.getContext("2d");
|
|
621
|
+
if (!context) return null;
|
|
622
|
+
context.drawImage(image, 0, 0);
|
|
623
|
+
const row = y => {
|
|
624
|
+
const data = context.getImageData(0, Math.min(canvas.height - 1, Math.max(0, Math.round(y))), canvas.width, 1).data;
|
|
625
|
+
return Array.from({
|
|
626
|
+
length: canvas.width
|
|
627
|
+
}, (_, x) => [data[x * 4], data[x * 4 + 1], data[x * 4 + 2]]);
|
|
628
|
+
};
|
|
629
|
+
const differs = (a, b) => Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]) + Math.abs(a[2] - b[2]) > 12;
|
|
630
|
+
// The run of pixels between two columns that differ from the background on both sides (the columns left and right
|
|
631
|
+
// of a line may have different fills), in device pixels.
|
|
632
|
+
const run = (pixels, from, to) => {
|
|
633
|
+
const before = pixels[from];
|
|
634
|
+
const after = pixels[to];
|
|
635
|
+
let first = -1;
|
|
636
|
+
let last = -1;
|
|
637
|
+
for (let x = from; x <= to; x++) if (differs(pixels[x], before) && differs(pixels[x], after)) {
|
|
638
|
+
if (first < 0) first = x;
|
|
639
|
+
last = x;
|
|
640
|
+
}
|
|
641
|
+
return first < 0 ? null : {
|
|
642
|
+
first,
|
|
643
|
+
last
|
|
644
|
+
};
|
|
645
|
+
};
|
|
646
|
+
const s = input.scale;
|
|
647
|
+
// Only inside the grip and 3 px around it, so text beside it never counts.
|
|
648
|
+
const from = Math.round((input.left - 3) * s);
|
|
649
|
+
const to = Math.round((input.left + input.width + 3) * s) - 1;
|
|
650
|
+
// The line 3 px above the pill, and the pill 3 px below its top edge, above its first dots. A horizontal line or a
|
|
651
|
+
// band edge can cross the column right there, so the line is also looked for further up and below the pill.
|
|
652
|
+
const line = [input.top - 3, input.top - 5, input.top + input.height + 3, input.top + input.height + 5].map(y => run(row(y * s + s / 2), from, to)).find(found => found !== null);
|
|
653
|
+
const pill = run(row((input.top + 3) * s + s / 2), from, to);
|
|
654
|
+
if (!line || !pill) return null;
|
|
655
|
+
const centre = r => (r.first + r.last + 1) / 2;
|
|
656
|
+
return {
|
|
657
|
+
offset: (centre(pill) - centre(line)) / s,
|
|
658
|
+
lineWidth: (line.last - line.first + 1) / s
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/** Measures the v2 layout rules on the page as it stands. Restores the page before it returns. */
|
|
663
|
+
export async function measureV2Layout(page, options = {}) {
|
|
664
|
+
// Fonts, observers and animations first: a measurement of a page that still moves measures a moment, not the page.
|
|
665
|
+
await settled(page);
|
|
666
|
+
const roles = Object.values(typeRoles);
|
|
667
|
+
const limits = {
|
|
668
|
+
root: options.root ?? "body",
|
|
669
|
+
columns: options.columns ?? "[data-k2-column]",
|
|
670
|
+
grips: options.grips ?? "[data-k2-grip]",
|
|
671
|
+
prose: options.prose ?? "[data-k2-prose]",
|
|
672
|
+
skip: options.skip ?? "[data-k2-canvas]",
|
|
673
|
+
figure: options.figure ?? "[data-k2-figure]",
|
|
674
|
+
untyped: options.untyped ?? "[data-k2-avatar]",
|
|
675
|
+
unbroken: options.unbroken ?? "[data-k2-part='Band'], [data-k2-part='Group']",
|
|
676
|
+
controls: options.controls ?? ".k2-btn, .k2-chip, .k2-tab",
|
|
677
|
+
// A row of three lines cuts each line; in a row of one or two lines the title may wrap (it is what the row is about).
|
|
678
|
+
oneLine: options.oneLine ?? ".k2-row[data-lines='3'] > :is(.k2-row-l1, .k2-row-l2, .k2-row-l3), .k2-row-meta, .k2-table[data-sized] .k2-cell-line:not([data-wrap])",
|
|
679
|
+
sizes: roles.map(role => role.size),
|
|
680
|
+
lineHeights: [...new Set(roles.map(role => role.lineHeight))],
|
|
681
|
+
weights: [...fontWeights],
|
|
682
|
+
hairline: hairline.width,
|
|
683
|
+
tolerance: gripTolerance
|
|
684
|
+
};
|
|
685
|
+
const dom = await page.evaluate(measureInPage, limits);
|
|
686
|
+
const scale = await page.evaluate(() => window.devicePixelRatio);
|
|
687
|
+
const margin = 6;
|
|
688
|
+
const grips = [];
|
|
689
|
+
const grip = [...dom.grip];
|
|
690
|
+
for (const measure of dom.grips) {
|
|
691
|
+
if (!measure.line) continue;
|
|
692
|
+
const {
|
|
693
|
+
left,
|
|
694
|
+
top,
|
|
695
|
+
width,
|
|
696
|
+
height
|
|
697
|
+
} = measure.rect;
|
|
698
|
+
// A clip on whole CSS pixels, so the engine never resamples the picture.
|
|
699
|
+
const x = Math.floor(left - margin);
|
|
700
|
+
const y = Math.floor(top - margin);
|
|
701
|
+
const png = await page.screenshot({
|
|
702
|
+
clip: {
|
|
703
|
+
x,
|
|
704
|
+
y,
|
|
705
|
+
width: Math.ceil(width + 2 * margin) + 1,
|
|
706
|
+
height: Math.ceil(height + 2 * margin) + 1
|
|
707
|
+
},
|
|
708
|
+
scale: "device",
|
|
709
|
+
animations: "disabled",
|
|
710
|
+
caret: "hide"
|
|
711
|
+
});
|
|
712
|
+
const pixels = await page.evaluate(gripPixelsInPage, {
|
|
713
|
+
png: png.toString("base64"),
|
|
714
|
+
scale,
|
|
715
|
+
left: left - x,
|
|
716
|
+
top: top - y,
|
|
717
|
+
width,
|
|
718
|
+
height
|
|
719
|
+
});
|
|
720
|
+
grips.push({
|
|
721
|
+
grip: measure.grip,
|
|
722
|
+
dom: measure.dom,
|
|
723
|
+
pixels: pixels?.offset ?? null,
|
|
724
|
+
lineWidth: pixels?.lineWidth ?? null
|
|
725
|
+
});
|
|
726
|
+
if (!pixels) grip.push(`grip ${measure.grip}: pill or line not found in the screenshot at DPR ${scale}`);else {
|
|
727
|
+
if (Math.abs(pixels.offset) > gripTolerance) grip.push(`grip ${measure.grip}: pill centre ${pixels.offset} px off its line in the screenshot at DPR ${scale}`);
|
|
728
|
+
if (Math.abs(pixels.lineWidth - hairline.width) > 0.01) grip.push(`grip ${measure.grip}: its line is ${pixels.lineWidth} px wide in the screenshot at DPR ${scale}`);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
// The DOM measurement and the screenshots are separate round trips; a grip that moved in between was not measured.
|
|
732
|
+
if (dom.grips.length) {
|
|
733
|
+
const now = await page.evaluate(({
|
|
734
|
+
root,
|
|
735
|
+
grips: selector
|
|
736
|
+
}) => Array.from(document.querySelector(root)?.querySelectorAll(selector) ?? []).filter(element => element.checkVisibility({
|
|
737
|
+
visibilityProperty: true,
|
|
738
|
+
opacityProperty: true
|
|
739
|
+
})).map(element => element.getBoundingClientRect()).filter(r => r.width > 0 && r.height > 0).map(r => [r.left, r.top, r.width, r.height]), {
|
|
740
|
+
root: limits.root,
|
|
741
|
+
grips: limits.grips
|
|
742
|
+
});
|
|
743
|
+
const then = dom.grips.map(({
|
|
744
|
+
rect
|
|
745
|
+
}) => [rect.left, rect.top, rect.width, rect.height]);
|
|
746
|
+
if (JSON.stringify(now) !== JSON.stringify(then)) grip.push("the grips moved between the DOM measurement and the screenshots; measure a page that holds still");
|
|
747
|
+
}
|
|
748
|
+
const problems = [...dom.pseudo, ...dom.edge, ...dom.double, ...dom.inside, ...dom.cut, ...dom.type, ...grip];
|
|
749
|
+
return {
|
|
750
|
+
edge: dom.edge,
|
|
751
|
+
double: dom.double,
|
|
752
|
+
inside: dom.inside,
|
|
753
|
+
cut: dom.cut,
|
|
754
|
+
type: dom.type,
|
|
755
|
+
grip,
|
|
756
|
+
grips,
|
|
757
|
+
pseudo: dom.pseudo,
|
|
758
|
+
measured: dom.measured,
|
|
759
|
+
problems
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
//# sourceMappingURL=measure-v2-layout.js.map
|