@@ -181,6 +181,7 @@ fn root_node_to_tokens_ssr(cx: &Ident, node: &Node) -> TokenStream {
181
181
Node :: Block ( node) => {
182
182
let value = node. value . as_ref ( ) ;
183
183
quote ! {
184
+ #[ allow( unused_braces) ]
184
185
#value
185
186
}
186
187
}
@@ -205,32 +206,43 @@ fn fragment_to_tokens_ssr(cx: &Ident, _span: Span, nodes: &[Node]) -> TokenStrea
205
206
}
206
207
207
208
fn root_element_to_tokens_ssr ( cx : & Ident , node : & NodeElement ) -> TokenStream {
208
- let mut template = String :: new ( ) ;
209
- let mut holes = Vec :: < TokenStream > :: new ( ) ;
210
- let mut exprs_for_compiler = Vec :: < TokenStream > :: new ( ) ;
209
+ if is_component_node ( & node) {
210
+ component_to_tokens ( cx, node)
211
+ } else {
212
+ let mut template = String :: new ( ) ;
213
+ let mut holes = Vec :: < TokenStream > :: new ( ) ;
214
+ let mut exprs_for_compiler = Vec :: < TokenStream > :: new ( ) ;
211
215
212
- element_to_tokens_ssr ( cx, node, & mut template, & mut holes, & mut exprs_for_compiler) ;
216
+ element_to_tokens_ssr (
217
+ cx,
218
+ node,
219
+ & mut template,
220
+ & mut holes,
221
+ & mut exprs_for_compiler,
222
+ true ,
223
+ ) ;
224
+
225
+ let template = if holes. is_empty ( ) {
226
+ quote ! {
227
+ #template
228
+ }
229
+ } else {
230
+ quote ! {
231
+ format!(
232
+ #template,
233
+ #( #holes) *
234
+ )
235
+ }
236
+ } ;
213
237
214
- let template = if holes. is_empty ( ) {
238
+ let tag_name = node. name . to_string ( ) ;
239
+ let typed_element_name = Ident :: new ( & camel_case_tag_name ( & tag_name) , node. name . span ( ) ) ;
215
240
quote ! {
216
- #template
241
+ {
242
+ #( #exprs_for_compiler) *
243
+ :: leptos:: HtmlElement :: from_html( cx, leptos:: #typed_element_name:: default ( ) , #template)
217
244
}
218
- } else {
219
- quote ! {
220
- format!(
221
- #template,
222
- #( #holes) *
223
- )
224
245
}
225
- } ;
226
-
227
- let tag_name = node. name . to_string ( ) ;
228
- let typed_element_name = Ident :: new ( & camel_case_tag_name ( & tag_name) , node. name . span ( ) ) ;
229
- quote ! {
230
- {
231
- #( #exprs_for_compiler) *
232
- :: leptos:: HtmlElement :: from_html( cx, leptos:: #typed_element_name:: default ( ) , #template)
233
- }
234
246
}
235
247
}
236
248
@@ -240,6 +252,7 @@ fn element_to_tokens_ssr(
240
252
template : & mut String ,
241
253
holes : & mut Vec < TokenStream > ,
242
254
exprs_for_compiler : & mut Vec < TokenStream > ,
255
+ is_root : bool ,
243
256
) {
244
257
if is_component_node ( node) {
245
258
template. push_str ( "{}" ) ;
@@ -257,6 +270,26 @@ fn element_to_tokens_ssr(
257
270
}
258
271
}
259
272
273
+ // insert hydration ID
274
+ let hydration_id = if is_root {
275
+ quote ! { leptos:: HydrationCtx :: peek( ) , }
276
+ } else {
277
+ quote ! { leptos:: HydrationCtx :: id( ) , }
278
+ } ;
279
+ match node
280
+ . attributes
281
+ . iter ( )
282
+ . find ( |node| matches ! ( node, Node :: Attribute ( attr) if attr. key. to_string( ) == "id" ) )
283
+ {
284
+ Some ( _) => {
285
+ template. push_str ( & format ! ( " leptos-hk=\" _{{}}\" " ) ) ;
286
+ }
287
+ None => {
288
+ template. push_str ( & format ! ( " id=\" _{{}}\" " ) ) ;
289
+ }
290
+ }
291
+ holes. push ( hydration_id) ;
292
+
260
293
set_class_attribute_ssr ( cx, node, template, holes) ;
261
294
262
295
if is_self_closing ( node) {
@@ -266,7 +299,7 @@ fn element_to_tokens_ssr(
266
299
for child in & node. children {
267
300
match child {
268
301
Node :: Element ( child) => {
269
- element_to_tokens_ssr ( cx, child, template, holes, exprs_for_compiler)
302
+ element_to_tokens_ssr ( cx, child, template, holes, exprs_for_compiler, false )
270
303
}
271
304
Node :: Text ( text) => {
272
305
if let Some ( value) = value_to_string ( & text. value ) {
@@ -345,13 +378,12 @@ fn attribute_to_tokens_ssr(
345
378
. parse :: < TokenStream > ( )
346
379
. expect ( "couldn't parse event name" ) ;
347
380
381
+ let event_type = if is_force_undelegated {
382
+ quote ! { :: leptos:: ev:: undelegated( :: leptos:: ev:: #event_type) }
383
+ } else {
384
+ quote ! { :: leptos:: ev:: #event_type }
385
+ } ;
348
386
exprs_for_compiler. push ( quote ! {
349
- let event_type = if is_force_undelegated {
350
- quote! { :: leptos:: ev:: undelegated( :: leptos:: ev:: #event_type) }
351
- } else {
352
- quote! { :: leptos:: ev:: #event_type }
353
- } ;
354
-
355
387
leptos:: ssr_event_listener( #event_type, #handler) ;
356
388
} )
357
389
} else if name. strip_prefix ( "prop:" ) . is_some ( ) || name. strip_prefix ( "class:" ) . is_some ( ) {
@@ -366,15 +398,16 @@ fn attribute_to_tokens_ssr(
366
398
367
399
if let Some ( value) = node. value . as_ref ( ) {
368
400
if let Some ( value) = value_to_string ( value) {
401
+ template. push_str ( "=\" " ) ;
369
402
template. push_str ( & value) ;
403
+ template. push ( '"' ) ;
370
404
} else {
371
- template. push_str ( "{} " ) ;
405
+ template. push_str ( "= \" {} \" " ) ;
372
406
let value = value. as_ref ( ) ;
373
407
holes. push ( quote ! {
374
- leptos:: escape_attr( & { #value} . into_attribute( #cx) . as_value_string ( #name ) ) ,
408
+ leptos:: escape_attr( & { #value} . into_attribute( #cx) . as_nameless_value_string ( ) ) ,
375
409
} )
376
410
}
377
- template. push ( '"' ) ;
378
411
}
379
412
}
380
413
}
0 commit comments