From 1d7c440b739529e1b0d0d48b10da3860a5dd8c4e Mon Sep 17 00:00:00 2001 From: daiwei Date: Fri, 1 Nov 2024 11:09:43 +0800 Subject: [PATCH] fix(compiler-ssr): handle ssr attr fallthrough when preserve whitespace --- .../__tests__/ssrFallthroughAttrs.spec.ts | 25 +++++++++++++++++++ .../transforms/ssrInjectFallthroughAttrs.ts | 4 ++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/compiler-ssr/__tests__/ssrFallthroughAttrs.spec.ts b/packages/compiler-ssr/__tests__/ssrFallthroughAttrs.spec.ts index 7b3d1962c3..3adfe6480d 100644 --- a/packages/compiler-ssr/__tests__/ssrFallthroughAttrs.spec.ts +++ b/packages/compiler-ssr/__tests__/ssrFallthroughAttrs.spec.ts @@ -48,6 +48,31 @@ describe('ssr: attrs fallthrough', () => { `) }) + //#8072 + test(`fallthrough component content (with whitespace: 'preserve')`, () => { + expect( + compile( + ` + Foo + Bar + `, + { + whitespace: 'preserve', + }, + ).code, + ).toMatchInlineSnapshot(` + "const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + if (_ctx.to) { + _push(\`Foo\`) + } else { + _push(\`Bar\`) + } + }" + `) + }) + test('should not inject to fallthrough component content if not root', () => { expect(compile(`
`).code) .toMatchInlineSnapshot(` diff --git a/packages/compiler-ssr/src/transforms/ssrInjectFallthroughAttrs.ts b/packages/compiler-ssr/src/transforms/ssrInjectFallthroughAttrs.ts index b1aac0d74c..045783aa63 100644 --- a/packages/compiler-ssr/src/transforms/ssrInjectFallthroughAttrs.ts +++ b/packages/compiler-ssr/src/transforms/ssrInjectFallthroughAttrs.ts @@ -11,7 +11,9 @@ import { } from '@vue/compiler-dom' const filterChild = (node: ParentNode) => - node.children.filter(n => n.type !== NodeTypes.COMMENT) + node.children.filter( + n => n.type !== NodeTypes.COMMENT && n.type !== NodeTypes.TEXT, + ) const hasSingleChild = (node: ParentNode): boolean => filterChild(node).length === 1 -- 2.47.2