Skip to content

Commit 8152258

Browse files
authored
Merge pull request #3766 from tdonohue/port_3322_to_7x
[Port dspace-7_x] fix #3241: Configuring the URI link target
2 parents eb3db9c + c6ef2f1 commit 8152258

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/app/item-page/field-components/metadata-values/metadata-values.component.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
<!-- Render value as a link (href and label) -->
2020
<ng-template #link let-value="value">
21-
<a class="dont-break-out ds-simple-metadata-link" target="_blank" [href]="value">
21+
<a class="dont-break-out ds-simple-metadata-link"
22+
[href]="value"
23+
[attr.target]="getLinkAttributes(value).target"
24+
[attr.rel]="getLinkAttributes(value).rel">
2225
{{value}}
2326
</a>
2427
</ng-template>

src/app/item-page/field-components/metadata-values/metadata-values.component.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,20 @@ describe('MetadataValuesComponent', () => {
7373
expect(comp.hasLink(mdValue)).toBe(true);
7474
});
7575

76+
it('should return correct target and rel for internal links', () => {
77+
spyOn(comp, 'hasInternalLink').and.returnValue(true);
78+
const urlValue = '/internal-link';
79+
const result = comp.getLinkAttributes(urlValue);
80+
expect(result.target).toBe('_self');
81+
expect(result.rel).toBe('');
82+
});
83+
84+
it('should return correct target and rel for external links', () => {
85+
spyOn(comp, 'hasInternalLink').and.returnValue(false);
86+
const urlValue = 'https://www.dspace.org';
87+
const result = comp.getLinkAttributes(urlValue);
88+
expect(result.target).toBe('_blank');
89+
expect(result.rel).toBe('noopener noreferrer');
90+
});
91+
7692
});

src/app/item-page/field-components/metadata-values/metadata-values.component.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { APP_CONFIG, AppConfig } from '../../../../config/app-config.interface';
44
import { BrowseDefinition } from '../../../core/shared/browse-definition.model';
55
import { hasValue } from '../../../shared/empty.util';
66
import { VALUE_LIST_BROWSE_DEFINITION } from '../../../core/shared/value-list-browse-definition.resource-type';
7+
import { environment } from '../../../../environments/environment';
78

89
/**
910
* This component renders the configured 'values' into the ds-metadata-field-wrapper component.
@@ -90,4 +91,25 @@ export class MetadataValuesComponent implements OnChanges {
9091
}
9192
return queryParams;
9293
}
94+
95+
/**
96+
* Checks if the given link value is an internal link.
97+
* @param linkValue - The link value to check.
98+
* @returns True if the link value starts with the base URL defined in the environment configuration, false otherwise.
99+
*/
100+
hasInternalLink(linkValue: string): boolean {
101+
return linkValue.startsWith(environment.ui.baseUrl);
102+
}
103+
104+
/**
105+
* This method performs a validation and determines the target of the url.
106+
* @returns - Returns the target url.
107+
*/
108+
getLinkAttributes(urlValue: string): { target: string, rel: string } {
109+
if (this.hasInternalLink(urlValue)) {
110+
return { target: '_self', rel: '' };
111+
} else {
112+
return { target: '_blank', rel: 'noopener noreferrer' };
113+
}
114+
}
93115
}

0 commit comments

Comments
 (0)