Skip to content

Commit f0e9260

Browse files
wartabAndrewKushnir
authored andcommitted
fix(common): make Location.normalize() return the correct path when the base path contains characters that interfere with regex syntax. (#49181)
Fix the function stripping the base path from the URL, as the current implementation uses the base path as part of a regex, which wrongly makes paths fails that contain characters such as a parenthesis (example: C:/Users/MyUser(Test)/project). Fixes #49179 PR Close #49181
1 parent 7ec76de commit f0e9260

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

packages/common/src/location/location.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,14 @@ export function createLocation() {
301301
}
302302

303303
function _stripBasePath(basePath: string, url: string): string {
304-
return basePath && new RegExp(`^${basePath}([/;?#]|$)`).test(url) ?
305-
url.substring(basePath.length) :
306-
url;
304+
if (!basePath || !url.startsWith(basePath)) {
305+
return url;
306+
}
307+
const strippedUrl = url.substring(basePath.length);
308+
if (strippedUrl === '' || ['/', ';', '?', '#'].includes(strippedUrl[0])) {
309+
return strippedUrl;
310+
}
311+
return url;
307312
}
308313

309314
function _stripIndexHtml(url: string): string {

packages/common/test/location/location_spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,18 @@ describe('Location Class', () => {
286286
expect(location.normalize(baseHref + matrixParams)).toBe(matrixParams);
287287
expect(location.normalize(baseHref + fragment)).toBe(fragment);
288288
});
289+
290+
it('in case APP_BASE_HREF contains characters that have special meaning in a regex', () => {
291+
const baseHref = 'c:/users/name(test)/en';
292+
const path = '/test-path';
293+
294+
TestBed.configureTestingModule({providers: [{provide: APP_BASE_HREF, useValue: baseHref}]});
295+
296+
const location = TestBed.inject(Location);
297+
298+
expect(location.normalize(path)).toBe(path);
299+
expect(location.normalize(baseHref)).toBe('');
300+
expect(location.normalize(baseHref + path)).toBe(path);
301+
});
289302
});
290303
});

0 commit comments

Comments
 (0)