Description
What problem does this feature solve?
Currently we have an boolean option for vue-template-compiler
: preserveWhitespace
. It either removes all whitespace-only text nodes, or leave them untouched. There were already earlier questions about either behavior: #6200, #7701, #9021, #9127. I think the current behavior is kind of oversimplified to cover actual usage.
When we write a template we tend to leverage line breaks and indents to make it more readable, like:
<div class="item">
<div class="aside">Aside</div>
<div class="main">Main</div>
</div>
And if we choose to layout this component with inline formatting context, sometimes we may not want to precisely control the margin between the inner parts with CSS, instead of using the size of a whitespace (which is related to those font-*
styles). For similar cases we don't want these whitespace-only text nodes. This leads to preserveWhitespace: false
(and it even became the default behavior for Vue CLI 3: vuejs/vue-cli@1864cef).
But when we craft some document/article-like content, this behavior becomes annoying. With preserveWhitespace: false
, the following template:
<p>
Welcome to <b>Vue.js</b> <i>world</i>.
Have fun!
</p>
Will generate:
<p>
Welcome to <b>Vue.js</b><i>world</i>.
Have fun!
</p>
Which looks like:
Welcome to Vue.jsworld. Have fun!
And this is clearly not desired.
What does the proposed API look like?
In short, I suggest we offer a new compiler option, to apply the strategy React uses to handles whitespaces for JSX (source):
JSX removes whitespace at the beginning and ending of a line. It also removes blank lines. New lines adjacent to tags are removed; new lines that occur in the middle of string literals are condensed into a single space.
For examples:
<p>
Welcome to <b>Vue.js</b> <i>world</i>.
Have fun!
</p>
The whitespaces between <p>
and Welcome
are removed but the one between </b>
and <i>
and the one between .
and Have
are preserved thus giving us:
<p>Welcome to <b>Vue.js</b> <i>world</i>. Have fun!</p>
This seem to be much more reasonable IMO. And in this mode users will have more flexibility to better serve their different purposes.
In general, the proposal is:
- Keep
preserveWhitespace
but mark it as deprecated. - Offer a new option to specify whether/how to remove whitespaces:
removeWhitespace: 'with-line-break' | 'any' | 'none'
. - Ignore
preserveWhitespace
ifremoveWhitespace
is specified.
(Still need more suggestions on specific API.)