Skip to content

Commit 2a8ec9c

Browse files
fix(web): single-digit numbered lists no longer clip in wide fonts (pingdotgg#23573)
1 parent 4d1808d commit 2a8ec9c

3 files changed

Lines changed: 56 additions & 9 deletions

File tree

‎apps/web/src/appearanceFonts.browser.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,40 @@ describe("applyAppearanceFontVariables", () => {
5252
else root.setAttribute("style", style);
5353
}
5454
});
55+
56+
it("widens the numbered-list gutter only when the interface font's marker would clip", () => {
57+
const root = document.documentElement;
58+
const style = root.getAttribute("style");
59+
const gutter = (sans: string) => {
60+
applyAppearanceFontVariables(root, {
61+
sans,
62+
code: "",
63+
composer: "",
64+
sizeInterface: 16,
65+
sizePrompt: 14,
66+
sizeCode: 13,
67+
smoothing: true,
68+
separate: false,
69+
});
70+
const markdown = document.createElement("div");
71+
markdown.className = "chat-markdown";
72+
markdown.style.fontSize = "14px";
73+
const list = document.createElement("ol");
74+
markdown.append(list);
75+
document.body.append(markdown);
76+
try {
77+
return Number.parseFloat(getComputedStyle(list).paddingInlineStart);
78+
} finally {
79+
markdown.remove();
80+
}
81+
};
82+
try {
83+
// `0. ` in a 14px monospace font is about 25px, past the 20px default.
84+
expect(gutter("monospace")).toBeGreaterThan(24);
85+
expect(gutter("sans-serif")).toBe(20);
86+
} finally {
87+
if (style === null) root.removeAttribute("style");
88+
else root.setAttribute("style", style);
89+
}
90+
});
5591
});

‎apps/web/src/appearanceFonts.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ export function applyAppearanceFontVariables(
8888
}
8989
}
9090

91+
const markerWidth = probeWidth(
92+
appearanceFontStack(preferences.sans, DEFAULT_SANS_FONT_STACK),
93+
LIST_MARKER_PROBE_TEXT,
94+
);
95+
if (markerWidth === null) {
96+
root.style.removeProperty("--list-marker-sans");
97+
} else {
98+
root.style.setProperty("--list-marker-sans", `${(markerWidth / PROBE_FONT_SIZE).toFixed(3)}em`);
99+
}
100+
91101
root.style.fontSize = `${clampInterfaceFontSize(preferences.sizeInterface)}px`;
92102
root.style.setProperty(
93103
"--font-size-prompt",
@@ -127,15 +137,18 @@ function clampCodeFontSize(value: number): number {
127137
}
128138

129139
const FONT_PROBE_TEXT = "mmmmmmmmMMWli1O0@# fjord";
140+
// The widest single-digit `ol` marker; CSS can't measure it to size the list gutter.
141+
const LIST_MARKER_PROBE_TEXT = "0. ";
142+
const PROBE_FONT_SIZE = 16;
130143
let fontProbeContext: CanvasRenderingContext2D | null | undefined;
131144

132-
function probeWidth(fontList: string): number | null {
145+
function probeWidth(fontList: string, text = FONT_PROBE_TEXT): number | null {
133146
if (fontProbeContext === undefined) {
134147
fontProbeContext = document.createElement("canvas").getContext("2d");
135148
}
136149
if (fontProbeContext === null) return null;
137-
fontProbeContext.font = `16px ${fontList}`;
138-
return fontProbeContext.measureText(FONT_PROBE_TEXT).width;
150+
fontProbeContext.font = `${PROBE_FONT_SIZE}px ${fontList}`;
151+
return fontProbeContext.measureText(text).width;
139152
}
140153

141154
/** Probes canvas metrics because document.fonts.check() accepts uninstalled families. */

‎apps/web/src/index.css‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,13 +1933,11 @@ code {
19331933
list-style-type: disc;
19341934
}
19351935

1936-
/* --list-gutter defaults to the same 1.25rem as .chat-markdown ul, but
1937-
ChatMarkdown's `ol` renderer widens it (via inline style) for lists whose
1938-
widest marker has multiple characters so it isn't clipped by list-style-position:
1939-
outside painting the marker past the padding box. Reset it here too so a
1940-
nested ol without its own widened marker doesn't inherit the outer one. */
1936+
/* Outside markers paint past the padding box, so the gutter must fit `0. `,
1937+
measured per font in appearanceFonts. ChatMarkdown widens it inline for
1938+
multi-digit markers; setting it here stops a nested ol inheriting that. */
19411939
:is(.chat-markdown, .composer-tiptap) ol {
1942-
--list-gutter: 1.25rem;
1940+
--list-gutter: max(1.25rem, var(--list-marker-sans, 0px));
19431941
padding-inline-start: var(--list-gutter, 1.25rem);
19441942
list-style-type: decimal;
19451943
}

0 commit comments

Comments
 (0)