Skip to content

Commit a3261e8

Browse files
jjuutilatroch
authored andcommitted
fix: fix path with a plus character not matched
The root cause for the problem is that when `consumedPath` contains a regexp character, then the regexp `new RegExp('^' + consumedPath, 'i')` doesn't match `segment`. For example when `consumedPath` is `/foo+bar` then the regexp is `^/foo+bar` and that doesn't match to `"/foo+bar"`. So it is better to avoid the use of regexp here so we don't need to escape user input.
1 parent 1729950 commit a3261e8

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

modules/matchChildren.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ const matchChildren = (
5858
consumedPath = consumedPath.replace(/\/$/, '')
5959
}
6060

61-
remainingPath = segment.replace(
62-
new RegExp('^' + consumedPath, 'i'),
63-
''
64-
)
61+
// Can't create a regexp from the path because it might contain a
62+
// regexp character.
63+
if (segment.toLowerCase().indexOf(consumedPath.toLowerCase()) === 0) {
64+
remainingPath = segment.slice(consumedPath.length)
65+
}
6566

6667
if (!strictTrailingSlash && !child.children.length) {
6768
remainingPath = remainingPath.replace(/^\/\?/, '?')

test/main.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('RouteNode', function() {
5555
i.should.not.equal(0)
5656
})
5757

58-
it.only('should perform a final sort all routes after adding them', () => {
58+
it('should perform a final sort all routes after adding them', () => {
5959
const routes = [...Array(10)].map((_, index) => ({
6060
name: `r${index}`,
6161
path: `/${index}`,
@@ -763,6 +763,26 @@ describe('RouteNode', function() {
763763
c: ['1']
764764
})
765765
})
766+
767+
it('should match path with a plus character', () => {
768+
const routes = [
769+
{
770+
name: 'page',
771+
path: '/:name<.+>/:id<(\\w{2}[0-9]{4})>.html'
772+
}
773+
]
774+
const options = {
775+
allowNotFound: true,
776+
trailingSlashMode: 'never',
777+
queryParamsMode: 'loose',
778+
queryParams: {nullFormat: 'hidden'}
779+
}
780+
const node = new RouteNode('', '', routes)
781+
node.matchPath('/foo+bar/AB1234.html', options).params.should.eql({
782+
id: 'AB1234',
783+
name: 'foo+bar'
784+
})
785+
})
766786
})
767787

768788
function getRoutes(trailingSlash) {

0 commit comments

Comments
 (0)