// Data bars keep --pk-bar-inset (3 px) from their cell's left and right edge, so the bars of two // neighbouring cells never touch and read as one (#969). Measured in the pixels the browser draws. import { expect, test } from "./fixtures.js"; /** * Where the longest bar is drawn, in CSS px, read from the pixels of its row's middle line: how far it * stays from its cell's left and right edge, and the room between it and the bar of the next cell * (the playground's start puts it next to a shorter bar). The bar's colour is taken at a quarter of * its cell, where no digit of the right-aligned number is drawn. * @param {import("@playwright/test").Page} page */ async function gaps(page) { const bars = page.locator('[data-testid="pivot-table"] td.pk-has-bar'); await expect(bars.first()).toBeVisible(); const longest = await bars.evaluateAll((tds) => tds.findIndex((td) => getComputedStyle(td).getPropertyValue("--pk-bar").trim() === "100%")); expect(longest).toBeGreaterThanOrEqual(0); const cell = bars.nth(longest); const row = cell.locator("xpath=.."); // the cell and its neighbour, from the row's left edge const [at, next] = await cell.evaluate((td) => { const left = /** @type {Element} */ (td.parentElement).getBoundingClientRect().left; const box = td.getBoundingClientRect(); const after = td.nextElementSibling?.getBoundingClientRect(); return [{ from: box.left - left, to: box.right - left }, after && td.nextElementSibling?.classList.contains("pk-has-bar") ? { from: after.left - left, to: after.right - left } : null]; }); expect(next, "the longest bar has a bar beside it").not.toBeNull(); const png = (await row.screenshot({ animations: "disabled", scale: "css" })).toString("base64"); return page.evaluate( async ({ png, at, next }) => { const img = new Image(); img.src = `data:image/png;base64,${png}`; await img.decode(); const canvas = document.createElement("canvas"); canvas.width = img.width; canvas.height = img.height; const ctx = /** @type {CanvasRenderingContext2D} */ (canvas.getContext("2d", { willReadFrequently: true })); ctx.drawImage(img, 0, 0); const line = ctx.getImageData(0, Math.floor(img.height / 2), img.width, 1).data; const rgb = (x) => [line[x * 4], line[x * 4 + 1], line[x * 4 + 2]]; const bar = rgb(Math.round(at.from + (at.to - at.from) / 4)); const isBar = (x) => rgb(x).every((c, i) => Math.abs(c - bar[i]) <= 2); const [from, to] = [Math.round(at.from), Math.round(at.to)]; let first = from; while (first < to && !isBar(first)) first++; let last = to - 1; while (last > from && !isBar(last)) last--; let nextFirst = to; while (nextFirst < Math.round(next.to) && !isBar(nextFirst)) nextFirst++; return { left: first - at.from, right: at.to - 1 - last, gap: nextFirst - last - 1 }; }, { png, at, next } ); } /** * The bar keeps `inset` px from both edges of its cell (a px more where the cell's border line or a * fraction of a px falls), and two bars side by side keep twice that between them. * @param {{ left: number, right: number, gap: number }} measured * @param {number} inset */ function expectInset(measured, inset) { const said = JSON.stringify(measured); for (const side of [measured.left, measured.right]) { expect(side, said).toBeGreaterThanOrEqual(inset - 0.5); expect(side, said).toBeLessThanOrEqual(inset + 1.5); } expect(measured.gap, said).toBeGreaterThanOrEqual(2 * inset - 1); expect(measured.gap, said).toBeLessThanOrEqual(2 * inset + 2); } for (const theme of ["light", "dark"]) { test(`${theme}: the longest bar keeps 3 px from both edges of its cell, 6 px from the next bar`, async ({ page }) => { await page.emulateMedia({ colorScheme: theme }); // the weather start "How many rain days per season? (2025)": Mumbai's Summer and Autumn sit side by side await page.goto("?data=weather&preset=2"); expectInset(await gaps(page), 3); }); } test("a host's --pk-bar-inset sets the room", async ({ page }) => { await page.goto("?data=weather&preset=2"); await page.addStyleTag({ content: ".pk-root { --pk-bar-inset: 8px; }" }); expectInset(await gaps(page), 8); });