Add matchesSomeScheme helper

Useful for checking against multiple allowed schemes
This commit is contained in:
Matt Bierner 2021-11-08 17:25:33 -08:00
parent 2d271e5e73
commit 9b455176e9
No known key found for this signature in database
GPG key ID: 099C331567E11888
3 changed files with 15 additions and 5 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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));
}