From 4f8d8078221ee52deed266677a227ad2a6d8dd22 Mon Sep 17 00:00:00 2001 From: edison Date: Fri, 15 Nov 2024 22:32:39 +0800 Subject: [PATCH] fix(ssr): handle initial selected state for select with v-model + v-for option (#12399) close #12395 --- .../compiler-ssr/__tests__/ssrVModel.spec.ts | 46 +++++++++++++++++++ .../compiler-ssr/src/transforms/ssrVModel.ts | 18 ++++++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/packages/compiler-ssr/__tests__/ssrVModel.spec.ts b/packages/compiler-ssr/__tests__/ssrVModel.spec.ts index f2c04f0a5e8..0bf7673d00d 100644 --- a/packages/compiler-ssr/__tests__/ssrVModel.spec.ts +++ b/packages/compiler-ssr/__tests__/ssrVModel.spec.ts @@ -52,6 +52,52 @@ describe('ssr: v-model', () => { }" `) + expect( + compileWithWrapper( + ``, + ).code, + ).toMatchInlineSnapshot(` + "const { ssrRenderAttr: _ssrRenderAttr, ssrIncludeBooleanAttr: _ssrIncludeBooleanAttr, ssrLooseContain: _ssrLooseContain, ssrLooseEqual: _ssrLooseEqual, ssrRenderAttrs: _ssrRenderAttrs, ssrRenderList: _ssrRenderList } = require("vue/server-renderer") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + _push(\`\`) + }" + `) + + expect( + compileWithWrapper( + ``, + ).code, + ).toMatchInlineSnapshot(` + "const { ssrRenderAttr: _ssrRenderAttr, ssrIncludeBooleanAttr: _ssrIncludeBooleanAttr, ssrLooseContain: _ssrLooseContain, ssrLooseEqual: _ssrLooseEqual, ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + _push(\`\`) + }" + `) + expect( compileWithWrapper( ``, diff --git a/packages/compiler-ssr/src/transforms/ssrVModel.ts b/packages/compiler-ssr/src/transforms/ssrVModel.ts index 9a645397e1a..80e3839318b 100644 --- a/packages/compiler-ssr/src/transforms/ssrVModel.ts +++ b/packages/compiler-ssr/src/transforms/ssrVModel.ts @@ -5,6 +5,7 @@ import { type ExpressionNode, NodeTypes, type PlainElementNode, + type TemplateChildNode, createCallExpression, createConditionalExpression, createDOMCompilerError, @@ -162,11 +163,18 @@ export const ssrTransformModel: DirectiveTransform = (dir, node, context) => { checkDuplicatedValue() node.children = [createInterpolation(model, model.loc)] } else if (node.tag === 'select') { - node.children.forEach(child => { - if (child.type === NodeTypes.ELEMENT) { - processOption(child as PlainElementNode) - } - }) + const processChildren = (children: TemplateChildNode[]) => { + children.forEach(child => { + if (child.type === NodeTypes.ELEMENT) { + processOption(child as PlainElementNode) + } else if (child.type === NodeTypes.FOR) { + processChildren(child.children) + } else if (child.type === NodeTypes.IF) { + child.branches.forEach(b => processChildren(b.children)) + } + }) + } + processChildren(node.children) } else { context.onError( createDOMCompilerError(