// titlefit.browser.mjs: open the stacked bar with each title, in each // engine and viewport, and measure the title field chrome.js fitted. // Driven by title_browser_test.go (go test -tags browser ./chrome), // which serves the page and makes every assertion; this file only drives // the browsers and reports what it measured. // // ESTILO_PLAYWRIGHT= \ // node titlefit.browser.mjs // // Job: {origin, engines, viewports, titles}. Output: one entry per // engine, {engine, version, error, fields: [...]}, as the test reads it. import { createRequire } from 'node:module' const require = createRequire(import.meta.url) const pw = require(process.env.ESTILO_PLAYWRIGHT || 'playwright') const job = JSON.parse(process.argv[2]) // measure runs in the page, once chrome.js has fitted the field and the // fonts have settled. function measure() { const f = document.querySelector('[estilo-bar-title-input][size]') const cs = getComputedStyle(f) const px = (v) => parseFloat(v) || 0 const frame = px(cs.paddingInlineStart) + px(cs.paddingInlineEnd) + px(cs.borderInlineStartWidth) + px(cs.borderInlineEndWidth) // The title as a span in the field's font draws it. (In WebKit a // field draws its text a little wider than this; never narrower.) const span = document.createElement('span') for (const p of ['font', 'letterSpacing', 'wordSpacing', 'fontKerning', 'fontFeatureSettings', 'fontVariationSettings', 'textTransform']) span.style[p] = cs[p] span.style.whiteSpace = 'pre' span.style.position = 'absolute' span.textContent = f.value document.body.append(span) const text = span.getBoundingClientRect().width span.remove() const width = f.getBoundingClientRect().width const inline = f.style.inlineSize const cut = () => f.scrollWidth > f.clientWidth || f.getBoundingClientRect().width - frame + 0.01 < text const out = { inline, width, content: width - frame, text, scroll: f.scrollWidth, client: f.clientWidth, // The bar's room set the width: the field is narrower than chrome.js asked. capped: width + 0.5 < px(inline), } // What chrome.js reads, at zero width. f.style.inlineSize = '0px' out.zero = { scroll: f.scrollWidth, client: f.clientWidth, offset: f.offsetWidth } // The control: the field as the bug had it, both paddings short. f.style.inlineSize = px(inline) - px(cs.paddingInlineStart) - px(cs.paddingInlineEnd) + 'px' out.narrowed = cut() f.style.inlineSize = inline return out } const results = [] for (const engine of job.engines) { const result = { engine, version: '', error: '', fields: [] } results.push(result) let browser try { browser = await pw[engine].launch() result.version = browser.version() for (const vw of job.viewports) { const page = await browser.newPage({ viewport: { width: vw, height: 400 } }) for (const title of job.titles) { await page.goto(job.origin + '/?title=' + encodeURIComponent(title), { waitUntil: 'load' }) await page.evaluate(() => document.fonts.ready) // chrome.js fits again once fonts settle; let that land. await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)))) result.fields.push({ title, viewport: vw, ...(await page.evaluate(measure)) }) } await page.close() } } catch (e) { result.error = String(e && e.message || e) } finally { if (browser) await browser.close() } } process.stdout.write(JSON.stringify(results))