fix($route): fix regex escaping in route matcher

This commit is contained in:
Igor Minar 2011-09-21 13:47:17 +02:00
parent b3ed7a8a7a
commit f7a5f1788a
2 changed files with 29 additions and 4 deletions

View file

@ -197,14 +197,16 @@ angularServiceInject('$route', function(location, $updateView) {
function switchRouteMatcher(on, when, dstName) {
var regex = '^' + when.replace(/[\.\\\(\)\^\$]/g, "\$1") + '$',
// TODO(i): this code is convoluted and inefficient, we should construct the route matching
// regex only once and then reuse it
var regex = '^' + when.replace(/([\.\\\(\)\^\$])/g, "\\$1") + '$',
params = [],
dst = {};
forEach(when.split(/\W/), function(param){
if (param) {
var paramRegExp = new RegExp(":" + param + "([\\W])");
if (regex.match(paramRegExp)) {
regex = regex.replace(paramRegExp, "([^\/]*)$1");
regex = regex.replace(paramRegExp, "([^\\/]*)$1");
params.push(param);
}
}

View file

@ -55,14 +55,37 @@ describe('$route', function() {
it('should return fn registered with onChange()', function() {
var scope = angular.scope(),
$route = scope.$service('$route'),
var $route = scope.$service('$route'),
fn = function() {};
expect($route.onChange(fn)).toBe(fn);
});
it('should match a route that contains special chars in the path', function() {
var $route = scope.$service('$route'),
$location = scope.$service('$location');
$route.when('/$test.23/foo(bar)/:baz', {template: 'test.html'});
$location.hashPath = '/test';
scope.$eval();
expect($route.current).toBe(null);
$location.hashPath = '/$testX23/foo(bar)/222';
scope.$eval();
expect($route.current).toBe(null);
$location.hashPath = '/$test.23/foo(bar)/222';
scope.$eval();
expect($route.current).toBeDefined();
$location.hashPath = '/$test.23/foo\\(bar)/222';
scope.$eval();
expect($route.current).toBe(null);
});
it('should allow routes to be defined with just templates without controllers', function() {
var scope = angular.scope(),
$location = scope.$service('$location'),