refactor: use the URL api to resolve urls in window setup (#18611)

This commit is contained in:
Jeremy Apthorp 2019-06-05 10:21:06 -07:00 committed by GitHub
parent bd80e68698
commit 291ee2dafc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,12 +20,7 @@ import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-in
const { defineProperty } = Object
// Helper function to resolve relative url.
let a: HTMLAnchorElement
const resolveURL = function (url: string) {
a = a || window.document.createElement('a')
a.href = url
return a.href
}
const resolveURL = (url: string, base: string) => new URL(url, base).href
// Use this method to ensure values expected as strings in the main process
// are convertible to strings in the renderer process. This ensures exceptions
@ -129,7 +124,7 @@ class BrowserWindowProxy {
return this._location
}
public set location (url: string | any) {
url = resolveURL(url)
url = resolveURL(url, this.location.href)
this._invokeWebContentsMethodSync('loadURL', url)
}
@ -194,7 +189,7 @@ export const windowSetup = (
// Make the browser window or guest view emit "new-window" event.
(window as any).open = function (url?: string, frameName?: string, features?: string) {
if (url != null && url !== '') {
url = resolveURL(url)
url = resolveURL(url, location.href)
}
const guestId = ipcRendererInternal.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', url, toString(frameName), toString(features))
if (guestId != null) {