Skip to content

Commit e760b2d

Browse files
committed
fix(aio): handle clicks deep within anchor tags
1 parent e07b7ea commit e760b2d

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

‎aio/src/app/app.component.spec.ts‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,35 @@ describe('AppComponent', () => {
309309
describe('click intercepting', () => {
310310
it('should intercept clicks on anchors and call `location.handleAnchorClick()`',
311311
inject([LocationService], (location: LocationService) => {
312-
const anchorElement: HTMLAnchorElement = document.createElement('a');
313-
anchorElement.href = 'some/local/url';
314-
fixture.nativeElement.append(anchorElement);
312+
313+
const el = fixture.nativeElement as Element;
314+
el.innerHTML = '<a href="some/local/url">click me</a>';
315+
const anchorElement = el.getElementsByTagName('a')[0];
315316
anchorElement.click();
316317
expect(location.handleAnchorClick).toHaveBeenCalledWith(anchorElement, 0, false, false);
317318
}));
318319

320+
it('should intercept clicks on elements deep within an anchor tag',
321+
inject([LocationService], (location: LocationService) => {
322+
323+
const el = fixture.nativeElement as Element;
324+
el.innerHTML = '<a href="some/local/url"><div><img></div></a>';
325+
const imageElement = el.getElementsByTagName('img')[0];
326+
const anchorElement = el.getElementsByTagName('a')[0];
327+
imageElement.click();
328+
expect(location.handleAnchorClick).toHaveBeenCalledWith(anchorElement, 0, false, false);
329+
}));
330+
331+
it('should ignore clicks on elements without an anchor ancestor',
332+
inject([LocationService], (location: LocationService) => {
333+
334+
const el = fixture.nativeElement as Element;
335+
el.innerHTML = '<div><p><div><img></div></p></div>';
336+
const imageElement = el.getElementsByTagName('img')[0];
337+
imageElement.click();
338+
expect(location.handleAnchorClick).not.toHaveBeenCalled();
339+
}));
340+
319341
it('should intercept clicks not on the search elements and hide the search results', () => {
320342
const searchResults: SearchResultsComponent = fixture.debugElement.query(By.directive(SearchResultsComponent)).componentInstance;
321343
spyOn(searchResults, 'hideResults');

‎aio/src/app/app.component.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,16 @@ export class AppComponent implements OnInit {
133133

134134
if (eventTarget.tagName === 'FOOTER' && metaKey && altKey) {
135135
this.dtOn = !this.dtOn;
136+
return false;
136137
}
137138

138-
// Deal with anchor clicks
139-
if (eventTarget instanceof HTMLImageElement) {
140-
eventTarget = eventTarget.parentElement; // assume image wrapped in Anchor
139+
// Deal with anchor clicks; climb DOM tree until anchor found (or null)
140+
let target = eventTarget;
141+
while (target && !(target instanceof HTMLAnchorElement)) {
142+
target = target.parentElement;
141143
}
142-
if (eventTarget instanceof HTMLAnchorElement) {
143-
return this.locationService.handleAnchorClick(eventTarget, button, ctrlKey, metaKey);
144+
if (target) {
145+
return this.locationService.handleAnchorClick(target as HTMLAnchorElement, button, ctrlKey, metaKey);
144146
}
145147
return true;
146148
}

0 commit comments

Comments
 (0)