From 9826d11b060098ca66c67c8de00f469af3b78425 Mon Sep 17 00:00:00 2001 From: Sean Murphy Date: Wed, 10 Feb 2016 10:58:24 -0800 Subject: [PATCH 1/3] feat(ngRoute):change pathRegExp to respect optional wildcard route params change pathRegExp to accept the two-character option of '*?' on route params to allow them to be wildcard and optional at the same time. Motivation for change inspired by the need to accept an auth token on the URL that contained slashes (need for wildcard param) but this token on the URL was not required (need for optional param). Previous behavior only saw the param as wildcard and not optional, which resulted in the need for a trailing slash on the url (e.g. /login/ vs. /login) new feature - does not address any open issues --- package.json | 4 ++-- src/ngRoute/route.js | 6 +++--- test/ngRoute/routeSpec.js | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 7fa1dbd38fc2..1fc06c58d2ef 100644 --- a/package.json +++ b/package.json @@ -32,13 +32,13 @@ "dgeni-packages": "^0.11.0", "event-stream": "~3.1.0", "glob": "^6.0.1", - "grunt": "~0.4.2", + "grunt": "https://registry.npmjs.org/grunt/-/grunt-0.4.5.tgz", "grunt-bump": "~0.0.13", "grunt-contrib-clean": "~0.6.0", "grunt-contrib-compress": "~0.12.0", "grunt-contrib-connect": "~0.8.0", "grunt-contrib-copy": "~0.6.0", - "grunt-contrib-jshint": "~0.10.0", + "grunt-contrib-jshint": "https://registry.npmjs.org/grunt-contrib-jshint/-/grunt-contrib-jshint-0.10.0.tgz", "grunt-ddescribe-iit": "~0.0.1", "grunt-jasmine-node": "git://github.com/vojtajina/grunt-jasmine-node.git#fix-grunt-exit-code", "grunt-jscs": "^2.1.0", diff --git a/src/ngRoute/route.js b/src/ngRoute/route.js index 90db1140d0bc..149d0bb96a69 100644 --- a/src/ngRoute/route.js +++ b/src/ngRoute/route.js @@ -213,9 +213,9 @@ function $RouteProvider() { path = path .replace(/([().])/g, '\\$1') - .replace(/(\/)?:(\w+)([\?\*])?/g, function(_, slash, key, option) { - var optional = option === '?' ? option : null; - var star = option === '*' ? option : null; + .replace(/(\/)?:(\w+)(\*\?|[\?\*])?/g, function(_, slash, key, option) { + var optional = (option === '?' || option === '*?') ? '?' : null; + var star = (option === '*' || option === '*?') ? '*' : null; keys.push({ name: key, optional: !!optional }); slash = slash || ''; return '' diff --git a/test/ngRoute/routeSpec.js b/test/ngRoute/routeSpec.js index 1305ba51cfe0..1c7c27d1424a 100644 --- a/test/ngRoute/routeSpec.js +++ b/test/ngRoute/routeSpec.js @@ -974,6 +974,32 @@ describe('$route', function() { }); + it('should properly process route params which are both eager and optional', function() { + module(function($routeProvider) { + $routeProvider.when('/foo/:param1*?/:param2', {templateUrl: 'foo.html'}); + }); + + inject(function($location, $rootScope, $route) { + $location.path('/foo/bar1/bar2/bar3/baz'); + $rootScope.$digest(); + + expect($location.path()).toEqual('/foo/bar1/bar2/bar3/baz'); + expect($route.current.params.param1).toEqual('bar1/bar2/bar3'); + expect($route.current.params.param2).toEqual('baz'); + expect($route.current.templateUrl).toEqual('foo.html'); + + $location.path('/foo/baz'); + $rootScope.$digest(); + + expect($location.path()).toEqual('/foo/baz'); + expect($route.current.params.param1).toEqual(undefined); + expect($route.current.params.param2).toEqual('baz'); + expect($route.current.templateUrl).toEqual('foo.html'); + + }); + }); + + it('should properly interpolate optional and eager route vars ' + 'when redirecting from path with trailing slash', function() { module(function($routeProvider) { From 6b9a697a823fa6ea9143a79341c26a2dd7316dc9 Mon Sep 17 00:00:00 2001 From: Sean Murphy Date: Thu, 11 Feb 2016 12:26:22 -0800 Subject: [PATCH 2/3] feat(ngRoute):change pathRegExp to respect optional wildcard route params change pathRegExp to accept the two-character option of '*?' on route params to allow them to be wildcard and optional at the same time. Motivation for change inspired by the need to accept an auth token on the URL that contained slashes (need for wildcard param) but this token on the URL was not required (need for optional param). Previous behavior only saw the param as wildcard and not optional, which resulted in the need for a trailing slash on the url (e.g. /login/ vs. /login) new feature - does not address any open issues --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 1fc06c58d2ef..7fa1dbd38fc2 100644 --- a/package.json +++ b/package.json @@ -32,13 +32,13 @@ "dgeni-packages": "^0.11.0", "event-stream": "~3.1.0", "glob": "^6.0.1", - "grunt": "https://registry.npmjs.org/grunt/-/grunt-0.4.5.tgz", + "grunt": "~0.4.2", "grunt-bump": "~0.0.13", "grunt-contrib-clean": "~0.6.0", "grunt-contrib-compress": "~0.12.0", "grunt-contrib-connect": "~0.8.0", "grunt-contrib-copy": "~0.6.0", - "grunt-contrib-jshint": "https://registry.npmjs.org/grunt-contrib-jshint/-/grunt-contrib-jshint-0.10.0.tgz", + "grunt-contrib-jshint": "~0.10.0", "grunt-ddescribe-iit": "~0.0.1", "grunt-jasmine-node": "git://github.com/vojtajina/grunt-jasmine-node.git#fix-grunt-exit-code", "grunt-jscs": "^2.1.0", From bcb5abba68170b48525e28affb4a8b85b08aaf1c Mon Sep 17 00:00:00 2001 From: Sean Murphy Date: Thu, 11 Feb 2016 12:37:52 -0800 Subject: [PATCH 3/3] feat(ngRoute):change pathRegExp to respect optional wildcard route params change pathRegExp to accept the two-character option of '*?' on route params to allow them to be wildcard and optional at the same time. Motivation for change inspired by the need to accept an auth token on the URL that contained slashes (need for wildcard param) but this token on the URL was not required (need for optional param). Previous behavior only saw the param as wildcard and not optional, which resulted in the need for a trailing slash on the url (e.g. /login/ vs. /login) new feature - does not address any open issues --- test/ngRoute/routeSpec.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/ngRoute/routeSpec.js b/test/ngRoute/routeSpec.js index 1c7c27d1424a..889489a0d63d 100644 --- a/test/ngRoute/routeSpec.js +++ b/test/ngRoute/routeSpec.js @@ -981,20 +981,20 @@ describe('$route', function() { inject(function($location, $rootScope, $route) { $location.path('/foo/bar1/bar2/bar3/baz'); - $rootScope.$digest(); + $rootScope.$digest(); - expect($location.path()).toEqual('/foo/bar1/bar2/bar3/baz'); - expect($route.current.params.param1).toEqual('bar1/bar2/bar3'); - expect($route.current.params.param2).toEqual('baz'); - expect($route.current.templateUrl).toEqual('foo.html'); + expect($location.path()).toEqual('/foo/bar1/bar2/bar3/baz'); + expect($route.current.params.param1).toEqual('bar1/bar2/bar3'); + expect($route.current.params.param2).toEqual('baz'); + expect($route.current.templateUrl).toEqual('foo.html'); - $location.path('/foo/baz'); - $rootScope.$digest(); + $location.path('/foo/baz'); + $rootScope.$digest(); - expect($location.path()).toEqual('/foo/baz'); - expect($route.current.params.param1).toEqual(undefined); - expect($route.current.params.param2).toEqual('baz'); - expect($route.current.templateUrl).toEqual('foo.html'); + expect($location.path()).toEqual('/foo/baz'); + expect($route.current.params.param1).toEqual(undefined); + expect($route.current.params.param2).toEqual('baz'); + expect($route.current.templateUrl).toEqual('foo.html'); }); });