Skip to content

Commit 97f3d5b

Browse files
authored
Use adopted stylesheets for notebook codeblock theming (microsoft#185616)
Fixes microsoft#185251
1 parent b9048ee commit 97f3d5b

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/vs/workbench/contrib/notebook/browser/view/renderers/backLayerWebView.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,10 @@ export class BackLayerWebView<T extends ICommonCellInfo> extends Themable {
285285
outputWordWrap: this.options.outputWordWrap
286286
};
287287
const preloadScript = preloadsScriptStr(
288-
this.options,
288+
{
289+
...this.options,
290+
tokenizationCss: getTokenizationCss(),
291+
},
289292
{ dragAndDropEnabled: this.options.dragAndDropEnabled },
290293
renderOptions,
291294
renderersData,
@@ -447,7 +450,6 @@ export class BackLayerWebView<T extends ICommonCellInfo> extends Themable {
447450
background-color: var(--vscode-editor-findMatchBackground);
448451
}
449452
</style>
450-
<style id="vscode-tokenization-styles" nonce="${this.nonce}">${getTokenizationCss()}</style>
451453
</head>
452454
<body style="overflow: hidden;">
453455
<div id='findStart' tabIndex=-1></div>

src/vs/workbench/contrib/notebook/browser/view/renderers/webviewPreloads.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ interface EmitterLike<T> {
5858
interface PreloadStyles {
5959
readonly outputNodePadding: number;
6060
readonly outputNodeLeftPadding: number;
61+
readonly tokenizationCss: string;
6162
}
6263

6364
export interface PreloadOptions {
@@ -98,7 +99,9 @@ async function webviewPreloads(ctx: PreloadContext) {
9899
const vscode = acquireVsCodeApi();
99100
delete (globalThis as any).acquireVsCodeApi;
100101

101-
const tokenizationStyleElement = document.querySelector('style#vscode-tokenization-styles');
102+
const tokenizationStyle = new CSSStyleSheet();
103+
tokenizationStyle.replaceSync(ctx.style.tokenizationCss);
104+
102105
const runWhenIdle: (callback: (idle: IdleDeadline) => void, timeout?: number) => IDisposable = (typeof requestIdleCallback !== 'function' || typeof cancelIdleCallback !== 'function')
103106
? (runner) => {
104107
setTimeout(() => {
@@ -1441,9 +1444,7 @@ async function webviewPreloads(ctx: PreloadContext) {
14411444
break;
14421445
}
14431446
case 'tokenizedStylesChanged': {
1444-
if (tokenizationStyleElement) {
1445-
tokenizationStyleElement.textContent = event.data.css;
1446-
}
1447+
tokenizationStyle.replaceSync(event.data.css);
14471448
break;
14481449
}
14491450
case 'find': {
@@ -2059,8 +2060,11 @@ async function webviewPreloads(ctx: PreloadContext) {
20592060
}
20602061
const trustedHtml = ttPolicy?.createHTML(html) ?? html;
20612062
el.innerHTML = trustedHtml as string;
2062-
if (tokenizationStyleElement) {
2063-
el.insertAdjacentElement('beforebegin', tokenizationStyleElement.cloneNode(true) as HTMLElement);
2063+
const root = el.getRootNode();
2064+
if (root instanceof ShadowRoot) {
2065+
if (!root.adoptedStyleSheets.includes(tokenizationStyle)) {
2066+
root.adoptedStyleSheets.push(tokenizationStyle);
2067+
}
20642068
}
20652069
}
20662070

0 commit comments

Comments
 (0)