Skip to content

Commit 6b98113

Browse files
authored
fix(client): don't throw on using special chars in element ids (#2560)
1 parent 1ef33fe commit 6b98113

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

src/client/app/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ if (inBrowser) {
152152

153153
// scroll to hash on new tab during dev
154154
if (import.meta.env.DEV && location.hash) {
155-
const target = document.querySelector(decodeURIComponent(location.hash))
155+
const target = document.getElementById(
156+
decodeURIComponent(location.hash).slice(1)
157+
)
156158
if (target) {
157159
scrollTo(target, location.hash)
158160
}

src/client/app/router.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ export function createRouter(
104104
if (targetLoc.hash && !scrollPosition) {
105105
let target: HTMLElement | null = null
106106
try {
107-
target = document.querySelector(
108-
decodeURIComponent(targetLoc.hash)
107+
target = document.getElementById(
108+
decodeURIComponent(targetLoc.hash).slice(1)
109109
)
110110
} catch (e) {
111111
console.warn(e)
@@ -238,7 +238,7 @@ export function scrollTo(el: Element, hash: string, smooth = false) {
238238
try {
239239
target = el.classList.contains('header-anchor')
240240
? el
241-
: document.querySelector(decodeURIComponent(hash))
241+
: document.getElementById(decodeURIComponent(hash).slice(1))
242242
} catch (e) {
243243
console.warn(e)
244244
}

src/client/theme-default/components/VPDocOutlineItem.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ defineProps<{
88
99
function onClick({ target: el }: Event) {
1010
const id = '#' + (el as HTMLAnchorElement).href!.split('#')[1]
11-
const heading = document.querySelector<HTMLAnchorElement>(
12-
decodeURIComponent(id)
13-
)
11+
const heading = document.getElementById(decodeURIComponent(id).slice(1))
1412
heading?.focus()
1513
}
1614
</script>

src/client/theme-default/components/VPSkipLink.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const backToTop = ref()
88
watch(() => route.path, () => backToTop.value.focus())
99
1010
function focusOnTargetAnchor({ target }: Event) {
11-
const el = document.querySelector<HTMLAnchorElement>(
12-
decodeURIComponent((target as HTMLAnchorElement).hash)
11+
const el = document.getElementById(
12+
decodeURIComponent((target as HTMLAnchorElement).hash).slice(1)
1313
)
1414
1515
if (el) {

0 commit comments

Comments
 (0)