Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class CollectionItemMapperComponent implements OnInit {

this.collectionName$ = this.collectionRD$.pipe(
map((rd: RemoteData<Collection>) => {
return this.dsoNameService.getName(rd.payload);
return this.dsoNameService.getName(rd.payload, true);
})
);
this.searchOptions$ = this.searchConfigService.paginatedSearchOptions;
Expand Down
47 changes: 31 additions & 16 deletions src/app/core/breadcrumbs/dso-name.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,28 @@ describe(`DSONameService`, () => {
service = new DSONameService({ instant: (a) => a } as any);
});

describe(`getHitHighlights with escapeHTML`, () => {
const item = Object.assign(new Item(), {
metadata: { 'dc.title': [{ value: 'Title <img src="x">' }] },
});

it(`should escape the title`, () => {
expect(service.getHitHighlights({}, item, true)).toBe('Title &lt;img src=&quot;x&quot;&gt;');
});

it(`should escape a highlighted title but keep its <em> tags`, () => {
const hitHighlights = { 'dc.title': [{ value: '<em>Title</em> <img src="x">' }] };
expect(service.getHitHighlights({ hitHighlights }, item, true)).toBe('<em>Title</em> &lt;img src=&quot;x&quot;&gt;');
});
});

describe(`getName`, () => {
it(`should use the Person factory for Person entities`, () => {
spyOn((service as any).factories, 'Person').and.returnValue('Bingo!');

const result = service.getName(mockPerson);

expect((service as any).factories.Person).toHaveBeenCalledWith(mockPerson);
expect((service as any).factories.Person).toHaveBeenCalledWith(mockPerson, undefined);
expect(result).toBe('Bingo!');
});

Expand All @@ -87,7 +102,7 @@ describe(`DSONameService`, () => {

const result = service.getName(mockOrgUnit);

expect((service as any).factories.OrgUnit).toHaveBeenCalledWith(mockOrgUnit);
expect((service as any).factories.OrgUnit).toHaveBeenCalledWith(mockOrgUnit, undefined);
expect(result).toBe('Bingo!');
});

Expand All @@ -96,7 +111,7 @@ describe(`DSONameService`, () => {

const result = service.getName(mockEPerson);

expect((service as any).factories.EPerson).toHaveBeenCalledWith(mockEPerson);
expect((service as any).factories.EPerson).toHaveBeenCalledWith(mockEPerson, undefined);
expect(result).toBe('Bingo!');
});

Expand All @@ -105,7 +120,7 @@ describe(`DSONameService`, () => {

const result = service.getName(mockDSO);

expect((service as any).factories.Default).toHaveBeenCalledWith(mockDSO);
expect((service as any).factories.Default).toHaveBeenCalledWith(mockDSO, undefined);
expect(result).toBe('Bingo!');
});
});
Expand All @@ -119,9 +134,9 @@ describe(`DSONameService`, () => {
it(`should return 'person.familyName, person.givenName'`, () => {
const result = (service as any).factories.Person(mockPerson);
expect(result).toBe(mockPersonName);
expect(mockPerson.firstMetadataValue).toHaveBeenCalledWith('person.familyName');
expect(mockPerson.firstMetadataValue).toHaveBeenCalledWith('person.givenName');
expect(mockPerson.firstMetadataValue).not.toHaveBeenCalledWith('dc.title');
expect(mockPerson.firstMetadataValue).toHaveBeenCalledWith('person.familyName', undefined, undefined);
expect(mockPerson.firstMetadataValue).toHaveBeenCalledWith('person.givenName', undefined, undefined);
expect(mockPerson.firstMetadataValue).not.toHaveBeenCalledWith('dc.title', undefined, undefined);
});
});

Expand All @@ -133,9 +148,9 @@ describe(`DSONameService`, () => {
it(`should return dc.title`, () => {
const result = (service as any).factories.Person(mockPerson);
expect(result).toBe(mockPersonName);
expect(mockPerson.firstMetadataValue).toHaveBeenCalledWith('person.familyName');
expect(mockPerson.firstMetadataValue).toHaveBeenCalledWith('person.givenName');
expect(mockPerson.firstMetadataValue).toHaveBeenCalledWith('dc.title');
expect(mockPerson.firstMetadataValue).toHaveBeenCalledWith('person.familyName', undefined, undefined);
expect(mockPerson.firstMetadataValue).toHaveBeenCalledWith('person.givenName', undefined, undefined);
expect(mockPerson.firstMetadataValue).toHaveBeenCalledWith('dc.title', undefined, undefined);
});
});
});
Expand All @@ -149,8 +164,8 @@ describe(`DSONameService`, () => {
it(`should return 'eperson.firstname' and 'eperson.lastname'`, () => {
const result = (service as any).factories.EPerson(mockEPerson);
expect(result).toBe(mockEPersonName);
expect(mockEPerson.firstMetadataValue).toHaveBeenCalledWith('eperson.firstname');
expect(mockEPerson.firstMetadataValue).toHaveBeenCalledWith('eperson.lastname');
expect(mockEPerson.firstMetadataValue).toHaveBeenCalledWith('eperson.firstname', undefined, undefined);
expect(mockEPerson.firstMetadataValue).toHaveBeenCalledWith('eperson.lastname', undefined, undefined);
});
});

Expand All @@ -162,8 +177,8 @@ describe(`DSONameService`, () => {
it(`should return 'eperson.firstname'`, () => {
const result = (service as any).factories.EPerson(mockEPersonFirst);
expect(result).toBe(mockEPersonNameFirst);
expect(mockEPersonFirst.firstMetadataValue).toHaveBeenCalledWith('eperson.firstname');
expect(mockEPersonFirst.firstMetadataValue).toHaveBeenCalledWith('eperson.lastname');
expect(mockEPersonFirst.firstMetadataValue).toHaveBeenCalledWith('eperson.firstname', undefined, undefined);
expect(mockEPersonFirst.firstMetadataValue).toHaveBeenCalledWith('eperson.lastname', undefined, undefined);
});
});
});
Expand All @@ -177,7 +192,7 @@ describe(`DSONameService`, () => {
it(`should return 'organization.legalName'`, () => {
const result = (service as any).factories.OrgUnit(mockOrgUnit);
expect(result).toBe(mockOrgUnitName);
expect(mockOrgUnit.firstMetadataValue).toHaveBeenCalledWith('organization.legalName');
expect(mockOrgUnit.firstMetadataValue).toHaveBeenCalledWith('organization.legalName', undefined, undefined);
});
});

Expand All @@ -189,7 +204,7 @@ describe(`DSONameService`, () => {
it(`should return 'dc.title'`, () => {
const result = (service as any).factories.Default(mockDSO);
expect(result).toBe(mockDSOName);
expect(mockDSO.firstMetadataValue).toHaveBeenCalledWith('dc.title');
expect(mockDSO.firstMetadataValue).toHaveBeenCalledWith('dc.title', undefined, undefined);
});
});
});
49 changes: 26 additions & 23 deletions src/app/core/breadcrumbs/dso-name.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export class DSONameService {
* With only two exceptions those solutions seem overkill for now.
*/
private readonly factories = {
EPerson: (dso: DSpaceObject): string => {
const firstName = dso.firstMetadataValue('eperson.firstname');
const lastName = dso.firstMetadataValue('eperson.lastname');
EPerson: (dso: DSpaceObject, escapeHTML?: boolean): string => {
const firstName = dso.firstMetadataValue('eperson.firstname', undefined, escapeHTML);
const lastName = dso.firstMetadataValue('eperson.lastname', undefined, escapeHTML);
if (isEmpty(firstName) && isEmpty(lastName)) {
return this.translateService.instant('dso.name.unnamed');
} else if (isEmpty(firstName) || isEmpty(lastName)) {
Expand All @@ -38,32 +38,33 @@ export class DSONameService {
return `${firstName} ${lastName}`;
}
},
Person: (dso: DSpaceObject): string => {
const familyName = dso.firstMetadataValue('person.familyName');
const givenName = dso.firstMetadataValue('person.givenName');
Person: (dso: DSpaceObject, escapeHTML?: boolean): string => {
const familyName = dso.firstMetadataValue('person.familyName', undefined, escapeHTML);
const givenName = dso.firstMetadataValue('person.givenName', undefined, escapeHTML);
if (isEmpty(familyName) && isEmpty(givenName)) {
return dso.firstMetadataValue('dc.title') || this.translateService.instant('dso.name.unnamed');
return dso.firstMetadataValue('dc.title', undefined, escapeHTML) || this.translateService.instant('dso.name.unnamed');
} else if (isEmpty(familyName) || isEmpty(givenName)) {
return familyName || givenName;
} else {
return `${familyName}, ${givenName}`;
}
},
OrgUnit: (dso: DSpaceObject): string => {
return dso.firstMetadataValue('organization.legalName') || this.translateService.instant('dso.name.untitled');
OrgUnit: (dso: DSpaceObject, escapeHTML?: boolean): string => {
return dso.firstMetadataValue('organization.legalName', undefined, escapeHTML);
},
Default: (dso: DSpaceObject): string => {
Default: (dso: DSpaceObject, escapeHTML?: boolean): string => {
// If object doesn't have dc.title metadata use name property
return dso.firstMetadataValue('dc.title') || dso.name || this.translateService.instant('dso.name.untitled');
}
return dso.firstMetadataValue('dc.title', undefined, escapeHTML) || dso.name || this.translateService.instant('dso.name.untitled');
Comment thread
milanmajchrak marked this conversation as resolved.
Resolved
},
};

/**
* Get the name for the given {@link DSpaceObject}
*
* @param dso The {@link DSpaceObject} you want a name for
* @param escapeHTML Whether the HTML is used inside a `[innerHTML]` attribute
*/
getName(dso: DSpaceObject | undefined): string {
getName(dso: DSpaceObject | undefined, escapeHTML?: boolean): string {
if (dso) {
const types = dso.getRenderTypes();
const match = types
Expand All @@ -72,10 +73,10 @@ export class DSONameService {

let name;
if (hasValue(match)) {
name = this.factories[match](dso);
name = this.factories[match](dso, escapeHTML);
}
if (isEmpty(name)) {
name = this.factories.Default(dso);
name = this.factories.Default(dso, escapeHTML);
}
return name;
} else {
Expand All @@ -88,27 +89,28 @@ export class DSONameService {
*
* @param object
* @param dso
* @param escapeHTML Whether the HTML is used inside a `[innerHTML]` attribute
*
* @returns {string} html embedded hit highlight.
*/
getHitHighlights(object: any, dso: DSpaceObject): string {
getHitHighlights(object: any, dso: DSpaceObject, escapeHTML?: boolean): string {
const types = dso.getRenderTypes();
const entityType = types
.filter((type) => typeof type === 'string')
.find((type: string) => (['Person', 'OrgUnit']).includes(type)) as string;
if (entityType === 'Person') {
const familyName = this.firstMetadataValue(object, dso, 'person.familyName');
const givenName = this.firstMetadataValue(object, dso, 'person.givenName');
const familyName = this.firstMetadataValue(object, dso, 'person.familyName', escapeHTML);
const givenName = this.firstMetadataValue(object, dso, 'person.givenName', escapeHTML);
if (isEmpty(familyName) && isEmpty(givenName)) {
return this.firstMetadataValue(object, dso, 'dc.title') || dso.name;
return this.firstMetadataValue(object, dso, 'dc.title', escapeHTML) || dso.name;
} else if (isEmpty(familyName) || isEmpty(givenName)) {
return familyName || givenName;
}
return `${familyName}, ${givenName}`;
} else if (entityType === 'OrgUnit') {
return this.firstMetadataValue(object, dso, 'organization.legalName') || this.translateService.instant('dso.name.untitled');
return this.firstMetadataValue(object, dso, 'organization.legalName', escapeHTML);
Comment thread
milanmajchrak marked this conversation as resolved.
Resolved
}
return this.firstMetadataValue(object, dso, 'dc.title') || dso.name || this.translateService.instant('dso.name.untitled');
return this.firstMetadataValue(object, dso, 'dc.title', escapeHTML) || dso.name || this.translateService.instant('dso.name.untitled');
}

/**
Expand All @@ -117,11 +119,12 @@ export class DSONameService {
* @param object
* @param dso
* @param {string|string[]} keyOrKeys The metadata key(s) in scope. Wildcards are supported; see [[Metadata]].
* @param escapeHTML Whether the HTML is used inside a `[innerHTML]` attribute
*
* @returns {string} the first matching string value, or `undefined`.
*/
firstMetadataValue(object: any, dso: DSpaceObject, keyOrKeys: string | string[]): string {
return Metadata.firstValue([object.hitHighlights, dso.metadata], keyOrKeys);
firstMetadataValue(object: any, dso: DSpaceObject, keyOrKeys: string | string[], escapeHTML?: boolean): string {
return Metadata.firstValue(dso.metadata, keyOrKeys, object.hitHighlights, undefined, escapeHTML);
}

}
32 changes: 18 additions & 14 deletions src/app/core/shared/dspace-object.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,60 +108,64 @@ export class DSpaceObject extends ListableObject implements CacheableObject {
* Gets all matching metadata in this DSpaceObject.
*
* @param {string|string[]} keyOrKeys The metadata key(s) in scope. Wildcards are supported; see [[Metadata]].
* @param {MetadataValueFilter} filter The value filter to use. If unspecified, no filtering will be done.
* @param {MetadataValueFilter} valueFilter The value filter to use. If unspecified, no filtering will be done.
* @param escapeHTML Whether the HTML is used inside a `[innerHTML]` attribute
* @returns {MetadataValue[]} the matching values or an empty array.
*/
allMetadata(keyOrKeys: string | string[], valueFilter?: MetadataValueFilter): MetadataValue[] {
return Metadata.all(this.metadata, keyOrKeys, valueFilter);
allMetadata(keyOrKeys: string | string[], valueFilter?: MetadataValueFilter, escapeHTML?: boolean): MetadataValue[] {
return Metadata.all(this.metadata, keyOrKeys, undefined, valueFilter, escapeHTML);
}

/**
* Like [[allMetadata]], but only returns string values.
*
* @param {string|string[]} keyOrKeys The metadata key(s) in scope. Wildcards are supported; see [[Metadata]].
* @param {MetadataValueFilter} filter The value filter to use. If unspecified, no filtering will be done.
* @param {MetadataValueFilter} valueFilter The value filter to use. If unspecified, no filtering will be done.
* @param escapeHTML Whether the HTML is used inside a `[innerHTML]` attribute
* @returns {string[]} the matching string values or an empty array.
*/
allMetadataValues(keyOrKeys: string | string[], valueFilter?: MetadataValueFilter): string[] {
return Metadata.allValues(this.metadata, keyOrKeys, valueFilter);
allMetadataValues(keyOrKeys: string | string[], valueFilter?: MetadataValueFilter, escapeHTML?: boolean): string[] {
return Metadata.allValues(this.metadata, keyOrKeys, undefined, valueFilter, escapeHTML);
}

/**
* Gets the first matching MetadataValue object in this DSpaceObject, or `undefined`.
*
* @param {string|string[]} keyOrKeys The metadata key(s) in scope. Wildcards are supported; see [[Metadata]].
* @param {MetadataValueFilter} filter The value filter to use. If unspecified, no filtering will be done.
* @param {MetadataValueFilter} valueFilter The value filter to use. If unspecified, no filtering will be done.
* @param escapeHTML Whether the HTML is used inside a `[innerHTML]` attribute
* @returns {MetadataValue} the first matching value, or `undefined`.
*/
firstMetadata(keyOrKeys: string | string[], valueFilter?: MetadataValueFilter): MetadataValue {
return Metadata.first(this.metadata, keyOrKeys, valueFilter);
firstMetadata(keyOrKeys: string | string[], valueFilter?: MetadataValueFilter, escapeHTML?: boolean): MetadataValue {
return Metadata.first(this.metadata, keyOrKeys, undefined, valueFilter, escapeHTML);
}

/**
* Like [[firstMetadata]], but only returns a string value, or `undefined`.
*
* @param {string|string[]} keyOrKeys The metadata key(s) in scope. Wildcards are supported; see [[Metadata]].
* @param {MetadataValueFilter} valueFilter The value filter to use. If unspecified, no filtering will be done.
* @param escapeHTML Whether the HTML is used inside a `[innerHTML]` attribute
* @returns {string} the first matching string value, or `undefined`.
*/
firstMetadataValue(keyOrKeys: string | string[], valueFilter?: MetadataValueFilter): string {
return Metadata.firstValue(this.metadata, keyOrKeys, valueFilter);
firstMetadataValue(keyOrKeys: string | string[], valueFilter?: MetadataValueFilter, escapeHTML?: boolean): string {
return Metadata.firstValue(this.metadata, keyOrKeys, undefined, valueFilter, escapeHTML);
}

/**
* Checks for a matching metadata value in this DSpaceObject.
*
* @param {string|string[]} keyOrKeys The metadata key(s) in scope. Wildcards are supported; see [[Metadata]].
* @param {MetadataValueFilter} filter The value filter to use. If unspecified, no filtering will be done.
* @param {MetadataValueFilter} valueFilter The value filter to use. If unspecified, no filtering will be done.
* @returns {boolean} whether a match is found.
*/
hasMetadata(keyOrKeys: string | string[], valueFilter?: MetadataValueFilter): boolean {
return Metadata.has(this.metadata, keyOrKeys, valueFilter);
return Metadata.has(this.metadata, keyOrKeys, undefined, valueFilter);
}

/**
* Find metadata on a specific field and order all of them using their "place" property.
* @param key
* @param keyOrKeys The metadata key(s) in scope. Wildcards are supported; see [[Metadata]].
*/
findMetadataSortedByPlace(keyOrKeys: string | string[]): MetadataValue[] {
return this.allMetadata(keyOrKeys).sort((a: MetadataValue, b: MetadataValue) => {
Expand Down
Loading
Loading