Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- petersendidit
- RobHannay
- sergiodxa
- shamsup
- shivamsinghchahar
- thisiskartik
- timdorr
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from "react";
import * as TestRenderer from "react-test-renderer";
import { MemoryRouter, Outlet, Routes, Route } from "react-router";
import { MemoryRouter, Outlet, Routes, Route, useParams } from "react-router";

describe("Descendant <Routes> splat matching", () => {
describe("when the parent route path ends with /*", () => {
Expand Down Expand Up @@ -57,5 +57,72 @@ describe("Descendant <Routes> splat matching", () => {
</div>
`);
});
it("works with paths beginning with special characters", () => {
function PrintParams() {
return <p>The params are {JSON.stringify(useParams())}</p>;
}
function ReactCourses() {
return (
<div>
<h1>React</h1>
<Routes>
<Route
path=":splat"
element={
<div>
<h1>React Fundamentals</h1>
<PrintParams />
</div>
}
/>
</Routes>
</div>
);
}

function Courses() {
return (
<div>
<h1>Courses</h1>
<Outlet />
</div>
);
}

let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/courses/react/-react-fundamentals"]}>
<Routes>
<Route path="courses" element={<Courses />}>
<Route path="react/*" element={<ReactCourses />} />
</Route>
</Routes>
</MemoryRouter>
);
});

expect(renderer.toJSON()).toMatchInlineSnapshot(`
<div>
<h1>
Courses
</h1>
<div>
<h1>
React
</h1>
<div>
<h1>
React Fundamentals
</h1>
<p>
The params are
{"*":"-react-fundamentals","splat":"-react-fundamentals"}
</p>
</div>
</div>
</div>
`);
});
});
});
207 changes: 207 additions & 0 deletions packages/react-router/__tests__/layout-routes-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,211 @@ describe("A layout route", () => {
</h1>
`);
});
describe("matches when a nested splat route begins with a special character", () => {
it("allows routes starting with `-`", () => {
let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/-splat"]}>
<Routes>
<Route
element={
<div>
<h1>Layout</h1>
<Outlet />
</div>
}
>
<Route
path="*"
element={
<div>
<h1>Splat</h1>
</div>
}
/>
</Route>
</Routes>
</MemoryRouter>
);
});

expect(renderer.toJSON()).toMatchInlineSnapshot(`
<div>
<h1>
Layout
</h1>
<div>
<h1>
Splat
</h1>
</div>
</div>
`);
});
it("allows routes starting with `~`", () => {
let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/~splat"]}>
<Routes>
<Route
element={
<div>
<h1>Layout</h1>
<Outlet />
</div>
}
>
<Route
path="*"
element={
<div>
<h1>Splat</h1>
</div>
}
/>
</Route>
</Routes>
</MemoryRouter>
);
});

expect(renderer.toJSON()).toMatchInlineSnapshot(`
<div>
<h1>
Layout
</h1>
<div>
<h1>
Splat
</h1>
</div>
</div>
`);
});
it("allows routes starting with `_`", () => {
let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/_splat"]}>
<Routes>
<Route
element={
<div>
<h1>Layout</h1>
<Outlet />
</div>
}
>
<Route
path="*"
element={
<div>
<h1>Splat</h1>
</div>
}
/>
</Route>
</Routes>
</MemoryRouter>
);
});

expect(renderer.toJSON()).toMatchInlineSnapshot(`
<div>
<h1>
Layout
</h1>
<div>
<h1>
Splat
</h1>
</div>
</div>
`);
});
it("allows routes starting with `.`", () => {
let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/.splat"]}>
<Routes>
<Route
element={
<div>
<h1>Layout</h1>
<Outlet />
</div>
}
>
<Route
path="*"
element={
<div>
<h1>Splat</h1>
</div>
}
/>
</Route>
</Routes>
</MemoryRouter>
);
});

expect(renderer.toJSON()).toMatchInlineSnapshot(`
<div>
<h1>
Layout
</h1>
<div>
<h1>
Splat
</h1>
</div>
</div>
`);
});
it("allows routes starting with url-encoded entities", () => {
let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/%20splat"]}>
<Routes>
<Route
element={
<div>
<h1>Layout</h1>
<Outlet />
</div>
}
>
<Route
path="*"
element={
<div>
<h1>Splat</h1>
</div>
}
/>
</Route>
</Routes>
</MemoryRouter>
);
});

expect(renderer.toJSON()).toMatchInlineSnapshot(`
<div>
<h1>
Layout
</h1>
<div>
<h1>
Splat
</h1>
</div>
</div>
`);
});
});
});
5 changes: 4 additions & 1 deletion packages/react-router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,10 @@ function compilePath(
: // Otherwise, match a word boundary or a proceeding /. The word boundary restricts
// parent routes to matching only their own words and nothing more, e.g. parent
// route "/home" should not match "/home2".
"(?:\\b|\\/|$)";
// Additionally, allow paths starting with `.`, `-`, `~`, and url-encoded entities,
// but do not consume the character in the matched path so they can match against
// nested paths.
"(?:(?=[.~-]|%[0-7][0-9A-F])|(?:\\b|\\/|$))";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(?=...) is a positive lookahead that doesn't consume the characters. Using this, we can use the characters to match, but they won't be part of the match string that gets removed to make the remaining path for child routes.

ie

(/(?:(?=a)|b)/).exec('a'); // => ['']
(/(?:(?=a)|b)/).exec('b'); // => ['b']

}

let matcher = new RegExp(regexpSource, caseSensitive ? undefined : "i");
Expand Down