Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class OrgUnitSearchResultListSubmissionElementComponent extends SearchRes

if (this.useNameVariants) {
const defaultValue = this.dso ? this.dsoNameService.getName(this.dso) : undefined;
const alternatives = this.allMetadataValues(this.alternativeField);
const alternatives = this.allMetadataValues(this.alternativeField, false);
this.allSuggestions = [defaultValue, ...alternatives];

this.relationshipService.getNameVariant(this.listID, this.dso.uuid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@for (suggestionOption of suggestions; track suggestionOption) {
<div>
<a href="javascript:void(0);" class="d-block dropdown-item" (click)="onClickSuggestion(suggestionOption)" #suggestion>
<span [innerHTML]="suggestionOption"></span>
<span>{{suggestionOption}}</span>
</a>
</div>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@ describe('OrgUnitInputSuggestionsComponent', () => {
});
});

describe('When a suggestion has HTML markup', () => {
it('should render the markup as text', () => {
component.suggestions = ['Science <b>Unit</b>'];
fixture.detectChanges();
const option: HTMLElement = fixture.nativeElement.querySelector('.dropdown-item span');
expect(option.querySelector('b')).toBeNull();
expect(option.textContent).toEqual('Science <b>Unit</b>');
});
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class PersonSearchResultListSubmissionElementComponent extends SearchResu
ngOnInit() {
super.ngOnInit();
const defaultValue = this.dso ? this.dsoNameService.getName(this.dso) : undefined;
const alternatives = this.allMetadataValues(this.alternativeField);
const alternatives = this.allMetadataValues(this.alternativeField, false);
this.allSuggestions = [defaultValue, ...alternatives];

this.relationshipService.getNameVariant(this.listID, this.dso.uuid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@for (suggestionOption of suggestions; track suggestionOption) {
<div>
<a href="javascript:void(0);" class="d-block dropdown-item" (click)="onClickSuggestion(suggestionOption)" #suggestion>
<span [innerHTML]="suggestionOption"></span>
<span>{{suggestionOption}}</span>
</a>
</div>
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/item-page/full/full-item-page.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<td>{{mdEntry.key}}</td>
<td>
@for (value of (mdValue.value || '').split(SEPARATOR); track $index; let last = $last) {
<span><span [innerHTML]="makeLinks(value) | dsReplace: [';', ' ']"></span>@if (!last) {<span [innerHTML]="' '"></span>}</span>
<span><span [innerHTML]="makeLinks(value | dsReplace: [';', ' '])"></span>@if (!last) {<span [innerHTML]="' '"></span>}</span>
}
</td>
@if ((isAdmin$|async)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ describe('ClarinDescriptionItemFieldComponent', () => {
expect(anchor.nativeElement.getAttribute('target')).toEqual('_blank');
});

it('should show markup in the description as text and keep URLs as links', () => {
render(itemWithDescriptions([
{ value: '<b>Bold</b> <img src="pixel.png"> See https://example.org', language: 'en_US' },
]));

const element: HTMLElement = fixture.nativeElement;
expect(element.querySelector('b, img')).toBeNull();
expect(element.textContent).toContain('<b>Bold</b> <img src="pixel.png"> See https://example.org');
expect(element.querySelector('a').getAttribute('href')).toEqual('https://example.org');
});

it('should render nothing when the item has no description', () => {
render(Object.assign(new Item(), { metadata: {} }));

Expand Down
3 changes: 3 additions & 0 deletions src/app/process-page/detail/process-detail.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ describe('ProcessDetailComponent', () => {
const outputProcess = fixture.debugElement.query(By.css('#process-output pre'));
expect(outputProcess.nativeElement.textContent).toContain(processOutput);
});
it('should request the output logs without a token in the URL', () => {
expect((httpClient.get as jasmine.Spy).calls.mostRecent().args[0]).toBe('log-selflink');
});
});

describe('if press show output logs and process has no output logs', () => {
Expand Down
12 changes: 3 additions & 9 deletions src/app/process-page/detail/process-detail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import {
getFirstSucceededRemoteData,
getFirstSucceededRemoteDataPayload,
} from '../../core/shared/operators';
import { URLCombiner } from '../../core/url-combiner/url-combiner';
import { AlertType } from '../../shared/alert/alert-type';
import { hasValue } from '../../shared/empty.util';
import { ThemedFileDownloadLinkComponent } from '../../shared/file-download-link/themed-file-download-link.component';
Expand Down Expand Up @@ -108,7 +107,7 @@ export class ProcessDetailComponent implements OnInit, OnDestroy {
filesRD$: Observable<RemoteData<PaginatedList<Bitstream>>>;

/**
* File link that contain the output logs with auth token
* Link to the file that contains the output logs
*/
outputLogFileUrl$: Observable<string>;

Expand Down Expand Up @@ -225,13 +224,8 @@ export class ProcessDetailComponent implements OnInit, OnDestroy {
this.showOutputLogs = true;
}
}),
switchMap((processOutput: RemoteData<Bitstream>) => {
const url = processOutput.payload._links.content.href;
return this.authService.getShortlivedToken().pipe(take(1),
map((token: string) => {
return hasValue(token) ? new URLCombiner(url, `?authentication-token=${token}`).toString() : url;
}));
}),
// HttpClient sends the Authorization header, so the URL needs no token.
map((processOutput: RemoteData<Bitstream>) => processOutput.payload._links.content.href),
);
});
this.outputLogFileUrl$.pipe(take(1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ describe('GrantDenyRequestCopyComponent', () => {
});
});

it('should request the item name HTML-escaped', (done) => {
component.itemName$.subscribe(() => {
expect(nameService.getName).toHaveBeenCalledWith(item, true);
done();
});
});

it('should initialise itemUrl$', (done) => {
component.itemUrl$.subscribe((result) => {
expect(result).toEqual(itemUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class GrantDenyRequestCopyComponent implements OnInit {
);
this.itemName$ = this.itemRD$.pipe(
getFirstSucceededRemoteDataPayload(),
map((item) => this.nameService.getName(item)),
map((item) => this.nameService.getName(item, true)),
);
this.itemUrl$ = this.itemRD$.pipe(
getFirstSucceededRemoteDataPayload(),
Expand Down
18 changes: 18 additions & 0 deletions src/app/shared/clarin-shared-util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import {
buildAuthoritySearchFilter,
convertMetadataFieldIntoSearchType,
makeLinks,
} from './clarin-shared-util';

describe('clarin-shared-util', () => {
describe('makeLinks', () => {
it('turns URLs into links', () => {
expect(makeLinks('See https://example.org/a?b=1&c=2 now'))
.toBe('See <a href="https://example.org/a?b=1&amp;c=2" target="_blank" rel="noopener noreferrer">https://example.org/a?b=1&amp;c=2</a> now');
});

it('escapes markup in the text', () => {
expect(makeLinks('Read <a href="https://example.org/">here</a><img src="x">'))
.toBe('Read &lt;a href=&quot;<a href="https://example.org/" target="_blank" rel="noopener noreferrer">https://example.org/</a>&quot;&gt;here&lt;/a&gt;&lt;img src=&quot;x&quot;&gt;');
});

it('keeps empty values', () => {
expect(makeLinks(undefined)).toBeUndefined();
expect(makeLinks('')).toBe('');
});
});

describe('buildAuthoritySearchFilter', () => {
it('uses the authority operator and key when an authority is present', () => {
expect(buildAuthoritySearchFilter('publisher', { value: 'ACME Press', authority: '02mhbdp94' }))
Expand Down
11 changes: 8 additions & 3 deletions src/app/shared/clarin-shared-util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DomSanitizer } from '@angular/platform-browser';
import escape from 'lodash/escape';

import { ConfigurationDataService } from '../core/data/configuration-data.service';
import { MetadataValue } from '../core/shared/metadata.models';
Expand Down Expand Up @@ -97,9 +98,13 @@ export function loadItemAuthors(item, itemAuthors, baseUrl, fields) {
}

export function makeLinks(text: string): string {
// Use a regular expression to find URLs and convert them into clickable links
const regex = /(?:https?|ftp):\/\/[^\s)]+|www\.[^\s)]+/g;
return text?.replace(regex, (url) => `<a href="${url}" target="_blank">${url}</a>`);
if (!text) {
return text;
}
// Split the text into plain parts and URLs (the odd parts). Everything is escaped, so the links are the only HTML.
return text.split(/((?:https?|ftp):\/\/[^\s)"<>]+|www\.[^\s)"<>]+)/)
.map((part, i) => i % 2 === 0 ? escape(part) : `<a href="${escape(part)}" target="_blank" rel="noopener noreferrer">${escape(part)}</a>`)
.join('');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ describe('DsDynamicAutocompleteComponent test suite', () => {
it('should format object values using display fallback', () => {
expect(autComp.formatter({ display: 'Alumu-Tesu' })).toEqual('Alumu-Tesu');
});

it('should escape markup in the suggestions', () => {
const suggestion: any = Object.assign(new VocabularyEntry(), { display: 'A & B <img src="x">', value: 1 });
expect(autComp.suggestionFormatter(suggestion)).toEqual('A &amp; B &lt;img src=&quot;x&quot;&gt;');
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
DynamicFormValidationService,
} from '@ng-dynamic-forms/core';
import { TranslateService } from '@ngx-translate/core';
import escape from 'lodash/escape';
import {
BehaviorSubject,
Observable,
Expand Down Expand Up @@ -257,7 +258,7 @@ export class DsDynamicAutocompleteComponent extends DsDynamicTagComponent implem
return DsDynamicAutocompleteService.pretifyLanguageSuggestion(suggestion, this.translateService);
}
// @ts-ignore
return suggestion.display;
return escape(suggestion.display);
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TranslateService } from '@ngx-translate/core';
import escape from 'lodash/escape';
import { take } from 'rxjs/operators';

import { isEmpty } from '../../../../../empty.util';
Expand Down Expand Up @@ -26,7 +27,7 @@ export class DsDynamicAutocompleteService {
.pipe(take(1))
.subscribe( pn => { projectName = pn; });

return (fundingCode + ': ').bold() + fundingProjectCode + '<br>' + (projectName + ': ').bold() + fundingName;
return (fundingCode + ': ').bold() + escape(fundingProjectCode) + '<br>' + (projectName + ': ').bold() + escape(fundingName);
}

static pretifyLanguageSuggestion(suggestion, translateService: TranslateService) {
Expand All @@ -35,7 +36,7 @@ export class DsDynamicAutocompleteService {
// fetch language message
const languageMessage = translateService.instant('autocomplete.suggestion.language.title');

return (isoMessage + ': ').bold() + suggestion?.value + '<br>' + (languageMessage + ': ').bold() +
suggestion?.display;
return (isoMessage + ': ').bold() + escape(suggestion?.value) + '<br>' + (languageMessage + ': ').bold() +
escape(suggestion?.display);
}
}
13 changes: 13 additions & 0 deletions src/app/shared/utils/emphasize.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { EmphasizePipe } from './emphasize.pipe';

describe('EmphasizePipe', () => {
const pipe = new EmphasizePipe();

it('should wrap the matches in <em> tags', () => {
expect(pipe.transform('Smith, Sam', 's')).toEqual('<em>S</em>mith, <em>S</em>am');
});

it('should escape markup in the value', () => {
expect(pipe.transform('Smith & Co <img src="x">', 'smi')).toEqual('<em>Smi</em>th &amp; Co &lt;img src=&quot;x&quot;&gt;');
});
});
9 changes: 6 additions & 3 deletions src/app/shared/utils/emphasize.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Pipe,
PipeTransform,
} from '@angular/core';
import escape from 'lodash/escape';

@Pipe({
name: 'dsEmphasize',
Expand Down Expand Up @@ -45,9 +46,11 @@ export class EmphasizePipe implements PipeTransform {
* @returns {any} Transformed haystack with the needle emphasized
*/
transform(haystack, needle): any {
const escaped = this.escapeRegExp(needle);
const reg = new RegExp(escaped, 'gi');
return haystack.replace(reg, '<em>$&</em>');
const reg = new RegExp('(' + this.escapeRegExp(needle) + ')', 'gi');
// The matches are the odd parts. Everything is escaped, so the <em> tags are the only HTML.
return haystack.split(reg)
.map((part, i) => i % 2 === 0 ? escape(part) : '<em>' + escape(part) + '</em>')
.join('');
}

/**
Expand Down
Loading