Skip to content

Commit 489711d

Browse files
MeAkibAndrewKushnir
authored andcommitted
docs(docs-infra): add rel="noopener" to external links with target="_blank" in ExternalLink directive (angular#64276)
PR Close angular#64276
1 parent f521c1f commit 489711d

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

adev/shared-docs/directives/external-link/external-link.directive.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,42 @@ describe('ExternalLink', () => {
4141
By.css('a[href="https://stackoverflow.com/questions/tagged/angular"]'),
4242
);
4343
expect(externalLink.attributes['target']).toEqual('_blank');
44+
expect(externalLink.attributes['rel']).toEqual('noopener');
4445
});
4546

4647
it('should not internal link have target=_blank attribute', () => {
4748
const internalLink = fixture.debugElement.query(By.css('a[href="/roadmap"]'));
4849
expect(internalLink.attributes['target']).toBeFalsy();
50+
expect(internalLink.attributes['rel']).toBeFalsy();
4951
});
5052

5153
it('should not set target=_blank attribute external link when anchor has got noBlankForExternalLink attribute', () => {
5254
const externalLink = fixture.debugElement.query(
5355
By.css('a[href="https://github.com/angular/angular/issues"]'),
5456
);
5557
expect(externalLink.attributes['target']).toBeFalsy();
58+
expect(externalLink.attributes['rel']).toBeFalsy();
5659
});
5760
});
5861

5962
@Component({
6063
template: `
6164
<a
65+
class="external"
6266
href="https://stackoverflow.com/questions/tagged/angular"
6367
title="Stack Overflow: where the community answers your technical Angular questions."
6468
>
6569
Stack Overflow
6670
</a>
67-
<a routerLink="/roadmap" title="Roadmap">Roadmap</a>
71+
<a class="internal" routerLink="/roadmap" title="Roadmap">Roadmap</a>
6872
<a
73+
class="optout"
6974
href="https://github.com/angular/angular/issues"
7075
title="Post issues and suggestions on github"
7176
noBlankForExternalLink
72-
></a>
77+
>
78+
GitHub Issues
79+
</a>
7380
`,
7481
imports: [ExternalLink, RouterLink],
7582
})

adev/shared-docs/directives/external-link/external-link.directive.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ import {isExternalLink} from '../../utils/index';
1818
selector: 'a[href]:not([noBlankForExternalLink])',
1919
host: {
2020
'[attr.target]': 'target',
21+
'[attr.rel]': 'rel',
2122
},
2223
})
2324
export class ExternalLink {
2425
private readonly anchor: ElementRef<HTMLAnchorElement> = inject(ElementRef);
2526
private readonly platformId = inject(PLATFORM_ID);
2627

2728
target?: '_blank' | '_self' | '_parent' | '_top' | '';
29+
rel?: string;
2830

2931
constructor() {
3032
this.setAnchorTarget();
@@ -37,6 +39,7 @@ export class ExternalLink {
3739

3840
if (isExternalLink(this.anchor.nativeElement.href)) {
3941
this.target = '_blank';
42+
this.rel = 'noopener';
4043
}
4144
}
4245
}

0 commit comments

Comments
 (0)