Skip to content

Commit 6dbc6c4

Browse files
committed
fix(sfc): fix scoped style regression for child component with single root + comment
fix #2046
1 parent 5f40539 commit 6dbc6c4

File tree

2 files changed

+26
-34
lines changed

2 files changed

+26
-34
lines changed

packages/runtime-core/src/componentRenderUtils.ts

+19-9
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ export function renderComponentRoot(
214214

215215
/**
216216
* dev only
217+
* In dev mode, template root level comments are rendered, which turns the
218+
* template into a fragment root, but we need to locate the single element
219+
* root for attrs and scope id processing.
217220
*/
218221
const getChildRoot = (
219222
vnode: VNode
@@ -223,17 +226,10 @@ const getChildRoot = (
223226
}
224227
const rawChildren = vnode.children as VNodeArrayChildren
225228
const dynamicChildren = vnode.dynamicChildren as VNodeArrayChildren
226-
const children = rawChildren.filter(child => {
227-
return !(
228-
isVNode(child) &&
229-
child.type === Comment &&
230-
child.children !== 'v-if'
231-
)
232-
})
233-
if (children.length !== 1) {
229+
const childRoot = filterSingleRoot(rawChildren)
230+
if (!childRoot) {
234231
return [vnode, undefined]
235232
}
236-
const childRoot = children[0]
237233
const index = rawChildren.indexOf(childRoot)
238234
const dynamicIndex = dynamicChildren ? dynamicChildren.indexOf(childRoot) : -1
239235
const setRoot = (updatedRoot: VNode) => {
@@ -247,6 +243,20 @@ const getChildRoot = (
247243
return [normalizeVNode(childRoot), setRoot]
248244
}
249245

246+
/**
247+
* dev only
248+
*/
249+
export function filterSingleRoot(children: VNodeArrayChildren): VNode | null {
250+
const filtered = children.filter(child => {
251+
return !(
252+
isVNode(child) &&
253+
child.type === Comment &&
254+
child.children !== 'v-if'
255+
)
256+
})
257+
return filtered.length === 1 && isVNode(filtered[0]) ? filtered[0] : null
258+
}
259+
250260
const getFunctionalFallthrough = (attrs: Data): Data | undefined => {
251261
let res: Data | undefined
252262
for (const key in attrs) {

packages/runtime-core/src/renderer.ts

+7-25
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
setupComponent
2020
} from './component'
2121
import {
22+
filterSingleRoot,
2223
renderComponentRoot,
2324
shouldUpdateComponent,
2425
updateHOCHostEl
@@ -746,30 +747,6 @@ function baseCreateRenderer(
746747
}
747748
// scopeId
748749
setScopeId(el, scopeId, vnode, parentComponent)
749-
// if (scopeId) {
750-
// hostSetScopeId(el, scopeId)
751-
// }
752-
// if (parentComponent) {
753-
// const treeOwnerId = parentComponent.type.__scopeId
754-
// // vnode's own scopeId and the current patched component's scopeId is
755-
// // different - this is a slot content node.
756-
// if (treeOwnerId && treeOwnerId !== scopeId) {
757-
// hostSetScopeId(el, treeOwnerId + '-s')
758-
// }
759-
// const parentScopeId =
760-
// vnode === parentComponent.subTree && parentComponent.vnode.scopeId
761-
// if (parentScopeId) {
762-
// hostSetScopeId(el, parentScopeId)
763-
// if (parentComponent.parent) {
764-
// const treeOwnerId = parentComponent.parent.type.__scopeId
765-
// // vnode's own scopeId and the current patched component's scopeId is
766-
// // different - this is a slot content node.
767-
// if (treeOwnerId && treeOwnerId !== parentScopeId) {
768-
// hostSetScopeId(el, treeOwnerId + '-s')
769-
// }
770-
// }
771-
// }
772-
// }
773750
}
774751
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
775752
Object.defineProperty(el, '__vnode', {
@@ -823,7 +800,12 @@ function baseCreateRenderer(
823800
if (treeOwnerId && treeOwnerId !== scopeId) {
824801
hostSetScopeId(el, treeOwnerId + '-s')
825802
}
826-
if (vnode === parentComponent.subTree) {
803+
let subTree = parentComponent.subTree
804+
if (__DEV__ && subTree.type === Fragment) {
805+
subTree =
806+
filterSingleRoot(subTree.children as VNodeArrayChildren) || subTree
807+
}
808+
if (vnode === subTree) {
827809
setScopeId(
828810
el,
829811
parentComponent.vnode.scopeId,

0 commit comments

Comments
 (0)