@@ -119,6 +119,53 @@ describe('AnchorNavigationDirective', () => {
119119 expect ( router . navigateByUrl ) . toBeCalledWith ( '/page?query=analog#section1' ) ;
120120 } ) ) ;
121121
122+ it ( 'navigates when clicking an element nested inside an internal link' , fakeAsync ( ( ) => {
123+ const { router, handleNavigation, selectById } = setup ( ) ;
124+
125+ selectById ( 'nested-code' ) . click ( ) ;
126+ tick ( ) ;
127+
128+ expect ( handleNavigation . mock . results [ 0 ] . value ) . toEqual ( false ) ;
129+ expect ( router . navigateByUrl ) . toBeCalledWith ( '/docs' ) ;
130+ } ) ) ;
131+
132+ it ( 'ignores links that wrap the directive host' , fakeAsync ( ( ) => {
133+ const { router, selectById } = setup ( ) ;
134+
135+ selectById ( 'inside-wrapped-host' ) . click ( ) ;
136+ tick ( ) ;
137+
138+ expect ( router . navigateByUrl ) . not . toBeCalled ( ) ;
139+ } ) ) ;
140+
141+ it . each ( [
142+ { ctrlKey : true } ,
143+ { metaKey : true } ,
144+ { shiftKey : true } ,
145+ { altKey : true } ,
146+ { button : 1 } ,
147+ ] ) ( 'leaves modified clicks to the browser (%o)' , ( init ) => {
148+ const { router, handleNavigation, selectById } = setup ( ) ;
149+
150+ selectById ( 'relative-page' ) . dispatchEvent (
151+ new MouseEvent ( 'click' , { bubbles : true , cancelable : true , ...init } ) ,
152+ ) ;
153+
154+ expect ( handleNavigation . mock . results [ 0 ] . value ) . toEqual ( true ) ;
155+ expect ( router . navigateByUrl ) . not . toBeCalled ( ) ;
156+ } ) ;
157+
158+ it ( 'leaves clicks already prevented by another handler to the browser' , ( ) => {
159+ const { router, handleNavigation, selectById } = setup ( ) ;
160+ const link = selectById ( 'relative-page' ) ;
161+ link . addEventListener ( 'click' , ( event ) => event . preventDefault ( ) ) ;
162+
163+ link . click ( ) ;
164+
165+ expect ( handleNavigation . mock . results [ 0 ] . value ) . toEqual ( true ) ;
166+ expect ( router . navigateByUrl ) . not . toBeCalled ( ) ;
167+ } ) ;
168+
122169 it ( 'navigates to a fragment-only anchor using the current page path and search when a <base> element is present' , fakeAsync ( ( ) => {
123170 // A <base> element makes the browser resolve a fragment-only href
124171 // (e.g. href="#section1") against the base URL, not the current
@@ -160,9 +207,11 @@ function setup() {
160207 class PreventRealNavigationDirective {
161208 @HostListener ( 'click' , [ '$event' ] )
162209 preventClick ( event : Event ) {
163- if ( event . target instanceof HTMLAnchorElement ) {
210+ const anchor =
211+ event . target instanceof Element ? event . target . closest ( 'a' ) : null ;
212+ if ( anchor ) {
164213 event . preventDefault ( ) ;
165- navigationResult . navigatedTo = event . target . href ;
214+ navigationResult . navigatedTo = anchor . href ;
166215 }
167216 }
168217 }
@@ -202,7 +251,14 @@ function setup() {
202251 Page with Search, Hash, and Trailing Slash
203252 </a>
204253 <a id="fragment-only" href="#section1">Fragment Only</a>
254+ <a href="/docs"><code id="nested-code">docs</code></a>
205255 </div>
256+
257+ <a href="/outer">
258+ <span analogAnchorNavigation>
259+ <span id="inside-wrapped-host">Inside Wrapped Host</span>
260+ </span>
261+ </a>
206262 ` ,
207263 } )
208264 class TestComponent {
0 commit comments