Skip to content

Commit 94ed9ff

Browse files
committed
fix(aio): do not nav to an image url
closes #16608 Formerly, tried to navigate when user clicked an anchor with an image url (to view image in a new tab) resulting in 404. Now ignores href URL with any extension and lets browser handle it.
1 parent 5d4b36f commit 94ed9ff

3 files changed

Lines changed: 93 additions & 43 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ export class AppComponent implements OnInit {
188188
while (target && !(target instanceof HTMLAnchorElement)) {
189189
target = target.parentElement;
190190
}
191-
if (target) {
192-
return this.locationService.handleAnchorClick(target as HTMLAnchorElement, button, ctrlKey, metaKey);
191+
if (target instanceof HTMLAnchorElement) {
192+
return this.locationService.handleAnchorClick(target, button, ctrlKey, metaKey);
193193
}
194194
return true;
195195
}

‎aio/src/app/shared/location.service.spec.ts‎

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ describe('LocationService', () => {
296296
service.go('https://some/far/away/land');
297297
expect(localUrl).toBeFalsy('should not set local url');
298298
});
299+
299300
});
300301

301302
describe('search', () => {
@@ -378,118 +379,156 @@ describe('LocationService', () => {
378379

379380
beforeEach(() => {
380381
anchor = document.createElement('a');
382+
spyOn(service, 'go');
381383
});
382384

383-
describe('intercepting', () => {
384-
it('should intercept clicks on anchors for relative local urls', () => {
385+
describe('should try to navigate with go() when anchor clicked for', () => {
386+
it('relative local url', () => {
385387
anchor.href = 'some/local/url';
386-
spyOn(service, 'go');
387-
const result = service.handleAnchorClick(anchor, 0, false, false);
388+
const result = service.handleAnchorClick(anchor);
388389
expect(service.go).toHaveBeenCalledWith('/some/local/url');
389390
expect(result).toBe(false);
390391
});
391392

392-
it('should intercept clicks on anchors for absolute local urls', () => {
393+
it('absolute local url', () => {
393394
anchor.href = '/some/local/url';
394-
spyOn(service, 'go');
395-
const result = service.handleAnchorClick(anchor, 0, false, false);
395+
const result = service.handleAnchorClick(anchor);
396396
expect(service.go).toHaveBeenCalledWith('/some/local/url');
397397
expect(result).toBe(false);
398398
});
399399

400-
it('should intercept clicks on anchors for local urls, with query params', () => {
400+
it('local url with query params', () => {
401401
anchor.href = 'some/local/url?query=xxx&other=yyy';
402-
spyOn(service, 'go');
403-
const result = service.handleAnchorClick(anchor, 0, false, false);
402+
const result = service.handleAnchorClick(anchor);
404403
expect(service.go).toHaveBeenCalledWith('/some/local/url?query=xxx&other=yyy');
405404
expect(result).toBe(false);
406405
});
407406

408-
it('should intercept clicks on anchors for local urls, with hash fragment', () => {
407+
it('local url with hash fragment', () => {
409408
anchor.href = 'some/local/url#somefragment';
410-
spyOn(service, 'go');
411-
const result = service.handleAnchorClick(anchor, 0, false, false);
409+
const result = service.handleAnchorClick(anchor);
412410
expect(service.go).toHaveBeenCalledWith('/some/local/url#somefragment');
413411
expect(result).toBe(false);
414412
});
415413

416-
it('should intercept clicks on anchors for local urls, with query params and hash fragment', () => {
414+
it('local url with query params and hash fragment', () => {
417415
anchor.href = 'some/local/url?query=xxx&other=yyy#somefragment';
418-
spyOn(service, 'go');
419-
const result = service.handleAnchorClick(anchor, 0, false, false);
416+
const result = service.handleAnchorClick(anchor);
420417
expect(service.go).toHaveBeenCalledWith('/some/local/url?query=xxx&other=yyy#somefragment');
421418
expect(result).toBe(false);
422419
});
420+
421+
it('local url with period in a path segment but no extension', () => {
422+
anchor.href = 'tut.or.ial/toh-p2';
423+
const result = service.handleAnchorClick(anchor);
424+
expect(service.go).toHaveBeenCalled();
425+
expect(result).toBe(false);
426+
});
423427
});
424428

425-
describe('not intercepting', () => {
426-
it('should not intercept clicks on anchors for external urls', () => {
429+
describe('should let browser handle anchor click when', () => {
430+
it('url is external to the site', () => {
427431
anchor.href = 'http://other.com/some/local/url?query=xxx&other=yyy#somefragment';
428-
spyOn(service, 'go');
429-
let result = service.handleAnchorClick(anchor, 0, false, false);
432+
let result = service.handleAnchorClick(anchor);
430433
expect(service.go).not.toHaveBeenCalled();
431434
expect(result).toBe(true);
432435

433436
anchor.href = 'some/local/url.pdf';
434437
anchor.protocol = 'ftp';
435-
result = service.handleAnchorClick(anchor, 0, false, false);
438+
result = service.handleAnchorClick(anchor);
436439
expect(service.go).not.toHaveBeenCalled();
437440
expect(result).toBe(true);
438441
});
439442

440-
it('should not intercept clicks on anchors if button is not zero', () => {
443+
it('mouse button is not zero (middle or right)', () => {
441444
anchor.href = 'some/local/url';
442-
spyOn(service, 'go');
443445
const result = service.handleAnchorClick(anchor, 1, false, false);
444446
expect(service.go).not.toHaveBeenCalled();
445447
expect(result).toBe(true);
446448
});
447449

448-
it('should not intercept clicks on anchors if ctrl key is pressed', () => {
450+
it('ctrl key is pressed', () => {
449451
anchor.href = 'some/local/url';
450-
spyOn(service, 'go');
451452
const result = service.handleAnchorClick(anchor, 0, true, false);
452453
expect(service.go).not.toHaveBeenCalled();
453454
expect(result).toBe(true);
454455
});
455456

456-
it('should not intercept clicks on anchors if meta key is pressed', () => {
457+
it('meta key is pressed', () => {
457458
anchor.href = 'some/local/url';
458-
spyOn(service, 'go');
459459
const result = service.handleAnchorClick(anchor, 0, false, true);
460460
expect(service.go).not.toHaveBeenCalled();
461461
expect(result).toBe(true);
462462
});
463463

464-
it('should not intercept clicks on links with (non-_self) targets', () => {
464+
it('anchor has (non-_self) target', () => {
465465
anchor.href = 'some/local/url';
466-
spyOn(service, 'go');
467-
468466
anchor.target = '_blank';
469-
let result = service.handleAnchorClick(anchor, 0, false, false);
467+
let result = service.handleAnchorClick(anchor);
470468
expect(service.go).not.toHaveBeenCalled();
471469
expect(result).toBe(true);
472470

473471
anchor.target = '_parent';
474-
result = service.handleAnchorClick(anchor, 0, false, false);
472+
result = service.handleAnchorClick(anchor);
475473
expect(service.go).not.toHaveBeenCalled();
476474
expect(result).toBe(true);
477475

478476
anchor.target = '_top';
479-
result = service.handleAnchorClick(anchor, 0, false, false);
477+
result = service.handleAnchorClick(anchor);
480478
expect(service.go).not.toHaveBeenCalled();
481479
expect(result).toBe(true);
482480

483481
anchor.target = 'other-frame';
484-
result = service.handleAnchorClick(anchor, 0, false, false);
482+
result = service.handleAnchorClick(anchor);
485483
expect(service.go).not.toHaveBeenCalled();
486484
expect(result).toBe(true);
487485

488486
anchor.target = '_self';
489-
result = service.handleAnchorClick(anchor, 0, false, false);
487+
result = service.handleAnchorClick(anchor);
490488
expect(service.go).toHaveBeenCalledWith('/some/local/url');
491489
expect(result).toBe(false);
492490
});
491+
492+
it('zip url', () => {
493+
anchor.href = 'tutorial/toh-p2.zip';
494+
const result = service.handleAnchorClick(anchor);
495+
expect(service.go).not.toHaveBeenCalled();
496+
expect(result).toBe(true);
497+
});
498+
499+
it('image or media url', () => {
500+
anchor.href = 'cat-photo.png';
501+
let result = service.handleAnchorClick(anchor);
502+
expect(service.go).not.toHaveBeenCalled();
503+
expect(result).toBe(true, 'png');
504+
505+
anchor.href = 'cat-photo.gif';
506+
result = service.handleAnchorClick(anchor);
507+
expect(service.go).not.toHaveBeenCalled();
508+
expect(result).toBe(true, 'gif');
509+
510+
anchor.href = 'cat-photo.jpg';
511+
result = service.handleAnchorClick(anchor);
512+
expect(service.go).not.toHaveBeenCalled();
513+
expect(result).toBe(true, 'jpg');
514+
515+
anchor.href = 'dog-bark.mp3';
516+
result = service.handleAnchorClick(anchor);
517+
expect(service.go).not.toHaveBeenCalled();
518+
expect(result).toBe(true, 'mp3');
519+
520+
anchor.href = 'pet-tricks.mp4';
521+
result = service.handleAnchorClick(anchor);
522+
expect(service.go).not.toHaveBeenCalled();
523+
expect(result).toBe(true, 'mp4');
524+
});
525+
526+
it('url has any extension', () => {
527+
anchor.href = 'tutorial/toh-p2.html';
528+
const result = service.handleAnchorClick(anchor);
529+
expect(service.go).not.toHaveBeenCalled();
530+
expect(result).toBe(true);
531+
});
493532
});
494533
});
495534

‎aio/src/app/shared/location.service.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ export class LocationService {
8787
}
8888

8989
/**
90+
* Handle user's anchor click
91+
*
92+
* @param anchor {HTMLAnchorElement} - the anchor element clicked
93+
* @param button Number of the mouse button held down. 0 means left or none
94+
* @param ctrlKey True if control key held down
95+
* @param metaKey True if command or window key held down
96+
* @return false if service navigated with `go()`; true if browser should handle it.
97+
*
9098
* Since we are using `LocationService` to navigate between docs, without the browser
9199
* reloading the page, we must intercept clicks on links.
92100
* If the link is to a document that we will render, then we navigate using `Location.go()`
@@ -99,9 +107,10 @@ export class LocationService {
99107
* `AppComponent`, whose element contains all the of the application and so captures all
100108
* link clicks both inside and outside the `DocViewerComponent`.
101109
*/
102-
handleAnchorClick(anchor: HTMLAnchorElement, button: number, ctrlKey: boolean, metaKey: boolean) {
103110

104-
// Check for modifier keys, which indicate the user wants to control navigation
111+
handleAnchorClick(anchor: HTMLAnchorElement, button = 0, ctrlKey = false, metaKey = false) {
112+
113+
// Check for modifier keys and non-left-button, which indicate the user wants to control navigation
105114
if (button !== 0 || ctrlKey || metaKey) {
106115
return true;
107116
}
@@ -114,19 +123,21 @@ export class LocationService {
114123
return true;
115124
}
116125

117-
// don't navigate if external link or zip
118-
const { pathname, search, hash } = anchor;
119-
120126
if (anchor.getAttribute('download') != null) {
121127
return true; // let the download happen
122128
}
123129

130+
const { pathname, search, hash } = anchor;
124131
const relativeUrl = pathname + search + hash;
125132
this.urlParser.href = relativeUrl;
126-
if (anchor.href !== this.urlParser.href) {
133+
134+
// don't navigate if external link or has extension
135+
if ( anchor.href !== this.urlParser.href ||
136+
!/\/[^/.]*$/.test(pathname) ) {
127137
return true;
128138
}
129139

140+
// approved for navigation
130141
this.go(relativeUrl);
131142
return false;
132143
}

0 commit comments

Comments
 (0)