Skip to content

Commit bf2a76d

Browse files
Sean Murphygkalpak
Sean Murphy
authored andcommitted
fix($routeProvider): properly handle optional eager path named groups
Closes angular#14011
1 parent 9421674 commit bf2a76d

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/ngRoute/route.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ function $RouteProvider() {
213213

214214
path = path
215215
.replace(/([().])/g, '\\$1')
216-
.replace(/(\/)?:(\w+)([\?\*])?/g, function(_, slash, key, option) {
217-
var optional = option === '?' ? option : null;
218-
var star = option === '*' ? option : null;
216+
.replace(/(\/)?:(\w+)(\*\?|[\?\*])?/g, function(_, slash, key, option) {
217+
var optional = (option === '?' || option === '*?') ? '?' : null;
218+
var star = (option === '*' || option === '*?') ? '*' : null;
219219
keys.push({ name: key, optional: !!optional });
220220
slash = slash || '';
221221
return ''

test/ngRoute/routeSpec.js

+26
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,32 @@ describe('$route', function() {
974974
});
975975

976976

977+
it('should properly process route params which are both eager and optional', function() {
978+
module(function($routeProvider) {
979+
$routeProvider.when('/foo/:param1*?/:param2', {templateUrl: 'foo.html'});
980+
});
981+
982+
inject(function($location, $rootScope, $route) {
983+
$location.path('/foo/bar1/bar2/bar3/baz');
984+
$rootScope.$digest();
985+
986+
expect($location.path()).toEqual('/foo/bar1/bar2/bar3/baz');
987+
expect($route.current.params.param1).toEqual('bar1/bar2/bar3');
988+
expect($route.current.params.param2).toEqual('baz');
989+
expect($route.current.templateUrl).toEqual('foo.html');
990+
991+
$location.path('/foo/baz');
992+
$rootScope.$digest();
993+
994+
expect($location.path()).toEqual('/foo/baz');
995+
expect($route.current.params.param1).toEqual(undefined);
996+
expect($route.current.params.param2).toEqual('baz');
997+
expect($route.current.templateUrl).toEqual('foo.html');
998+
999+
});
1000+
});
1001+
1002+
9771003
it('should properly interpolate optional and eager route vars ' +
9781004
'when redirecting from path with trailing slash', function() {
9791005
module(function($routeProvider) {

0 commit comments

Comments
 (0)