Skip to content

Commit

Permalink
Add external link directive to easily set required attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
mgriffin-scottlogic committed Aug 2, 2024
1 parent a7a53ab commit 5c2fa6a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/app/directives/external-link.directive.spec.ts
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');
});
});
12 changes: 12 additions & 0 deletions src/app/directives/external-link.directive.ts
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');
}
}

0 comments on commit 5c2fa6a

Please sign in to comment.