mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-12-05 01:29:32 +01:00
fix(web): a table column of linked paths keeps a readable width
The first commit left one case of the same squeeze. Codeman turns the paths in an answer into links, and a linked path keeps `word-break: break-all` so that it can wrap anywhere. That lets the browser count every letter as a break point when it sizes the column, so in a table whose headers fill the width, a column of paths shrank to three or four letters per line. A cell that holds a linked path now has a minimum width of 6em. Other cells keep their natural width, so a narrow column such as row numbers stays narrow. On a 390 px view in Firefox and Chromium, a path column went from 48 px to 83 px, and a `#` column kept its 32 px. The comment on the cell rule also said wrongly that a cell breaks a word wider than its column. In automatic table layout a column is never narrower than its longest word, so that never happens, and the comment now says that. The test reads every rule that names a selector, in styles.css and mobile.css, with comments removed first, so a later override fails it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2ea593c2e2
commit
5588a54e3f
2 files changed
+49
-24
No files matched your search
@@ -1,45 +1,64 @@
|
||||
/**
|
||||
* @fileoverview Table cells in rendered markdown break only words wider than their column
|
||||
* @fileoverview Table columns in rendered markdown never shrink below a readable width
|
||||
* (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.
|
||||
* line. Cells now use `break-word`, so a column is never narrower than its 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.
|
||||
* A linkified path (`a.rv-path`) keeps `word-break: break-all`, which has the same effect on
|
||||
* its column, so a cell that holds one gets a minimum width.
|
||||
*
|
||||
* jsdom has no layout, so this pins the declarations. Firefox and Chromium were measured
|
||||
* when the change was made, on a 390 px view: the label column went from one letter per
|
||||
* line to whole words, a path column from 48 px to 83 px, and a `#` column kept its 32 px.
|
||||
*/
|
||||
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');
|
||||
const publicCss = (name: string) =>
|
||||
readFileSync(resolve(import.meta.dirname, '../src/web/public', name), 'utf8').replace(/\/\*[\s\S]*?\*\//g, '');
|
||||
|
||||
/** 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, '');
|
||||
/** 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)) {
|
||||
if (match[1].split(',').some((s) => s.trim() === selector)) bodies.push(match[2]);
|
||||
}
|
||||
}
|
||||
throw new Error(`no rule for ${selector}`);
|
||||
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;
|
||||
}
|
||||
|
||||
describe('rendered markdown table cells', () => {
|
||||
it('still wraps long words in prose anywhere', () => {
|
||||
expect(ruleBody('.rv-text')).toMatch(/overflow-wrap:\s*anywhere/);
|
||||
expect(lastValue('.rv-text', 'overflow-wrap')).toBe('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',
|
||||
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',
|
||||
(selector) => {
|
||||
expect(ruleBody(selector)).toMatch(/overflow-wrap:\s*break-word/);
|
||||
expect(lastValue(selector, 'overflow-wrap')).toBe('break-word');
|
||||
}
|
||||
);
|
||||
|
||||
it('gives a cell holding a linked path a minimum width, since the path breaks anywhere', () => {
|
||||
expect(lastValue('.rv-text a.rv-path', 'word-break')).toBe('break-all');
|
||||
expect(lastValue('.rv-table-wrap td:has(a.rv-path:not(.rv-path-link))', 'min-width')).toBe('6em');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user