From 25ab69ff213f80e1cc48704317abeee92a6071dc Mon Sep 17 00:00:00 2001 From: igorbaptist4 Date: Wed, 11 Sep 2024 14:48:59 -0300 Subject: [PATCH 1/3] Configuring the URI link target --- .../metadata-values.component.html | 5 ++++- .../metadata-values.component.ts | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/app/item-page/field-components/metadata-values/metadata-values.component.html b/src/app/item-page/field-components/metadata-values/metadata-values.component.html index a598815d4cc..a9576da26a3 100644 --- a/src/app/item-page/field-components/metadata-values/metadata-values.component.html +++ b/src/app/item-page/field-components/metadata-values/metadata-values.component.html @@ -18,7 +18,10 @@ - diff --git a/src/app/item-page/field-components/metadata-values/metadata-values.component.ts b/src/app/item-page/field-components/metadata-values/metadata-values.component.ts index cbbae9006da..20df92099f2 100644 --- a/src/app/item-page/field-components/metadata-values/metadata-values.component.ts +++ b/src/app/item-page/field-components/metadata-values/metadata-values.component.ts @@ -4,6 +4,7 @@ import { APP_CONFIG, AppConfig } from '../../../../config/app-config.interface'; import { BrowseDefinition } from '../../../core/shared/browse-definition.model'; import { hasValue } from '../../../shared/empty.util'; import { VALUE_LIST_BROWSE_DEFINITION } from '../../../core/shared/value-list-browse-definition.resource-type'; +import { environment } from '../../../../environments/environment'; /** * This component renders the configured 'values' into the ds-metadata-field-wrapper component. @@ -90,4 +91,25 @@ export class MetadataValuesComponent implements OnChanges { } return queryParams; } + + /** + * Checks if the given link value is an internal link. + * @param linkValue - The link value to check. + * @returns True if the link value starts with the base URL defined in the environment configuration, false otherwise. + */ + 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' }; + } + } } From 2a1ef02d7583eacb2e59d58c33486102ba6e2f17 Mon Sep 17 00:00:00 2001 From: igorbaptist4 Date: Wed, 11 Sep 2024 15:15:26 -0300 Subject: [PATCH 2/3] fix identation --- .../metadata-values/metadata-values.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/item-page/field-components/metadata-values/metadata-values.component.ts b/src/app/item-page/field-components/metadata-values/metadata-values.component.ts index 20df92099f2..a902e48ed1d 100644 --- a/src/app/item-page/field-components/metadata-values/metadata-values.component.ts +++ b/src/app/item-page/field-components/metadata-values/metadata-values.component.ts @@ -105,7 +105,7 @@ export class MetadataValuesComponent implements OnChanges { * This method performs a validation and determines the target of the url. * @returns - Returns the target url. */ - getLinkAttributes(urlValue: string): { target: string, rel: string } { + getLinkAttributes(urlValue: string): { target: string, rel: string } { if (this.hasInternalLink(urlValue)) { return { target: '_self', rel: '' }; } else { From c6ef2f1bd095e5e1bd417600cc817198cd8fd494 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 11 Dec 2024 13:07:48 -0300 Subject: [PATCH 3/3] Addition of unit tests for the getLinkAttributes() method --- .../metadata-values.component.spec.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/app/item-page/field-components/metadata-values/metadata-values.component.spec.ts b/src/app/item-page/field-components/metadata-values/metadata-values.component.spec.ts index 23f80982077..de46cd9e4c8 100644 --- a/src/app/item-page/field-components/metadata-values/metadata-values.component.spec.ts +++ b/src/app/item-page/field-components/metadata-values/metadata-values.component.spec.ts @@ -73,4 +73,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'); + }); + });