All files / internal / vdom / jsdom.ts

100.00% Branches 1/1
100.00% Lines 9/9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
 
x41
 
 
 
 
 
 
 
 
 
 
x41
x3048
x762
x917
x762
x762
x41
x41


















// Imports
import type { VirtualWindow } from "../engine/mod.ts"
import { JSDOM } from "@npm/jsdom"
export type { VirtualWindow }

/**
 * Virtual {@linkcode https://developer.mozilla.org/docs/Web/API/Window | Window} implementation based on {@link https://github.com/jsdom/jsdom | JSDOM}.
 *
 * ```ts
 * await using window = new Window("<html></html>")
 * console.assert(window.document.documentElement.tagName === "HTML")
 * ```
 */
const Window = (function (content: string) {
  const { window } = new JSDOM(content, { url: globalThis.location?.href, contentType: "text/html" })
  window[Symbol.asyncDispose] = async () => {
    await window.close()
  }
  return window
}) as unknown as (new (content?: string) => VirtualWindow)
export { Window }