Open JounQin opened 4 years ago
This is on purpose, actually. Whitespace is significant in HTML but collapsed to a single whitespace Because of this, the following is equivalent when rendered:
<strong>Bold</strong> text
<strong>Bold</strong> text
<strong>Bold</strong>
text
However, the following would not be:
<strong>Bold</strong>text
as it would join the two words together.
Yep, this is working as designed and we've got tests to prove it: https://github.com/angular/angular/blob/1197965e69d658c92422d49b1f2a1ba90498af67/packages/compiler/test/ml_parser/html_whitespaces_spec.ts#L62-L65
I can see how one would argue for the behaviour specified by @JounQin though.
At the same time @JoostK point is super-valid here (although we've got the described problem already with <span>two</span> <span>words</span>
Also this approach is pretty standard across the industry. For example paste the example given into http://minifycode.com/html-minifier or https://htmlcompressor.com/compressor/
@petebacondarwin How about html-minifier-terser https://danielruf.github.io/html-minifier-terser which is used by html-loader?
Two things to note with that tool:
As @pkozlowski-opensource mentioned, there is room for discussion here but I don't think this would be a high priority for us.
Just wrap the interpolation in ng-container
and it won't apply the white spaces around it.
<div>
<ng-container>{{ value }}</ng-container>
</div>
I would argue that this is more of a problem than stated. Consider multi-line text that's been wrapped. Since an unnecessary white space is added before the interpolated text, it appears that the text is indented by one space on the first line. It also creates issues around spacing because you have to account for an extra white space before and after the interpolated text. If I want a space, I'll add a space. Don't need it added automatically for me.
Just wrap the interpolation in
ng-container
and it won't apply the white spaces around it.<div> <ng-container>{{ value }}</ng-container> </div>
I would argue that this is more of a problem than stated. Consider multi-line text that's been wrapped. Since an unnecessary white space is added before the interpolated text, it appears that the text is indented by one space on the first line. It also creates issues around spacing because you have to account for an extra white space before and after the interpolated text. If I want a space, I'll add a space. Don't need it added automatically for me.
@dgardner-nv Angular does not introduce spaces into templates. It only removes non-significant whitespace. It may be that you're using a formatter that introduces whitespace, but if this affects the rendered output then that's a bug in your formatter.
To overcome this issue you can set htmlWhitespaceSensitivity
to "strict"
in Prettier config.
Then this:
<span>Very very very very very long text</span>
will be formatted into this:
<span
>Very very very very very long text</span
>
instead of this:
<span>
Very very very very very long text
</span>
π bug report
Affected Package
The issue is caused by package
@angular/compiler
Is this a regression?
Yes, the previous version in which this bug was not present was: .... No idea ### Description A clear and concise description of the problem... https://github.com/angular/angular/blob/d1ea1f4c7f3358b730b0d94e65b00bc28cae279c/packages/compiler/src/ml_parser/html_whitespaces.ts#L78 ```html{{ 'text' }}
{{ 'text' }}
{{ 'text' }}