fix(client): don't throw on using special chars in element ids (#2560)

pull/2562/head
Divyansh Singh 1 year ago committed by GitHub
parent 1ef33fe1c4
commit 6b98113a42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -152,7 +152,9 @@ if (inBrowser) {
// scroll to hash on new tab during dev // scroll to hash on new tab during dev
if (import.meta.env.DEV && location.hash) { if (import.meta.env.DEV && location.hash) {
const target = document.querySelector(decodeURIComponent(location.hash)) const target = document.getElementById(
decodeURIComponent(location.hash).slice(1)
)
if (target) { if (target) {
scrollTo(target, location.hash) scrollTo(target, location.hash)
} }

@ -104,8 +104,8 @@ export function createRouter(
if (targetLoc.hash && !scrollPosition) { if (targetLoc.hash && !scrollPosition) {
let target: HTMLElement | null = null let target: HTMLElement | null = null
try { try {
target = document.querySelector( target = document.getElementById(
decodeURIComponent(targetLoc.hash) decodeURIComponent(targetLoc.hash).slice(1)
) )
} catch (e) { } catch (e) {
console.warn(e) console.warn(e)
@ -238,7 +238,7 @@ export function scrollTo(el: Element, hash: string, smooth = false) {
try { try {
target = el.classList.contains('header-anchor') target = el.classList.contains('header-anchor')
? el ? el
: document.querySelector(decodeURIComponent(hash)) : document.getElementById(decodeURIComponent(hash).slice(1))
} catch (e) { } catch (e) {
console.warn(e) console.warn(e)
} }

@ -8,9 +8,7 @@ defineProps<{
function onClick({ target: el }: Event) { function onClick({ target: el }: Event) {
const id = '#' + (el as HTMLAnchorElement).href!.split('#')[1] const id = '#' + (el as HTMLAnchorElement).href!.split('#')[1]
const heading = document.querySelector<HTMLAnchorElement>( const heading = document.getElementById(decodeURIComponent(id).slice(1))
decodeURIComponent(id)
)
heading?.focus() heading?.focus()
} }
</script> </script>

@ -8,8 +8,8 @@ const backToTop = ref()
watch(() => route.path, () => backToTop.value.focus()) watch(() => route.path, () => backToTop.value.focus())
function focusOnTargetAnchor({ target }: Event) { function focusOnTargetAnchor({ target }: Event) {
const el = document.querySelector<HTMLAnchorElement>( const el = document.getElementById(
decodeURIComponent((target as HTMLAnchorElement).hash) decodeURIComponent((target as HTMLAnchorElement).hash).slice(1)
) )
if (el) { if (el) {

Loading…
Cancel
Save