@@ -174,62 +174,64 @@ export function createRouter(
174
174
window . addEventListener (
175
175
'click' ,
176
176
( 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
180
188
181
- const link = ( e . target as Element | SVGElement ) . closest <
182
- HTMLAnchorElement | SVGAElement
183
- > ( 'a' )
189
+ const link = e . target . closest < HTMLAnchorElement | SVGAElement > ( 'a' )
184
190
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 ( )
198
211
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
206
214
) {
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' ) )
230
230
} else {
231
- go ( href )
231
+ window . scrollTo ( 0 , 0 )
232
232
}
233
+ } else {
234
+ go ( href )
233
235
}
234
236
}
235
237
} ,
0 commit comments