Skip to content

Commit

Permalink
Merge pull request #3322 from Andrea-Guevara/TargetUrl
Browse files Browse the repository at this point in the history
fix #3241: Configuring the URI link target
  • Loading branch information
tdonohue authored Dec 19, 2024
2 parents 5b3da02 + 8572bfb commit aba988a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

<!-- Render value as a link (href and label) -->
<ng-template #link let-value="value">
<a class="dont-break-out ds-simple-metadata-link" target="_blank" [href]="value">
<a class="dont-break-out ds-simple-metadata-link"
[href]="value"
[attr.target]="getLinkAttributes(value).target"
[attr.rel]="getLinkAttributes(value).rel">
{{value}}
</a>
</ng-template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,20 @@ describe('MetadataValuesComponent', () => {
expect(comp.hasLink(mdValue)).toBe(true);
});

it('should return correct target and rel for internal links', () => {
spyOn(comp, 'hasInternalLink').and.returnValue(true);
const urlValue = '/internal-link';
const result = comp.getLinkAttributes(urlValue);
expect(result.target).toBe('_self');
expect(result.rel).toBe('');
});

it('should return correct target and rel for external links', () => {
spyOn(comp, 'hasInternalLink').and.returnValue(false);
const urlValue = 'https://www.dspace.org';
const result = comp.getLinkAttributes(urlValue);
expect(result.target).toBe('_blank');
expect(result.rel).toBe('noopener noreferrer');
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,16 @@ export class MetadataValuesComponent implements OnChanges {
hasInternalLink(linkValue: string): boolean {
return linkValue.startsWith(environment.ui.baseUrl);
}

/**
* This method performs a validation and determines the target of the url.
* @returns - Returns the target url.
*/
getLinkAttributes(urlValue: string): { target: string, rel: string } {
if (this.hasInternalLink(urlValue)) {
return { target: '_self', rel: '' };
} else {
return { target: '_blank', rel: 'noopener noreferrer' };
}
}
}

0 comments on commit aba988a

Please sign in to comment.