Skip to content

fix($urlRouter): resolve clashing of routes #1585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2015
Merged
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
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