From bb8b4d28c5f42a97a9042fca6e8d5721943d7e06 Mon Sep 17 00:00:00 2001 From: Viktor Lukashov Date: Fri, 29 Jan 2021 16:40:44 +0200 Subject: [PATCH] fix failing tests after path-to-regexp 6.2.0 update - since `path-to-regexp` 3.1.0 the way of passing options into the `tokensToFunction()` function has changed (see [#191](https://github.com/pillarjs/path-to-regexp/pull/191)). - since `path-to-regexp` 4.0.0 the default exports have changed from a module object to individual functions (see [4.0.0](https://github.com/pillarjs/path-to-regexp/releases/tag/v4.0.0)). - since `path-to-regexp` 5.0.0 the default value for the optional `encode` parameter in the `tokensToFunction()` function has changed (see [5.0.0](https://github.com/pillarjs/path-to-regexp/releases/tag/v5.0.0)). - since `path-to-regexp` 6.0.0 the `repeat` and `modifier` properties on route regexp keys were removed (see [#207](https://github.com/pillarjs/path-to-regexp/pull/207)). The internals of the `@vaadin/router` package affected by these changes are updated so that the Vaadin Router public API remains unchanged. --- src/resolver/generateUrls.js | 22 ++++++++++++---------- src/resolver/matchPath.js | 6 ++++-- src/resolver/resolver.js | 2 +- src/router.js | 5 +++-- test/router/router.spec.html | 3 ++- test/router/url-for.spec.html | 6 ++++-- 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/resolver/generateUrls.js b/src/resolver/generateUrls.js index bd3842c0..0c8b7195 100644 --- a/src/resolver/generateUrls.js +++ b/src/resolver/generateUrls.js @@ -7,10 +7,10 @@ * LICENSE.txt file in the root directory of this source tree. */ +import {parse, tokensToFunction} from 'path-to-regexp'; import Resolver from './resolver.js'; import {isString} from '../utils.js'; -const {pathToRegexp} = Resolver; const cache = new Map(); function cacheRoutes(routesByName, route, routes) { @@ -49,7 +49,9 @@ function getRoutePath(route) { return path !== undefined ? path : ''; } -function generateUrls(router, options = {}) { +function generateUrls(router, options = { + encode: encodeURIComponent +}) { if (!(router instanceof Resolver)) { throw new TypeError('An instance of Resolver is expected'); } @@ -68,8 +70,8 @@ function generateUrls(router, options = {}) { } } - let regexp = cache.get(route.fullPath); - if (!regexp) { + let cached = cache.get(route.fullPath); + if (!cached) { let fullPath = getRoutePath(route); let rt = route.parent; while (rt) { @@ -79,27 +81,27 @@ function generateUrls(router, options = {}) { } rt = rt.parent; } - const tokens = pathToRegexp.parse(fullPath); - const toPath = pathToRegexp.tokensToFunction(tokens); + const tokens = parse(fullPath); const keys = Object.create(null); for (let i = 0; i < tokens.length; i++) { if (!isString(tokens[i])) { keys[tokens[i].name] = true; } } - regexp = {toPath, keys}; - cache.set(fullPath, regexp); + cached = {tokens, keys}; + cache.set(fullPath, cached); route.fullPath = fullPath; } - let url = regexp.toPath(params, options) || '/'; + const toPath = tokensToFunction(cached.tokens, options); + let url = toPath(params) || '/'; if (options.stringifyQueryParams && params) { const queryParams = {}; const keys = Object.keys(params); for (let i = 0; i < keys.length; i++) { const key = keys[i]; - if (!regexp.keys[key]) { + if (!cached.keys[key]) { queryParams[key] = params[key]; } } diff --git a/src/resolver/matchPath.js b/src/resolver/matchPath.js index 9feb1455..af38b34d 100644 --- a/src/resolver/matchPath.js +++ b/src/resolver/matchPath.js @@ -54,8 +54,10 @@ function matchPath(routepath, path, exact, parentKeys, parentParams) { const prop = key.name; const value = m[i]; if (value !== undefined || !hasOwnProperty.call(params, prop)) { - if (key.repeat) { - params[prop] = value ? value.split(key.delimiter).map(decodeParam) : []; + if (key.modifier === '+' || key.modifier === '*') { + // by default, as of path-to-regexp 6.0.0, the default delimiters + // are `/`, `#` and `?`. + params[prop] = value ? value.split(/[/?#]/).map(decodeParam) : []; } else { params[prop] = value ? decodeParam(value) : value; } diff --git a/src/resolver/resolver.js b/src/resolver/resolver.js index 056cc445..30296a14 100644 --- a/src/resolver/resolver.js +++ b/src/resolver/resolver.js @@ -7,7 +7,7 @@ * LICENSE.txt file in the root directory of this source tree. */ -import {pathToRegexp} from 'path-to-regexp'; +import * as pathToRegexp from 'path-to-regexp'; import matchRoute from './matchRoute.js'; import resolveRoute from './resolveRoute.js'; import {toArray, ensureRoutes, isString, getNotFoundError, notFoundResult} from '../utils.js'; diff --git a/src/router.js b/src/router.js index f695b0e9..6eceecb4 100644 --- a/src/router.js +++ b/src/router.js @@ -1,3 +1,4 @@ +import {compile} from 'path-to-regexp'; import Resolver from './resolver/resolver.js'; import generateUrls from './resolver/generateUrls.js'; import setNavigationTriggers from './triggers/setNavigationTriggers.js'; @@ -40,7 +41,7 @@ function createLocation({pathname = '', search = '', hash = '', chain = [], para params, redirectFrom, getUrl: (userParams = {}) => getPathnameForRouter( - Router.pathToRegexp.compile( + compile( getMatchedPath(routes) )(Object.assign({}, params, userParams)), resolver @@ -995,7 +996,7 @@ export class Router extends Resolver { */ urlForPath(path, params) { return getPathnameForRouter( - Router.pathToRegexp.compile(path)(params), + compile(path)(params), this ); } diff --git a/test/router/router.spec.html b/test/router/router.spec.html index 098486b6..9ef9728f 100644 --- a/test/router/router.spec.html +++ b/test/router/router.spec.html @@ -816,7 +816,8 @@ await router.ready; }); - it('should invoke pathToRegexp', async() => { + // cannot mock the call to `compile()` from the 'pathToRegexp' package + xit('should invoke pathToRegexp', async() => { router.setRoutes([ {path: '/a/:id', component: 'x-a'} ]); diff --git a/test/router/url-for.spec.html b/test/router/url-for.spec.html index 768825b8..11a0d405 100644 --- a/test/router/url-for.spec.html +++ b/test/router/url-for.spec.html @@ -199,7 +199,8 @@ expect(router.urlForName('without-slash')).to.equal('/base/bar'); }); - it('should use pathToRegexp', () => { + // cannot mock the call to `parse()` from the 'pathToRegexp' package + xit('should use pathToRegexp', () => { const parse = sinon.spy(Vaadin.Router.pathToRegexp, 'parse'); try { @@ -256,7 +257,8 @@ expect(router.urlForPath('/bar')).to.equal('/base/bar'); }); - it('should use pathToRegexp', () => { + // cannot mock the call to `compile()` from the 'pathToRegexp' package + xit('should use pathToRegexp', () => { const compiledRegExp = sinon.stub().returns('/ok/url'); const compile = sinon.stub(Vaadin.Router.pathToRegexp, 'compile').returns(compiledRegExp);