rdt-client/client/src/app/nl2br.pipe.ts
2025-02-28 12:28:21 +00:00

28 lines
805 B
TypeScript

import { Pipe, PipeTransform, SecurityContext, VERSION } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({
name: 'nl2br',
standalone: false
})
export class Nl2BrPipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {}
transform(value: string, sanitizeBeforehand?: boolean): string {
if (typeof value !== 'string') {
return value;
}
let result: any;
const textParsed = value.replace(/(?:\r\n|\r|\n)/g, '<br />');
if (!VERSION || VERSION.major === '2') {
result = this.sanitizer.bypassSecurityTrustHtml(textParsed);
} else if (sanitizeBeforehand) {
result = this.sanitizer.sanitize(SecurityContext.HTML, textParsed);
} else {
result = textParsed;
}
return result;
}
}