mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
docs(docs-infra): add rel="noopener" to external links with target="_blank" in ExternalLink directive (#64276)
PR Close #64276
This commit is contained in:
parent
f521c1fd76
commit
489711d75c
2 changed files with 12 additions and 2 deletions
|
|
@ -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],
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue