-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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(gatsby-react-router-scroll): replace querySelector with getElementById #25161
fix(gatsby-react-router-scroll): replace querySelector with getElementById #25161
Conversation
@hartshorne could you add a test case for this? |
HTML:
JS:
Or are you looking for something with the Gatsby environment wrapped around it? Or are you looking for unit tests? |
@wardpeet I missed the fact that hash would be prefixed with # to work with querySelector. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for unit tests on this, we have e2e tests for this so if it passes we're good! Also this makes a lot of sense. Thank you!
Holy buckets, @hartshorne — we just merged your PR to Gatsby! 💪💜 Gatsby is built by awesome people like you. Let us say “thanks” in two ways:
If there’s anything we can do to help, please don’t hesitate to reach out to us: tweet at @gatsbyjs and we’ll come a-runnin’. Thanks again! |
Description
@blainekasten introduced a number of improvements to scroll handling in #24306.
In the
scrollToHash
function, we should usedocument.getElementById
to find the correct node instead ofdocument.querySelector
.The HTML standard allows almost any string to be used as an
id
attribute:From: https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute
document.querySelector
expects a valid CSS selector string, and will throw an exception if you do not supply one:https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
document.getElementById
is designed to be used with the HTMLid
attribute: https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementByIdAnd does not seem to throw any exceptions, even if you give it invalid input.
document.getElementById
also works with non-ascii text, like theáccentuated
mentioned in #24306.And for bonus points, it looks like
document.getElementById
is about twice as fast asdocument.querySelector
:https://www.measurethat.net/Benchmarks/Show/2488/0/getelementbyid-vs-queryselector
Documentation
None.
Related Issues
None.