Skip to content

Commit 6e6ea20

Browse files
committed
[vscode] Add WebviewOptions.enableForms
Added the optional `enableForms` field to `WebviewOptions`. This allows the `allow-forms` sandbox attribute to be set. Contributed on behalf of STMicroelectronics
1 parent ac7a9ff commit 6e6ea20

File tree

6 files changed

+26
-5
lines changed

6 files changed

+26
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
## v1.33.0 - unreleased
88

99
- [scripts] integrated start-up performance scripts into nightly master build [#10463](https://github.com/eclipse-theia/theia/pull/10463) - Contributed on behalf of STMicroelectronics
10+
- [plugin] added `enableForms` field to `WebviewOptions` [#11983](https://github.com/eclipse-theia/theia/pull/11983) - Contributed on behalf of STMicroelectronics
1011

1112
<a name="breaking_changes_1.33.0">[Breaking Changes:](#breaking_changes_1.33.0)</a>
1213

packages/plugin-ext/src/main/browser/custom-editors/custom-editors-main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,12 @@ export class CustomEditorsMainImpl implements CustomEditorsMain, Disposable {
223223
const view = await this.widgetManager.getOrCreateWidget<CustomEditorWidget>(CustomEditorWidget.FACTORY_ID, <WebviewWidgetIdentifier>{ id: panelId });
224224
this.webviewsMain.hookWebview(view);
225225
view.title.label = title;
226-
const { enableFindWidget, retainContextWhenHidden, enableScripts, localResourceRoots, ...contentOptions } = options;
226+
const { enableFindWidget, retainContextWhenHidden, enableScripts, enableForms, localResourceRoots, ...contentOptions } = options;
227227
view.viewColumn = ViewColumn.One; // behaviour might be overridden later using widgetOpenerOptions (if available)
228228
view.options = { enableFindWidget, retainContextWhenHidden };
229229
view.setContentOptions({
230230
allowScripts: enableScripts,
231+
allowForms: enableForms,
231232
localResourceRoots: localResourceRoots && localResourceRoots.map(root => root.toString()),
232233
...contentOptions,
233234
...view.contentOptions

packages/plugin-ext/src/main/browser/webview/pre/main.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,14 @@
479479
const newFrame = document.createElement('iframe');
480480
newFrame.setAttribute('id', 'pending-frame');
481481
newFrame.setAttribute('frameborder', '0');
482-
newFrame.setAttribute('sandbox', options.allowScripts ? 'allow-scripts allow-forms allow-same-origin allow-downloads' : 'allow-same-origin');
482+
const sandboxOptions = ['allow-same-origin'];
483+
if (options.allowScripts) {
484+
sandboxOptions.push('allow-scripts', 'allow-downloads');
485+
}
486+
if (options.allowForms || (options.allowScripts && options.allowForms === undefined)) {
487+
sandboxOptions.push('allow-forms');
488+
}
489+
newFrame.setAttribute('sandbox', sandboxOptions.join(' '));
483490
if (host.fakeLoad) {
484491
// We should just be able to use srcdoc, but I wasn't
485492
// seeing the service worker applying properly.

packages/plugin-ext/src/main/browser/webview/webview.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export const enum WebviewMessageChannels {
7373

7474
export interface WebviewContentOptions {
7575
readonly allowScripts?: boolean;
76+
readonly allowForms?: boolean;
7677
readonly localResourceRoots?: ReadonlyArray<string>;
7778
readonly portMapping?: ReadonlyArray<WebviewPortMapping>;
7879
readonly enableCommandUris?: boolean;

packages/plugin-ext/src/main/browser/webviews-main.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ export class WebviewsMainImpl implements WebviewsMain, Disposable {
6767
this.hookWebview(view);
6868
view.viewType = viewType;
6969
view.title.label = title;
70-
const { enableFindWidget, retainContextWhenHidden, enableScripts, localResourceRoots, ...contentOptions } = options;
70+
const { enableFindWidget, retainContextWhenHidden, enableScripts, enableForms, localResourceRoots, ...contentOptions } = options;
7171
view.options = { enableFindWidget, retainContextWhenHidden };
7272
view.setContentOptions({
7373
allowScripts: enableScripts,
74+
allowForms: enableForms,
7475
localResourceRoots: localResourceRoots && localResourceRoots.map(root => root.toString()),
7576
...contentOptions
7677
});
@@ -171,9 +172,10 @@ export class WebviewsMainImpl implements WebviewsMain, Disposable {
171172

172173
async $setOptions(handle: string, options: WebviewOptions): Promise<void> {
173174
const webview = await this.getWebview(handle);
174-
const { enableScripts, localResourceRoots, ...contentOptions } = options;
175+
const { enableScripts, enableForms, localResourceRoots, ...contentOptions } = options;
175176
webview.setContentOptions({
176177
allowScripts: enableScripts,
178+
allowForms: enableForms,
177179
localResourceRoots: localResourceRoots && localResourceRoots.map(root => root.toString()),
178180
...contentOptions
179181
});
@@ -210,10 +212,11 @@ export class WebviewsMainImpl implements WebviewsMain, Disposable {
210212
}
211213

212214
const options = widget.options;
213-
const { allowScripts, localResourceRoots, ...contentOptions } = widget.contentOptions;
215+
const { allowScripts, allowForms, localResourceRoots, ...contentOptions } = widget.contentOptions;
214216
this.updateViewState(widget);
215217
await this.proxy.$deserializeWebviewPanel(handle, widget.viewType, title, state, widget.viewState, {
216218
enableScripts: allowScripts,
219+
enableForms: allowForms,
217220
localResourceRoots: localResourceRoots && localResourceRoots.map(root => URI.parse(root)),
218221
...contentOptions,
219222
...options

packages/plugin/src/theia.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3768,6 +3768,14 @@ export module '@theia/plugin' {
37683768
*/
37693769
readonly enableScripts?: boolean;
37703770

3771+
/**
3772+
* Controls whether forms are enabled in the webview content or not.
3773+
*
3774+
* Defaults to true if {@link WebviewOptions.enableScripts scripts are enabled}. Otherwise defaults to false.
3775+
* Explicitly setting this property to either true or false overrides the default.
3776+
*/
3777+
readonly enableForms?: boolean;
3778+
37713779
/**
37723780
* Controls whether command uris are enabled in webview content or not.
37733781
*

0 commit comments

Comments
 (0)