Skip to content

Commit

Permalink
refactor(router): allow routes ending with #id
Browse files Browse the repository at this point in the history
  • Loading branch information
Masquerade-Circus committed Sep 23, 2023
1 parent 118a388 commit 08e010f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion dist/router/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 23 additions & 0 deletions test/router_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => <div>Hello {router.url}</div>;

router.add("/world", () => <Component />);
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: "<div>Hello /world#world</div>",
after: "<div>Hello /world#Mike</div>"
});
});

it("Test the initial prefix", async () => {
let subrouter = new Router();

Expand Down

0 comments on commit 08e010f

Please sign in to comment.