Skip to content

Commit b1894b9

Browse files
committed
chore: add more cases
1 parent 2b4aa83 commit b1894b9

File tree

2 files changed

+84
-23
lines changed

2 files changed

+84
-23
lines changed

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,76 @@ describe('ssr: v-model', () => {
222222
_push(\`</optgroup></select></div>\`)
223223
}"
224224
`)
225+
226+
expect(
227+
compileWithWrapper(`
228+
<select multiple v-model="model">
229+
<optgroup>
230+
<template v-if="ok">
231+
<option v-for="item in items" :value="item">{{item}}</option>
232+
</template>
233+
</optgroup>
234+
</select>`).code,
235+
).toMatchInlineSnapshot(`
236+
"const { ssrRenderAttr: _ssrRenderAttr, ssrIncludeBooleanAttr: _ssrIncludeBooleanAttr, ssrLooseContain: _ssrLooseContain, ssrLooseEqual: _ssrLooseEqual, ssrRenderAttrs: _ssrRenderAttrs, ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require("vue/server-renderer")
237+
238+
return function ssrRender(_ctx, _push, _parent, _attrs) {
239+
_push(\`<div\${_ssrRenderAttrs(_attrs)}><select multiple><optgroup>\`)
240+
if (_ctx.ok) {
241+
_push(\`<!--[-->\`)
242+
_ssrRenderList(_ctx.items, (item) => {
243+
_push(\`<option\${
244+
_ssrRenderAttr("value", item)
245+
}\${
246+
(_ssrIncludeBooleanAttr((Array.isArray(_ctx.model))
247+
? _ssrLooseContain(_ctx.model, item)
248+
: _ssrLooseEqual(_ctx.model, item))) ? " selected" : ""
249+
}>\${
250+
_ssrInterpolate(item)
251+
}</option>\`)
252+
})
253+
_push(\`<!--]-->\`)
254+
} else {
255+
_push(\`<!---->\`)
256+
}
257+
_push(\`</optgroup></select></div>\`)
258+
}"
259+
`)
260+
261+
expect(
262+
compileWithWrapper(`
263+
<select multiple v-model="model">
264+
<optgroup>
265+
<template v-for="item in items" :value="item">
266+
<option v-if="item===1" :value="item">{{item}}</option>
267+
</template>
268+
</optgroup>
269+
</select>`).code,
270+
).toMatchInlineSnapshot(`
271+
"const { ssrRenderAttr: _ssrRenderAttr, ssrIncludeBooleanAttr: _ssrIncludeBooleanAttr, ssrLooseContain: _ssrLooseContain, ssrLooseEqual: _ssrLooseEqual, ssrRenderAttrs: _ssrRenderAttrs, ssrInterpolate: _ssrInterpolate, ssrRenderList: _ssrRenderList } = require("vue/server-renderer")
272+
273+
return function ssrRender(_ctx, _push, _parent, _attrs) {
274+
_push(\`<div\${_ssrRenderAttrs(_attrs)}><select multiple><optgroup><!--[-->\`)
275+
_ssrRenderList(_ctx.items, (item) => {
276+
_push(\`<!--[-->\`)
277+
if (item===1) {
278+
_push(\`<option\${
279+
_ssrRenderAttr("value", item)
280+
}\${
281+
(_ssrIncludeBooleanAttr((Array.isArray(_ctx.model))
282+
? _ssrLooseContain(_ctx.model, item)
283+
: _ssrLooseEqual(_ctx.model, item))) ? " selected" : ""
284+
}>\${
285+
_ssrInterpolate(item)
286+
}</option>\`)
287+
} else {
288+
_push(\`<!---->\`)
289+
}
290+
_push(\`<!--]-->\`)
291+
})
292+
_push(\`<!--]--></optgroup></select></div>\`)
293+
}"
294+
`)
225295
})
226296

227297
test('<input type="radio">', () => {

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

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ export const ssrTransformModel: DirectiveTransform = (dir, node, context) => {
3939
}
4040
}
4141

42+
const processSelectChildren = (children: TemplateChildNode[]) => {
43+
children.forEach(child => {
44+
if (child.type === NodeTypes.ELEMENT) {
45+
processOption(child as PlainElementNode)
46+
} else if (child.type === NodeTypes.FOR) {
47+
processSelectChildren(child.children)
48+
} else if (child.type === NodeTypes.IF) {
49+
child.branches.forEach(b => processSelectChildren(b.children))
50+
}
51+
})
52+
}
53+
4254
function processOption(plainNode: PlainElementNode) {
4355
if (plainNode.tag === 'option') {
4456
if (plainNode.props.findIndex(p => p.name === 'selected') === -1) {
@@ -65,17 +77,7 @@ export const ssrTransformModel: DirectiveTransform = (dir, node, context) => {
6577
)
6678
}
6779
} else if (plainNode.tag === 'optgroup') {
68-
plainNode.children.forEach(option => {
69-
if (option.type === NodeTypes.ELEMENT) {
70-
processOption(option as PlainElementNode)
71-
} else if (option.type === NodeTypes.FOR) {
72-
option.children.forEach(c => processOption(c as PlainElementNode))
73-
} else if (option.type === NodeTypes.IF) {
74-
option.branches.forEach(b =>
75-
b.children.forEach(c => processOption(c as PlainElementNode)),
76-
)
77-
}
78-
})
80+
processSelectChildren(plainNode.children)
7981
}
8082
}
8183

@@ -171,18 +173,7 @@ export const ssrTransformModel: DirectiveTransform = (dir, node, context) => {
171173
checkDuplicatedValue()
172174
node.children = [createInterpolation(model, model.loc)]
173175
} else if (node.tag === 'select') {
174-
const processChildren = (children: TemplateChildNode[]) => {
175-
children.forEach(child => {
176-
if (child.type === NodeTypes.ELEMENT) {
177-
processOption(child as PlainElementNode)
178-
} else if (child.type === NodeTypes.FOR) {
179-
processChildren(child.children)
180-
} else if (child.type === NodeTypes.IF) {
181-
child.branches.forEach(b => processChildren(b.children))
182-
}
183-
})
184-
}
185-
processChildren(node.children)
176+
processSelectChildren(node.children)
186177
} else {
187178
context.onError(
188179
createDOMCompilerError(

0 commit comments

Comments
 (0)