1 | class Iframe {
|
2 | constructor({ el, sandboxAttrs = [] }) {
|
3 | if (!el) throw new Error('Expect "el" to mount iframe to!');
|
4 | this.$el = el;
|
5 | this.sandboxAttrs = sandboxAttrs;
|
6 | }
|
7 |
|
8 | createIframe() {
|
9 | const iframe = document.createElement('iframe');
|
10 | iframe.setAttribute('sandbox', this.sandboxAttrs.join(' '));
|
11 | iframe.setAttribute('scrolling', 'yes');
|
12 | iframe.style.width = '100%';
|
13 | iframe.style.height = '100%';
|
14 | iframe.style.border = 0;
|
15 | return iframe;
|
16 | }
|
17 |
|
18 | setContent({ head = '', body = '' }) {
|
19 | const content = `<!DOCTYPE html><html><head>${head}</head><body>${body}</body></html>`;
|
20 | const iframe = this.createIframe();
|
21 | this.$el.parentNode.replaceChild(iframe, this.$el);
|
22 |
|
23 | iframe.contentWindow.document.open();
|
24 | iframe.contentWindow.document.write(content);
|
25 | iframe.contentWindow.document.close();
|
26 |
|
27 | this.$el = iframe;
|
28 | }
|
29 | }
|
30 |
|
31 | export default (...args) => new Iframe(...args);
|