| @@ -5,13 +5,19 @@ |
| 5 | 5 | * fit their segments, the tooltip stays inside its lane, the narrow layout puts |
| 6 | 6 | * each track's label above its lane, and nothing overflows the page, at a |
| 7 | 7 | * phone's width and a desktop's. Then the table view ([show-table]: the table |
| 8 | | − * under the plot, its numbers right-aligned, its rules), stress tables of 6 and |
| 9 | | − * 7 columns with long cells that must never be wider than the element, from |
| 10 | | − * 280px up, and the motion (a rewritten row's segment glides to its new width; |
| 11 | | − * a shown label is never clipped mid-glide; under reduced motion it jumps). |
| 8 | + * under the plot, its numbers right-aligned, its rules); the sample's five |
| 9 | + * columns in English and French, whose words stay whole from 320px (French |
| 10 | + * labels may hyphenate at 320) and whose "Fig. 2" is never split; stress |
| 11 | + * tables of 6, 7 and 9 columns with long cells that must never be wider than |
| 12 | + * the element, from 280px up, with every swatch inside its cell; --start and |
| 13 | + * --size registered (@property); and the motion (a rewritten row's segment |
| 14 | + * glides to its new width; a shown label is never clipped mid-glide; under |
| 15 | + * reduced motion it jumps). |
| 12 | 16 | * |
| 13 | | − * Run: `bun examples/timeline-smoke.ts`. Exits non-zero on failure. Bun's |
| 14 | | − * WebSocket speaks CDP to the playwright-pinned Chromium (tablet-smoke.ts's pattern). |
| 17 | + * Run: `bun examples/timeline-smoke.ts`. Exits non-zero on failure. |
| 18 | + * TIMELINE_BUNDLE=path serves another build of the bundle (an older commit's, |
| 19 | + * to see a new check fail). Bun's WebSocket speaks CDP to the playwright-pinned |
| 20 | + * Chromium (tablet-smoke.ts's pattern). |
| 15 | 21 | */ |
| 16 | 22 | import { existsSync, readdirSync } from "node:fs"; |
| 17 | 23 | import { spawn, spawnSync, type ChildProcess } from "node:child_process"; |
| @@ -24,6 +30,7 @@ function chrome(): string { |
| 24 | 30 | } |
| 25 | 31 | |
| 26 | 32 | function bundle(): string { |
| 33 | + if (process.env.TIMELINE_BUNDLE) return process.env.TIMELINE_BUNDLE; |
| 27 | 34 | const path = `${import.meta.dir}/../dist/timeline.js`; |
| 28 | 35 | if (!existsSync(path)) { |
| 29 | 36 | const built = spawnSync("bun", ["run", "build"], { cwd: `${import.meta.dir}/..` }); |
| @@ -32,58 +39,145 @@ function bundle(): string { |
| 32 | 39 | return path; |
| 33 | 40 | } |
| 34 | 41 | |
| 35 | | −const PAGE = `<!doctype html> |
| 36 | | −<html lang="en"><head><meta name="viewport" content="width=device-width, initial-scale=1"></head> |
| 42 | +/** The sample's table in two languages: five columns, a source cell whose "Fig. 2" must stay whole. */ |
| 43 | +const SAMPLE = { |
| 44 | + en: { |
| 45 | + unit: "wk", |
| 46 | + axis: "Weeks from taking the cutting", |
| 47 | + head: ["Method", "Phase", "Starts (day)", "Days", "Source"], |
| 48 | + tracks: ["Standard topping", "Early topping"], |
| 49 | + phases: ["Rooting", "Vegetative growth", "Recovery (extra vegetative growth)", "Flowering", "Mother-plant wait"], |
| 50 | + }, |
| 51 | + fr: { |
| 52 | + unit: "sem.", |
| 53 | + axis: "Semaines depuis le prélèvement de la bouture", |
| 54 | + head: ["Méthode", "Phase", "Début (jour)", "Durée (jours)", "Source"], |
| 55 | + tracks: ["Écimage standard", "Écimage précoce"], |
| 56 | + phases: ["Enracinement", "Croissance végétative", "Récupération (croissance supplémentaire)", "Floraison", "Attente du pied-mère"], |
| 57 | + }, |
| 58 | +} as const; |
| 59 | +type Lang = keyof typeof SAMPLE; |
| 60 | + |
| 61 | +/** |
| 62 | + * The sample page in \`lang\`, with \`extra\` cells appended to every row under \`more\` headers. |
| 63 | + * Flowering's length (4th row) is the one the motion checks rewrite. |
| 64 | + */ |
| 65 | +function samplePage(lang: Lang, { showTable = false, more = [] as string[], extra = [] as string[] } = {}): string { |
| 66 | + const t = SAMPLE[lang]; |
| 67 | + const rows: Array<[number, number, number, number, string]> = [ |
| 68 | + // track, phase, start, length, row attributes |
| 69 | + [0, 0, 0, 10, ""], |
| 70 | + [0, 1, 10, 14, ` data-series="2"`], |
| 71 | + [0, 2, 24, 14, ` data-series="2" data-texture="hatch"`], |
| 72 | + [0, 3, 38, 56, ` data-series="3"`], |
| 73 | + [1, 4, -14, 14, ` data-series="muted"`], |
| 74 | + [1, 0, 0, 10, ""], |
| 75 | + [1, 1, 10, 14, ` data-series="2"`], |
| 76 | + [1, 3, 24, 56, ` data-series="3"`], |
| 77 | + ]; |
| 78 | + const cells = (values: readonly string[], tag: string) => values.map(v => `<${tag}>${v}</${tag}>`).join(""); |
| 79 | + const body = rows |
| 80 | + .map(([track, phase, start, length, attributes]) => |
| 81 | + `<tr${attributes}><th>${t.tracks[track]}</th>${cells([t.phases[phase]!, String(start).replace("-", "−"), String(length), "Fig. 2", ...extra], "td")}</tr>`) |
| 82 | + .join("\n "); |
| 83 | + return `<!doctype html> |
| 84 | +<html lang="${lang}"><head><meta name="viewport" content="width=device-width, initial-scale=1"></head> |
| 37 | 85 | <body style="margin: 0; padding: 16px; font-family: system-ui"> |
| 38 | | −<pebble-timeline min="-14" max="105" step="14" per="7" unit="wk" axis="Weeks from taking the cutting"> |
| 86 | +<pebble-timeline${showTable ? " show-table" : ""} min="-14" max="105" step="14" per="7" unit="${t.unit}" axis="${t.axis}"> |
| 39 | 87 | <table> |
| 40 | 88 | <caption>Two schedules</caption> |
| 41 | | − <thead><tr><th>Method</th><th>Phase</th><th>Starts (day)</th><th>Days</th><th>Source</th></tr></thead> |
| 89 | + <thead><tr>${cells([...t.head, ...more], "th")}</tr></thead> |
| 42 | 90 | <tbody> |
| 43 | | − <tr><th>Standard topping</th><td>Rooting</td><td>0</td><td>10</td><td>Fig. 2</td></tr> |
| 44 | | − <tr data-series="2"><th>Standard topping</th><td>Vegetative growth</td><td>10</td><td>14</td><td>Fig. 2</td></tr> |
| 45 | | − <tr data-series="2" data-texture="hatch"><th>Standard topping</th><td>Recovery (extra vegetative growth)</td><td>24</td><td>14</td><td>Fig. 2</td></tr> |
| 46 | | − <tr data-series="3"><th>Standard topping</th><td>Flowering</td><td>38</td><td>56</td><td>Fig. 2</td></tr> |
| 47 | | − <tr data-series="muted"><th>Early topping</th><td>Mother-plant wait</td><td>−14</td><td>14</td><td>Fig. 2</td></tr> |
| 48 | | − <tr><th>Early topping</th><td>Rooting</td><td>0</td><td>10</td><td>Fig. 2</td></tr> |
| 49 | | − <tr data-series="2"><th>Early topping</th><td>Vegetative growth</td><td>10</td><td>14</td><td>Fig. 2</td></tr> |
| 50 | | − <tr data-series="3"><th>Early topping</th><td>Flowering</td><td>24</td><td>56</td><td>Fig. 2</td></tr> |
| 91 | + ${body} |
| 51 | 92 | </tbody> |
| 52 | 93 | </table> |
| 53 | 94 | </pebble-timeline> |
| 54 | 95 | <script type="module" src="/timeline.js"></script> |
| 55 | 96 | </body></html>`; |
| 97 | +} |
| 98 | +const PAGE = samplePage("en"); |
| 56 | 99 | |
| 57 | 100 | /** |
| 58 | | − * A table the page could well write: the sample's five columns, then a long note and a |
| 59 | | − * reference (an unbreakable DOI), long header words too. `cols` is 6 or 7. |
| 101 | + * A table the page could well write: the sample's five columns, then a long note, a reference |
| 102 | + * (an unbreakable DOI), a cultivar and a batch, long header words too. \`cols\` is 6 to 9. |
| 60 | 103 | */ |
| 61 | | −const NOTE = "Measured on the greenhouse bench under the long-day regime, see supplementary table S4"; |
| 62 | | −const REFERENCE = "doi:10.1016/j.scienta.2019.108712"; |
| 104 | +const EXTRA = [ |
| 105 | + ["Observations", "Measured on the greenhouse bench under the long-day regime, see supplementary table S4"], |
| 106 | + ["Reference", "doi:10.1016/j.scienta.2019.108712"], |
| 107 | + ["Cultivar", "Cannabis sativa ‘Finola’"], |
| 108 | + ["Batch", "LOT-2019-1004-A"], |
| 109 | +] as const; |
| 63 | 110 | function stressPage(cols: number): string { |
| 64 | | − const extra = (note: string, reference: string) => (cols > 5 ? `<td>${note}</td>` : "") + (cols > 6 ? `<td>${reference}</td>` : ""); |
| 65 | | − const body = PAGE.match(/<tbody>([\s\S]*?)<\/tbody>/)![1]!.replace(/<\/tr>/g, () => `${extra(NOTE, REFERENCE)}</tr>`); |
| 66 | | − const head = `<th>Method</th><th>Phase</th><th>Starts (day)</th><th>Days</th><th>Source</th>${cols > 5 ? "<th>Observations</th>" : ""}${cols > 6 ? "<th>Reference</th>" : ""}`; |
| 67 | | − return PAGE.replace("<pebble-timeline ", "<pebble-timeline show-table ") |
| 68 | | − .replace(/<thead>[\s\S]*?<\/thead>/, `<thead><tr>${head}</tr></thead>`) |
| 69 | | − .replace(/<tbody>[\s\S]*?<\/tbody>/, `<tbody>${body}</tbody>`); |
| 111 | + const extra = EXTRA.slice(0, cols - 5); |
| 112 | + return samplePage("en", { showTable: true, more: extra.map(e => e[0]), extra: extra.map(e => e[1]) }); |
| 70 | 113 | } |
| 114 | +const STRESS_COLUMNS = [6, 7, 9]; |
| 71 | 115 | const STRESS_WIDTHS = [280, 320, 390, 600, 1280]; |
| 116 | +const SAMPLE_WIDTHS = [280, 320, 360, 390, 1280]; |
| 117 | +/** From this width up, the five-column sample's words stay whole (French labels may hyphenate at 320). */ |
| 118 | +const WHOLE_FROM = 320; |
| 72 | 119 | |
| 73 | | −type Stress = { host: number; table: number; display: string; overflow: number; cells: number; spilled: number }; |
| 74 | | −/** The table against its element: its box, every cell's box, and the page. */ |
| 120 | +type Stress = { |
| 121 | + host: number; |
| 122 | + table: number; |
| 123 | + display: string; |
| 124 | + fit: string; |
| 125 | + overflow: number; |
| 126 | + cells: number; |
| 127 | + spilled: number; |
| 128 | + swatches: Array<{ text: string; scroll: number; client: number; width: number }>; |
| 129 | + broken: Array<{ word: string; column: number; lines: number; width: number; room: number }>; |
| 130 | + figures: number; |
| 131 | + figuresSplit: number; |
| 132 | +}; |
| 133 | +/** |
| 134 | + * The table against its element: its box, every cell's box, the page; each label cell's swatch |
| 135 | + * (::before) against its cell; and every word: one the browser broke over two lines, or wider |
| 136 | + * than its cell's content box (the review's measure), is broken. A word is a run between spaces |
| 137 | + * (a no-break space joins: "Fig. 2" is one), ending after a hyphen ("Mother-" may end a line). |
| 138 | + */ |
| 75 | 139 | const STRESS = `(() => { |
| 76 | 140 | const el = document.querySelector("pebble-timeline"); |
| 77 | 141 | const table = el.querySelector("table"); |
| 78 | 142 | const host = el.getBoundingClientRect(); |
| 79 | 143 | const cells = [...table.querySelectorAll("th, td")]; |
| 144 | + const canvas = document.createElement("canvas").getContext("2d"); |
| 145 | + const broken = []; |
| 146 | + let figures = 0, figuresSplit = 0; |
| 147 | + for (const cell of cells) { |
| 148 | + const style = getComputedStyle(cell); |
| 149 | + const room = cell.clientWidth - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight); |
| 150 | + canvas.font = style.fontWeight + " " + style.fontSize + " " + style.fontFamily; |
| 151 | + const walker = document.createTreeWalker(cell, NodeFilter.SHOW_TEXT); |
| 152 | + for (let node = walker.nextNode(); node; node = walker.nextNode()) { |
| 153 | + for (const match of node.data.matchAll(/[^ \\t\\n\\r-]+-?/g)) { |
| 154 | + const range = document.createRange(); |
| 155 | + range.setStart(node, match.index); |
| 156 | + range.setEnd(node, match.index + match[0].length); |
| 157 | + const lines = new Set([...range.getClientRects()].filter(r => r.width > 0).map(r => Math.round(r.top))).size; |
| 158 | + const width = canvas.measureText(match[0]).width; |
| 159 | + if (match[0] === "Fig.\\u00a02") { figures++; if (lines > 1) figuresSplit++; } |
| 160 | + if (lines > 1 || width > room + 0.5) broken.push({ word: match[0], column: cell.cellIndex + 1, lines, width, room }); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 80 | 164 | return { |
| 81 | 165 | host: host.width, |
| 82 | 166 | table: table.getBoundingClientRect().width, |
| 83 | 167 | display: getComputedStyle(table).display, |
| 168 | + fit: el.getAttribute("data-table-fit") ?? "whole", |
| 84 | 169 | overflow: document.documentElement.scrollWidth - document.documentElement.clientWidth, |
| 85 | 170 | cells: cells.length, |
| 86 | 171 | spilled: cells.filter(c => c.getBoundingClientRect().right > host.right + 0.5 || c.scrollWidth > c.clientWidth + 1).length, |
| 172 | + swatches: [...table.querySelectorAll("tr[data-fill] > :nth-child(2)")].map(cell => ({ |
| 173 | + text: cell.textContent, |
| 174 | + scroll: cell.scrollWidth, |
| 175 | + client: cell.clientWidth, |
| 176 | + width: parseFloat(getComputedStyle(cell, "::before").width), |
| 177 | + })), |
| 178 | + broken, |
| 179 | + figures, |
| 180 | + figuresSplit, |
| 87 | 181 | }; |
| 88 | 182 | })()`; |
| 89 | 183 | |
| @@ -180,6 +274,26 @@ class Cdp { |
| 180 | 274 | } |
| 181 | 275 | } |
| 182 | 276 | |
| 277 | +type Registered = { rule: boolean; typed: string; unit: string; value: number; inline: number; label: string }; |
| 278 | +/** |
| 279 | + * --start and --size registered (@property, an unscoped adopted sheet): the rule is in the |
| 280 | + * document's adopted sheets, a segment's --size computes to a typed number, and its label does |
| 281 | + * not inherit it (inherits: false: the label's --size is the initial 0). |
| 282 | + */ |
| 283 | +const REGISTERED = `(() => { |
| 284 | + const segment = document.querySelector("pebble-timeline .segment"); |
| 285 | + const size = segment.computedStyleMap().get("--size"); |
| 286 | + return { |
| 287 | + rule: document.adoptedStyleSheets.some(sheet => [...sheet.cssRules].some(r => r instanceof CSSPropertyRule && r.name === "--size")) |
| 288 | + && document.adoptedStyleSheets.some(sheet => [...sheet.cssRules].some(r => r instanceof CSSPropertyRule && r.name === "--start")), |
| 289 | + typed: size.constructor.name, |
| 290 | + unit: size.unit ?? "", |
| 291 | + value: size.value ?? NaN, |
| 292 | + inline: Number(segment.style.getPropertyValue("--size")), |
| 293 | + label: getComputedStyle(segment.querySelector(".segment-label")).getPropertyValue("--size").trim(), |
| 294 | + }; |
| 295 | +})()`; |
| 296 | + |
| 183 | 297 | type Rect = { left: number; right: number; top: number; bottom: number; width: number }; |
| 184 | 298 | type Measure = { |
| 185 | 299 | enhanced: boolean; |
| @@ -283,7 +397,9 @@ const server = Bun.serve({ |
| 283 | 397 | const url = new URL(request.url); |
| 284 | 398 | if (url.pathname === "/timeline.js") return new Response(Bun.file(bundle()), { headers: { "content-type": "text/javascript" } }); |
| 285 | 399 | const cols = Number(url.searchParams.get("cols") ?? 0); |
| 286 | | − return new Response(cols > 0 ? stressPage(cols) : PAGE, { headers: { "content-type": "text/html; charset=utf-8" } }); |
| 400 | + const sample = url.searchParams.get("sample") as Lang | null; |
| 401 | + const page = cols > 0 ? stressPage(cols) : sample ? samplePage(sample, { showTable: true }) : PAGE; |
| 402 | + return new Response(page, { headers: { "content-type": "text/html; charset=utf-8" } }); |
| 287 | 403 | }, |
| 288 | 404 | }); |
| 289 | 405 | const chromium = spawn(chrome(), ["--headless=new", "--disable-gpu", "--no-sandbox", "--remote-debugging-port=0", "about:blank"], { |
| @@ -335,6 +451,15 @@ try { |
| 335 | 451 | check(`${at} every shown label fits its segment`, m.segments.every(s => s.unlabelled || s.labelFits)); |
| 336 | 452 | check(`${at} some labels show`, m.segments.some(s => !s.unlabelled)); |
| 337 | 453 | |
| 454 | + const registered = await cdp.eval<Registered>(REGISTERED); |
| 455 | + check(`${at} --start and --size are registered by an adopted @property sheet`, registered.rule, JSON.stringify(registered)); |
| 456 | + check( |
| 457 | + `${at} a segment's --size computes to a typed number`, |
| 458 | + registered.typed === "CSSUnitValue" && registered.unit === "number" && near(registered.value, registered.inline, 1e-4), |
| 459 | + JSON.stringify(registered), |
| 460 | + ); |
| 461 | + check(`${at} a label does not inherit its segment's --size`, registered.label === "0", JSON.stringify(registered)); |
| 462 | + |
| 338 | 463 | // The tooltip of the first and the last segment stays inside the lane. |
| 339 | 464 | for (const key of ["Home", "End"]) { |
| 340 | 465 | await cdp.eval(`(() => { const plot = document.querySelector(".plot"); plot.focus(); |
| @@ -389,10 +514,31 @@ try { |
| 389 | 514 | await cdp.closeTab(); |
| 390 | 515 | } |
| 391 | 516 | |
| 517 | + // The sample's five columns, English and French: the words stay whole from 320px (no |
| 518 | + // fallback to the fixed layout, no word over two lines or wider than its cell). French labels |
| 519 | + // need a few pixels more than 320px has (their longest words alone take 258 of its 288): there |
| 520 | + // the label column hyphenates, a typographer's break, and the other columns stay whole. |
| 521 | + // "Fig. 2" is never split, down to 280px. |
| 522 | + for (const lang of ["en", "fr"] as const) |
| 523 | + for (const width of SAMPLE_WIDTHS) { |
| 524 | + await open(cdp, width, `/?sample=${lang}`); |
| 525 | + const s = await cdp.eval<Stress>(STRESS); |
| 526 | + const at = `${width}px ${lang} 5 columns`; |
| 527 | + check(`${at}: the table fits its element`, s.table <= s.host + 0.5 && s.overflow <= 0 && s.spilled === 0, JSON.stringify(s)); |
| 528 | + check(`${at}: "Fig. 2" is never split`, s.figures === 8 && s.figuresSplit === 0, `${s.figuresSplit} of ${s.figures} split, fit ${s.fit}`); |
| 529 | + if (width >= WHOLE_FROM) { |
| 530 | + const hyphenated = (b: Stress["broken"][number]) => s.fit === "hyphens" && b.column === 2; |
| 531 | + check(`${at}: auto layout, no word broken but by a hyphenated label (${s.fit})`, s.fit !== "fixed" && s.broken.filter(b => !hyphenated(b)).length === 0, `fit ${s.fit}: ${JSON.stringify(s.broken)}`); |
| 532 | + if (width > WHOLE_FROM || lang === "en") check(`${at}: not even a hyphen`, s.broken.length === 0, `fit ${s.fit}: ${JSON.stringify(s.broken)}`); |
| 533 | + } |
| 534 | + await cdp.closeTab(); |
| 535 | + } |
| 536 | + |
| 392 | 537 | // Stress: wider tables than the sample's, with long cells and an unbreakable reference. The |
| 393 | 538 | // table is never wider than its element, no cell spills out of it, the page never scrolls |
| 394 | | − // sideways, and the table stays a table. |
| 395 | | − for (const cols of [6, 7]) |
| 539 | + // sideways, the table stays a table, and every swatch is inside its label cell. Where the |
| 540 | + // element is wide enough (1280px), the words stay whole. |
| 541 | + for (const cols of STRESS_COLUMNS) |
| 396 | 542 | for (const width of STRESS_WIDTHS) { |
| 397 | 543 | await open(cdp, width, `/?cols=${cols}`); |
| 398 | 544 | const s = await cdp.eval<Stress>(STRESS); |
| @@ -401,6 +547,9 @@ try { |
| 401 | 547 | check(`${at}: no cell spills out`, s.spilled === 0, `${s.spilled} of ${s.cells} cells`); |
| 402 | 548 | check(`${at}: no horizontal overflow`, s.overflow <= 0, `${s.overflow}px`); |
| 403 | 549 | check(`${at}: the table keeps display: table`, s.display === "table", s.display); |
| 550 | + const outside = s.swatches.filter(w => w.scroll > w.client + 0.5 || w.width <= 0); |
| 551 | + check(`${at}: every swatch is inside its cell`, s.swatches.length === 8 && outside.length === 0, JSON.stringify(outside)); |
| 552 | + if (width >= 1280 && cols < 9) check(`${at}: words whole (${s.fit})`, s.fit === "whole" && s.broken.length === 0, JSON.stringify(s.broken)); |
| 404 | 553 | await cdp.closeTab(); |
| 405 | 554 | } |
| 406 | 555 | cdp.close(); |