Skip to content

Commit

Permalink
fix: handle special chars in anchors when navigating (#8806)
Browse files Browse the repository at this point in the history
fixes #8804
  • Loading branch information
msonnberger authored Jan 31, 2023
1 parent 5c63f4d commit 5571e4a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/hip-eggs-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: handle anchors with special chars when navigating
3 changes: 2 additions & 1 deletion packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ export function create_client({ target, base }) {
}

if (autoscroll) {
const deep_linked = url.hash && document.querySelector(decodeURIComponent(url.hash));
const deep_linked =
url.hash && document.getElementById(decodeURIComponent(url.hash.slice(1)));
if (scroll) {
scrollTo(scroll.x, scroll.y);
} else if (deep_linked) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<h1>Welcome to a test project</h1>
<a id="scroll-anchor" href="#last-anchor-2">Bottom of this page</a>
<a id="non-ascii-anchor" href="/anchor/anchor#go-to-encöded">Anchor demo (non-ASCII)</a>
<a id="special-char-anchor" href="/anchor/anchor#go-to-.=">Anchor demo (special characters)</a>
<a id="first-anchor" href="/anchor/anchor#go-to-element">Anchor demo (first)</a>
<div>Spacing</div>
<a id="second-anchor" href="/anchor/anchor#go-to-element">Anchor demo (second)</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
<div style="height: 180vh; background-color: tomato;">
<p id="go-to-encöded">The browser scrolls to me</p>
</div>
<div style="height: 180vh; background-color: honeydew;">
<p id="go-to-.=" class="special-char-id">The browser scrolls to me</p>
</div>
10 changes: 10 additions & 0 deletions packages/kit/test/apps/basics/test/cross-platform/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ test.describe('Scrolling', () => {
expect(await in_view('#go-to-encöded')).toBe(true);
});

test('url-supplied anchor with special characters works on navigation to page', async ({
page,
in_view,
clicknav
}) => {
await page.goto('/anchor');
await clicknav('#special-char-anchor');
expect(await in_view('.special-char-id')).toBe(true);
});

test('url-supplied anchor works when navigated from scrolled page', async ({
page,
clicknav,
Expand Down

0 comments on commit 5571e4a

Please sign in to comment.