Attribute Inheritance broken when comment in template root
Vue version
3.3.8
Link to minimal reproduction
Steps to reproduce
Create a Component with a comment in template root
<template>
<!-- comment -->
<div>
Title
</div>
</template>
Use the component.
<script setup>
import { ref } from 'vue'
import Comp from './Comp.vue';
const msg = ref('Hello World!')
</script>
<template>
<h1>{{ msg }}</h1>
<input v-model="msg">
<Comp style="color: blue;" />
</template>
What is expected?
What is actually happening?
System Info
No response
Any additional comments?
No response
class、style、inheritAttrs are only effective when there is a single root element in the template.
https://vuejs.org/guide/components/attrs.html#attribute-inheritance
Comments should be ignored in that consideration, though. They are in dev, which is making this a bug.
I think this is a playground bug. Vue's compiler will delete the comment node in the production mode build, but in the playground, it is a fragment.
The behavior is inconsistent between the development and production env, and the reason is that commented-out nodes have not removed in the production env.
Also encountering this issue in Vue3 when migrating a Vue2 app which uses conditional (singular) roots, where there are template comments above each root option. The comments seem to be interpreted as fragments, and thus throwing the following warning about the class
attribute being passed in. Attributes do not then get inherited to the current conditional root.
[Vue warn]: Extraneous non-props attributes (class) were passed to component but could not be automatically inherited because component renders fragment or text root nodes.
Without removing root-level template comments, or adding v-bind="$attrs"
on every root element, this is causing inconsistent behaviour between local/dev/prod builds.
Here is a reproduction to the issue:
If you toggle between prod/dev modes and recent Vue versions v3.4 and v3.3, you will observe inconsistencies with how the class
attribute is applied to the template with root-level comments.
Whilst I see there is a potential fix associated with the SFC playground (#9594), in practice I am seeing this issue on locally served dev builds (through vite) using v3.3.x. I would not expect the behaviour of attribute inheritance to differ between environments based on comments in the template - they should be ignored and not interpreted as fragments.
Edit: I note that for local dev builds using later revisions of v3.4 this seems to be now fixed, but this open issue is not linked to the specific change in the core that fixed it, so the behaviour might have been fixed by coincidence.
Edit 2: I've identified this behaviour was specifically fixed in v3.4.11, so can be considered a duplicate of https://github.com/vuejs/core/issues/5203, anybody else experiencing this should upgrade to v3.4 (v.3.4.15+) now v3.4 appears more stable.