Skip to content

Commit a47a164

Browse files
fix(content): handle nested link targets and modified clicks in anchor navigation (#2768)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent fee292b commit a47a164

2 files changed

Lines changed: 80 additions & 6 deletions

File tree

‎packages/content/src/lib/anchor-navigation.directive.spec.ts‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

‎packages/content/src/lib/anchor-navigation.directive.ts‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Directive, HostListener, inject } from '@angular/core';
1+
import { Directive, ElementRef, HostListener, inject } from '@angular/core';
22
import { DOCUMENT, Location } from '@angular/common';
33
import { Router } from '@angular/router';
44

@@ -8,13 +8,20 @@ import { Router } from '@angular/router';
88
})
99
export class AnchorNavigationDirective {
1010
private readonly document = inject(DOCUMENT);
11+
private readonly host = inject<ElementRef<HTMLElement>>(ElementRef);
1112
private readonly location = inject(Location);
1213
private readonly router = inject(Router);
1314

14-
@HostListener('click', ['$event.target'])
15-
handleNavigation(element: EventTarget | null): boolean {
15+
@HostListener('click', ['$event.target', '$event'])
16+
handleNavigation(target: EventTarget | null, event?: MouseEvent): boolean {
17+
if (event && shouldSkipClick(event)) {
18+
return true;
19+
}
20+
21+
const element = target instanceof Element ? target.closest('a') : null;
1622
if (
17-
element instanceof HTMLAnchorElement &&
23+
element &&
24+
this.host.nativeElement.contains(element) &&
1825
isInternalUrl(element, this.document) &&
1926
hasTargetSelf(element) &&
2027
!hasDownloadAttribute(element)
@@ -45,6 +52,17 @@ export class AnchorNavigationDirective {
4552
}
4653
}
4754

55+
function shouldSkipClick(event: MouseEvent): boolean {
56+
return (
57+
event.defaultPrevented ||
58+
event.button !== 0 ||
59+
event.ctrlKey ||
60+
event.metaKey ||
61+
event.shiftKey ||
62+
event.altKey
63+
);
64+
}
65+
4866
function hasDownloadAttribute(anchorElement: HTMLAnchorElement): boolean {
4967
return anchorElement.getAttribute('download') !== null;
5068
}

0 commit comments

Comments
 (0)