docs(docs-infra): add rel="noopener" to external links with target="_blank" in ExternalLink directive (#64276)

PR Close #64276
This commit is contained in:
Shuaib Hasan Akib 2025-10-07 20:46:22 +06:00 committed by Andrew Kushnir
parent f521c1fd76
commit 489711d75c
2 changed files with 12 additions and 2 deletions

View file

@ -41,11 +41,13 @@ describe('ExternalLink', () => {
By.css('a[href="https://stackoverflow.com/questions/tagged/angular"]'),
);
expect(externalLink.attributes['target']).toEqual('_blank');
expect(externalLink.attributes['rel']).toEqual('noopener');
});
it('should not internal link have target=_blank attribute', () => {
const internalLink = fixture.debugElement.query(By.css('a[href="/roadmap"]'));
expect(internalLink.attributes['target']).toBeFalsy();
expect(internalLink.attributes['rel']).toBeFalsy();
});
it('should not set target=_blank attribute external link when anchor has got noBlankForExternalLink attribute', () => {
@ -53,23 +55,28 @@ describe('ExternalLink', () => {
By.css('a[href="https://github.com/angular/angular/issues"]'),
);
expect(externalLink.attributes['target']).toBeFalsy();
expect(externalLink.attributes['rel']).toBeFalsy();
});
});
@Component({
template: `
<a
class="external"
href="https://stackoverflow.com/questions/tagged/angular"
title="Stack Overflow: where the community answers your technical Angular questions."
>
Stack Overflow
</a>
<a routerLink="/roadmap" title="Roadmap">Roadmap</a>
<a class="internal" routerLink="/roadmap" title="Roadmap">Roadmap</a>
<a
class="optout"
href="https://github.com/angular/angular/issues"
title="Post issues and suggestions on github"
noBlankForExternalLink
></a>
>
GitHub Issues
</a>
`,
imports: [ExternalLink, RouterLink],
})

View file

@ -18,6 +18,7 @@ import {isExternalLink} from '../../utils/index';
selector: 'a[href]:not([noBlankForExternalLink])',
host: {
'[attr.target]': 'target',
'[attr.rel]': 'rel',
},
})
export class ExternalLink {
@ -25,6 +26,7 @@ export class ExternalLink {
private readonly platformId = inject(PLATFORM_ID);
target?: '_blank' | '_self' | '_parent' | '_top' | '';
rel?: string;
constructor() {
this.setAnchorTarget();
@ -37,6 +39,7 @@ export class ExternalLink {
if (isExternalLink(this.anchor.nativeElement.href)) {
this.target = '_blank';
this.rel = 'noopener';
}
}
}