Skip to content

Commit

Permalink
Fixes #503: Can we allow isTrusted configuration for MarkdownString?
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaeumer authored Oct 7, 2020
1 parent f933432 commit 9c9d6ec
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"kind": "build",
"isDefault": true
},
"runOptions": {
"runOn": "folderOpen"
},
"isBackground": true,
"presentation": {
"panel": "dedicated",
Expand Down
4 changes: 2 additions & 2 deletions client-node-tests/src/converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as vscode from 'vscode';
import { CompletionItemTag, SymbolTag } from 'vscode-languageserver-protocol';

const c2p: codeConverter.Converter = codeConverter.createConverter();
const p2c: protocolConverter.Converter = protocolConverter.createConverter();
const p2c: protocolConverter.Converter = protocolConverter.createConverter(undefined, false);

interface InsertReplaceRange {
inserting: vscode.Range;
Expand Down Expand Up @@ -919,7 +919,7 @@ suite('Protocol Converter', () => {
test('Uri Rewrite', () => {
let converter = protocolConverter.createConverter((value: string) => {
return vscode.Uri.parse(`${value}.vscode`);
});
}, false);

let result = converter.asUri('file://localhost/folder/file');
strictEqual('file://localhost/folder/file.vscode', result.toString());
Expand Down
16 changes: 14 additions & 2 deletions client/src/common/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@ export interface LanguageClientOptions {
};
workspaceFolder?: VWorkspaceFolder;
connectionOptions?: ConnectionOptions;
markdown?: {
isTrusted?: boolean;
}
}

interface ResolvedClientOptions {
Expand All @@ -537,6 +540,9 @@ interface ResolvedClientOptions {
};
workspaceFolder?: VWorkspaceFolder;
connectionOptions?: ConnectionOptions;
markdown: {
isTrusted: boolean;
}
}

export enum State {
Expand Down Expand Up @@ -2616,6 +2622,11 @@ export abstract class BaseLanguageClient {

clientOptions = clientOptions || {};

const markdown = { isTrusted: false };
if (clientOptions.markdown !== undefined && clientOptions.markdown.isTrusted === true) {
markdown.isTrusted = true;
}

this._clientOptions = {
documentSelector: clientOptions.documentSelector || [],
synchronize: clientOptions.synchronize || {},
Expand All @@ -2630,7 +2641,8 @@ export abstract class BaseLanguageClient {
middleware: clientOptions.middleware || {},
uriConverters: clientOptions.uriConverters,
workspaceFolder: clientOptions.workspaceFolder,
connectionOptions: clientOptions.connectionOptions
connectionOptions: clientOptions.connectionOptions,
markdown
};
this._clientOptions.synchronize = this._clientOptions.synchronize || {};

Expand Down Expand Up @@ -2669,7 +2681,7 @@ export abstract class BaseLanguageClient {
},
};
this._c2p = c2p.createConverter(clientOptions.uriConverters ? clientOptions.uriConverters.code2Protocol : undefined);
this._p2c = p2c.createConverter(clientOptions.uriConverters ? clientOptions.uriConverters.protocol2Code : undefined);
this._p2c = p2c.createConverter(clientOptions.uriConverters ? clientOptions.uriConverters.protocol2Code : undefined, this._clientOptions.markdown.isTrusted);
this._syncedDocuments = new Map<string, TextDocument>();
this.registerBuiltinFeatures();
}
Expand Down
26 changes: 18 additions & 8 deletions client/src/common/protocolConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ namespace CodeBlock {
}
}

export function createConverter(uriConverter?: URIConverter): Converter {
export function createConverter(uriConverter: URIConverter | undefined, trustMarkdown: boolean | undefined): Converter {

trustMarkdown = !trustMarkdown;

const nullConverter = (value: string) => code.Uri.parse(value);

Expand Down Expand Up @@ -348,14 +350,14 @@ export function createConverter(uriConverter?: URIConverter): Converter {

function asHoverContent(value: ls.MarkedString | ls.MarkedString[] | ls.MarkupContent): code.MarkdownString | code.MarkdownString[] {
if (Is.string(value)) {
return new code.MarkdownString(value);
return asMarkdownString(value);
} else if (CodeBlock.is(value)) {
let result = new code.MarkdownString();
let result = asMarkdownString();
return result.appendCodeblock(value.value, value.language);
} else if (Array.isArray(value)) {
let result: code.MarkdownString[] = [];
for (let element of value) {
let item = new code.MarkdownString();
let item = asMarkdownString();
if (CodeBlock.is(element)) {
item.appendCodeblock(element.value, element.language);
} else {
Expand All @@ -368,13 +370,13 @@ export function createConverter(uriConverter?: URIConverter): Converter {
let result: code.MarkdownString;
switch (value.kind) {
case ls.MarkupKind.Markdown:
return new code.MarkdownString(value.value);
return asMarkdownString(value.value);
case ls.MarkupKind.PlainText:
result = new code.MarkdownString();
result = asMarkdownString();
result.appendText(value.value);
return result;
default:
result = new code.MarkdownString();
result = asMarkdownString();
result.appendText(`Unsupported Markup content received. Kind is: ${value.kind}`);
return result;
}
Expand All @@ -387,7 +389,7 @@ export function createConverter(uriConverter?: URIConverter): Converter {
} else {
switch (value.kind) {
case ls.MarkupKind.Markdown:
return new code.MarkdownString(value.value);
return asMarkdownString(value.value);
case ls.MarkupKind.PlainText:
return value.value;
default:
Expand All @@ -396,6 +398,14 @@ export function createConverter(uriConverter?: URIConverter): Converter {
}
}

function asMarkdownString(value?: string): code.MarkdownString {
const result = new code.MarkdownString(value);
if (trustMarkdown === true) {
result.isTrusted = trustMarkdown;
}
return result;
}

function asHover(hover: ls.Hover): code.Hover;
function asHover(hover: undefined | null): undefined;
function asHover(hover: ls.Hover | undefined | null): code.Hover | undefined;
Expand Down

0 comments on commit 9c9d6ec

Please sign in to comment.