123 lines
5.2 KiB
TypeScript
123 lines
5.2 KiB
TypeScript
import type { Page } from '@playwright/test';
|
|
|
|
/** What breaks a page's layout, measured in the browser: overlapping texts (also inside
|
|
* Plotly figures: legend against axis titles, tick labels), labels clipped at a figure's
|
|
* edge, labels that wrap where they should not (buttons, badges), a header taller than one
|
|
* row, text cut by its own box, and a page wider than the window. Empty: nothing found. */
|
|
export async function layoutProblems(page: Page): Promise<string[]> {
|
|
return page.evaluate(() => {
|
|
const found: string[] = [];
|
|
const box = (e: Element) => e.getBoundingClientRect();
|
|
const hit = (a: DOMRect, b: DOMRect) =>
|
|
a.width > 0 &&
|
|
b.width > 0 &&
|
|
a.left < b.right - 1 &&
|
|
b.left < a.right - 1 &&
|
|
a.top < b.bottom - 1 &&
|
|
b.top < a.bottom - 1;
|
|
const where = (el: Element) => {
|
|
const card = el.closest('mat-card, section, .card');
|
|
const h = card?.querySelector('h1,h2,h3,h4,mat-card-title');
|
|
return (h?.textContent ?? '').trim().slice(0, 60);
|
|
};
|
|
if (document.documentElement.scrollWidth > window.innerWidth + 1)
|
|
found.push(
|
|
`page overflows horizontally: ${document.documentElement.scrollWidth} > ${window.innerWidth}`,
|
|
);
|
|
for (const plot of document.querySelectorAll('.js-plotly-plot')) {
|
|
const texts = [
|
|
...plot.querySelectorAll(
|
|
'.legend, .g-xtitle text, .g-ytitle text, .xtitle, .ytitle, .gtitle',
|
|
),
|
|
];
|
|
for (let i = 0; i < texts.length; i++)
|
|
for (let j = i + 1; j < texts.length; j++)
|
|
if (
|
|
!texts[i].contains(texts[j]) &&
|
|
!texts[j].contains(texts[i]) &&
|
|
hit(box(texts[i]), box(texts[j]))
|
|
)
|
|
found.push(
|
|
`plot "${where(plot)}": ${texts[i].getAttribute('class')} overlaps ${texts[j].getAttribute('class')}`,
|
|
);
|
|
const host = box(plot);
|
|
for (const t of plot.querySelectorAll('.slicetext, .xtick text, .ytick text, .legendtext')) {
|
|
const b = box(t);
|
|
if (b.width && (b.left < host.left - 1 || b.right > host.right + 1))
|
|
found.push(`plot "${where(plot)}": text "${t.textContent}" clipped at the edge`);
|
|
}
|
|
const ticks = [...plot.querySelectorAll('.xtick text')].map(box);
|
|
for (let i = 1; i < ticks.length; i++)
|
|
if (hit(ticks[i - 1], ticks[i])) {
|
|
found.push(`plot "${where(plot)}": x tick labels overlap`);
|
|
break;
|
|
}
|
|
}
|
|
// Text elements overlapping each other (headings, labels, buttons, table cells).
|
|
const els = [
|
|
...document.querySelectorAll<HTMLElement>(
|
|
'h1,h2,h3,h4,p,label,button,a,dt,dd,th,td,mat-card-title,.mdc-tab__text-label,mat-label,li',
|
|
),
|
|
].filter(
|
|
(e: any) =>
|
|
e.offsetParent &&
|
|
box(e).width > 0 &&
|
|
box(e).height > 0 &&
|
|
!e.closest('.js-plotly-plot, .cdk-overlay-container') &&
|
|
!e.closest('details:not([open]) > :not(summary)'),
|
|
);
|
|
for (let i = 0; i < els.length; i++)
|
|
for (let j = i + 1; j < Math.min(els.length, i + 40); j++) {
|
|
const a = els[i],
|
|
b = els[j];
|
|
if (a.contains(b) || b.contains(a)) continue;
|
|
if (hit(box(a), box(b)))
|
|
found.push(
|
|
`"${(a.textContent ?? '').trim().slice(0, 30)}" overlaps "${(b.textContent ?? '').trim().slice(0, 30)}" (${where(a)})`,
|
|
);
|
|
}
|
|
// Text cut by its own box (ellipsis or overflow hidden).
|
|
for (const e of document.querySelectorAll<HTMLElement>(
|
|
'button, .mdc-tab__text-label, th, mat-label, .mat-mdc-select-value-text',
|
|
))
|
|
if (
|
|
e.offsetParent &&
|
|
e.scrollWidth > e.clientWidth + 2 &&
|
|
getComputedStyle(e).overflow !== 'visible'
|
|
)
|
|
found.push(`text cut: "${(e.textContent ?? '').trim().slice(0, 40)}"`);
|
|
const header = document.querySelector('.header');
|
|
if (header && header.getBoundingClientRect().height > 90 && window.innerWidth > 720)
|
|
found.push(
|
|
`header taller than one row: ${Math.round(header.getBoundingClientRect().height)}px`,
|
|
);
|
|
for (const b of document.querySelectorAll<HTMLElement>(
|
|
'button:not(.mat-button-toggle-button), a.mdc-button, .badge, .mat-mdc-chip',
|
|
)) {
|
|
const label = (b.querySelector('.mdc-button__label') as HTMLElement | null) ?? b;
|
|
const lines =
|
|
label.getClientRects().length &&
|
|
Math.round(
|
|
label.getBoundingClientRect().height /
|
|
parseFloat(getComputedStyle(label).lineHeight || '20'),
|
|
);
|
|
if (b.offsetParent && lines > 1 && (label.textContent ?? '').trim().length < 40)
|
|
found.push(`label wraps: "${(label.textContent ?? '').trim()}"`);
|
|
}
|
|
const icons = [
|
|
...document.querySelectorAll('header mat-icon, header a, header button, header .title'),
|
|
];
|
|
for (let i = 0; i < icons.length; i++)
|
|
for (let j = i + 1; j < icons.length; j++)
|
|
if (
|
|
!icons[i].contains(icons[j]) &&
|
|
!icons[j].contains(icons[i]) &&
|
|
hit(box(icons[i]), box(icons[j]))
|
|
)
|
|
found.push(
|
|
`header: "${(icons[i].textContent ?? '').trim().slice(0, 20)}" overlaps "${(icons[j].textContent ?? '').trim().slice(0, 20)}"`,
|
|
);
|
|
return [...new Set(found)];
|
|
});
|
|
}
|