@@ -131,6 +131,22 @@ function Iframe( {
131131 function preventFileDropDefault ( event ) {
132132 event . preventDefault ( ) ;
133133 }
134+ // Prevent clicks on link fragments from navigating away. Note that links
135+ // inside `contenteditable` are already disabled by the browser, so
136+ // this is for links in blocks outside of `contenteditable`.
137+ function interceptLinkClicks ( event ) {
138+ if (
139+ event . target . tagName === 'A' &&
140+ event . target . getAttribute ( 'href' ) ?. startsWith ( '#' )
141+ ) {
142+ event . preventDefault ( ) ;
143+ // Manually handle link fragment navigation within the iframe to prevent page reloads.
144+ // This ensures smooth scrolling to anchor links (e.g., footnotes) despite the iframe's different base URL.
145+ iFrameDocument . defaultView . location . hash = event . target
146+ . getAttribute ( 'href' )
147+ . slice ( 1 ) ;
148+ }
149+ }
134150
135151 const { ownerDocument } = node ;
136152
@@ -185,22 +201,7 @@ function Iframe( {
185201 preventFileDropDefault ,
186202 false
187203 ) ;
188- // Prevent clicks on links from navigating away. Note that links
189- // inside `contenteditable` are already disabled by the browser, so
190- // this is for links in blocks outside of `contenteditable`.
191- iFrameDocument . addEventListener ( 'click' , ( event ) => {
192- if ( event . target . tagName === 'A' ) {
193- event . preventDefault ( ) ;
194-
195- // Appending a hash to the current URL will not reload the
196- // page. This is useful for e.g. footnotes.
197- const href = event . target . getAttribute ( 'href' ) ;
198- if ( href ?. startsWith ( '#' ) ) {
199- iFrameDocument . defaultView . location . hash =
200- href . slice ( 1 ) ;
201- }
202- }
203- } ) ;
204+ iFrameDocument . addEventListener ( 'click' , interceptLinkClicks ) ;
204205 }
205206
206207 node . addEventListener ( 'load' , onLoad ) ;
@@ -216,6 +217,7 @@ function Iframe( {
216217 'drop' ,
217218 preventFileDropDefault
218219 ) ;
220+ iFrameDocument ?. removeEventListener ( 'click' , interceptLinkClicks ) ;
219221 } ;
220222 } , [ ] ) ;
221223
0 commit comments