Skip to content

Commit 9547c2b

Browse files
authored
fix(sfc): inherit parent scopeId on child rooot (#756)
1 parent 1b9b235 commit 9547c2b

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

packages/runtime-core/__tests__/helpers/scopeId.spec.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@ describe('scopeId runtime support', () => {
1717
expect(serializeInner(root)).toBe(`<div parent><div parent></div></div>`)
1818
})
1919

20+
test('should attach scopeId to components in parent component', () => {
21+
const Child = {
22+
__scopeId: 'child',
23+
render: withChildId(() => {
24+
return h('div')
25+
})
26+
}
27+
const App = {
28+
__scopeId: 'parent',
29+
render: withParentId(() => {
30+
return h('div', [h(Child)])
31+
})
32+
}
33+
34+
const root = nodeOps.createElement('div')
35+
render(h(App), root)
36+
expect(serializeInner(root)).toBe(
37+
`<div parent><div parent child></div></div>`
38+
)
39+
})
40+
2041
test('should work on slots', () => {
2142
const Child = {
2243
__scopeId: 'child',
@@ -41,7 +62,7 @@ describe('scopeId runtime support', () => {
4162
// - scopeId from parent
4263
// - slotted scopeId (with `-s` postfix) from child (the tree owner)
4364
expect(serializeInner(root)).toBe(
44-
`<div child><div parent child-s></div></div>`
65+
`<div parent child><div parent child-s></div></div>`
4566
)
4667
})
4768
})

packages/runtime-core/src/componentRenderUtils.ts

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export function renderComponentRoot(
3939
): VNode {
4040
const {
4141
type: Component,
42+
parent,
4243
vnode,
4344
proxy,
4445
withProxy,
@@ -102,6 +103,11 @@ export function renderComponentRoot(
102103
if (vnodeHooks !== EMPTY_OBJ) {
103104
result = cloneVNode(result, vnodeHooks)
104105
}
106+
// inherit scopeId
107+
const parentScopeId = parent && parent.type.__scopeId
108+
if (parentScopeId) {
109+
result = cloneVNode(result, { [parentScopeId]: '' })
110+
}
105111
// inherit directives
106112
if (vnode.dirs != null) {
107113
if (__DEV__ && !isElementRoot(result)) {
@@ -127,6 +133,7 @@ export function renderComponentRoot(
127133
result = createVNode(Comment)
128134
}
129135
currentRenderingInstance = null
136+
130137
return result
131138
}
132139

packages/server-renderer/__tests__/renderToString.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ describe('ssr: renderToString', () => {
562562
}
563563

564564
expect(await renderToString(h(Parent))).toBe(
565-
`<div data-v-child><span data-v-test data-v-child-s>slot</span></div>`
565+
`<div data-v-test data-v-child><span data-v-test data-v-child-s>slot</span></div>`
566566
)
567567
})
568568
})

packages/server-renderer/__tests__/ssrRenderAttrs.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ describe('ssr: renderAttrs', () => {
2626
).toBe(` id="foo" title="bar"`)
2727
})
2828

29+
test('empty value attrs', () => {
30+
expect(
31+
ssrRenderAttrs({
32+
'data-v-abc': ''
33+
})
34+
).toBe(` data-v-abc`)
35+
})
36+
2937
test('escape attrs', () => {
3038
expect(
3139
ssrRenderAttrs({

packages/server-renderer/src/helpers/ssrRenderAttrs.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ export function ssrRenderDynamicAttr(
5353
if (isBooleanAttr(attrKey)) {
5454
return value === false ? `` : ` ${attrKey}`
5555
} else if (isSSRSafeAttrName(attrKey)) {
56-
return ` ${attrKey}="${escapeHtml(value)}"`
56+
return value === ''
57+
? ` ${attrKey}`
58+
: ` ${attrKey}="${escapeHtml(value)}"`
5759
} else {
5860
console.warn(
5961
`[@vue/server-renderer] Skipped rendering unsafe attribute name: ${attrKey}`

0 commit comments

Comments
 (0)