UNPKG

happy-dom

Version:

Happy DOM is a JavaScript implementation of a web browser without its graphical user interface. It includes many web standards from WHATWG DOM and HTML.

31 lines (27 loc) 791 B
import type IBrowserFrame from '../types/IBrowserFrame.js'; import { URL } from 'url'; import * as PropertySymbol from '../../PropertySymbol.js'; /** * Browser frame URL utility. */ export default class BrowserFrameURL { /** * Returns relative URL. * * @param frame Frame. * @param url URL. * @returns Relative URL. */ public static getRelativeURL(frame: IBrowserFrame, url?: string | URL | null): URL { url = url ? String(url) : 'about:blank'; if (url.startsWith('about:') || url.startsWith('javascript:')) { return new URL(url); } try { // Use internal PropertySymbol.location to avoid being affected by mocks on window.location return new URL(url, frame.window[PropertySymbol.location].href); } catch (e) { return new URL('about:blank'); } } }