fix: properly handle svg anchor elements

pull/3989/head
Divyansh Singh 3 months ago
parent 8538e22f2c
commit b785bd6ab3

@ -174,62 +174,64 @@ export function createRouter(
window.addEventListener( window.addEventListener(
'click', 'click',
(e) => { (e) => {
// temporary fix for docsearch action buttons if (
const button = (e.target as Element).closest('button') e.defaultPrevented ||
if (button) return !(e.target instanceof Element) ||
e.target.closest('button') || // temporary fix for docsearch action buttons
e.button !== 0 ||
e.ctrlKey ||
e.shiftKey ||
e.altKey ||
e.metaKey
)
return
const link = (e.target as Element | SVGElement).closest< const link = e.target.closest<HTMLAnchorElement | SVGAElement>('a')
HTMLAnchorElement | SVGAElement
>('a')
if ( if (
link && !link ||
!link.closest('.vp-raw') && link.closest('.vp-raw') ||
(link instanceof SVGElement || !link.download) link.hasAttribute('download') ||
) { link.hasAttribute('target')
const { target } = link )
const linkHref = link.getAttribute('href') return
if (linkHref == null) return
const { href, origin, pathname, hash, search } = new URL( const linkHref =
linkHref, link.getAttribute('href') ??
link.baseURI (link instanceof SVGAElement ? link.getAttribute('xlink:href') : null)
) if (linkHref == null) return
const currentUrl = new URL(location.href) // copy to keep old data
// only intercept inbound html links const { href, origin, pathname, hash, search } = new URL(
linkHref,
link.baseURI
)
const currentUrl = new URL(location.href) // copy to keep old data
// only intercept inbound html links
if (origin === currentUrl.origin && treatAsHtml(pathname)) {
e.preventDefault()
if ( if (
!e.ctrlKey && pathname === currentUrl.pathname &&
!e.shiftKey && search === currentUrl.search
!e.altKey &&
!e.metaKey &&
!target &&
origin === currentUrl.origin &&
treatAsHtml(pathname)
) { ) {
e.preventDefault() // scroll between hash anchors in the same page
if ( // avoid duplicate history entries when the hash is same
pathname === currentUrl.pathname && if (hash !== currentUrl.hash) {
search === currentUrl.search history.pushState({}, '', href)
) { // still emit the event so we can listen to it in themes
// scroll between hash anchors in the same page window.dispatchEvent(
// avoid duplicate history entries when the hash is same new HashChangeEvent('hashchange', {
if (hash !== currentUrl.hash) { oldURL: currentUrl.href,
history.pushState({}, '', href) newURL: href
// still emit the event so we can listen to it in themes })
window.dispatchEvent( )
new HashChangeEvent('hashchange', { }
oldURL: currentUrl.href, if (hash) {
newURL: href // use smooth scroll when clicking on header anchor links
}) scrollTo(link, hash, link.classList.contains('header-anchor'))
)
}
if (hash) {
// use smooth scroll when clicking on header anchor links
scrollTo(link, hash, link.classList.contains('header-anchor'))
} else {
window.scrollTo(0, 0)
}
} else { } else {
go(href) window.scrollTo(0, 0)
} }
} else {
go(href)
} }
} }
}, },

Loading…
Cancel
Save