Skip to content

Commit 20ab946

Browse files
jeswinsimonfacebook-github-bot
authored andcommitted
Support bare hosts. Add missing / between url (#26050)
Summary: Fix for #26019 URL cannot handle "localhost" domain for base url. Also noticed another issue where `/` is not added between base URL and path if it is missing. Added fix for that too. ## Changelog [Javascript] [Fixed] - `URL`: Bare Hosts are now supported. Pull Request resolved: #26050 Test Plan: * `new URL('home', 'http://localhost')` now returns `http://localhost/home` instead of throwing an error. * `new URL('en-US/docs', 'https://developer.mozilla.org')` now returns `https://developer.mozilla.org/en-US/docs` and not `https://developer.mozilla.orgen-US/docs`. Differential Revision: D17314137 Pulled By: cpojer fbshipit-source-id: ef56c4f4032187a7efee32b28e2b3c935b6a2599
1 parent 314eba9 commit 20ab946

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Libraries/Blob/URL.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class URLSearchParams {
107107

108108
function validateBaseUrl(url: string) {
109109
// from this MIT-licensed gist: https://gist.github.com/dperini/729294
110-
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(
110+
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(
111111
url,
112112
);
113113
}
@@ -144,9 +144,12 @@ export class URL {
144144
} else if (typeof base === 'object') {
145145
baseUrl = base.toString();
146146
}
147-
if (baseUrl.endsWith('/') && url.startsWith('/')) {
147+
if (baseUrl.endsWith('/')) {
148148
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
149149
}
150+
if (!url.startsWith('/')) {
151+
url = `/${url}`;
152+
}
150153
if (baseUrl.endsWith(url)) {
151154
url = '';
152155
}

Libraries/Blob/__tests__/URL-test.js

+6
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,11 @@ describe('URL', function() {
3333
expect(h.href).toBe('https://developer.mozilla.org/en-US/docs');
3434
const i = new URL('http://github.com', 'http://google.com');
3535
expect(i.href).toBe('https://github.com/');
36+
// Support Bare Hosts
37+
const j = new URL('home', 'http://localhost');
38+
expect(j.href).toBe('http://localhost/home');
39+
// Insert / between Base and Path if missing
40+
const k = new URL('en-US/docs', 'https://developer.mozilla.org');
41+
expect(k.href).toBe('https://developer.mozilla.org/en-US/docs');
3642
});
3743
});

0 commit comments

Comments
 (0)