@@ -93,4 +93,239 @@ describe('ssr: attrs fallthrough', () => {
9393 }"
9494 ` )
9595 } )
96+
97+ // what a vdom `cloneVNode` adds to the content of a vapor slot comes as the
98+ // slot function's `_slotAttrs`, for the single root of the slot body
99+ describe ( 'vapor slot root' , ( ) => {
100+ const ssr = ( src : string , vapor = true ) =>
101+ compile ( src , { vapor } ) . code . replace ( / \s * \n \s * / g, '' )
102+
103+ // merged only when there are attrs: the tag is otherwise as it would be
104+ test ( 'element' , ( ) => {
105+ expect (
106+ compile ( `<Comp><button class="a">open</button></Comp>` , {
107+ vapor : true ,
108+ } ) . code ,
109+ ) . toMatchInlineSnapshot ( `
110+ "const { resolveComponent: _resolveComponent, mergeProps: _mergeProps, withCtx: _withCtx, createVNode: _createVNode } = require("vue")
111+ const { ssrRenderAttrs: _ssrRenderAttrs, ssrRenderComponent: _ssrRenderComponent } = require("vue/server-renderer")
112+
113+ return function ssrRender(_ctx, _push, _parent, _attrs) {
114+ const _component_Comp = _resolveComponent("Comp")
115+
116+ _push(\`<!--{-->\`)
117+ _push(_ssrRenderComponent(_component_Comp, _attrs, {
118+ default: _withCtx((_, _push, _parent, _scopeId, _slotAttrs) => {
119+ if (_push) {
120+ _push(\`<button\${
121+ _slotAttrs ? _ssrRenderAttrs(_mergeProps({ class: "a" }, _slotAttrs)) : \` class="a"\`
122+ }\${
123+ _scopeId
124+ }>open</button>\`)
125+ } else {
126+ return [
127+ _createVNode("button", { class: "a" }, "open")
128+ ]
129+ }
130+ }),
131+ _: 1 /* STABLE */
132+ }, _parent))
133+ _push(\`<!--}-->\`)
134+ }"
135+ ` )
136+ } )
137+
138+ const bare = ( tag : string ) =>
139+ `<${ tag } \${_slotAttrs ? _ssrRenderAttrs(_slotAttrs) : \`\`}\${_scopeId}>`
140+
141+ test . each ( [
142+ [
143+ 'element with bindings' ,
144+ `<Comp><i :id="id" class="a" :class="b" v-show="ok" /></Comp>` ,
145+ [
146+ '_slotAttrs ? ' +
147+ '_ssrRenderAttrs(_mergeProps({id: _ctx.id,class: ["a", _ctx.b]}, _slotAttrs, {style: (_ctx.ok) ? null : { display: "none" }}))' +
148+ ' : `${_ssrRenderAttr("id", _ctx.id)} class="${_ssrRenderClass([_ctx.b, "a"])}" style="${_ssrRenderStyle((_ctx.ok) ? null : { display: "none" })}"`' ,
149+ ] ,
150+ ] ,
151+ [
152+ 'v-model input' ,
153+ `<Comp><input v-model="s" class="a"></Comp>` ,
154+ [
155+ '_slotAttrs ? ' +
156+ '_ssrRenderAttrs((_temp0 = _mergeProps({ class: "a" }, _slotAttrs), _mergeProps(_temp0, _ssrGetDynamicModelProps(_temp0, _ctx.s))))' +
157+ ' : `${_ssrRenderAttr("value", _ctx.s)} class="a"`' ,
158+ ] ,
159+ ] ,
160+ [
161+ 'component' ,
162+ `<Comp><Btn foo="1" /></Comp>` ,
163+ [
164+ '_push(_ssrRenderComponent(_component_Btn, _slotAttrs ? _mergeProps({ foo: "1" }, _slotAttrs) : { foo: "1" }, null, _parent, _scopeId))' ,
165+ ] ,
166+ ] ,
167+ [
168+ 'component with v-bind' ,
169+ `<Comp><Btn v-bind="obj" foo="1" /></Comp>` ,
170+ [
171+ '_push(_ssrRenderComponent(_component_Btn, _mergeProps(_ctx.obj, { foo: "1" }, _slotAttrs), null, _parent, _scopeId))' ,
172+ ] ,
173+ ] ,
174+ [
175+ 'dynamic component' ,
176+ `<Comp><component :is="x" /></Comp>` ,
177+ [
178+ '_ssrRenderDynamicComponent(_push, _resolveDynamicComponent(_ctx.x), _slotAttrs, null, _parent, _scopeId)' ,
179+ ] ,
180+ ] ,
181+ [
182+ 'v-if chain' ,
183+ `<Comp><i v-if="ok" /><b v-else /></Comp>` ,
184+ [ bare ( 'i' ) , bare ( 'b' ) ] ,
185+ ] ,
186+ [
187+ 'template v-if' ,
188+ `<Comp><template v-if="ok"><i /></template></Comp>` ,
189+ [ bare ( 'i' ) ] ,
190+ ] ,
191+ [
192+ 'v-if chain in a template v-if' ,
193+ `<Comp><template v-if="a"><i v-if="b" /><b v-else /></template></Comp>` ,
194+ [ bare ( 'i' ) , bare ( 'b' ) ] ,
195+ ] ,
196+ [
197+ 'template v-if with a comment' ,
198+ `<Comp><template v-if="a"><!--c--><i /></template></Comp>` ,
199+ [ bare ( 'i' ) ] ,
200+ ] ,
201+ [
202+ 'template v-if in a template v-if' ,
203+ `<Comp><template v-if="a"><template v-if="b"><i /></template></template></Comp>` ,
204+ [ bare ( 'i' ) ] ,
205+ ] ,
206+ [
207+ 'v-if chain in a slot template' ,
208+ `<Comp><template #a><i v-if="ok" /><b v-else /></template></Comp>` ,
209+ [ bare ( 'i' ) , bare ( 'b' ) ] ,
210+ ] ,
211+ [
212+ 'slot template with a comment' ,
213+ `<Comp><template #a><!--c--><i /></template></Comp>` ,
214+ [ bare ( 'i' ) ] ,
215+ ] ,
216+ [
217+ 'slot template, next to the implicit default slot' ,
218+ `<Comp><template #a><i /></template><b /></Comp>` ,
219+ [ bare ( 'i' ) , bare ( 'b' ) ] ,
220+ ] ,
221+ [
222+ 'the slot of a nested component' ,
223+ `<Comp><div><Inner><p /></Inner></div></Comp>` ,
224+ [ bare ( 'div' ) , bare ( 'p' ) ] ,
225+ ] ,
226+ [
227+ 'Transition child' ,
228+ `<Comp><Transition><i v-if="ok" /></Transition></Comp>` ,
229+ [ bare ( 'i' ) ] ,
230+ ] ,
231+ [
232+ 'Transition branches' ,
233+ `<Comp><Transition><i v-if="ok" /><b v-else /></Transition></Comp>` ,
234+ [ bare ( 'i' ) , bare ( 'b' ) ] ,
235+ ] ,
236+ [
237+ 'KeepAlive child' ,
238+ `<Comp><KeepAlive><Btn /></KeepAlive></Comp>` ,
239+ [
240+ '_push(_ssrRenderComponent(_component_Btn, _slotAttrs, null, _parent, _scopeId))' ,
241+ ] ,
242+ ] ,
243+ // these merge props, or render content off them, either way
244+ [
245+ 'element with v-bind' ,
246+ `<Comp><i v-bind="obj" class="a" /></Comp>` ,
247+ [
248+ '<i${_ssrRenderAttrs(_mergeProps(_ctx.obj, { class: "a" }, _slotAttrs))}${_scopeId}>' ,
249+ ] ,
250+ ] ,
251+ [
252+ 'v-model select' ,
253+ `<Comp><select v-model="s" class="a" /></Comp>` ,
254+ [
255+ '<select${_ssrRenderAttrs(_mergeProps({ class: "a" }, _slotAttrs))}${_scopeId}>' ,
256+ ] ,
257+ ] ,
258+ [
259+ 'textarea' ,
260+ `<Comp><textarea v-model="s" class="a" /></Comp>` ,
261+ [
262+ '<textarea${_ssrRenderAttrs(_mergeProps({ class: "a" }, _slotAttrs), "textarea")}${_scopeId}>' ,
263+ ] ,
264+ ] ,
265+ // no attrs when the content is not cloned
266+ [
267+ 'textarea without props' ,
268+ `<Comp><textarea>hello</textarea></Comp>` ,
269+ [
270+ '<textarea${_ssrRenderAttrs(_temp0 = _slotAttrs, "textarea")}${_scopeId}>${_ssrInterpolate((_temp0 && "value" in _temp0) ? _temp0.value : "hello")}</textarea>' ,
271+ ] ,
272+ ] ,
273+ ] ) ( '%s' , ( _ , src , expected ) => {
274+ const code = ssr ( src )
275+ for ( const part of expected ) expect ( code ) . toContain ( part )
276+ } )
277+
278+ test . each ( [
279+ [ 'several roots' , `<Comp><i /><b /></Comp>` ] ,
280+ [ 'v-for' , `<Comp><i v-for="n in 2" /></Comp>` ] ,
281+ [ 'text' , `<Comp>{{ msg }}</Comp>` ] ,
282+ [ 'slot outlet' , `<Comp><slot /></Comp>` ] ,
283+ [ 'below the root' , `<Comp><div><i /></div><b /></Comp>` ] ,
284+ [
285+ 'several roots in a template v-if' ,
286+ `<Comp><template v-if="a"><i /><b /></template></Comp>` ,
287+ ] ,
288+ [
289+ 'a v-if next to another root in a template v-if' ,
290+ `<Comp><template v-if="a"><i v-if="b" /><b /></template></Comp>` ,
291+ ] ,
292+ [
293+ 'Transition below an element' ,
294+ `<div><Transition><i /></Transition></div>` ,
295+ ] ,
296+ [ 'Suspense slot' , `<Suspense><i /></Suspense>` ] ,
297+ [
298+ 'Suspense slot template' ,
299+ `<Suspense><template #fallback><i v-if="a" /><b v-else /></template></Suspense>` ,
300+ ] ,
301+ [
302+ 'several roots in a slot template' ,
303+ `<Comp><template #a><i v-if="a" /><b /></template></Comp>` ,
304+ ] ,
305+ [ 'Teleport content' , `<Teleport to="body"><i /></Teleport>` ] ,
306+ [
307+ 'TransitionGroup child' ,
308+ `<TransitionGroup><i key="a" /></TransitionGroup>` ,
309+ ] ,
310+ ] ) ( 'not for %s' , ( _ , src ) => {
311+ // but as the parameter
312+ expect ( ssr ( src ) ) . not . toMatch ( / _ s l o t A t t r s (? ! \) = > ) / )
313+ } )
314+
315+ // a component's own root keeps its rule
316+ test . each ( [
317+ `<template v-if="a"><i v-if="b" /><b v-else /></template>` ,
318+ `<template v-if="a"><!--c--><i /></template>` ,
319+ ] ) ( 'not for the root in %s' , src => {
320+ expect ( ssr ( src ) ) . not . toMatch ( / _ a t t r s \b (? ! \) { ) / )
321+ } )
322+
323+ test ( 'not in a vdom component' , ( ) => {
324+ const code = ssr ( `<Comp><button class="a">open</button></Comp>` , false )
325+ expect ( code ) . not . toContain ( '_slotAttrs' )
326+ expect ( code ) . toContain (
327+ '_push(`<button class="a"${_scopeId}>open</button>`)' ,
328+ )
329+ } )
330+ } )
96331} )
0 commit comments