2026-11-30 13:01:47 +01:00
|
|
|
/**
|
2026-11-30 13:50:00 +01:00
|
|
|
* @fileoverview Table columns in rendered markdown never shrink below a readable width
|
2026-11-30 13:01:47 +01:00
|
|
|
* (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
|
2026-11-30 13:50:00 +01:00
|
|
|
* line. Cells now use `break-word`, so a column is never narrower than its longest word.
|
2026-11-30 13:01:47 +01:00
|
|
|
*
|
2026-11-30 15:37:05 +01:00
|
|
|
* Under `break-word`, a token with no break point sets its column's width. Chromium never
|
|
|
|
|
* breaks after `/`, so a URL, an identifier or a hash in backticks made the whole table
|
|
|
|
|
* scroll sideways on a phone. Inline code and links in a cell therefore break anywhere,
|
|
|
|
|
* like a linkified path (`a.rv-path`, `word-break: break-all`) always did. That lets their
|
|
|
|
|
* column shrink the same way, so a cell holding one gets a minimum width.
|
2026-11-30 13:50:00 +01:00
|
|
|
*
|
|
|
|
|
* jsdom has no layout, so this pins the declarations. Firefox and Chromium were measured
|
2026-11-30 15:37:05 +01:00
|
|
|
* when the change was made, in a 360 px column: URL, identifier and hash tables fit as they
|
|
|
|
|
* did on master, a path column went from 48 px to 80 px, and a `-v` column from 52 px to
|
|
|
|
|
* 80 px.
|
2026-11-30 13:01:47 +01:00
|
|
|
*/
|
|
|
|
|
import { readFileSync } from 'node:fs';
|
|
|
|
|
import { resolve } from 'node:path';
|
|
|
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
|
|
2026-11-30 13:50:00 +01:00
|
|
|
const publicCss = (name: string) =>
|
|
|
|
|
readFileSync(resolve(import.meta.dirname, '../src/web/public', name), 'utf8').replace(/\/\*[\s\S]*?\*\//g, '');
|
2026-11-30 13:01:47 +01:00
|
|
|
|
2026-11-30 15:37:05 +01:00
|
|
|
/** The selectors of a rule, split on the commas outside `:is()`, `:has()` and `:not()`. */
|
|
|
|
|
function selectorList(prelude: string): string[] {
|
|
|
|
|
const parts: string[] = [];
|
|
|
|
|
let depth = 0;
|
|
|
|
|
let start = 0;
|
|
|
|
|
for (let i = 0; i < prelude.length; i++) {
|
|
|
|
|
if (prelude[i] === '(') depth++;
|
|
|
|
|
else if (prelude[i] === ')') depth--;
|
|
|
|
|
else if (prelude[i] === ',' && depth === 0) {
|
|
|
|
|
parts.push(prelude.slice(start, i).trim());
|
|
|
|
|
start = i + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
parts.push(prelude.slice(start).trim());
|
|
|
|
|
return parts;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-30 13:50:00 +01:00
|
|
|
/** Every rule whose selector list contains `selector`, in cascade order across both sheets. */
|
|
|
|
|
function rulesFor(selector: string): string[] {
|
|
|
|
|
const bodies: string[] = [];
|
|
|
|
|
for (const css of [publicCss('styles.css'), publicCss('mobile.css')]) {
|
|
|
|
|
for (const match of css.matchAll(/([^{}]+)\{([^{}]*)\}/g)) {
|
2026-11-30 15:37:05 +01:00
|
|
|
if (selectorList(match[1]).includes(selector)) bodies.push(match[2]);
|
2026-11-30 13:50:00 +01:00
|
|
|
}
|
2026-11-30 13:01:47 +01:00
|
|
|
}
|
2026-11-30 13:50:00 +01:00
|
|
|
return bodies;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The value the last rule naming `selector` gives `property`, or undefined. */
|
|
|
|
|
function lastValue(selector: string, property: string): string | undefined {
|
|
|
|
|
let value: string | undefined;
|
|
|
|
|
for (const body of rulesFor(selector)) {
|
|
|
|
|
for (const decl of body.split(';')) {
|
|
|
|
|
const [name, ...rest] = decl.split(':');
|
|
|
|
|
if (name.trim() === property) value = rest.join(':').trim();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return value;
|
2026-11-30 13:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('rendered markdown table cells', () => {
|
|
|
|
|
it('still wraps long words in prose anywhere', () => {
|
2026-11-30 13:50:00 +01:00
|
|
|
expect(lastValue('.rv-text', 'overflow-wrap')).toBe('anywhere');
|
2026-11-30 13:01:47 +01:00
|
|
|
});
|
|
|
|
|
|
2026-11-30 13:50:00 +01:00
|
|
|
it.each(['.rv-text td', '.rv-text th', '.rv-table-wrap td', '.rv-table-wrap th'])(
|
|
|
|
|
'%s wraps between words, so its column is never narrower than its longest word',
|
2026-11-30 13:01:47 +01:00
|
|
|
(selector) => {
|
2026-11-30 13:50:00 +01:00
|
|
|
expect(lastValue(selector, 'overflow-wrap')).toBe('break-word');
|
2026-11-30 13:01:47 +01:00
|
|
|
}
|
|
|
|
|
);
|
2026-11-30 13:50:00 +01:00
|
|
|
|
2026-11-30 15:37:05 +01:00
|
|
|
it('breaks inline code and links in a cell anywhere, and gives their cell a minimum width', () => {
|
2026-11-30 13:50:00 +01:00
|
|
|
expect(lastValue('.rv-text a.rv-path', 'word-break')).toBe('break-all');
|
2026-11-30 15:37:05 +01:00
|
|
|
expect(lastValue('.rv-table-wrap td :is(code, a:not(.rv-path-link))', 'overflow-wrap')).toBe('anywhere');
|
|
|
|
|
expect(lastValue('.rv-table-wrap td:has(code, a:not(.rv-path-link))', 'min-width')).toBe('6em');
|
2026-11-30 13:50:00 +01:00
|
|
|
});
|
2026-11-30 13:01:47 +01:00
|
|
|
});
|