fix: Error with search term including %, closes #1891

This commit is contained in:
Tom Moor
2021-02-20 13:03:41 -08:00
parent 903e83a618
commit e66611e771
4 changed files with 24 additions and 3 deletions

View File

@@ -74,7 +74,7 @@ export function searchUrl(
let route = "/search";
if (query) {
route += `/${encodeURIComponent(query)}`;
route += `/${encodeURIComponent(query.replace("%", "%25"))}`;
}
search = search ? `?${search}` : "";

View File

@@ -28,3 +28,9 @@ export function cdnPath(path: string): string {
export function imagePath(path: string): string {
return cdnPath(`/images/${path}`);
}
export function decodeURIComponentSafe(text: string) {
return text
? decodeURIComponent(text.replace(/%(?![0-9][0-9a-fA-F]+)/g, "%25"))
: text;
}

14
app/utils/urls.test.js Normal file
View File

@@ -0,0 +1,14 @@
// @flow
import { decodeURIComponentSafe } from "./urls";
describe("decodeURIComponentSafe", () => {
test("to handle % symbols", () => {
expect(decodeURIComponentSafe("%")).toBe("%");
expect(decodeURIComponentSafe("%25")).toBe("%");
});
test("to correctly account for encoded symbols", () => {
expect(decodeURIComponentSafe("%7D")).toBe("}");
expect(decodeURIComponentSafe("%2F")).toBe("/");
});
});