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

Commit

Permalink
feat($route): add caseInsensitiveMatch option for url matching
Browse files Browse the repository at this point in the history
with this property urls can be matched case-insensitively which
enables some new use cases.
  • Loading branch information
davidchang authored and IgorMinar committed Feb 27, 2013
1 parent 60f1f09 commit 5e18a15
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/ng/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,18 @@ function $RouteProvider(){
* If the option is set to `false` and url in the browser changes, then
* `$routeUpdate` event is broadcasted on the root scope.
*
* - `[caseInsensitiveMatch=false]` - {boolean=} - match routes without being case sensitive
*
* If the option is set to `true`, then the particular route can be matched without being
* case sensitive
*
* @returns {Object} self
*
* @description
* Adds a new route definition to the `$route` service.
*/
this.when = function(path, route) {
routes[path] = extend({reloadOnSearch: true}, route);
routes[path] = extend({reloadOnSearch: true, caseInsensitiveMatch: false}, route);

// create redirection for trailing slashes
if (path) {
Expand Down Expand Up @@ -343,14 +348,16 @@ function $RouteProvider(){
/**
* @param on {string} current url
* @param when {string} route when template to match the url against
* @param whenProperties {Object} properties to define when's matching behavior
* @return {?Object}
*/
function switchRouteMatcher(on, when) {
function switchRouteMatcher(on, when, whenProperties) {
// TODO(i): this code is convoluted and inefficient, we should construct the route matching
// regex only once and then reuse it

// Escape regexp special characters.
when = '^' + when.replace(/[-\/\\^$:*+?.()|[\]{}]/g, "\\$&") + '$';

var regex = '',
params = [],
dst = {};
Expand All @@ -377,7 +384,7 @@ function $RouteProvider(){
// Append trailing path part.
regex += when.substr(lastMatchedIndex);

var match = on.match(new RegExp(regex));
var match = on.match(new RegExp(regex, whenProperties.caseInsensitiveMatch ? 'i' : ''));
if (match) {
forEach(params, function(name, index) {
dst[name] = match[index + 1];
Expand Down Expand Up @@ -466,7 +473,7 @@ function $RouteProvider(){
// Match a route
var params, match;
forEach(routes, function(route, path) {
if (!match && (params = switchRouteMatcher($location.path(), path))) {
if (!match && (params = switchRouteMatcher($location.path(), path, route))) {
match = inherit(route, {
params: extend({}, $location.search(), params),
pathParams: params});
Expand Down
66 changes: 66 additions & 0 deletions test/ng/routeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,72 @@ describe('$route', function() {
});
});


it('should route and fire change event correctly whenever the case insensitive flag is utilized', function() {
var log = '',
lastRoute,
nextRoute;

module(function($routeProvider) {
$routeProvider.when('/Book1/:book/Chapter/:chapter/*highlight/edit',
{controller: noop, templateUrl: 'Chapter.html', caseInsensitiveMatch: true});
$routeProvider.when('/Book2/:book/*highlight/Chapter/:chapter',
{controller: noop, templateUrl: 'Chapter.html'});
$routeProvider.when('/Blank', {});
});
inject(function($route, $location, $rootScope) {
$rootScope.$on('$routeChangeStart', function(event, next, current) {
log += 'before();';
expect(current).toBe($route.current);
lastRoute = current;
nextRoute = next;
});
$rootScope.$on('$routeChangeSuccess', function(event, current, last) {
log += 'after();';
expect(current).toBe($route.current);
expect(lastRoute).toBe(last);
expect(nextRoute).toBe(current);
});

$location.path('/Book1/Moby/Chapter/Intro/one/edit').search('p=123');
$rootScope.$digest();
$httpBackend.flush();
expect(log).toEqual('before();after();');
expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', highlight:'one', p:'123'});

log = '';
$location.path('/BOOK1/Moby/CHAPTER/Intro/one/EDIT').search('p=123');
$rootScope.$digest();
expect(log).toEqual('before();after();');
expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', highlight:'one', p:'123'});

log = '';
$location.path('/Blank').search('ignore');
$rootScope.$digest();
expect(log).toEqual('before();after();');
expect($route.current.params).toEqual({ignore:true});

log = '';
$location.path('/BLANK');
$rootScope.$digest();
expect(log).toEqual('before();after();');
expect($route.current).toEqual(null);

log = '';
$location.path('/Book2/Moby/one/two/Chapter/Intro').search('p=123');
$rootScope.$digest();
expect(log).toEqual('before();after();');
expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', highlight:'one/two', p:'123'});

log = '';
$location.path('/BOOK2/Moby/one/two/CHAPTER/Intro').search('p=123');
$rootScope.$digest();
expect(log).toEqual('before();after();');
expect($route.current).toEqual(null);
});
});


it('should not change route when location is canceled', function() {
module(function($routeProvider) {
$routeProvider.when('/somePath', {template: 'some path'});
Expand Down

0 comments on commit 5e18a15

Please sign in to comment.