From 08e010fb3bcf7ae0c6050730f88e8ccb0d692f09 Mon Sep 17 00:00:00 2001 From: Masquerade Circus Date: Fri, 22 Sep 2023 20:10:24 -0600 Subject: [PATCH] refactor(router): allow routes ending with #id --- dist/router/index.js | 2 +- dist/router/index.mjs | 2 +- lib/router/index.ts | 2 +- test/router_test.js | 23 +++++++++++++++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/dist/router/index.js b/dist/router/index.js index 4bf88cf..73ab4c2 100644 --- a/dist/router/index.js +++ b/dist/router/index.js @@ -165,7 +165,7 @@ var Router = class { const parts = constructedPath.split("?", 2); this.url = constructedPath; this.query = parseQuery(parts[1]); - const middlewares = searchMiddlewares(this, parts[0].replace(/(.+)\/$/, "$1")); + const middlewares = searchMiddlewares(this, parts[0].replace(/(.+)\/$/, "$1").split("#")[0]); let component = await searchComponent(this, middlewares); if (component === false) { return; diff --git a/dist/router/index.mjs b/dist/router/index.mjs index 845cee0..195c487 100644 --- a/dist/router/index.mjs +++ b/dist/router/index.mjs @@ -148,7 +148,7 @@ var Router = class { const parts = constructedPath.split("?", 2); this.url = constructedPath; this.query = parseQuery(parts[1]); - const middlewares = searchMiddlewares(this, parts[0].replace(/(.+)\/$/, "$1")); + const middlewares = searchMiddlewares(this, parts[0].replace(/(.+)\/$/, "$1").split("#")[0]); let component = await searchComponent(this, middlewares); if (component === false) { return; diff --git a/lib/router/index.ts b/lib/router/index.ts index 5aaf611..bb725d2 100644 --- a/lib/router/index.ts +++ b/lib/router/index.ts @@ -270,7 +270,7 @@ export class Router implements RouterInterface { this.url = constructedPath; this.query = parseQuery(parts[1]); - const middlewares = searchMiddlewares(this as RouterInterface, parts[0].replace(/(.+)\/$/, "$1")); + const middlewares = searchMiddlewares(this as RouterInterface, parts[0].replace(/(.+)\/$/, "$1").split("#")[0]); let component = await searchComponent(this as RouterInterface, middlewares); if (component === false) { diff --git a/test/router_test.js b/test/router_test.js index 6e4f575..7cc3845 100644 --- a/test/router_test.js +++ b/test/router_test.js @@ -430,6 +430,29 @@ describe("Router", () => { }); }); + it("Test the onClick handler from a route with # to another route with #", async () => { + let router = new Router(); + let Component = () =>
Hello {router.url}
; + + router.add("/world", () => ); + mountRouter("body", router); + let result = { + before: await router.go("/world#world") + }; + + let handler = router.getOnClickHandler("/world#Mike"); + handler({ preventDefault: () => {} }); + + await new Promise((resolve) => setTimeout(resolve, 10)); + + result.after = update(); + + expect(result).toEqual({ + before: "
Hello /world#world
", + after: "
Hello /world#Mike
" + }); + }); + it("Test the initial prefix", async () => { let subrouter = new Router();