diff --git a/src/web/public/styles.css b/src/web/public/styles.css index 990a981e..b2b3a6d8 100644 --- a/src/web/public/styles.css +++ b/src/web/public/styles.css @@ -14119,6 +14119,11 @@ html:is([data-skin="paper-gray"], [data-skin="solarized-light"], [data-skin="cat text-align: start; vertical-align: top; white-space: normal; + /* A cell breaks a word only when the word alone is wider than the column. The `anywhere` + that cells inherit from .rv-text also lets the table count every letter as a break + point when it sizes its columns, so a column beside wide headers shrank to one letter + per line. */ + overflow-wrap: break-word; } .rv-text th:last-child, .rv-text td:last-child, diff --git a/test/response-viewer-table-cells.test.ts b/test/response-viewer-table-cells.test.ts new file mode 100644 index 00000000..b17fb687 --- /dev/null +++ b/test/response-viewer-table-cells.test.ts @@ -0,0 +1,45 @@ +/** + * @fileoverview Table cells in rendered markdown break only words wider than their column + * (src/web/public/styles.css). + * + * Rendered markdown sets `overflow-wrap: anywhere` so that a long path in prose wraps, and + * table cells inherited it. `anywhere` also counts every letter as a break point when the + * browser computes a cell's minimum width, and the header cells never wrap. In a table + * whose headers filled the width, the column under an empty header shrank to one letter per + * line. Cells now use `break-word`, which breaks a word only when it would overflow and + * leaves the minimum width at the longest word. + * + * jsdom has no layout, so this pins the declaration. Chromium was measured on a five-column + * table when the change was made: the label column went from 36 px, one letter per line, to + * 74 px, wrapped between words. + */ +import { readFileSync } from 'node:fs'; +import { resolve } from 'node:path'; +import { describe, expect, it } from 'vitest'; + +const css = readFileSync(resolve(import.meta.dirname, '../src/web/public/styles.css'), 'utf8'); + +/** The declarations of the first rule whose selector list contains `selector`. */ +function ruleBody(selector: string): string { + for (const match of css.matchAll(/([^{}]+)\{([^{}]*)\}/g)) { + const selectors = match[1] + .replace(/\/\*[\s\S]*?\*\//g, '') + .split(',') + .map((s) => s.trim()); + if (selectors.includes(selector)) return match[2].replace(/\/\*[\s\S]*?\*\//g, ''); + } + throw new Error(`no rule for ${selector}`); +} + +describe('rendered markdown table cells', () => { + it('still wraps long words in prose anywhere', () => { + expect(ruleBody('.rv-text')).toMatch(/overflow-wrap:\s*anywhere/); + }); + + it.each(['.rv-text td', '.rv-text th', '.rv-table-wrap td', '.response-viewer-body > table td'])( + '%s breaks a word only when the word overflows', + (selector) => { + expect(ruleBody(selector)).toMatch(/overflow-wrap:\s*break-word/); + } + ); +});