UNPKG

iportal

Version:

web-portal

71 lines (69 loc) 1.9 kB
class Sandbox { public sandbox: HTMLIFrameElement public contentDocument!: Document public contentWindow!: Window constructor (src?: string, setting?: string) { const sandbox = this.sandbox = document.createElement('iframe') sandbox.src = src || 'about:blank' sandbox.style.display = 'none' if (setting !== undefined) { this.set(setting) } return this } set src (src: string) { this.sandbox.src = src } set unload (beforeunload: (this: WindowEventHandlers, ev: BeforeUnloadEvent) => any) { this.contentWindow.onbeforeunload = beforeunload } set onload (onload: (this: GlobalEventHandlers, ev: Event) => any) { this.sandbox.onload = onload } set onerror (onerror: OnErrorEventHandler) { this.sandbox.onerror = onerror } public set (allow: string) { this.sandbox.setAttribute('sandbox', allow) } public reset (allow: string) { this.exit() this.set(allow) return this } public open () { this.contentDocument.open() return this } public write (context = '<head><meta charset="utf-8"></head>') { this.contentDocument.write(context) return this } public close () { this.contentDocument.close() return this } public append (context: string | undefined, container: HTMLElement) { this.enter(container) this.attach() this.open() this.write(context) this.close() } public enter (container: HTMLElement) { container.appendChild(this.sandbox) } public attach () { const contentWindow = this.sandbox.contentWindow as Window const contentDocument = this.sandbox.contentDocument as Document this.contentWindow = contentWindow this.contentDocument = contentDocument } public exit () { const parentNode = this.sandbox.parentNode as HTMLElement parentNode && parentNode.removeChild(this.sandbox) } } export { Sandbox }