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

Not working with identifiers starting with a digit. #532

Open
JulianCataldo opened this issue May 24, 2021 · 3 comments
Open

Not working with identifiers starting with a digit. #532

JulianCataldo opened this issue May 24, 2021 · 3 comments

Comments

@JulianCataldo
Copy link

JulianCataldo commented May 24, 2021

Hello there.

Got this error when trying to scroll to an anchor with an ID starting with a digit :

vue-scrollto.js?f13c:180 Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '#3-scripts' is not a valid selector.

Notes : IDs are generated with rehype slug, using github-slugger

ID with a digit first are valid spec int HTML, but not a valid CSS Selector. Could be possible to failsafe that without changing slug generation policy though.

Some insights :
https://stackoverflow.com/questions/20306204/using-queryselector-with-ids-that-are-numbers

https://developer.mozilla.org/en-US/docs/Web/API/CSS/escape

document.querySelector("[id='1']")

@JulianCataldo JulianCataldo changed the title Not working with identifiers starting with a digit. Not working with identifiers starting with a digit (ID ok with HTML, but not a valid CSS Selector). May 24, 2021
@JulianCataldo JulianCataldo changed the title Not working with identifiers starting with a digit (ID ok with HTML, but not a valid CSS Selector). Not working with identifiers starting with a digit. May 24, 2021
@hacknug
Copy link

hacknug commented Dec 10, 2021

@rigor789 the solution would be to use CSS.escape() to get the actual className you can use with JS.

document.querySelector('#' + CSS.escape('999-id-with-special-characters'))

@rigor789
Copy link
Owner

@hacknug I'm open to adding that - but definitely need to check for CSS.escape existence and fallback to not using it for unsupported browsers. Mind opening a PR?

@hacknug
Copy link

hacknug commented Dec 10, 2021

@rigor789 sure! Will take a look on Monday. Keep in mind we will most likely need to use a polyfill or something to make it work on node.

Do we have tests to ensure nothing breaks after the changes?

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

No branches or pull requests

3 participants