Skip to content

Commit

Permalink
Add matchesSomeScheme helper
Browse files Browse the repository at this point in the history
Useful for checking against multiple allowed schemes
  • Loading branch information
mjbvz committed Nov 9, 2021
1 parent 2d271e5 commit 9b45517
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/vs/editor/browser/services/openerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/vs/editor/test/browser/services/openerService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down
6 changes: 5 additions & 1 deletion src/vs/platform/opener/common/opener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

0 comments on commit 9b45517

Please sign in to comment.