From e104204ae083d31e0b9967373ce79f2f1ca8fbb6 Mon Sep 17 00:00:00 2001 From: jeswinsimon Date: Tue, 13 Aug 2019 02:51:47 -0700 Subject: [PATCH] URL: Do not prepend baseUrl if the URL is not a relative URL (#26009) Summary: Fix for bug https://github.com/facebook/react-native/issues/26006 URL with base is always used, even when absolute URL is provided ## Changelog [Javascript] [Fixed] - `URL`: Base url value is ignored when the input url is not a relative url. Pull Request resolved: https://github.com/facebook/react-native/pull/26009 Test Plan: `new URL('http://github.com', 'http://google.com')` now returns `http://github.com/` Added a test case to `URL-test.js` to verify the same. Differential Revision: D16781921 Pulled By: cpojer fbshipit-source-id: 038aca3610e34f513f603e8993f9a925b7d28626 --- Libraries/Blob/URL.js | 12 ++++++------ Libraries/Blob/__tests__/URL-test.js | 2 ++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Libraries/Blob/URL.js b/Libraries/Blob/URL.js index 692980c69ca1b8..9869dcbeaded11 100644 --- a/Libraries/Blob/URL.js +++ b/Libraries/Blob/URL.js @@ -130,7 +130,12 @@ export class URL { constructor(url: string, base: string) { let baseUrl = null; - if (base) { + if (!base || validateBaseUrl(url)) { + this._url = url; + if (!this._url.endsWith('/')) { + this._url += '/'; + } + } else { if (typeof base === 'string') { baseUrl = base; if (!validateBaseUrl(baseUrl)) { @@ -146,11 +151,6 @@ export class URL { url = ''; } this._url = `${baseUrl}${url}`; - } else { - this._url = url; - if (!this._url.endsWith('/')) { - this._url += '/'; - } } } diff --git a/Libraries/Blob/__tests__/URL-test.js b/Libraries/Blob/__tests__/URL-test.js index 8ca71feb025315..281649ce7821d7 100644 --- a/Libraries/Blob/__tests__/URL-test.js +++ b/Libraries/Blob/__tests__/URL-test.js @@ -31,5 +31,7 @@ describe('URL', function() { // expect(g.href).toBe('https://developer.mozilla.org/en-US/docs'); const h = new URL('/en-US/docs', a); 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/'); }); });