Skip to content

Commit

Permalink
fix: fix default base as per URL spec (#133)
Browse files Browse the repository at this point in the history
The default value for base was an empty string which is strong, base is an optional param and so should be undefined.

https://url.spec.whatwg.org/#url-class

BREAKING CHANGE: behaviour of the constructor may break some use cases, but at least now it implements the spec correctly.
  • Loading branch information
acostalima committed Oct 21, 2020
1 parent 008e1d4 commit e816fea
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ node_modules
.nyc_output
coverage.lcov
yarn-error.log
package-lock.json
4 changes: 1 addition & 3 deletions src/url-browser.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit e816fea

Please sign in to comment.