Skip to content
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

[vscode] Add WebviewOptions.enableForms #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## v1.33.0 - unreleased

- [scripts] integrated start-up performance scripts into nightly master build [#10463](https://github.com/eclipse-theia/theia/pull/10463) - Contributed on behalf of STMicroelectronics
- [plugin] added `enableForms` field to `WebviewOptions` [#11983](https://github.com/eclipse-theia/theia/pull/11983) - Contributed on behalf of STMicroelectronics
- [vscode] stub 'notebook' VS Code APIs [#11993](https://github.com/eclipse-theia/theia/pull/11993)- Contributed on behalf of STMicroelectronics

<a name="breaking_changes_1.33.0">[Breaking Changes:](#breaking_changes_1.33.0)</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,12 @@ export class CustomEditorsMainImpl implements CustomEditorsMain, Disposable {
const view = await this.widgetManager.getOrCreateWidget<CustomEditorWidget>(CustomEditorWidget.FACTORY_ID, <WebviewWidgetIdentifier>{ id: panelId });
this.webviewsMain.hookWebview(view);
view.title.label = title;
const { enableFindWidget, retainContextWhenHidden, enableScripts, localResourceRoots, ...contentOptions } = options;
const { enableFindWidget, retainContextWhenHidden, enableScripts, enableForms, localResourceRoots, ...contentOptions } = options;
view.viewColumn = ViewColumn.One; // behaviour might be overridden later using widgetOpenerOptions (if available)
view.options = { enableFindWidget, retainContextWhenHidden };
view.setContentOptions({
allowScripts: enableScripts,
allowForms: enableForms,
localResourceRoots: localResourceRoots && localResourceRoots.map(root => root.toString()),
...contentOptions,
...view.contentOptions
Expand Down
9 changes: 8 additions & 1 deletion packages/plugin-ext/src/main/browser/webview/pre/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,14 @@
const newFrame = document.createElement('iframe');
newFrame.setAttribute('id', 'pending-frame');
newFrame.setAttribute('frameborder', '0');
newFrame.setAttribute('sandbox', options.allowScripts ? 'allow-scripts allow-forms allow-same-origin allow-downloads' : 'allow-same-origin');
const sandboxOptions = ['allow-same-origin'];
if (options.allowScripts) {
sandboxOptions.push('allow-scripts', 'allow-downloads');
}
if (options.allowForms ?? options.allowScripts) {
sandboxOptions.push('allow-forms');
}
newFrame.setAttribute('sandbox', sandboxOptions.join(' '));
if (host.fakeLoad) {
// We should just be able to use srcdoc, but I wasn't
// seeing the service worker applying properly.
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-ext/src/main/browser/webview/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const enum WebviewMessageChannels {

export interface WebviewContentOptions {
readonly allowScripts?: boolean;
readonly allowForms?: boolean;
readonly localResourceRoots?: ReadonlyArray<string>;
readonly portMapping?: ReadonlyArray<WebviewPortMapping>;
readonly enableCommandUris?: boolean;
Expand Down
9 changes: 6 additions & 3 deletions packages/plugin-ext/src/main/browser/webviews-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ export class WebviewsMainImpl implements WebviewsMain, Disposable {
this.hookWebview(view);
view.viewType = viewType;
view.title.label = title;
const { enableFindWidget, retainContextWhenHidden, enableScripts, localResourceRoots, ...contentOptions } = options;
const { enableFindWidget, retainContextWhenHidden, enableScripts, enableForms, localResourceRoots, ...contentOptions } = options;
view.options = { enableFindWidget, retainContextWhenHidden };
view.setContentOptions({
allowScripts: enableScripts,
allowForms: enableForms,
localResourceRoots: localResourceRoots && localResourceRoots.map(root => root.toString()),
...contentOptions
});
Expand Down Expand Up @@ -171,9 +172,10 @@ export class WebviewsMainImpl implements WebviewsMain, Disposable {

async $setOptions(handle: string, options: WebviewOptions): Promise<void> {
const webview = await this.getWebview(handle);
const { enableScripts, localResourceRoots, ...contentOptions } = options;
const { enableScripts, enableForms, localResourceRoots, ...contentOptions } = options;
webview.setContentOptions({
allowScripts: enableScripts,
allowForms: enableForms,
localResourceRoots: localResourceRoots && localResourceRoots.map(root => root.toString()),
...contentOptions
});
Expand Down Expand Up @@ -210,10 +212,11 @@ export class WebviewsMainImpl implements WebviewsMain, Disposable {
}

const options = widget.options;
const { allowScripts, localResourceRoots, ...contentOptions } = widget.contentOptions;
const { allowScripts, allowForms, localResourceRoots, ...contentOptions } = widget.contentOptions;
this.updateViewState(widget);
await this.proxy.$deserializeWebviewPanel(handle, widget.viewType, title, state, widget.viewState, {
enableScripts: allowScripts,
enableForms: allowForms,
localResourceRoots: localResourceRoots && localResourceRoots.map(root => URI.parse(root)),
...contentOptions,
...options
Expand Down
8 changes: 8 additions & 0 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3768,6 +3768,14 @@ export module '@theia/plugin' {
*/
readonly enableScripts?: boolean;

/**
* Controls whether forms are enabled in the webview content or not.
*
* Defaults to true if {@link WebviewOptions.enableScripts scripts are enabled}. Otherwise defaults to false.
* Explicitly setting this property to either true or false overrides the default.
*/
readonly enableForms?: boolean;

/**
* Controls whether command uris are enabled in webview content or not.
*
Expand Down