iframe.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { Widget } from '@lumino/widgets';
  4. /**
  5. * A phosphor widget which wraps an IFrame.
  6. */
  7. export class IFrame extends Widget {
  8. /**
  9. * Create a new IFrame widget.
  10. */
  11. constructor(options: IFrame.IOptions = {}) {
  12. super({ node: Private.createNode() });
  13. this.addClass('jp-IFrame');
  14. this.sandbox = options.sandbox || [];
  15. this.referrerPolicy = options.referrerPolicy || 'no-referrer';
  16. }
  17. /**
  18. * Referrer policy for the iframe.
  19. *
  20. * #### Notes
  21. * By default, `no-referrer` is chosen.
  22. *
  23. * For more information, see
  24. * https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/referrerPolicy
  25. */
  26. get referrerPolicy(): IFrame.ReferrerPolicy {
  27. return this._referrerPolicy;
  28. }
  29. set referrerPolicy(value: IFrame.ReferrerPolicy) {
  30. if (this._referrerPolicy === value) {
  31. return;
  32. }
  33. this._referrerPolicy = value;
  34. const iframe = this.node.querySelector('iframe')!;
  35. iframe.setAttribute('referrerpolicy', value);
  36. }
  37. /**
  38. * Exceptions to the sandboxing.
  39. *
  40. * #### Notes
  41. * By default, all sandboxing security policies are enabled.
  42. * This setting allows the user to selectively disable these
  43. * policies. This should be done with care, as it can
  44. * introduce security risks, and possibly allow malicious
  45. * sites to execute code in a JupyterLab session.
  46. *
  47. * For more information, see
  48. * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe
  49. */
  50. get sandbox(): IFrame.SandboxExceptions[] {
  51. return this._sandbox.slice();
  52. }
  53. set sandbox(values: IFrame.SandboxExceptions[]) {
  54. this._sandbox = values.slice();
  55. const iframe = this.node.querySelector('iframe')!;
  56. const exceptions = values.length ? values.join(' ') : '';
  57. iframe.setAttribute('sandbox', exceptions);
  58. }
  59. /**
  60. * The url of the IFrame.
  61. */
  62. get url(): string {
  63. return this.node.querySelector('iframe')!.getAttribute('src') || '';
  64. }
  65. set url(url: string) {
  66. this.node.querySelector('iframe')!.setAttribute('src', url);
  67. }
  68. private _sandbox: IFrame.SandboxExceptions[] = [];
  69. private _referrerPolicy: IFrame.ReferrerPolicy;
  70. }
  71. /**
  72. * A namespace for IFrame widget statics.
  73. */
  74. export namespace IFrame {
  75. /**
  76. * Referrer policy for the iframe.
  77. *
  78. * User documentation for the policies can be found here:
  79. * https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/referrerPolicy
  80. */
  81. export type ReferrerPolicy =
  82. | 'no-referrer'
  83. | 'no-referrer-when-downgrade'
  84. | 'origin'
  85. | 'origin-when-cross-origin'
  86. | 'same-origin'
  87. | 'strict-origin'
  88. | 'strict-origin-when-cross-origin'
  89. | 'unsafe-url';
  90. /**
  91. * Exceptions to the iframe sandboxing policies.
  92. * These are specified here:
  93. * https://www.w3.org/TR/2011/WD-html5-20110525/the-iframe-element.html#attr-iframe-sandbox
  94. *
  95. * More user-friendly documentation can be found here:
  96. * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#sandbox
  97. */
  98. export type SandboxExceptions =
  99. | 'allow-forms'
  100. | 'allow-modals'
  101. | 'allow-orientation-lock'
  102. | 'allow-pointer-lock'
  103. | 'allow-popups'
  104. | 'popups-to-escape-sandbox'
  105. | 'allow-presentation'
  106. | 'allow-same-origin'
  107. | 'allow-scripts'
  108. | 'allow-storage-access-by-user-activation'
  109. | 'allow-top-navigation'
  110. | 'allow-top-navigation-by-user-activation';
  111. /**
  112. * Options for creating a new IFrame widget.
  113. */
  114. export interface IOptions {
  115. /**
  116. * Exceptions for the iframe sandbox.
  117. */
  118. sandbox?: SandboxExceptions[];
  119. /**
  120. * Referrer policy for the iframe.
  121. */
  122. referrerPolicy?: ReferrerPolicy;
  123. }
  124. }
  125. /**
  126. * A namespace for private data.
  127. */
  128. namespace Private {
  129. /**
  130. * Create the main content node of an iframe widget.
  131. */
  132. export function createNode(): HTMLElement {
  133. const node = document.createElement('div');
  134. const iframe = document.createElement('iframe');
  135. iframe.setAttribute('sandbox', '');
  136. iframe.style.height = '100%';
  137. iframe.style.width = '100%';
  138. node.appendChild(iframe);
  139. return node;
  140. }
  141. }