Skip to content

Commit 4783118

Browse files
authored
fix(compiler-ssr): handle ssr attr fallthrough when preserve whitespace (#12304)
close #8072
1 parent 6611dda commit 4783118

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

packages/compiler-ssr/__tests__/ssrFallthroughAttrs.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,31 @@ describe('ssr: attrs fallthrough', () => {
4848
`)
4949
})
5050

51+
//#8072
52+
test(`fallthrough component content (with whitespace: 'preserve')`, () => {
53+
expect(
54+
compile(
55+
`
56+
<a v-if="to">Foo</a>
57+
<a v-else>Bar</a>
58+
`,
59+
{
60+
whitespace: 'preserve',
61+
},
62+
).code,
63+
).toMatchInlineSnapshot(`
64+
"const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer")
65+
66+
return function ssrRender(_ctx, _push, _parent, _attrs) {
67+
if (_ctx.to) {
68+
_push(\`<a\${_ssrRenderAttrs(_attrs)}>Foo</a>\`)
69+
} else {
70+
_push(\`<a\${_ssrRenderAttrs(_attrs)}>Bar</a>\`)
71+
}
72+
}"
73+
`)
74+
})
75+
5176
test('should not inject to fallthrough component content if not root', () => {
5277
expect(compile(`<div/><transition><div/></transition>`).code)
5378
.toMatchInlineSnapshot(`

packages/compiler-ssr/src/transforms/ssrInjectFallthroughAttrs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import {
77
type TemplateChildNode,
88
createSimpleExpression,
99
findDir,
10+
isCommentOrWhitespace,
1011
locStub,
1112
} from '@vue/compiler-dom'
1213

1314
const filterChild = (node: ParentNode) =>
14-
node.children.filter(n => n.type !== NodeTypes.COMMENT)
15+
node.children.filter(n => !isCommentOrWhitespace(n))
1516

1617
const hasSingleChild = (node: ParentNode): boolean =>
1718
filterChild(node).length === 1

0 commit comments

Comments
 (0)