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

Commit 2bc39bb

Browse files
committed
fix($route): fix regex escaping in route matcher
1 parent 62ae7fc commit 2bc39bb

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/service/route.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,16 @@ angularServiceInject('$route', function($location, $routeParams) {
235235
/////////////////////////////////////////////////////
236236

237237
function switchRouteMatcher(on, when) {
238-
var regex = '^' + when.replace(/[\.\\\(\)\^\$]/g, "\$1") + '$',
238+
// TODO(i): this code is convoluted and inefficient, we should construct the route matching
239+
// regex only once and then reuse it
240+
var regex = '^' + when.replace(/([\.\\\(\)\^\$])/g, "\\$1") + '$',
239241
params = [],
240242
dst = {};
241243
forEach(when.split(/\W/), function(param) {
242244
if (param) {
243245
var paramRegExp = new RegExp(":" + param + "([\\W])");
244246
if (regex.match(paramRegExp)) {
245-
regex = regex.replace(paramRegExp, "([^\/]*)$1");
247+
regex = regex.replace(paramRegExp, "([^\\/]*)$1");
246248
params.push(param);
247249
}
248250
}

test/service/routeSpec.js

+21
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ describe('$route', function() {
5959
});
6060

6161

62+
it('should match a route that contains special chars in the path', function() {
63+
$route.when('/$test.23/foo(bar)/:baz', {template: 'test.html'});
64+
65+
$location.path('/test');
66+
scope.$digest();
67+
expect($route.current).toBeUndefined();
68+
69+
$location.path('/$testX23/foo(bar)/222');
70+
scope.$digest();
71+
expect($route.current).toBeUndefined();
72+
73+
$location.path('/$test.23/foo(bar)/222');
74+
scope.$digest();
75+
expect($route.current).toBeDefined();
76+
77+
$location.path('/$test.23/foo\\(bar)/222');
78+
scope.$digest();
79+
expect($route.current).toBeUndefined();
80+
});
81+
82+
6283
it('should change route even when only search param changes', function() {
6384
var callback = jasmine.createSpy('onRouteChange');
6485

0 commit comments

Comments
 (0)