From bb89815cfb253af45a9cab30b9da7b89efa67ff1 Mon Sep 17 00:00:00 2001 From: Jan Kretschmer Date: Wed, 24 Nov 2021 22:07:31 +0100 Subject: [PATCH] use regex, not Uri.parse, to detect custom scheme --- .../client/src/customData.ts | 13 +++++------ .../client/src/requests.ts | 22 ++++++++++--------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/extensions/html-language-features/client/src/customData.ts b/extensions/html-language-features/client/src/customData.ts index 248ad18672c..146fef2f3ee 100644 --- a/extensions/html-language-features/client/src/customData.ts +++ b/extensions/html-language-features/client/src/customData.ts @@ -4,7 +4,8 @@ *--------------------------------------------------------------------------------------------*/ import { workspace, extensions, Uri, EventEmitter, Disposable } from 'vscode'; -import { resolvePath, joinPath } from './requests'; +import { resolvePath, joinPath, uriScheme } from './requests'; + export function getCustomDataSource(toDispose: Disposable[]) { let pathsInWorkspace = getCustomDataPathsInAllWorkspaces(); @@ -57,8 +58,7 @@ function getCustomDataPathsInAllWorkspaces(): Set { if (Array.isArray(paths)) { for (const path of paths) { if (typeof path === 'string') { - const uri = Uri.parse(path); - if (uri.scheme === 'file') { + if (!uriScheme.test(path)) { // only resolve file paths relative to extension dataPaths.add(resolvePath(rootFolder, path).toString()); } else { @@ -94,12 +94,11 @@ function getCustomDataPathsFromAllExtensions(): Set { const customData = extension.packageJSON?.contributes?.html?.customData; if (Array.isArray(customData)) { for (const rp of customData) { - const uri = Uri.parse(rp); - if (uri.scheme === 'file') { - // only resolve file paths relative to extension + if (!uriScheme.test(rp)) { + // no schame -> resolve relative to extension dataPaths.add(joinPath(extension.extensionUri, rp).toString()); } else { - // others schemes + // actual schemes dataPaths.add(rp); } diff --git a/extensions/html-language-features/client/src/requests.ts b/extensions/html-language-features/client/src/requests.ts index e5bc857dabd..aa75e6b615f 100644 --- a/extensions/html-language-features/client/src/requests.ts +++ b/extensions/html-language-features/client/src/requests.ts @@ -7,6 +7,8 @@ import { Uri, workspace } from 'vscode'; import { RequestType, CommonLanguageClient } from 'vscode-languageclient'; import { Runtime } from './htmlClient'; +export const uriScheme = /^(?\w[\w\d+.-]*):/; + export namespace FsContentRequest { export const type: RequestType<{ uri: string; encoding?: string; }, string, any> = new RequestType('fs/content'); } @@ -20,34 +22,34 @@ export namespace FsReadDirRequest { export function serveFileSystemRequests(client: CommonLanguageClient, runtime: Runtime, subscriptions: { dispose(): any }[]) { subscriptions.push(client.onRequest(FsContentRequest.type, (param: { uri: string; encoding?: string; }) => { - const uri = Uri.parse(param.uri); - if (uri.scheme === 'file') { + const uri = param.uri.match(uriScheme); + if (uri?.groups?.scheme === 'file') { if (runtime.fs) { return runtime.fs.getContent(param.uri); } else { - return workspace.fs.readFile(uri).then(buffer => { + return workspace.fs.readFile(Uri.parse(param.uri)).then(buffer => { return new runtime.TextDecoder(param.encoding).decode(buffer); }); } } else { - return workspace.openTextDocument(uri).then(doc => { + return workspace.openTextDocument(Uri.parse(param.uri)).then(doc => { return doc.getText(); }); } })); subscriptions.push(client.onRequest(FsReadDirRequest.type, (uriString: string) => { - const uri = Uri.parse(uriString); - if (uri.scheme === 'file' && runtime.fs) { + const uri = uriString.match(uriScheme); + if (uri?.groups?.scheme === 'file' && runtime.fs) { return runtime.fs.readDirectory(uriString); } - return workspace.fs.readDirectory(uri); + return workspace.fs.readDirectory(Uri.parse(uriString)); })); subscriptions.push(client.onRequest(FsStatRequest.type, (uriString: string) => { - const uri = Uri.parse(uriString); - if (uri.scheme === 'file' && runtime.fs) { + const uri = uriString.match(uriScheme); + if (uri?.groups?.scheme === 'file' && runtime.fs) { return runtime.fs.stat(uriString); } - return workspace.fs.stat(uri); + return workspace.fs.stat(Uri.parse(uriString)); })); }