fix(compiler-core): fix whitespace management for slots with whitespace: 'preserve' (#3767)

fix #3766
This commit is contained in:
HcySunYang
2021-05-14 06:24:43 +08:00
committed by GitHub
parent f3d30363ec
commit 47da92146c
3 changed files with 126 additions and 3 deletions

View File

@@ -311,7 +311,13 @@ export function buildSlots(
if (!hasTemplateSlots) {
// implicit default slot (on component)
slotsProperties.push(buildDefaultSlotProperty(undefined, children))
} else if (implicitDefaultChildren.length) {
} else if (
implicitDefaultChildren.length &&
// #3766
// with whitespace: 'preserve', whitespaces between slots will end up in
// implicitDefaultChildren. Ignore if all implicit children are whitespaces.
implicitDefaultChildren.some(node => isNonWhitespaceContent(node))
) {
// implicit default slot (mixed with named slots)
if (hasNamedDefaultSlot) {
context.onError(
@@ -397,3 +403,11 @@ function hasForwardedSlots(children: TemplateChildNode[]): boolean {
}
return false
}
function isNonWhitespaceContent(node: TemplateChildNode): boolean {
if (node.type !== NodeTypes.TEXT && node.type !== NodeTypes.TEXT_CALL)
return true
return node.type === NodeTypes.TEXT
? !!node.content.trim()
: isNonWhitespaceContent(node.content)
}