Skip to content

Commit 5bcf653

Browse files
committed
[fixed] Double slash in href when parent route has optional trailing slash
When a parent route of a child route has an optional trailing slash, the generated path for the child route will have two slashes in a row. Fix the trailing slash matcher pattern to account for this case. This bug occurs when calling `Path.injectParams(pattern, params)` to generate a path. The `pattern` for the path of a child route whose parent has an optional trailing slash looks something like `parent/?/child`. If we just replace `/?` with `/` we end up with `parent//child`, so we need to replace `/?/` with `/`. This fixes issue #768.
1 parent e43293d commit 5bcf653

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

modules/utils/Path.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var qs = require('qs');
44

55
var paramCompileMatcher = /:([a-zA-Z_$][a-zA-Z0-9_$]*)|[*.()\[\]\\+|{}^$]/g;
66
var paramInjectMatcher = /:([a-zA-Z_$][a-zA-Z0-9_$?]*[?]?)|[*]/g;
7-
var paramInjectTrailingSlashMatcher = /\/\/\?|\/\?/g;
7+
var paramInjectTrailingSlashMatcher = /\/\/\?|\/\?\/|\/\?/g;
88
var queryMatcher = /\?(.+)/;
99

1010
var _compiledPatterns = {};

modules/utils/__tests__/Path-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ describe('Path.injectParams', function () {
274274
expect(Path.injectParams('/foo.bar.baz')).toEqual('/foo.bar.baz');
275275
});
276276
});
277+
278+
describe('when a pattern has optional slashes', function () {
279+
it('returns the correct path', function () {
280+
expect(Path.injectParams('/foo/?/bar/?/baz/?')).toEqual('/foo/bar/baz/');
281+
});
282+
});
277283
});
278284

279285
describe('Path.extractQuery', function () {

0 commit comments

Comments
 (0)