-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add external link directive to easily set required attributes
- Loading branch information
1 parent
a7a53ab
commit 5c2fa6a
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ElementRef } from '@angular/core'; | ||
import { ExternalLinkDirective } from './external-link.directive'; | ||
|
||
describe('ExternalLinkDirective', () => { | ||
let element: ElementRef; | ||
|
||
beforeEach(() => { | ||
element = { | ||
nativeElement: jasmine.createSpyObj('nativeElement', ['setAttribute']), | ||
}; | ||
}); | ||
|
||
it('should set required attributes on element', () => { | ||
const directive = new ExternalLinkDirective(element); | ||
expect(directive).toBeTruthy(); | ||
expect(element.nativeElement.setAttribute).toHaveBeenCalledWith('target', '_blank'); | ||
expect(element.nativeElement.setAttribute).toHaveBeenCalledWith('rel', 'noreferrer'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Directive, ElementRef } from '@angular/core'; | ||
|
||
@Directive({ | ||
selector: '[tce-external-link]', | ||
standalone: true, | ||
}) | ||
export class ExternalLinkDirective { | ||
constructor(private element: ElementRef) { | ||
this.element.nativeElement.setAttribute('target', '_blank'); | ||
this.element.nativeElement.setAttribute('rel', 'noreferrer'); | ||
} | ||
} |