From 9b455176e9a3b7364caeae9f8f466bf2ad0bbb14 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Mon, 8 Nov 2021 17:25:33 -0800 Subject: [PATCH] Add matchesSomeScheme helper Useful for checking against multiple allowed schemes --- src/vs/editor/browser/services/openerService.ts | 6 +++--- src/vs/editor/test/browser/services/openerService.test.ts | 8 +++++++- src/vs/platform/opener/common/opener.ts | 6 +++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/vs/editor/browser/services/openerService.ts b/src/vs/editor/browser/services/openerService.ts index 98f9ba4de1b65..ffb075f20b51d 100644 --- a/src/vs/editor/browser/services/openerService.ts +++ b/src/vs/editor/browser/services/openerService.ts @@ -15,7 +15,7 @@ import { URI } from 'vs/base/common/uri'; import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService'; import { ICommandService } from 'vs/platform/commands/common/commands'; import { EditorOpenContext } from 'vs/platform/editor/common/editor'; -import { IExternalOpener, IExternalUriResolver, IOpener, IOpenerService, IResolvedExternalUri, IValidator, matchesScheme, OpenOptions, ResolveExternalUriOptions } from 'vs/platform/opener/common/opener'; +import { IExternalOpener, IExternalUriResolver, IOpener, IOpenerService, IResolvedExternalUri, IValidator, matchesScheme, matchesSomeScheme, OpenOptions, ResolveExternalUriOptions } from 'vs/platform/opener/common/opener'; class CommandOpener implements IOpener { @@ -119,7 +119,7 @@ export class OpenerService implements IOpenerService { // to not trigger a navigation. Any other link is // safe to be set as HREF to prevent a blank window // from opening. - if (matchesScheme(href, Schemas.http) || matchesScheme(href, Schemas.https)) { + if (matchesSomeScheme(href, Schemas.http, Schemas.https)) { dom.windowOpenNoOpener(href); } else { window.location.href = href; @@ -131,7 +131,7 @@ export class OpenerService implements IOpenerService { // Default opener: any external, maito, http(s), command, and catch-all-editors this._openers.push({ open: async (target: URI | string, options?: OpenOptions) => { - if (options?.openExternal || matchesScheme(target, Schemas.mailto) || matchesScheme(target, Schemas.http) || matchesScheme(target, Schemas.https) || matchesScheme(target, Schemas.vsls)) { + if (options?.openExternal || matchesSomeScheme(target, Schemas.mailto, Schemas.http, Schemas.https, Schemas.vsls)) { // open externally await this._doOpenExternal(target, options); return true; diff --git a/src/vs/editor/test/browser/services/openerService.test.ts b/src/vs/editor/test/browser/services/openerService.test.ts index fe11e47450961..af745652c97a8 100644 --- a/src/vs/editor/test/browser/services/openerService.test.ts +++ b/src/vs/editor/test/browser/services/openerService.test.ts @@ -9,7 +9,7 @@ import { OpenerService } from 'vs/editor/browser/services/openerService'; import { TestCodeEditorService } from 'vs/editor/test/browser/editorTestServices'; import { CommandsRegistry, ICommandService, NullCommandService } from 'vs/platform/commands/common/commands'; import { ITextEditorOptions } from 'vs/platform/editor/common/editor'; -import { matchesScheme } from 'vs/platform/opener/common/opener'; +import { matchesScheme, matchesSomeScheme } from 'vs/platform/opener/common/opener'; suite('OpenerService', function () { const editorService = new TestCodeEditorService(); @@ -247,6 +247,12 @@ suite('OpenerService', function () { assert.ok(!matchesScheme(URI.parse('z://microsoft.com'), 'http')); }); + test('matchesSomeScheme', function () { + assert.ok(matchesSomeScheme('https://microsoft.com', 'http', 'https')); + assert.ok(matchesSomeScheme('http://microsoft.com', 'http', 'https')); + assert.ok(!matchesSomeScheme('x://microsoft.com', 'http', 'https')); + }); + test('resolveExternalUri', async function () { const openerService = new OpenerService(editorService, NullCommandService); diff --git a/src/vs/platform/opener/common/opener.ts b/src/vs/platform/opener/common/opener.ts index fad7d23c761e6..ea79903e8a959 100644 --- a/src/vs/platform/opener/common/opener.ts +++ b/src/vs/platform/opener/common/opener.ts @@ -125,10 +125,14 @@ export const NullOpenerService = Object.freeze({ async resolveExternalUri(uri: URI) { return { resolved: uri, dispose() { } }; }, } as IOpenerService); -export function matchesScheme(target: URI | string, scheme: string) { +export function matchesScheme(target: URI | string, scheme: string): boolean { if (URI.isUri(target)) { return equalsIgnoreCase(target.scheme, scheme); } else { return startsWithIgnoreCase(target, scheme + ':'); } } + +export function matchesSomeScheme(target: URI | string, ...schemes: string[]): boolean { + return schemes.some(scheme => matchesScheme(target, scheme)); +}