Skip to content

Allow inclusion of JavaScript and CSS file paths in the HtmlContentView #944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/Assets/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var h1 = document.createElement('h1');
h1.appendChild(document.createTextNode('Hello from JavaScript!'));
document.body.appendChild(h1);
4 changes: 4 additions & 0 deletions examples/Assets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
color: white;
background-color: cornflowerblue;
}
8 changes: 8 additions & 0 deletions examples/ContentViewTest.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$params = @{
HtmlBodyContent = "Testing JavaScript and CSS paths..."
JavaScriptPaths = ".\Assets\script.js"
StyleSheetPaths = ".\Assets\style.css"
}

$view = New-VSCodeHtmlContentView -Title "Test View" -ShowInColumn Two
Set-VSCodeHtmlContentView -View $view @params
58 changes: 50 additions & 8 deletions src/features/CustomViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class CustomViewsFeature implements IFeature {
args => {
this.contentProvider.setHtmlContentView(
args.id,
args.htmlBodyContent);
args.htmlContent);
});

languageClient.onRequest(
Expand Down Expand Up @@ -119,7 +119,7 @@ class PowerShellContentProvider implements vscode.TextDocumentContentProvider {
)
}

public setHtmlContentView(id: string, content: string) {
public setHtmlContentView(id: string, content: HtmlContent) {
let uriString = this.getUri(id);
let view: CustomView = this.viewIndex[uriString];

Expand Down Expand Up @@ -160,7 +160,11 @@ abstract class CustomView {

class HtmlContentView extends CustomView {

private htmlContent: string = "";
private htmlContent: HtmlContent = {
bodyContent: "",
javaScriptPaths: [],
styleSheetPaths: []
};

constructor(
id: string,
Expand All @@ -169,17 +173,49 @@ class HtmlContentView extends CustomView {
super(id, title, CustomViewType.HtmlContent);
}

setContent(htmlContent: string) {
setContent(htmlContent: HtmlContent) {
this.htmlContent = htmlContent;
}

appendContent(content: string) {
this.htmlContent += content;
this.htmlContent.bodyContent += content;
}

getContent(): string {
// Return an HTML page which disables JavaScript in content by default
return `<html><head><meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src *; style-src 'self'; script-src 'none';"></head><body>${this.htmlContent}</body></html>`;
var styleSrc = "none";
var styleTags = "";

function getNonce(): number {
return Math.floor(Math.random() * 100000) + 100000;
}

if (this.htmlContent.styleSheetPaths &&
this.htmlContent.styleSheetPaths.length > 0) {
styleSrc = "";
this.htmlContent.styleSheetPaths.forEach(
p => {
var nonce = getNonce();
styleSrc += `'nonce-${nonce}' `;
styleTags += `<link nonce="${nonce}" href="${p}" rel="stylesheet" type="text/css" />\n`;
});
}

var scriptSrc = "none";
var scriptTags = "";

if (this.htmlContent.javaScriptPaths &&
this.htmlContent.javaScriptPaths.length > 0) {
scriptSrc = "";
this.htmlContent.javaScriptPaths.forEach(
p => {
var nonce = getNonce();
scriptSrc += `'nonce-${nonce}' `;
scriptTags += `<script nonce="${nonce}" src="${p}"></script>\n`;
});
}

// Return an HTML page with the specified content
return `<html><head><meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src *; style-src ${styleSrc}; script-src ${scriptSrc};">${styleTags}</head><body>\n${this.htmlContent.bodyContent}\n${scriptTags}</body></html>`;
}
}

Expand Down Expand Up @@ -226,9 +262,15 @@ namespace SetHtmlContentViewRequest {
'powerShell/setHtmlViewContent');
}

interface HtmlContent {
bodyContent: string;
javaScriptPaths: string[];
styleSheetPaths: string[];
}

interface SetHtmlContentViewRequestArguments {
id: string;
htmlBodyContent: string;
htmlContent: HtmlContent;
}

namespace AppendHtmlOutputViewRequest {
Expand Down