From cde1261f0cfa1a311e6d11770e94e2a7d3f3db00 Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Sun, 23 Nov 2014 18:51:20 +0200 Subject: [PATCH] fix($route): fix redirection with optional/eager params --- src/ngRoute/route.js | 2 +- test/ngRoute/routeSpec.js | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/ngRoute/route.js b/src/ngRoute/route.js index 97a253023308..d2da0dab51fc 100644 --- a/src/ngRoute/route.js +++ b/src/ngRoute/route.js @@ -577,7 +577,7 @@ function $RouteProvider(){ if (i === 0) { result.push(segment); } else { - var segmentMatch = segment.match(/(\w+)(.*)/); + var segmentMatch = segment.match(/(\w+)(?:[?*])?(.*)/); var key = segmentMatch[1]; result.push(params[key]); result.push(segmentMatch[2] || ''); diff --git a/test/ngRoute/routeSpec.js b/test/ngRoute/routeSpec.js index 072cf6bb8ab7..91daa63a7a02 100644 --- a/test/ngRoute/routeSpec.js +++ b/test/ngRoute/routeSpec.js @@ -219,13 +219,13 @@ describe('$route', function() { expect($route.current).toBeUndefined(); })); - it('matches literal *', inject(function($route, $location, $rootScope) { + it('matches literal .', inject(function($route, $location, $rootScope) { $location.path('/$testX23/foo*(bar)/222'); $rootScope.$digest(); expect($route.current).toBeUndefined(); })); - it('matches literal .', inject(function($route, $location, $rootScope) { + it('matches literal *', inject(function($route, $location, $rootScope) { $location.path('/$test.23/foooo(bar)/222'); $rootScope.$digest(); expect($route.current).toBeUndefined(); @@ -830,6 +830,29 @@ describe('$route', function() { }); + it('should properly interpolate optional and eager route vars ' + + 'when redirecting from path with trailing slash', function() { + module(function($routeProvider) { + $routeProvider.when('/foo/:id?/:subid?', {templateUrl: 'foo.html'}); + $routeProvider.when('/bar/:id*/:subid', {templateUrl: 'bar.html'}); + }); + + inject(function($location, $rootScope, $route) { + $location.path('/foo/id1/subid2/'); + $rootScope.$digest(); + + expect($location.path()).toEqual('/foo/id1/subid2'); + expect($route.current.templateUrl).toEqual('foo.html'); + + $location.path('/bar/id1/extra/subid2/'); + $rootScope.$digest(); + + expect($location.path()).toEqual('/bar/id1/extra/subid2'); + expect($route.current.templateUrl).toEqual('bar.html'); + }); + }); + + it('should allow custom redirectTo function to be used', function() { function customRedirectFn(routePathParams, path, search) { expect(routePathParams).toEqual({id: 'id3'});