Subscribe on changes!

v-model.trim not removing whitespaces

avatar
Mar 30th 2022

Version

3.2.31

Reproduction link

sfc.vuejs.org/

Steps to reproduce

  • See the SFC Playground link
  • When using v-model.trim on an input, the whitespaces in the message are not removed
  • (Additional info: More the one whitespaces are removed from the template, but this also happens if I'm not using .trim at all)

What is expected?

Input: Hello how are you? Trimmed message: Hellohowareyou?

What is actually happening?

Input: Hello how are you? Trimmed message: Hello how are you?

avatar
Mar 30th 2022

Triming means "remove leading and trailing white spaces". It does not mean "remove any whitespace found in this string".

avatar
Mar 30th 2022

@LinusBorg Thank you for the quick clarification.

Still, in the SFC Playground I posted above, any leading whitespaces I put into the input are trimmed in the div, no matter if I use v-model.trim or not. That's what struck me as odd because it doesn't seem to make a difference.

avatar
Mar 30th 2022

It's because consecutive whitespaces are collapsed by default in HTML. If you replace the <h1> element with <pre> you'll see Vue's current behavior is correct.

avatar
Mar 30th 2022

@Justineo I see! So it actually was the way HTML displays several whitespaces that tricked me into believing that it was a bug in the trim-modifier. My mistake! Thank you :)