Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add scopeId to components in component #756

Merged
merged 4 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion packages/runtime-core/__tests__/helpers/scopeId.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ describe('scopeId runtime support', () => {
expect(serializeInner(root)).toBe(`<div parent><div parent></div></div>`)
})

test('should attach scopeId to components in parent component', () => {
const Child = {
__scopeId: 'child',
render: withChildId(() => {
return h('div')
})
}
const App = {
__scopeId: 'parent',
render: withParentId(() => {
return h('div', [h(Child)])
})
}

const root = nodeOps.createElement('div')
render(h(App), root)
expect(serializeInner(root)).toBe(
`<div parent><div parent child></div></div>`
)
})

test('should work on slots', () => {
const Child = {
__scopeId: 'child',
Expand All @@ -41,7 +62,7 @@ describe('scopeId runtime support', () => {
// - scopeId from parent
// - slotted scopeId (with `-s` postfix) from child (the tree owner)
expect(serializeInner(root)).toBe(
`<div child><div parent child-s></div></div>`
`<div parent child><div parent child-s></div></div>`
)
})
})
7 changes: 7 additions & 0 deletions packages/runtime-core/src/componentRenderUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function renderComponentRoot(
): VNode {
const {
type: Component,
parent,
vnode,
proxy,
withProxy,
Expand Down Expand Up @@ -102,6 +103,11 @@ export function renderComponentRoot(
if (vnodeHooks !== EMPTY_OBJ) {
result = cloneVNode(result, vnodeHooks)
}
// inherit scopeId
const parentScopeId = parent && parent.type.__scopeId
if (parentScopeId) {
result = cloneVNode(result, { [parentScopeId]: '' })
}
// inherit directives
if (vnode.dirs != null) {
if (__DEV__ && !isElementRoot(result)) {
Expand All @@ -127,6 +133,7 @@ export function renderComponentRoot(
result = createVNode(Comment)
}
currentRenderingInstance = null

return result
}

Expand Down
2 changes: 1 addition & 1 deletion packages/server-renderer/__tests__/renderToString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ describe('ssr: renderToString', () => {
}

expect(await renderToString(h(Parent))).toBe(
`<div data-v-child><span data-v-test data-v-child-s>slot</span></div>`
`<div data-v-test data-v-child><span data-v-test data-v-child-s>slot</span></div>`
)
})
})
Expand Down
8 changes: 8 additions & 0 deletions packages/server-renderer/__tests__/ssrRenderAttrs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ describe('ssr: renderAttrs', () => {
).toBe(` id="foo" title="bar"`)
})

test('empty value attrs', () => {
expect(
ssrRenderAttrs({
'data-v-abc': ''
})
).toBe(` data-v-abc`)
})

test('escape attrs', () => {
expect(
ssrRenderAttrs({
Expand Down
4 changes: 3 additions & 1 deletion packages/server-renderer/src/helpers/ssrRenderAttrs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export function ssrRenderDynamicAttr(
if (isBooleanAttr(attrKey)) {
return value === false ? `` : ` ${attrKey}`
} else if (isSSRSafeAttrName(attrKey)) {
return ` ${attrKey}="${escapeHtml(value)}"`
return value === ''
? ` ${attrKey}`
: ` ${attrKey}="${escapeHtml(value)}"`
} else {
console.warn(
`[@vue/server-renderer] Skipped rendering unsafe attribute name: ${attrKey}`
Expand Down