From fb128af4f43fa17f351d50cf615c7598c751f50a Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sat, 24 Jul 2021 18:19:10 +0200 Subject: [PATCH] [fix] Use `'null'` as `origin` for non special URLs --- index.js | 4 ++-- test/test.js | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 6f2299a..8e31ae3 100644 --- a/index.js +++ b/index.js @@ -371,7 +371,7 @@ function Url(address, location, parser) { url.password = instruction[1] || ''; } - url.origin = url.protocol && url.host && url.protocol !== 'file:' + url.origin = url.protocol !== 'file:' && isSpecial(url.protocol) && url.host ? url.protocol +'//'+ url.host : 'null'; @@ -464,7 +464,7 @@ function set(part, value, fn) { if (ins[4]) url[ins[1]] = url[ins[1]].toLowerCase(); } - url.origin = url.protocol && url.host && url.protocol !== 'file:' + url.origin = url.protocol !== 'file:' && isSpecial(url.protocol) && url.host ? url.protocol +'//'+ url.host : 'null'; diff --git a/test/test.js b/test/test.js index fc240fc..d5a6cab 100644 --- a/test/test.js +++ b/test/test.js @@ -395,6 +395,13 @@ describe('url-parse', function () { assume(parsed.origin).equals('null'); }); + it('is null for non special URLs', function () { + var o = parse('foo://example.com/pathname'); + assume(o.hostname).equals('example.com'); + assume(o.pathname).equals('/pathname'); + assume(o.origin).equals('null'); + }); + it('removes default ports for http', function () { var o = parse('http://google.com:80/pathname'); assume(o.origin).equals('http://google.com');