forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Render html as plaintext when html not supported (microsoft#213265)
* Support rendering unsupported html tags as plaintext for chat * Render html as plaintext when html not supported * Add comment and test for trusted domains
- Loading branch information
1 parent
9c6dae2
commit 13f374a
Showing
14 changed files
with
280 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/vs/workbench/contrib/chat/browser/chatMarkdownRenderer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { MarkdownRenderOptions, MarkedOptions } from 'vs/base/browser/markdownRenderer'; | ||
import { IMarkdownString } from 'vs/base/common/htmlContent'; | ||
import { IMarkdownRendererOptions, IMarkdownRenderResult, MarkdownRenderer } from 'vs/editor/browser/widget/markdownRenderer/browser/markdownRenderer'; | ||
import { ILanguageService } from 'vs/editor/common/languages/language'; | ||
import { IOpenerService } from 'vs/platform/opener/common/opener'; | ||
import { ITrustedDomainService } from 'vs/workbench/contrib/url/browser/trustedDomainService'; | ||
|
||
const allowedHtmlTags = [ | ||
'b', | ||
'blockquote', | ||
'br', | ||
'code', | ||
'em', | ||
'h1', | ||
'h2', | ||
'h3', | ||
'h4', | ||
'h5', | ||
'h6', | ||
'hr', | ||
'i', | ||
'li', | ||
'ol', | ||
'p', | ||
'pre', | ||
'strong', | ||
'table', | ||
'tbody', | ||
'td', | ||
'th', | ||
'thead', | ||
'tr', | ||
'ul', | ||
'a', | ||
'img', | ||
|
||
// Not in the official list, but used for codicons and other vscode markdown extensions | ||
'span', | ||
]; | ||
|
||
/** | ||
* This wraps the MarkdownRenderer and applies sanitizer options needed for Chat. | ||
*/ | ||
export class ChatMarkdownRenderer extends MarkdownRenderer { | ||
constructor( | ||
options: IMarkdownRendererOptions | undefined, | ||
@ILanguageService languageService: ILanguageService, | ||
@IOpenerService openerService: IOpenerService, | ||
@ITrustedDomainService private readonly trustedDomainService: ITrustedDomainService, | ||
) { | ||
super(options ?? {}, languageService, openerService); | ||
} | ||
|
||
override render(markdown: IMarkdownString | undefined, options?: MarkdownRenderOptions, markedOptions?: MarkedOptions): IMarkdownRenderResult { | ||
options = { | ||
...options, | ||
remoteImageIsAllowed: (uri) => this.trustedDomainService.isValid(uri), | ||
sanitizerOptions: { | ||
replaceWithPlaintext: true, | ||
allowedTags: allowedHtmlTags, | ||
} | ||
}; | ||
|
||
const mdWithBody: IMarkdownString | undefined = (markdown && markdown.supportHtml) ? | ||
{ | ||
...markdown, | ||
|
||
// dompurify uses DOMParser, which strips leading comments. Wrapping it all in 'body' prevents this. | ||
value: `<body>${markdown.value}</body>`, | ||
} | ||
: markdown; | ||
return super.render(mdWithBody, options, markedOptions); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/vs/workbench/contrib/chat/test/browser/__snapshots__/ChatMarkdownRenderer_CDATA.0.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div class="rendered-markdown"><!--[CDATA[<div-->content]]></div> |
1 change: 1 addition & 0 deletions
1
...rkbench/contrib/chat/test/browser/__snapshots__/ChatMarkdownRenderer_html_comments.0.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div class="rendered-markdown"><!-- comment1 <div></div> --><div>content</div><!-- comment2 --></div> |
1 change: 1 addition & 0 deletions
1
...orkbench/contrib/chat/test/browser/__snapshots__/ChatMarkdownRenderer_invalid_HTML.0.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div class="rendered-markdown">1<canvas>2<div>3</div></canvas>4</div> |
1 change: 1 addition & 0 deletions
1
.../chat/test/browser/__snapshots__/ChatMarkdownRenderer_invalid_HTML_with_attributes.0.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div class="rendered-markdown">1<div id="id1" style="display: none">2<div id="my id 2">3</div></div>4</div> |
8 changes: 8 additions & 0 deletions
8
.../chat/test/browser/__snapshots__/ChatMarkdownRenderer_mixed_valid_and_invalid_HTML.0.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<div class="rendered-markdown"><h1>heading</h1> | ||
<div> | ||
<ul> | ||
<li><span><div><i>1</i></div></span></li> | ||
<li><b>hi</b></li> | ||
</ul> | ||
</div> | ||
<pre><canvas>canvas here</canvas></pre><details></details></div> |
1 change: 1 addition & 0 deletions
1
...rkbench/contrib/chat/test/browser/__snapshots__/ChatMarkdownRenderer_remote_images.0.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div class="rendered-markdown"><img src="http://allowed.com/image.jpg"> <div><img src="http://disallowed.com/image.jpg"></div></div> |
1 change: 1 addition & 0 deletions
1
...contrib/chat/test/browser/__snapshots__/ChatMarkdownRenderer_self-closing_elements.0.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div class="rendered-markdown"><area><hr><br><input type="text" value="test"></div> |
1 change: 1 addition & 0 deletions
1
src/vs/workbench/contrib/chat/test/browser/__snapshots__/ChatMarkdownRenderer_simple.0.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a |
6 changes: 6 additions & 0 deletions
6
.../workbench/contrib/chat/test/browser/__snapshots__/ChatMarkdownRenderer_valid_HTML.0.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<div class="rendered-markdown"><h1>heading</h1> | ||
<ul> | ||
<li>1</li> | ||
<li><b>hi</b></li> | ||
</ul> | ||
<pre><code>code here</code></pre></div> |
Oops, something went wrong.