:class="undefined" causes class being rendered
Version
3.2.6
Steps to reproduce
<template>
<div :title="undefined" :class="undefined" :style="undefined">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolore molestiae quod voluptas
reprehenderit possimus nihil eligendi iure et esse aut, iste natus porro enim rerum cumque
veniam quae! Quam, voluptatum.
</div>
</template>
What is expected?
None of title
, class
, style
should be rendered.
What is actually happening?
class
is rendered, the same thing applies to null
.
Additional info
This was fixed in v3.1.5 but is reproducible when using the latest commit.
@HcySunYang
<template>
<div :style="false" :class="false" :foo="false">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laudantium modi
repudiandae, illum sequi nemo est deleniti optio distinctio quaerat quasi
dolores vitae aspernatur quisquam, voluptatibus quibusdam ullam, ipsum aut
amet?
</div>
</template>
In vue ^2.6.10
, the above code will render class
too, could you please make a PR regarding that?
Check out the new attributes coercion behavior https://github.com/vuejs/rfcs/blob/master/active-rfcs/0024-attribute-coercion-behavior.md
@HcySunYang Yes, I've known that, I guess this issue belongs to the vue 2 repo, I've created one there (https://github.com/vuejs/vue/issues/11902).
Also,
The documented behavior is class
wouldn't be rendered if it's null
or undefined
, so I think it is a bug.
I think this was for performance reasons. There are no practical reasons to not remove class
and it was highlighted in other issues in Vue 2 and dismissed already.
Definitely not a bug but worth checking if the perf implication still exists
Only call removeAttribute
when the element does not have any class. In other cases, className
is used. I think it's fine.
I think this should be reopend as the fix in 7013e8f does not work. Problem is the value
argument of patchClass
is never null
because all class values (including null
, undefined
etc) are converted into strings using normalizeClass by the compiler
@Liwoj Just tried, reproducible for me as well. Ping @yyx990803
I made a repro
the value is empty string not null
@edison1105 For :class="undefined"
and :class="null"
, @vue/runtime-dom
v3.1.5 removed it, whereas @vue/runtime-dom
v3.2.4 kept it, which seems like a regression bug.
@edison1105 For
:class="undefined"
and:class="null"
,@vue/runtime-dom
v3.1.5 removedclass
, whereas@vue/runtime-dom
v3.2.4 kept it, which seems like a regression bug.
Good catch. Would you mind commit a new issue with a working repro?
@edison1105 I've updated the issue to reflect that, since the playground doesn't seem to keep the vue version, I included the repro for the latest commit.
The issue is that normalizeClass
will always coerce the :class
value to a string.
and patchClass
will only remove the attribute when it's nullish:
The issue still exists, does anyone have any solution?
https://github.com/vuejs/rfcs/blob/master/active-rfcs/0024-attribute-coercion-behavior.md#drawbacks https://vuejs.org/guide/essentials/template-syntax.html#attribute-bindings
when I use null, empty class property remains in the tag.
Vue version: 3.3.4