From eb574b386bddad974bd442a44196f40e2f934811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Costa=20Lima?= Date: Tue, 20 Oct 2020 12:52:46 +0100 Subject: [PATCH] fix: default base as per URL spec --- .gitignore | 1 + src/url-browser.js | 4 +--- test.js | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index a673b10..69d98af 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ node_modules .nyc_output coverage.lcov yarn-error.log +package-lock.json diff --git a/src/url-browser.js b/src/url-browser.js index 2fb48b4..03a9464 100644 --- a/src/url-browser.js +++ b/src/url-browser.js @@ -1,8 +1,6 @@ 'use strict'; -const defaultBase = self.location ? - self.location.protocol + '//' + self.location.host : - ''; +const defaultBase = self.location && self.location.protocol + '//' + self.location.host; const URL = self.URL; class URLWithLegacySupport { diff --git a/test.js b/test.js index b1d2a22..c5f2ef1 100644 --- a/test.js +++ b/test.js @@ -9,6 +9,26 @@ const isBrowser = typeof document === 'object' && document.nodeType === 9; +test('unspecified base should not throw', (t) => { + t.plan(1); + + if (isBrowser) { + t.doesNotThrow(() => new URL('http://localhost')); + } else { + // Hack to force construction of a browser URL in Node to simulate a React Native-like environment where .location does not exist + global.self = { + URL: global.URL, + URLSearchParams: global.URLSearchParams + }; + /* eslint-disable-next-line global-require */ + const { URLWithLegacySupport: URL } = require('./src/url-browser'); + + t.doesNotThrow(() => new URL('http://localhost')); + + delete global.self; + } +}); + test('relative', (t) => { t.plan(1);