Skip to content

Commit f7a5f17

Browse files
committed
fix($route): fix regex escaping in route matcher
1 parent b3ed7a8 commit f7a5f17

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/service/route.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,16 @@ angularServiceInject('$route', function(location, $updateView) {
197197

198198

199199
function switchRouteMatcher(on, when, dstName) {
200-
var regex = '^' + when.replace(/[\.\\\(\)\^\$]/g, "\$1") + '$',
200+
// TODO(i): this code is convoluted and inefficient, we should construct the route matching
201+
// regex only once and then reuse it
202+
var regex = '^' + when.replace(/([\.\\\(\)\^\$])/g, "\\$1") + '$',
201203
params = [],
202204
dst = {};
203205
forEach(when.split(/\W/), function(param){
204206
if (param) {
205207
var paramRegExp = new RegExp(":" + param + "([\\W])");
206208
if (regex.match(paramRegExp)) {
207-
regex = regex.replace(paramRegExp, "([^\/]*)$1");
209+
regex = regex.replace(paramRegExp, "([^\\/]*)$1");
208210
params.push(param);
209211
}
210212
}

test/service/routeSpec.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,37 @@ describe('$route', function() {
5555

5656

5757
it('should return fn registered with onChange()', function() {
58-
var scope = angular.scope(),
59-
$route = scope.$service('$route'),
58+
var $route = scope.$service('$route'),
6059
fn = function() {};
6160

6261
expect($route.onChange(fn)).toBe(fn);
6362
});
6463

6564

65+
it('should match a route that contains special chars in the path', function() {
66+
var $route = scope.$service('$route'),
67+
$location = scope.$service('$location');
68+
69+
$route.when('/$test.23/foo(bar)/:baz', {template: 'test.html'});
70+
71+
$location.hashPath = '/test';
72+
scope.$eval();
73+
expect($route.current).toBe(null);
74+
75+
$location.hashPath = '/$testX23/foo(bar)/222';
76+
scope.$eval();
77+
expect($route.current).toBe(null);
78+
79+
$location.hashPath = '/$test.23/foo(bar)/222';
80+
scope.$eval();
81+
expect($route.current).toBeDefined();
82+
83+
$location.hashPath = '/$test.23/foo\\(bar)/222';
84+
scope.$eval();
85+
expect($route.current).toBe(null);
86+
});
87+
88+
6689
it('should allow routes to be defined with just templates without controllers', function() {
6790
var scope = angular.scope(),
6891
$location = scope.$service('$location'),

0 commit comments

Comments
 (0)