From 0092aac275e8efca66838a8c266eec1d455883aa Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 29 Apr 2020 11:47:34 -0400 Subject: [PATCH] fixes to url parsing (#99) * fixes to url parsing * make it a little more intelligent to pass tests --- src/misc/utils.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/misc/utils.ts b/src/misc/utils.ts index 6131fbd836..5c86ccd94c 100644 --- a/src/misc/utils.ts +++ b/src/misc/utils.ts @@ -281,14 +281,14 @@ export class Utils { return null; } - const hasProtocol = uriString.indexOf('://') > -1; - if (!hasProtocol && uriString.indexOf('.') > -1) { - uriString = 'http://' + uriString; - } else if (!hasProtocol) { - return null; + let url = Utils.getUrlObject(uriString); + if (url == null) { + const hasHttpProtocol = uriString.indexOf('http://') === 0 || uriString.indexOf('https://') === 0; + if (!hasHttpProtocol && uriString.indexOf('.') > -1) { + url = Utils.getUrlObject('http://' + uriString); + } } - - return Utils.getUrlObject(uriString); + return url; } private static getUrlObject(uriString: string): URL { @@ -298,6 +298,12 @@ export class Utils { } else if (typeof URL === 'function') { return new URL(uriString); } else if (window != null) { + const hasProtocol = uriString.indexOf('://') > -1; + if (!hasProtocol && uriString.indexOf('.') > -1) { + uriString = 'http://' + uriString; + } else if (!hasProtocol) { + return null; + } const anchor = window.document.createElement('a'); anchor.href = uriString; return anchor as any;