Skip to content

Commit dbad9bf

Browse files
author
Gabriele Panico
committed
refactor: enhance HTML entities encoding by normalizing line breaks and handling null/undefined inputs
1 parent 32dc89b commit dbad9bf

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Pipe, PipeTransform } from '@angular/core';
2-
import { htmlEntities, replaceEndOfLine } from 'src/chat21-core/utils/utils';
2+
import { htmlEntities } from 'src/chat21-core/utils/utils';
33

44
@Pipe({
55
name: 'htmlEntiesEncode'
@@ -8,10 +8,25 @@ import { htmlEntities, replaceEndOfLine } from 'src/chat21-core/utils/utils';
88
export class HtmlEntitiesEncodePipe implements PipeTransform {
99

1010
transform(text: any, args?: any): any {
11-
text = htmlEntities(text);
12-
text = replaceEndOfLine(text);
13-
text = text.trim();
14-
return text;
11+
if (text === null || text === undefined) {
12+
return text;
13+
}
14+
15+
// Normalize line breaks BEFORE encoding HTML:
16+
// - real CRLF/CR/LF to LF
17+
// - escaped sequences (\n, \r, \r\n) to LF
18+
// - HTML <br> tags (if present) to LF
19+
let normalized = String(text)
20+
.replace(/\r\n/g, '\n')
21+
.replace(/\r/g, '\n')
22+
.replace(/\\r\\n/g, '\n')
23+
.replace(/\\n/g, '\n')
24+
.replace(/\\r/g, '\n')
25+
.replace(/<br\s*\/?>/gi, '\n');
26+
27+
normalized = htmlEntities(normalized);
28+
normalized = normalized.trim();
29+
return normalized;
1530
}
1631

1732
}

0 commit comments

Comments
 (0)