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: decode hash when clicking on same hash to correctly scroll on non ascii hashes #12699

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

henrikvilhelmberglund
Copy link

@henrikvilhelmberglund henrikvilhelmberglund commented Sep 21, 2024

This PR adds a missing decodeURIComponent(hash) in the same hash scrolling code to make sure non ascii hashes scroll properly. This exists for normal clicking code but is missing in the same hash code.

I have no idea how to write a test for this but here is code that can be checked in the playground:

Code
<nav>
  <a href="#一"></a>
  <a href="#2">2</a>
  <a href="#three">three</a>
<a href="#go-to-encöded">Anchor demo (non-ASCII)</a>
<a href="#go-to-.=">Anchor demo (special characters)</a>
  <a href="#4"></a>
  <a href="#五five"></a>
</nav>
<div class="scroll-container">
  <div class="scroll-page" id="一"><a href="#一"></a></div>
  <div class="scroll-page" id="2">2</div>
  <div class="scroll-page" id="three">three</div>
  <div class="scroll-page" id="go-to-encöded">non-ASCII</div>
  <div class="scroll-page" id="go-to-.=">special characters</div>
  <div class="scroll-page" id="4"><a href="#4"></a></div>
  <div class="scroll-page" id="五five"><a href="#五five"></a></div>
</div>

<style>
  a {
  	display: inline-block;
  	width: 50px;
  	text-decoration: none;
  }
  nav,
  .scroll-container {
  	display: block;
  	margin: 0 auto;
  	text-align: center;
  }
  nav {
  	width: 339px;
  	padding: 5px;
  	border: 1px solid black;
  }
  .scroll-container {
  	width: 350px;
  	height: 200px;
  	overflow-y: scroll;
  	scroll-behavior: smooth;
  }
  .scroll-page {
  	display: flex;
  	align-items: center;
  	justify-content: center;
  	height: 100px;
  	font-size: 2em;
  }
</style>

Repro:

  1. Click in the nav
  2. Manually scroll down in the div with the scrollbar
  3. Click in the nav again

Result pre PR: doesn't scroll
Result post PR: correctly scrolls to the top

fixes #12684


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

Copy link

changeset-bot bot commented Sep 21, 2024

🦋 Changeset detected

Latest commit: 534a5b0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug about the same page navigation.
1 participant