Skip to content

Commit b785bd6

Browse files
committed
fix: properly handle svg anchor elements
1 parent 8538e22 commit b785bd6

File tree

1 file changed

+52
-50
lines changed

1 file changed

+52
-50
lines changed

Diff for: src/client/app/router.ts

+52-50
Original file line numberDiff line numberDiff line change
@@ -174,62 +174,64 @@ export function createRouter(
174174
window.addEventListener(
175175
'click',
176176
(e) => {
177-
// temporary fix for docsearch action buttons
178-
const button = (e.target as Element).closest('button')
179-
if (button) return
177+
if (
178+
e.defaultPrevented ||
179+
!(e.target instanceof Element) ||
180+
e.target.closest('button') || // temporary fix for docsearch action buttons
181+
e.button !== 0 ||
182+
e.ctrlKey ||
183+
e.shiftKey ||
184+
e.altKey ||
185+
e.metaKey
186+
)
187+
return
180188

181-
const link = (e.target as Element | SVGElement).closest<
182-
HTMLAnchorElement | SVGAElement
183-
>('a')
189+
const link = e.target.closest<HTMLAnchorElement | SVGAElement>('a')
184190
if (
185-
link &&
186-
!link.closest('.vp-raw') &&
187-
(link instanceof SVGElement || !link.download)
188-
) {
189-
const { target } = link
190-
const linkHref = link.getAttribute('href')
191-
if (linkHref == null) return
192-
const { href, origin, pathname, hash, search } = new URL(
193-
linkHref,
194-
link.baseURI
195-
)
196-
const currentUrl = new URL(location.href) // copy to keep old data
197-
// only intercept inbound html links
191+
!link ||
192+
link.closest('.vp-raw') ||
193+
link.hasAttribute('download') ||
194+
link.hasAttribute('target')
195+
)
196+
return
197+
198+
const linkHref =
199+
link.getAttribute('href') ??
200+
(link instanceof SVGAElement ? link.getAttribute('xlink:href') : null)
201+
if (linkHref == null) return
202+
203+
const { href, origin, pathname, hash, search } = new URL(
204+
linkHref,
205+
link.baseURI
206+
)
207+
const currentUrl = new URL(location.href) // copy to keep old data
208+
// only intercept inbound html links
209+
if (origin === currentUrl.origin && treatAsHtml(pathname)) {
210+
e.preventDefault()
198211
if (
199-
!e.ctrlKey &&
200-
!e.shiftKey &&
201-
!e.altKey &&
202-
!e.metaKey &&
203-
!target &&
204-
origin === currentUrl.origin &&
205-
treatAsHtml(pathname)
212+
pathname === currentUrl.pathname &&
213+
search === currentUrl.search
206214
) {
207-
e.preventDefault()
208-
if (
209-
pathname === currentUrl.pathname &&
210-
search === currentUrl.search
211-
) {
212-
// scroll between hash anchors in the same page
213-
// avoid duplicate history entries when the hash is same
214-
if (hash !== currentUrl.hash) {
215-
history.pushState({}, '', href)
216-
// still emit the event so we can listen to it in themes
217-
window.dispatchEvent(
218-
new HashChangeEvent('hashchange', {
219-
oldURL: currentUrl.href,
220-
newURL: href
221-
})
222-
)
223-
}
224-
if (hash) {
225-
// use smooth scroll when clicking on header anchor links
226-
scrollTo(link, hash, link.classList.contains('header-anchor'))
227-
} else {
228-
window.scrollTo(0, 0)
229-
}
215+
// scroll between hash anchors in the same page
216+
// avoid duplicate history entries when the hash is same
217+
if (hash !== currentUrl.hash) {
218+
history.pushState({}, '', href)
219+
// still emit the event so we can listen to it in themes
220+
window.dispatchEvent(
221+
new HashChangeEvent('hashchange', {
222+
oldURL: currentUrl.href,
223+
newURL: href
224+
})
225+
)
226+
}
227+
if (hash) {
228+
// use smooth scroll when clicking on header anchor links
229+
scrollTo(link, hash, link.classList.contains('header-anchor'))
230230
} else {
231-
go(href)
231+
window.scrollTo(0, 0)
232232
}
233+
} else {
234+
go(href)
233235
}
234236
}
235237
},

0 commit comments

Comments
 (0)