-
-
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
Extra Blank Spaces Between Elements In Rendered Template #7701
Comments
Currently there is an option called |
The same thing happens with regular html and empty lines, the css does not apply: <ul>
</ul> If you want to have no spaces you can, however, remove any white spaces in the template: <ul><li v-for="item in items">
<span>{{ item }}</span>
</li><li v-if="false"></li><li v-if="false"></li></ul> Not closing because I'm not sure about the new line being replaced by an empty space but to me, this behaviour is more consistent than automatically removing whitespaces as using newlines in regular HTML would yield the same result |
That's not really a practical solution, though. Putting everything on one line turns it into an unreadable mess for anything more than the most trivial of templates.
But Vue does collapse everything inside the @Justineo, using |
Like Evan said here (except that this might not be solved by CSS), I think it's better that we keep it consistent across the whole project. Custom directives may work if you just remove whitespace-only text nodes instead of setting |
That doesn't really work for reusable components, a library can't (or at least shouldn't) force users to change project-wide settings. It's also not applicable if they're not using
You're right, that seemed to make it work nicely. Example directive below for anyone who comes across this looking for a solution: function trimEmptyTextNodes (el) {
for (let node of el.childNodes) {
if (node.nodeType === Node.TEXT_NODE && node.data.trim() === '') {
node.remove()
}
}
}
Vue.directive('trim-whitespace', {
inserted: trimEmptyTextNodes,
componentUpdated: trimEmptyTextNodes
}) So the open question remains regarding Vue chomping the newlines into a single space. Is there a rationale for this behavior? If it chomped the newlines into nothing, rather than a single space, this would work out of the box. |
Collapsing new lines and white spaces is consistent with the behavior of HTML. |
HTML renders any consecutive whitespaces (including newlines and everything) as a single space. So chomping consecutive whitespaces into a single space results in the exact same render output while reducing the payload. This is intended and is in fact consistent with what you'd expect when styling plain HTML with CSS. |
@yyx990803 Unless you use |
I agree that a way to disable (on demand like a directive or something else) newline to space transform may be realy usefull. The template code would still look nice and indented but would fix a major issue when working with inline element in css. |
Version
2.5.13
Reproduction link
https://jsfiddle.net/aoxo2rod/8/
Steps to reproduce
Run the JSFiddle
What is expected?
The background should be green, styled by
ul:empty
, as the children oful
are all conditionally not rendered: emptyv-for
andv-if
that evaluate to false.What is actually happening?
The background is red,
ul:empty
is not used because there is a blank space inside theul
tags, which CSS doesn't consider to be:empty
.The multiple children elements are important, a single
li
element withv-if="false"
will produce the correct result. It appears Vue is putting a blank space between the elements, even if they are conditionally not rendered (appearing as<!---->
instead). It's worth noting that if you manually remove the extra spaces using developer tools, everything looks as expected,<!---->
does not affect:empty
.Furthermore, this only reproduces if there is a newline between the elements. Modifying the provided JSFiddle to only have the two
li
withv-if="false"
will produce the correct result if there's no newline between them. So Vue seems to be turning the newline between the elements into a single space, which is causing the issue.The text was updated successfully, but these errors were encountered: