| 1 | // titlefit.browser.mjs: open the stacked bar with each title, in each |
| 2 | // engine and viewport, and measure the title field chrome.js fitted. |
| 3 | // Driven by title_browser_test.go (go test -tags browser ./chrome), |
| 4 | // which serves the page and makes every assertion; this file only drives |
| 5 | // the browsers and reports what it measured. |
| 6 | // |
| 7 | // ESTILO_PLAYWRIGHT=<path to the playwright package> \ |
| 8 | // node titlefit.browser.mjs <job JSON> |
| 9 | // |
| 10 | // Job: {origin, engines, viewports, titles}. Output: one entry per |
| 11 | // engine, {engine, version, error, fields: [...]}, as the test reads it. |
| 12 | import { createRequire } from 'node:module' |
| 13 | |
| 14 | const require = createRequire(import.meta.url) |
| 15 | const pw = require(process.env.ESTILO_PLAYWRIGHT || 'playwright') |
| 16 | const job = JSON.parse(process.argv[2]) |
| 17 | |
| 18 | // measure runs in the page, once chrome.js has fitted the field and the |
| 19 | // fonts have settled. |
| 20 | function measure() { |
| 21 | const f = document.querySelector('[estilo-bar-title-input][size]') |
| 22 | const cs = getComputedStyle(f) |
| 23 | const px = (v) => parseFloat(v) || 0 |
| 24 | const frame = px(cs.paddingInlineStart) + px(cs.paddingInlineEnd) + px(cs.borderInlineStartWidth) + px(cs.borderInlineEndWidth) |
| 25 | // The title as a span in the field's font draws it. (In WebKit a |
| 26 | // field draws its text a little wider than this; never narrower.) |
| 27 | const span = document.createElement('span') |
| 28 | for (const p of ['font', 'letterSpacing', 'wordSpacing', 'fontKerning', 'fontFeatureSettings', 'fontVariationSettings', 'textTransform']) span.style[p] = cs[p] |
| 29 | span.style.whiteSpace = 'pre' |
| 30 | span.style.position = 'absolute' |
| 31 | span.textContent = f.value |
| 32 | document.body.append(span) |
| 33 | const text = span.getBoundingClientRect().width |
| 34 | span.remove() |
| 35 | const width = f.getBoundingClientRect().width |
| 36 | const inline = f.style.inlineSize |
| 37 | const cut = () => f.scrollWidth > f.clientWidth || f.getBoundingClientRect().width - frame + 0.01 < text |
| 38 | const out = { |
| 39 | inline, width, content: width - frame, text, |
| 40 | scroll: f.scrollWidth, client: f.clientWidth, |
| 41 | // The bar's room set the width: the field is narrower than chrome.js asked. |
| 42 | capped: width + 0.5 < px(inline), |
| 43 | } |
| 44 | // What chrome.js reads, at zero width. |
| 45 | f.style.inlineSize = '0px' |
| 46 | out.zero = { scroll: f.scrollWidth, client: f.clientWidth, offset: f.offsetWidth } |
| 47 | // The control: the field as the bug had it, both paddings short. |
| 48 | f.style.inlineSize = px(inline) - px(cs.paddingInlineStart) - px(cs.paddingInlineEnd) + 'px' |
| 49 | out.narrowed = cut() |
| 50 | f.style.inlineSize = inline |
| 51 | return out |
| 52 | } |
| 53 | |
| 54 | const results = [] |
| 55 | for (const engine of job.engines) { |
| 56 | const result = { engine, version: '', error: '', fields: [] } |
| 57 | results.push(result) |
| 58 | let browser |
| 59 | try { |
| 60 | browser = await pw[engine].launch() |
| 61 | result.version = browser.version() |
| 62 | for (const vw of job.viewports) { |
| 63 | const page = await browser.newPage({ viewport: { width: vw, height: 400 } }) |
| 64 | for (const title of job.titles) { |
| 65 | await page.goto(job.origin + '/?title=' + encodeURIComponent(title), { waitUntil: 'load' }) |
| 66 | await page.evaluate(() => document.fonts.ready) |
| 67 | // chrome.js fits again once fonts settle; let that land. |
| 68 | await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)))) |
| 69 | result.fields.push({ title, viewport: vw, ...(await page.evaluate(measure)) }) |
| 70 | } |
| 71 | await page.close() |
| 72 | } |
| 73 | } catch (e) { |
| 74 | result.error = String(e && e.message || e) |
| 75 | } finally { |
| 76 | if (browser) await browser.close() |
| 77 | } |
| 78 | } |
| 79 | process.stdout.write(JSON.stringify(results)) |
| 80 | |