diff --git a/Libraries/Blob/URL.js b/Libraries/Blob/URL.js index 9869dcbeade..f2b6f7f277c 100644 --- a/Libraries/Blob/URL.js +++ b/Libraries/Blob/URL.js @@ -107,7 +107,7 @@ export class URLSearchParams { function validateBaseUrl(url: string) { // from this MIT-licensed gist: https://gist.github.com/dperini/729294 - return /^(?:(?:(?:https?|ftp):)?\/\/)(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i.test( + return /^(?:(?:(?:https?|ftp):)?\/\/)(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test( url, ); } @@ -144,9 +144,12 @@ export class URL { } else if (typeof base === 'object') { baseUrl = base.toString(); } - if (baseUrl.endsWith('/') && url.startsWith('/')) { + if (baseUrl.endsWith('/')) { baseUrl = baseUrl.slice(0, baseUrl.length - 1); } + if (!url.startsWith('/')) { + url = `/${url}`; + } if (baseUrl.endsWith(url)) { url = ''; } diff --git a/Libraries/Blob/__tests__/URL-test.js b/Libraries/Blob/__tests__/URL-test.js index 281649ce782..87b8558049b 100644 --- a/Libraries/Blob/__tests__/URL-test.js +++ b/Libraries/Blob/__tests__/URL-test.js @@ -33,5 +33,11 @@ describe('URL', function() { expect(h.href).toBe('https://developer.mozilla.org/en-US/docs'); const i = new URL('http://github.com', 'http://google.com'); expect(i.href).toBe('http://github.com/'); + // Support Bare Hosts + const j = new URL('home', 'http://localhost'); + expect(j.href).toBe('http://localhost/home'); + // Insert / between Base and Path if missing + const k = new URL('en-US/docs', 'https://developer.mozilla.org'); + expect(k.href).toBe('https://developer.mozilla.org/en-US/docs'); }); });