-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle whitespaces in templates more wisely #9208
Comments
A summary of what is landed in e1abedb:
In <p>
Welcome to <b>Vue.js</b> <i>world</i>.
Have fun!
</p> will be compiled as: <p> Welcome to <b>Vue.js</b> <i>world</i>. Have fun! </p> Essentially, this provides:
The reason for not entirely removing leading/ending whitespaces inside an element:
<span>foo</span>
<span>
bar
</span> Will render as We will likely use this new condense behavior as the default in 3.0. |
I think this new option should also be added to vue-loader. |
@a1p4ca vue-loader has compilerOptions which just passes through to the template compiler. |
Hey, sorry for bumping a closed topic, but is this meant to also condense whitespace from html entities like
Resulting in the buttons rendering like: |
BREAKING CHANGE: Detailed explanation: vuejs/vue#9208 (comment) Take the following template as example: ``` <p> Welcome to <b>Vue.js</b> <i>world</i>. Have fun! </p> ``` With `preserveWhitespace: false`, it was compiled as: ``` <p> Welcome to <b>Vue.js</b><i>world</i>. Have fun! </p> ``` With `whitespace: 'condense'`, it is now compiled as: ``` <p> Welcome to <b>Vue.js</b> <i>world</i>. Have fun! </p> ``` Note the **inline whitespace between tags** is preserved.
BREAKING CHANGE: Detailed explanation: vuejs/vue#9208 (comment) Take the following template as example: ``` <p> Welcome to <b>Vue.js</b> <i>world</i>. Have fun! </p> ``` With `preserveWhitespace: false`, it was compiled as: ``` <p> Welcome to <b>Vue.js</b><i>world</i>. Have fun! </p> ``` With `whitespace: 'condense'`, it is now compiled as: ``` <p> Welcome to <b>Vue.js</b> <i>world</i>. Have fun! </p> ``` Note the **inline whitespace between tags** is preserved. Closes #1020
BREAKING CHANGE: Detailed explanation: vuejs/vue#9208 (comment) Take the following template as example: ``` <p> Welcome to <b>Vue.js</b> <i>world</i>. Have fun! </p> ``` With `preserveWhitespace: false`, it was compiled as: ``` <p> Welcome to <b>Vue.js</b><i>world</i>. Have fun! </p> ``` With `whitespace: 'condense'`, it is now compiled as: ``` <p> Welcome to <b>Vue.js</b> <i>world</i>. Have fun! </p> ``` Note the **inline whitespace between tags** is preserved. Closes #1020
sets the compiler `whitespace` option to "condense" vuejs/vue#9208 (comment)
BREAKING CHANGE: Detailed explanation: vuejs/vue#9208 (comment) Take the following template as example: ``` <p> Welcome to <b>Vue.js</b> <i>world</i>. Have fun! </p> ``` With `preserveWhitespace: false`, it was compiled as: ``` <p> Welcome to <b>Vue.js</b><i>world</i>. Have fun! </p> ``` With `whitespace: 'condense'`, it is now compiled as: ``` <p> Welcome to <b>Vue.js</b> <i>world</i>. Have fun! </p> ``` Note the **inline whitespace between tags** is preserved. Closes #1020
BREAKING CHANGE: Detailed explanation: vuejs/vue#9208 (comment) Take the following template as example: ``` <p> Welcome to <b>Vue.js</b> <i>world</i>. Have fun! </p> ``` With `preserveWhitespace: false`, it was compiled as: ``` <p> Welcome to <b>Vue.js</b><i>world</i>. Have fun! </p> ``` With `whitespace: 'condense'`, it is now compiled as: ``` <p> Welcome to <b>Vue.js</b> <i>world</i>. Have fun! </p> ``` Note the **inline whitespace between tags** is preserved. Closes #1020
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:
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 topreserveWhitespace: 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:Will generate:
Which looks like:
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):
For examples:
The whitespaces between
<p>
andWelcome
are removed but the one between</b>
and<i>
and the one between.
andHave
are preserved thus giving us: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:
preserveWhitespace
but mark it as deprecated.removeWhitespace: 'with-line-break' | 'any' | 'none'
.preserveWhitespace
ifremoveWhitespace
is specified.(Still need more suggestions on specific API.)
The text was updated successfully, but these errors were encountered: