Skip to content

Commit 537a912

Browse files
Merge pull request #863 from tighten/current-check-encoded-hash
Fix current checks with encoded `#` in URL
2 parents d93b8f8 + 1a69ebd commit 537a912

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/js/Router.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ export default class Router extends String {
150150
const isSubset = (subset, full) => {
151151
return Object.entries(subset).every(([key, value]) => {
152152
if (Array.isArray(value) && Array.isArray(full[key])) {
153-
return value.every((v) => full[key].includes(v));
153+
return value.every(
154+
(v) => full[key].includes(v) || full[key].includes(decodeURIComponent(v)),
155+
);
154156
}
155157

156158
if (
@@ -162,7 +164,7 @@ export default class Router extends String {
162164
return isSubset(value, full[key]);
163165
}
164166

165-
return full[key] == value;
167+
return full[key] == value || full[key] == decodeURIComponent(value);
166168
});
167169
};
168170

tests/js/route.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,25 @@ describe('current()', () => {
15021502

15031503
global.window = oldWindow;
15041504
});
1505+
1506+
// https://github.com/tighten/ziggy/issues/844
1507+
test('url containing encoded hash', () => {
1508+
global.window.location.pathname = '/slashes/foo/Arcbees_c%23_doc.md';
1509+
1510+
expect(route().current()).toBe('slashes');
1511+
expect(route().current('slashes', { slug: 'Arcbees_c#_doc.md' })).toBe(true);
1512+
expect(route().current('slashes', { slug: 'Arcbees_c%23_doc.md' })).toBe(true);
1513+
});
1514+
1515+
test('url containing raw hash', () => {
1516+
// This specific case may not matter because it's not valid, # is a URL anchor and
1517+
// can't be in the path like this, but in general this should work both ways
1518+
global.window.location.pathname = '/slashes/foo/Arcbees_c#_doc.md';
1519+
1520+
expect(route().current()).toBe('slashes');
1521+
expect(route().current('slashes', { slug: 'Arcbees_c#_doc.md' })).toBe(true);
1522+
expect(route().current('slashes', { slug: 'Arcbees_c%23_doc.md' })).toBe(true);
1523+
});
15051524
});
15061525

15071526
describe('json', () => {

0 commit comments

Comments
 (0)