Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make url-encoding history-aware #9496

Merged
merged 4 commits into from Oct 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-countries-cheat.md
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

make url-encoding history-aware
272 changes: 271 additions & 1 deletion packages/react-router-dom/__tests__/special-characters-test.tsx
Expand Up @@ -9,7 +9,15 @@ import {
waitFor,
screen,
} from "@testing-library/react";
import type { Location, Params } from "react-router-dom";
import {
createHashRouter,
createMemoryRouter,
HashRouter,
Location,
MemoryRouter,
Params,
useNavigate,
} from "react-router-dom";
import {
BrowserRouter,
Link,
Expand Down Expand Up @@ -709,4 +717,266 @@ describe("special character tests", () => {
}
});
});

describe("encodes characters based on history implementation", () => {
function ShowPath() {
let { pathname, search, hash } = useLocation();
return <pre>{JSON.stringify({ pathname, search, hash })}</pre>;
}

describe("memory routers", () => {
it("does not encode characters in MemoryRouter", () => {
let ctx = render(
<MemoryRouter initialEntries={["/with space"]}>
<Routes>
<Route path="/with space" element={<ShowPath />} />
</Routes>
</MemoryRouter>
);

expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});

it("does not encode characters in MemoryRouter (navigate)", () => {
function Start() {
let navigate = useNavigate();
React.useEffect(() => navigate("/with space"), []);
return null;
}
let ctx = render(
<MemoryRouter>
<Routes>
<Route path="/" element={<Start />} />
<Route path="/with space" element={<ShowPath />} />
</Routes>
</MemoryRouter>
);

expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});

it("does not encode characters in createMemoryRouter", () => {
let router = createMemoryRouter(
[{ path: "/with space", element: <ShowPath /> }],
{ initialEntries: ["/with space"] }
);
let ctx = render(<RouterProvider router={router} />);

expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});

it("does not encode characters in createMemoryRouter (navigate)", () => {
function Start() {
let navigate = useNavigate();
React.useEffect(() => navigate("/with space"), []);
return null;
}
let router = createMemoryRouter([
{ path: "/", element: <Start /> },
{ path: "/with space", element: <ShowPath /> },
]);
let ctx = render(<RouterProvider router={router} />);

expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});
});

describe("browser routers", () => {
let testWindow: Window;

beforeEach(() => {
// Need to use our own custom DOM in order to get a working history
const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
url: "https://remix.run/",
});
testWindow = dom.window as unknown as Window;
testWindow.history.pushState({}, "", "/");
});

it("encodes characters in BrowserRouter", () => {
testWindow.history.replaceState(null, "", "/with space");

let ctx = render(
<BrowserRouter window={testWindow}>
<Routes>
<Route path="/with space" element={<ShowPath />} />
</Routes>
</BrowserRouter>
);

expect(testWindow.location.pathname).toBe("/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});

it("encodes characters in BrowserRouter (navigate)", () => {
testWindow.history.replaceState(null, "", "/");

function Start() {
let navigate = useNavigate();
React.useEffect(() => navigate("/with space"), []);
return null;
}

let ctx = render(
<BrowserRouter window={testWindow}>
<Routes>
<Route path="/" element={<Start />} />
<Route path="/with space" element={<ShowPath />} />
</Routes>
</BrowserRouter>
);

expect(testWindow.location.pathname).toBe("/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});

it("encodes characters in createBrowserRouter", () => {
testWindow.history.replaceState(null, "", "/with space");

let router = createBrowserRouter(
[{ path: "/with space", element: <ShowPath /> }],
{ window: testWindow }
);
let ctx = render(<RouterProvider router={router} />);

expect(testWindow.location.pathname).toBe("/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});

it("encodes characters in createBrowserRouter (navigate)", () => {
testWindow.history.replaceState(null, "", "/with space");

function Start() {
let navigate = useNavigate();
React.useEffect(() => navigate("/with space"), []);
return null;
}

let router = createBrowserRouter(
[
{ path: "/", element: <Start /> },
{ path: "/with space", element: <ShowPath /> },
],
{ window: testWindow }
);
let ctx = render(<RouterProvider router={router} />);

expect(testWindow.location.pathname).toBe("/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});
});

describe("hash routers", () => {
let testWindow: Window;

beforeEach(() => {
// Need to use our own custom DOM in order to get a working history
const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
url: "https://remix.run/",
});
testWindow = dom.window as unknown as Window;
testWindow.history.pushState({}, "", "/");
});

it("encodes characters in HashRouter", () => {
testWindow.history.replaceState(null, "", "/#/with space");

let ctx = render(
<HashRouter window={testWindow}>
<Routes>
<Route path="/with space" element={<ShowPath />} />
</Routes>
</HashRouter>
);

expect(testWindow.location.pathname).toBe("/");
expect(testWindow.location.hash).toBe("#/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});

it("encodes characters in HashRouter (navigate)", () => {
testWindow.history.replaceState(null, "", "/");

function Start() {
let navigate = useNavigate();
React.useEffect(() => navigate("/with space"), []);
return null;
}

let ctx = render(
<HashRouter window={testWindow}>
<Routes>
<Route path="/" element={<Start />} />
<Route path="/with space" element={<ShowPath />} />
</Routes>
</HashRouter>
);

expect(testWindow.location.pathname).toBe("/");
expect(testWindow.location.hash).toBe("#/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});

it("encodes characters in createHashRouter", () => {
testWindow.history.replaceState(null, "", "/#/with space");

let router = createHashRouter(
[{ path: "/with space", element: <ShowPath /> }],
{ window: testWindow }
);
let ctx = render(<RouterProvider router={router} />);

expect(testWindow.location.pathname).toBe("/");
expect(testWindow.location.hash).toBe("#/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});

it("encodes characters in createHashRouter (navigate)", () => {
testWindow.history.replaceState(null, "", "/");

function Start() {
let navigate = useNavigate();
React.useEffect(() => navigate("/with space"), []);
return null;
}

let router = createHashRouter(
[
{ path: "/", element: <Start /> },
{ path: "/with space", element: <ShowPath /> },
],
{ window: testWindow }
);
let ctx = render(<RouterProvider router={router} />);

expect(testWindow.location.pathname).toBe("/");
expect(testWindow.location.hash).toBe("#/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});
});
});
});
31 changes: 31 additions & 0 deletions packages/router/history.ts
Expand Up @@ -125,6 +125,15 @@ export interface History {
*/
createHref(to: To): string;

/**
* Encode a location the same way window.history would do (no-op for memory
* history) so we ensure our PUSH/REPLAC e navigations for data routers
* behave the same as POP
*
* @param location The incoming location from router.navigate()
*/
encodeLocation(location: Location): Location;

/**
* Pushes a new location onto the history stack, increasing its length by one.
* If there were any entries in the stack after the current one, they are
Expand Down Expand Up @@ -259,6 +268,9 @@ export function createMemoryHistory(
createHref(to) {
return typeof to === "string" ? to : createPath(to);
},
encodeLocation(location) {
return location;
},
push(to, state) {
action = Action.Push;
let nextLocation = createMemoryLocation(to, state);
Expand Down Expand Up @@ -527,6 +539,15 @@ export function parsePath(path: string): Partial<Path> {
return parsedPath;
}

export function createURL(location: Location | string): URL {
let base =
typeof window !== "undefined" && typeof window.location !== "undefined"
? window.location.origin
: "unknown://unknown";
let href = typeof location === "string" ? location : createPath(location);
return new URL(href, base);
}

export interface UrlHistory extends History {}

export type UrlHistoryOptions = {
Expand Down Expand Up @@ -610,6 +631,16 @@ function getUrlBasedHistory(
createHref(to) {
return createHref(window, to);
},
encodeLocation(location) {
// Encode a Location the same way window.location would
let url = createURL(createPath(location));
return {
...location,
pathname: url.pathname,
search: url.search,
hash: url.hash,
};
},
push,
replace,
go(n) {
Expand Down