Fix webview font-family so it falls back to monospace for custom editor.fontFamily#326118
Open
Alexoswin wants to merge 1 commit into
Open
Fix webview font-family so it falls back to monospace for custom editor.fontFamily#326118Alexoswin wants to merge 1 commit into
Alexoswin wants to merge 1 commit into
Conversation
Webviews (e.g. the Markdown preview) receive editor.fontFamily verbatim as the --vscode-editor-font-family CSS variable, with no fallback. If the configured font is unavailable or omits a generic family, code blocks render in the browser default (often serif) instead of monospace, unlike the real editor which always appends a monospace fallback via getMassagedFontFamily(). Extract that fallback logic into an exported applyFontFamilyFallback() and reuse it when computing the webview theme's font family. Fixes microsoft#324256
Contributor
There was a problem hiding this comment.
Pull request overview
Aligns webview editor-font fallback behavior with Monaco, preventing serif rendering when custom fonts are unavailable.
Changes:
- Extracts reusable font-family fallback logic.
- Applies it to webview theme data.
- Adds unit coverage for fallback behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/vs/editor/common/config/fontInfo.ts |
Extracts applyFontFamilyFallback. |
src/vs/editor/test/common/config/fontInfo.test.ts |
Tests fallback and quoting behavior. |
src/vs/workbench/contrib/webview/browser/themeing.ts |
Applies fallback to webview editor fonts. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Reported in #324256: when
editor.fontFamilyis set to a custom font that either doesn't exist or doesn't itself include a genericmonospacefallback, code blocks and inline code in the Markdown preview render in the browser's default font (often serif) instead of falling back to a monospace font.Root cause
Every webview (including the Markdown preview) gets its
--vscode-editor-font-familyCSS variable fromWebviewThemeDataProvider.getWebviewThemeData()insrc/vs/workbench/contrib/webview/browser/themeing.ts. That code used the raweditor.fontFamilysetting verbatim:extensions/markdown-language-features/media/markdown.cssconsumes this variable with avar(--vscode-editor-font-family, <fallback list>), but that CSS fallback list only applies when the variable is unset — sincethemeing.tsalways sets it to some string, the CSS-level fallback never kicks in.The real Monaco text editor doesn't have this problem because
BareFontInfo.getMassagedFontFamily()(src/vs/editor/common/config/fontInfo.ts) always appends the platform's default font family (which itself ends in the genericmonospacekeyword) as a fallback whenever the configured font differs from the default.themeing.tsnever used this helper, which is the actual gap.Fix
BareFontInfo.getMassagedFontFamily()into a standalone exported function,applyFontFamilyFallback(), infontInfo.ts(no behavior change for existing callers —getMassagedFontFamily()is now a thin wrapper around it).themeing.tsnow callsapplyFontFamilyFallback()when computing the webview'seditorFontFamily, so--vscode-editor-font-familyalways carries a monospace fallback chain, matching the real editor's behavior.This fixes the Markdown preview specifically, and also fixes the same issue for any other webview that relies on
--vscode-editor-font-family.Testing performed
src/vs/editor/test/common/config/fontInfo.test.tscoveringapplyFontFamilyFallback(): custom font gets the fallback appended, a font with spaces gets quoted before the fallback is appended, an already-escaped font family isn't double-wrapped, and a font family equal to the platform default isn't duplicated.tsc -p src/tsconfig.json --noEmitpasses with no errors.eslintpasses on all changed files.'Droid Sans Mono', monospaceon Linux).Fixes #324256