Skip to content

Ensuring stable sort of route rules used for matching URLs. #2502

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

Closed
wants to merge 1 commit 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: 3 additions & 1 deletion src/urlRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
*/
this.rule = function (rule) {
if (!isFunction(rule)) throw new Error("'rule' must be a function");
rule.position = rules.length;
rules.push(rule);
return this;
};
Expand Down Expand Up @@ -308,7 +309,8 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
rules.sort(function(ruleA, ruleB) {
var aLength = ruleA.prefix ? ruleA.prefix.length : 0;
var bLength = ruleB.prefix ? ruleB.prefix.length : 0;
return bLength - aLength;
var lengthDiff = bLength - aLength;
return lengthDiff !== 0 ? lengthDiff : ruleA.position - ruleB.position;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensures a stable sort in Chrome browsers

});

if (!interceptDeferred) listen();
Expand Down
34 changes: 32 additions & 2 deletions test/urlRouterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,31 @@ describe("UrlRouter", function () {
match = ['/foo/bar', $match];
}).when('/bar', function($match) {
match = ['/bar', $match];
});
}).when('/', function() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These rule definitions may seem abundant - but it was the only way I could consistently reproduce the stable sorting issue in Chrome.

match = '/home';
}).when('/abc', function() {
match = '/abc/step1';
}).when('/abc', function() {
match = '/abc/step2';
}).when('/defg', function() {
match = '/defg';
}).when('/hij', function() {
match = '/hij/step1';
}).when('/hij', function() {
match = '/hij/step2';
}).when('/hij', function() {
match = '/hij/step3';
}).when('/hij', function() {
match = '/hij/step4';
}).when('/klmno', function() {
match = '/klmno/step1';
}).when('/klmno', function() {
match = '/klmno/step2';
}).when('/pqr', function() {
match = '/pqr/step1';
}).when('/pqr', function() {
match = '/pqr/step2';}
);
});

module('ui.router.router', 'ui.router.router.test');
Expand All @@ -84,6 +108,12 @@ describe("UrlRouter", function () {
expect(match[0]).toBe("/foo/:param");
expect(match[1]).toEqual({param: 'baz'});
});

it('should default to the first rule defined for same url', function() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test always passed in PhantomJs, but when run in Chrome (grunt karma:debug) this test would fail (before I implemented the stable sort fix).

location.path('/hij');
scope.$emit('$locationChangeSuccess');
expect(match).toBe('/hij/step1');
});

it("should execute rewrite rules", function () {
location.path("/foo");
Expand Down Expand Up @@ -261,4 +291,4 @@ describe("UrlRouter", function () {
});
});

});
});