Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(ngRoute): extract path params containing hashes and/or question marks correctly #16670

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* See {@link ngMock.$httpBackend#when `when`} for more info.
*/
$httpBackend.whenRoute = function(method, url) {
var pathObj = routeToRegExp(url, {caseInsensitiveMatch: true, ignoreTrailingSlashes: true});
var pathObj = routeToRegExp(url, {caseInsensitiveMatch: true, ignoreTrailingSlashes: true, isUrl: true});
return $httpBackend.when(method, pathObj.regexp, undefined, undefined, pathObj.keys);
};

Expand Down Expand Up @@ -1955,7 +1955,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* See {@link ngMock.$httpBackend#expect `expect`} for more info.
*/
$httpBackend.expectRoute = function(method, url) {
var pathObj = routeToRegExp(url, {caseInsensitiveMatch: true, ignoreTrailingSlashes: true});
var pathObj = routeToRegExp(url, {caseInsensitiveMatch: true, ignoreTrailingSlashes: true, isUrl: true});
return $httpBackend.expect(method, pathObj.regexp, undefined, undefined, pathObj.keys);
};

Expand Down
10 changes: 5 additions & 5 deletions src/routeToRegExp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* global routeToRegExp: true */

/**
* @param path {string} path
* @param pathOrUrl {string} path or url
* @param opts {Object} options
* @return {?Object}
*
Expand All @@ -13,10 +13,10 @@
*
* Inspired by pathRexp in visionmedia/express/lib/utils.js.
*/
function routeToRegExp(path, opts) {
function routeToRegExp(pathOrUrl, opts) {
var keys = [];

var pattern = path
var pattern = pathOrUrl
.replace(/([().])/g, '\\$1')
.replace(/(\/)?:(\w+)(\*\?|[?*])?/g, function(_, slash, key, option) {
var optional = option === '?' || option === '*?';
Expand All @@ -25,7 +25,7 @@ function routeToRegExp(path, opts) {
slash = slash || '';
return (
(optional ? '(?:' + slash : slash + '(?:') +
(star ? '([^?#]+?)' : '([^/?#]+)') +
(opts.isUrl ? (star ? '([^?#]+?)' : '([^/?#]+)') : (star ? '(.+?)' : '([^/]+)')) +
(optional ? '?)?' : ')')
);
})
Expand All @@ -36,7 +36,7 @@ function routeToRegExp(path, opts) {
}

return {
originalPath: path,
originalPath: pathOrUrl,
keys: keys,
regexp: new RegExp(
'^' + pattern + '(?:[?#]|$)',
Expand Down
19 changes: 19 additions & 0 deletions test/ngRoute/routeParamsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,24 @@ describe('$routeParams', function() {
});
});

it('should correctly extract path params containing hashes and/or question marks', function() {
module(function($routeProvider) {
$routeProvider.when('/foo/:bar', {});
});

inject(function($rootScope, $route, $location, $routeParams) {
$location.path('/foo/bar#baz');
$rootScope.$digest();
expect($routeParams).toEqual({bar: 'bar#baz'});

$location.path('/foo/bar?baz');
$rootScope.$digest();
expect($routeParams).toEqual({bar: 'bar?baz'});

$location.path('/foo/bar#baz?qux');
$rootScope.$digest();
expect($routeParams).toEqual({bar: 'bar#baz?qux'});
});
});

});