@@ -35,13 +35,18 @@ const valid_link_options = /** @type {const} */ ({
3535 reload : [ '' , 'off' ]
3636} ) ;
3737
38+ /**
39+ * @template {LinkOptionName} T
40+ * @typedef {typeof valid_link_options[T][number] } ValidLinkOptions
41+ */
42+
3843/**
3944 * @template {LinkOptionName} T
4045 * @param {Element } element
4146 * @param {T } name
4247 */
4348function link_option ( element , name ) {
44- const value = /** @type {typeof valid_link_options[T][number] | null } */ (
49+ const value = /** @type {ValidLinkOptions<T> | null } */ (
4550 element . getAttribute ( `data-sveltekit-${ name } ` )
4651 ) ;
4752
@@ -52,7 +57,7 @@ function link_option(element, name) {
5257
5358/**
5459 * @template {LinkOptionName} T
55- * @template {typeof valid_link_options[T][number] | null} U
60+ * @template {ValidLinkOptions<T> | null} U
5661 * @param {Element } element
5762 * @param {T } name
5863 * @param {U } value
@@ -80,80 +85,88 @@ const levels = {
8085
8186/**
8287 * @param {Element } element
83- * @param { string } base
88+ * @returns { Element | null }
8489 */
85- export function find_anchor ( element , base ) {
86- /** @type {HTMLAnchorElement | SVGAElement | undefined } */
87- let a ;
88-
89- /** @type {typeof valid_link_options['noscroll'][number] | null } */
90- let noscroll = null ;
91-
92- /** @type {typeof valid_link_options['preload-code'][number] | null } */
93- let preload_code = null ;
90+ function parent_element ( element ) {
91+ let parent = element . assignedSlot ?? element . parentNode ;
9492
95- /** @type {typeof valid_link_options['preload-data'][number] | null } */
96- let preload_data = null ;
97-
98- /** @type {typeof valid_link_options['reload'][number] | null } */
99- let reload = null ;
93+ // @ts -expect-error handle shadow roots
94+ if ( parent ?. nodeType === 11 ) parent = parent . host ;
10095
101- while ( element !== document . documentElement ) {
102- if ( ! a && element . nodeName . toUpperCase ( ) === 'A' ) {
103- // SVG <a> elements have a lowercase name
104- a = /** @type {HTMLAnchorElement | SVGAElement } */ ( element ) ;
105- }
96+ return /** @type {Element } */ ( parent ) ;
97+ }
10698
107- if ( a ) {
108- if ( preload_code === null ) preload_code = link_option ( element , 'preload-code' ) ;
109- if ( preload_data === null ) preload_data = link_option ( element , 'preload-data' ) ;
110- if ( noscroll === null ) noscroll = link_option ( element , 'noscroll' ) ;
111- if ( reload === null ) reload = link_option ( element , 'reload' ) ;
99+ /**
100+ * @param {Element } element
101+ * @param {Element } target
102+ */
103+ export function find_anchor ( element , target ) {
104+ while ( element !== target ) {
105+ if ( element . nodeName . toUpperCase ( ) === 'A' ) {
106+ return /** @type {HTMLAnchorElement | SVGAElement } */ ( element ) ;
112107 }
113108
114- // @ts -expect-error handle shadow roots
115- element = element . assignedSlot ?? element . parentNode ;
116-
117- // @ts -expect-error handle shadow roots
118- if ( element . nodeType === 11 ) element = element . host ;
109+ element = /** @type {Element } */ ( parent_element ( element ) ) ;
119110 }
111+ }
120112
113+ /**
114+ * @param {HTMLAnchorElement | SVGAElement } a
115+ * @param {string } base
116+ */
117+ export function get_link_info ( a , base ) {
121118 /** @type {URL | undefined } */
122119 let url ;
123120
124121 try {
125- url = a && new URL ( a instanceof SVGAElement ? a . href . baseVal : a . href , document . baseURI ) ;
122+ url = new URL ( a instanceof SVGAElement ? a . href . baseVal : a . href , document . baseURI ) ;
126123 } catch { }
127124
128- const options = {
129- preload_code : levels [ preload_code ?? 'off' ] ,
130- preload_data : levels [ preload_data ?? 'off' ] ,
131- noscroll : noscroll === 'off' ? false : noscroll === '' ? true : null ,
132- reload : reload === 'off' ? false : reload === '' ? true : null
125+ const has = {
126+ rel_external : ( a . getAttribute ( 'rel' ) || '' ) . split ( / \s + / ) . includes ( 'external' ) ,
127+ download : a . hasAttribute ( 'download' ) ,
128+ target : ! ! ( a instanceof SVGAElement ? a . target . baseVal : a . target )
133129 } ;
134130
135- const has = a
136- ? {
137- rel_external : ( a . getAttribute ( 'rel' ) || '' ) . split ( / \s + / ) . includes ( 'external' ) ,
138- download : a . hasAttribute ( 'download' ) ,
139- target : ! ! ( a instanceof SVGAElement ? a . target . baseVal : a . target )
140- }
141- : { } ;
142-
143131 const external =
144- ! url ||
145- is_external_url ( url , base ) ||
146- options . reload ||
147- has . rel_external ||
148- has . target ||
149- has . download ;
132+ ! url || is_external_url ( url , base ) || has . rel_external || has . target || has . download ;
133+
134+ return { url, has, external } ;
135+ }
136+
137+ /**
138+ * @param {HTMLFormElement | HTMLAnchorElement | SVGAElement } element
139+ */
140+ export function get_router_options ( element ) {
141+ /** @type {ValidLinkOptions<'noscroll'> | null } */
142+ let noscroll = null ;
143+
144+ /** @type {ValidLinkOptions<'preload-code'> | null } */
145+ let preload_code = null ;
146+
147+ /** @type {ValidLinkOptions<'preload-data'> | null } */
148+ let preload_data = null ;
149+
150+ /** @type {ValidLinkOptions<'reload'> | null } */
151+ let reload = null ;
152+
153+ /** @type {Element } */
154+ let el = element ;
155+
156+ while ( el !== document . documentElement ) {
157+ if ( preload_code === null ) preload_code = link_option ( el , 'preload-code' ) ;
158+ if ( preload_data === null ) preload_data = link_option ( el , 'preload-data' ) ;
159+ if ( noscroll === null ) noscroll = link_option ( el , 'noscroll' ) ;
160+ if ( reload === null ) reload = link_option ( el , 'reload' ) ;
161+
162+ el = /** @type {Element } */ ( parent_element ( el ) ) ;
163+ }
150164
151165 return {
152- a,
153- url,
154- options,
155- external,
156- has
166+ preload_code : levels [ preload_code ?? 'off' ] ,
167+ preload_data : levels [ preload_data ?? 'off' ] ,
168+ noscroll : noscroll === 'off' ? false : noscroll === '' ? true : null ,
169+ reload : reload === 'off' ? false : reload === '' ? true : null
157170 } ;
158171}
159172
0 commit comments