mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
17a226d25d
Use signals to avoid markForCheck.
Simplify takeUntilDestroyed usage by relying on implicit DestroyRef.
Improve type safety by typing inject(ElementRef).
(cherry picked from commit a0b998e293)
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
/*!
|
|
* @license
|
|
* Copyright Google LLC All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.dev/license
|
|
*/
|
|
|
|
import {isPlatformBrowser} from '@angular/common';
|
|
import {Directive, ElementRef, PLATFORM_ID, inject} from '@angular/core';
|
|
import {isExternalLink} from '../../utils/index';
|
|
|
|
/**
|
|
* The directive will set target of anchor elements to '_blank' for the external links.
|
|
* We can opt-out this behavior by adding `noBlankForExternalLink` attribute to anchor element.
|
|
*/
|
|
@Directive({
|
|
selector: 'a[href]:not([noBlankForExternalLink])',
|
|
host: {
|
|
'[attr.target]': 'target',
|
|
'[attr.rel]': 'rel',
|
|
},
|
|
})
|
|
export class ExternalLink {
|
|
private readonly anchor = inject<ElementRef<HTMLAnchorElement>>(ElementRef);
|
|
private readonly platformId = inject(PLATFORM_ID);
|
|
|
|
target?: '_blank' | '_self' | '_parent' | '_top' | '';
|
|
rel?: string;
|
|
|
|
constructor() {
|
|
this.setAnchorTarget();
|
|
}
|
|
|
|
private setAnchorTarget(): void {
|
|
if (!isPlatformBrowser(this.platformId)) {
|
|
return;
|
|
}
|
|
|
|
if (isExternalLink(this.anchor.nativeElement.href)) {
|
|
this.target = '_blank';
|
|
this.rel = 'noopener';
|
|
}
|
|
}
|
|
}
|