Skip to content

Commit 13613d4

Browse files
pkozlowski-opensourcevicb
authored andcommitted
fix(compiler): skip   when trimming / removing whitespaces (#19310)
Fixes #19304
1 parent 9ad4b3b commit 13613d4

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

packages/compiler/src/ml_parser/html_whitespaces.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export const PRESERVE_WS_ATTR_NAME = 'ngPreserveWhitespaces';
1414

1515
const SKIP_WS_TRIM_TAGS = new Set(['pre', 'template', 'textarea', 'script', 'style']);
1616

17+
// Equivalent to \s with \u00a0 (non-breaking space) excluded.
18+
// Based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
19+
const WS_CHARS = ' \f\n\r\t\v\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff';
20+
const NO_WS_REGEXP = new RegExp(`[^${WS_CHARS}]`);
21+
const WS_REPLACE_REGEXP = new RegExp(`[${WS_CHARS}]{2,}`, 'g');
22+
1723
function hasPreserveWhitespacesAttr(attrs: html.Attribute[]): boolean {
1824
return attrs.some((attr: html.Attribute) => attr.name === PRESERVE_WS_ATTR_NAME);
1925
}
@@ -63,10 +69,11 @@ class WhitespaceVisitor implements html.Visitor {
6369
}
6470

6571
visitText(text: html.Text, context: any): any {
66-
const isBlank = text.value.trim().length === 0;
72+
const isNotBlank = text.value.match(NO_WS_REGEXP);
6773

68-
if (!isBlank) {
69-
return new html.Text(replaceNgsp(text.value).replace(/\s\s+/g, ' '), text.sourceSpan);
74+
if (isNotBlank) {
75+
return new html.Text(
76+
replaceNgsp(text.value).replace(WS_REPLACE_REGEXP, ' '), text.sourceSpan);
7077
}
7178

7279
return null;

packages/compiler/test/ml_parser/html_whitespaces_spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ export function main() {
6464
expect(parseAndRemoveWS(' \n foo \t ')).toEqual([[html.Text, ' foo ', 0]]);
6565
});
6666

67+
it('should not replace  ', () => {
68+
expect(parseAndRemoveWS(' ')).toEqual([[html.Text, '\u00a0', 0]]);
69+
});
70+
71+
it('should not replace sequences of  ', () => {
72+
expect(parseAndRemoveWS('  foo  ')).toEqual([
73+
[html.Text, '\u00a0\u00a0foo\u00a0\u00a0', 0]
74+
]);
75+
});
76+
6777
it('should not replace single tab and newline with spaces', () => {
6878
expect(parseAndRemoveWS('\nfoo')).toEqual([[html.Text, '\nfoo', 0]]);
6979
expect(parseAndRemoveWS('\tfoo')).toEqual([[html.Text, '\tfoo', 0]]);

0 commit comments

Comments
 (0)