Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 059d4f6

Browse files
committed
refactor(urlResolve): return already parsed URLs unchanged
Closes #14890
1 parent 1e90d03 commit 059d4f6

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/ng/urlUtils.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ var baseUrlParsingNode;
4040
* http://james.padolsey.com/javascript/parsing-urls-with-the-dom/
4141
*
4242
* @kind function
43-
* @param {string} url The URL to be parsed.
43+
* @param {string|object} url The URL to be parsed. If `url` is not a string, it will be returned
44+
* unchanged.
4445
* @description Normalizes and parses a URL.
4546
* @returns {object} Returns the normalized URL as a dictionary.
4647
*
@@ -57,6 +58,8 @@ var baseUrlParsingNode;
5758
*
5859
*/
5960
function urlResolve(url) {
61+
if (!isString(url)) return url;
62+
6063
var href = url;
6164

6265
// Support: IE 9-11 only
@@ -132,7 +135,7 @@ function urlIsAllowedOriginFactory(whitelistedOriginUrls) {
132135
* @returns {boolean} - Whether the specified URL is of an allowed origin.
133136
*/
134137
return function urlIsAllowedOrigin(requestUrl) {
135-
var parsedUrl = isString(requestUrl) ? urlResolve(requestUrl) : requestUrl;
138+
var parsedUrl = urlResolve(requestUrl);
136139
return parsedAllowedOriginUrls.some(urlsAreSameOrigin.bind(null, parsedUrl));
137140
};
138141
}
@@ -148,8 +151,8 @@ function urlIsAllowedOriginFactory(whitelistedOriginUrls) {
148151
* @returns {boolean} - True if both URLs have the same origin, and false otherwise.
149152
*/
150153
function urlsAreSameOrigin(url1, url2) {
151-
url1 = isString(url1) ? urlResolve(url1) : url1;
152-
url2 = isString(url2) ? urlResolve(url2) : url2;
154+
url1 = urlResolve(url1);
155+
url2 = urlResolve(url2);
153156

154157
return (url1.protocol === url2.protocol &&
155158
url1.host === url2.host);

test/ng/urlUtilsSpec.js

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
describe('urlUtils', function() {
44
describe('urlResolve', function() {
5+
it('should returned already parsed URLs unchanged', function() {
6+
var urlObj = urlResolve('/foo?bar=baz#qux');
7+
expect(urlResolve(urlObj)).toBe(urlObj);
8+
expect(urlResolve(true)).toBe(true);
9+
expect(urlResolve(null)).toBeNull();
10+
expect(urlResolve(undefined)).toBeUndefined();
11+
});
12+
13+
514
it('should normalize a relative url', function() {
615
expect(urlResolve('foo').href).toMatch(/^https?:\/\/[^/]+\/foo$/);
716
});

0 commit comments

Comments
 (0)