mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-25 02:58:56 +01:00
Move html rewriting for old webviews to (#163367)
The `asWebviewUri` methods was introduced in VS Code 1.38. It's silly that we still force every single webview to pay the cost of trying to rewrite the old style uris we supported in very old versions of VS Code Instead I've moved this logic into the extension host and disabled it for all extensions that target VS Code 1.60+ or newer. This means it never applies to internal webviews, notebooks, webview views, or custom editors (these public apis were all introduced after the switch to `asWebviewUri`)
This commit is contained in:
@@ -33,7 +33,8 @@ export class ExtHostWebview implements vscode.Webview {
|
||||
#isDisposed: boolean = false;
|
||||
#hasCalledAsWebviewUri = false;
|
||||
|
||||
#serializeBuffersForPostMessage = false;
|
||||
#serializeBuffersForPostMessage: boolean;
|
||||
#shouldRewriteOldResourceUris: boolean;
|
||||
|
||||
constructor(
|
||||
handle: extHostProtocol.WebviewHandle,
|
||||
@@ -51,6 +52,7 @@ export class ExtHostWebview implements vscode.Webview {
|
||||
this.#workspace = workspace;
|
||||
this.#extension = extension;
|
||||
this.#serializeBuffersForPostMessage = shouldSerializeBuffersForPostMessage(extension);
|
||||
this.#shouldRewriteOldResourceUris = shouldTryRewritingOldResourceUris(extension);
|
||||
this.#deprecationService = deprecationService;
|
||||
}
|
||||
|
||||
@@ -98,12 +100,12 @@ export class ExtHostWebview implements vscode.Webview {
|
||||
this.assertNotDisposed();
|
||||
if (this.#html !== value) {
|
||||
this.#html = value;
|
||||
if (!this.#hasCalledAsWebviewUri && /(["'])vscode-resource:([^\s'"]+?)(["'])/i.test(value)) {
|
||||
if (this.#shouldRewriteOldResourceUris && !this.#hasCalledAsWebviewUri && /(["'])vscode-resource:([^\s'"]+?)(["'])/i.test(value)) {
|
||||
this.#hasCalledAsWebviewUri = true;
|
||||
this.#deprecationService.report('Webview vscode-resource: uris', this.#extension,
|
||||
`Please migrate to use the 'webview.asWebviewUri' api instead: https://aka.ms/vscode-webview-use-aswebviewuri`);
|
||||
}
|
||||
this.#proxy.$setHtml(this.#handle, value);
|
||||
this.#proxy.$setHtml(this.#handle, this.rewriteOldResourceUrlsIfNeeded(value));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,6 +133,32 @@ export class ExtHostWebview implements vscode.Webview {
|
||||
throw new Error('Webview is disposed');
|
||||
}
|
||||
}
|
||||
|
||||
private rewriteOldResourceUrlsIfNeeded(value: string): string {
|
||||
if (!this.#shouldRewriteOldResourceUris) {
|
||||
return value;
|
||||
}
|
||||
|
||||
const isRemote = this.#extension.extensionLocation?.scheme === Schemas.vscodeRemote;
|
||||
const remoteAuthority = this.#extension.extensionLocation.scheme === Schemas.vscodeRemote ? this.#extension.extensionLocation.authority : undefined;
|
||||
return value
|
||||
.replace(/(["'])(?:vscode-resource):(\/\/([^\s\/'"]+?)(?=\/))?([^\s'"]+?)(["'])/gi, (_match, startQuote, _1, scheme, path, endQuote) => {
|
||||
const uri = URI.from({
|
||||
scheme: scheme || 'file',
|
||||
path: decodeURIComponent(path),
|
||||
});
|
||||
const webviewUri = asWebviewUri(uri, { isRemote, authority: remoteAuthority }).toString();
|
||||
return `${startQuote}${webviewUri}${endQuote}`;
|
||||
})
|
||||
.replace(/(["'])(?:vscode-webview-resource):(\/\/[^\s\/'"]+\/([^\s\/'"]+?)(?=\/))?([^\s'"]+?)(["'])/gi, (_match, startQuote, _1, scheme, path, endQuote) => {
|
||||
const uri = URI.from({
|
||||
scheme: scheme || 'file',
|
||||
path: decodeURIComponent(path),
|
||||
});
|
||||
const webviewUri = asWebviewUri(uri, { isRemote, authority: remoteAuthority }).toString();
|
||||
return `${startQuote}${webviewUri}${endQuote}`;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function shouldSerializeBuffersForPostMessage(extension: IExtensionDescription): boolean {
|
||||
@@ -142,6 +170,19 @@ export function shouldSerializeBuffersForPostMessage(extension: IExtensionDescri
|
||||
}
|
||||
}
|
||||
|
||||
function shouldTryRewritingOldResourceUris(extension: IExtensionDescription): boolean {
|
||||
try {
|
||||
const version = normalizeVersion(parseVersion(extension.engines.vscode));
|
||||
if (!version) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return version.majorBase < 1 || (version.majorBase === 1 && version.minorBase < 60);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export class ExtHostWebviews implements extHostProtocol.ExtHostWebviewsShape {
|
||||
|
||||
private readonly _webviewProxy: extHostProtocol.MainThreadWebviewsShape;
|
||||
|
||||
Reference in New Issue
Block a user