Skip to content

Commit

Permalink
fix($urlRouter): resolve clashing of routes
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Sep 7, 2015
1 parent 1f781c8 commit b5c57c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/urlRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
return listener;
}

rules.sort(function(ruleA, ruleB) {
var aLength = ruleA.prefix ? ruleA.prefix.length : 0;
var bLength = ruleB.prefix ? ruleB.prefix.length : 0;
return bLength - aLength;
});

if (!interceptDeferred) listen();

return {
Expand Down
14 changes: 14 additions & 0 deletions test/urlRouterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ describe("UrlRouter", function () {
return path.replace('baz', 'b4z');
}).when('/foo/:param', function($match) {
match = ['/foo/:param', $match];
}).when('/foo/bar', function($match) {
match = ['/foo/bar', $match];
}).when('/bar', function($match) {
match = ['/bar', $match];
});
Expand All @@ -71,6 +73,18 @@ describe("UrlRouter", function () {
});
});

it("should handle more specified url first", function() {
location.path("/foo/bar");
scope.$emit("$locationChangeSuccess");
expect(match[0]).toBe("/foo/bar");
expect(match[1]).toEqual({});

location.path("/foo/baz");
scope.$emit("$locationChangeSuccess");
expect(match[0]).toBe("/foo/:param");
expect(match[1]).toEqual({param: 'baz'});
});

it("should execute rewrite rules", function () {
location.path("/foo");
scope.$emit("$locationChangeSuccess");
Expand Down

0 comments on commit b5c57c8

Please sign in to comment.