dioxus_rsx/template_body.rs
1//! I'm so sorry this is so complicated. Here's my best to simplify and explain it:
2//!
3//! The `Callbody` is the contents of the rsx! macro - this contains all the information about every
4//! node that rsx! directly knows about. For loops, if statements, etc.
5//!
6//! However, there are multiple *templates* inside a callbody - due to how core clones templates and
7//! just generally rationalize the concept of a template, nested bodies like for loops and if statements
8//! and component children are all templates, contained within the same Callbody.
9//!
10//! This gets confusing fast since there's lots of IDs bouncing around.
11//!
12//! The IDs at play:
13//! - The id of the template itself so we can find it and apply it to the dom.
14//! This is challenging since all calls to file/line/col/id are relative to the macro invocation,
15//! so they will have to share the same base ID and we need to give each template a new ID.
16//! The id of the template will be something like file!():line!():col!():ID where ID increases for
17//! each nested template.
18//!
19//! - The IDs of dynamic nodes relative to the template they live in. This is somewhat easy to track
20//! but needs to happen on a per-template basis.
21//!
22//! - The IDs of formatted strings in debug mode only. Any formatted segments like "{x:?}" get pulled out
23//! into a pool so we can move them around during hot reloading on a per-template basis.
24//!
25//! - The IDs of component property literals in debug mode only. Any component property literals like
26//! 1234 get pulled into the pool so we can hot reload them with the context of the literal pool.
27//!
28//! We solve this by parsing the structure completely and then doing a second pass that fills in IDs
29//! by walking the structure.
30//!
31//! This means you can't query the ID of any node "in a vacuum" - these are assigned once - but at
32//! least they're stable enough for the purposes of hotreloading
33//!
34//! ```text
35//! rsx! {
36//! div {
37//! class: "hello",
38//! id: "node-{node_id}", <--- {node_id} has the formatted segment id 0 in the literal pool
39//! ..props, <--- spreads are not reloadable
40//!
41//! "Hello, world!" <--- not tracked but reloadable in the template since it's just a string
42//!
43//! for item in 0..10 { <--- both 0 and 10 are technically reloadable, but we don't hot reload them today...
44//! div { "cool-{item}" } <--- {item} has the formatted segment id 1 in the literal pool
45//! }
46//!
47//! Link {
48//! to: "/home", <--- hotreloadable since its a component prop literal (with component literal id 0)
49//! class: "link {is_ready}", <--- {is_ready} has the formatted segment id 2 in the literal pool and the property has the component literal id 1
50//! "Home" <--- hotreloadable since its a component child (via template)
51//! }
52//! }
53//! }
54//! ```
55
56use self::location::DynIdx;
57use crate::innerlude::Attribute;
58use crate::*;
59use proc_macro2::{Span, TokenStream as TokenStream2};
60use proc_macro2_diagnostics::SpanDiagnosticExt;
61use syn::parse_quote;
62
63type NodePath = Vec<u8>;
64type AttributePath = Vec<u8>;
65
66/// A set of nodes in a template position
67///
68/// this could be:
69/// - The root of a callbody
70/// - The children of a component
71/// - The children of a for loop
72/// - The children of an if chain
73///
74/// The TemplateBody when needs to be parsed into a surrounding `Body` to be correctly re-indexed
75/// By default every body has a `0` default index
76#[derive(PartialEq, Eq, Clone, Debug)]
77pub struct TemplateBody {
78 pub roots: Vec<BodyNode>,
79 pub template_idx: DynIdx,
80 pub node_paths: Vec<NodePath>,
81 pub attr_paths: Vec<(AttributePath, usize)>,
82 pub dynamic_text_segments: Vec<FormattedSegment>,
83 pub diagnostics: Diagnostics,
84}
85
86impl Parse for TemplateBody {
87 /// Parse the nodes of the callbody as `Body`.
88 fn parse(input: ParseStream) -> Result<Self> {
89 let children = RsxBlock::parse_children(input)?;
90 let mut myself = Self::new(children.children);
91 myself
92 .diagnostics
93 .extend(children.diagnostics.into_diagnostics());
94
95 Ok(myself)
96 }
97}
98
99/// Our ToTokens impl here just defers to rendering a template out like any other `Body`.
100/// This is because the parsing phase filled in all the additional metadata we need
101impl ToTokens for TemplateBody {
102 fn to_tokens(&self, tokens: &mut TokenStream2) {
103 // First normalize the template body for rendering
104 let node = self.normalized();
105
106 // If we have an implicit key, then we need to write its tokens
107 let key_tokens = match node.implicit_key() {
108 Some(tok) => quote! { Some( #tok.to_string() ) },
109 None => quote! { None },
110 };
111
112 let roots = node.quote_roots();
113
114 // Print paths is easy - just print the paths
115 let node_paths = node.node_paths.iter().map(|it| quote!(&[#(#it),*]));
116 let attr_paths = node.attr_paths.iter().map(|(it, _)| quote!(&[#(#it),*]));
117
118 // For printing dynamic nodes, we rely on the ToTokens impl
119 // Elements have a weird ToTokens - they actually are the entrypoint for Template creation
120 let dynamic_nodes: Vec<_> = node.dynamic_nodes().collect();
121 let dynamic_nodes_len = dynamic_nodes.len();
122
123 // We could add a ToTokens for Attribute but since we use that for both components and elements
124 // They actually need to be different, so we just localize that here
125 let dyn_attr_printer: Vec<_> = node
126 .dynamic_attributes()
127 .map(|attr| attr.rendered_as_dynamic_attr())
128 .collect();
129 let dynamic_attr_len = dyn_attr_printer.len();
130
131 let dynamic_text = node.dynamic_text_segments.iter();
132
133 let diagnostics = &node.diagnostics;
134 let index = node.template_idx.get();
135 let hot_reload_mapping = node.hot_reload_mapping();
136
137 tokens.append_all(quote! {
138 dioxus_core::Element::Ok({
139 #diagnostics
140
141 // Components pull in the dynamic literal pool and template in debug mode, so they need to be defined before dynamic nodes
142 #[cfg(debug_assertions)]
143 fn __original_template() -> &'static dioxus_core::internal::HotReloadedTemplate {
144 static __ORIGINAL_TEMPLATE: ::std::sync::OnceLock<dioxus_core::internal::HotReloadedTemplate> = ::std::sync::OnceLock::new();
145 if __ORIGINAL_TEMPLATE.get().is_none() {
146 _ = __ORIGINAL_TEMPLATE.set(#hot_reload_mapping);
147 }
148 __ORIGINAL_TEMPLATE.get().unwrap()
149 }
150 #[cfg(debug_assertions)]
151 let __template_read = {
152 use dioxus_signals::ReadableExt;
153
154 static __NORMALIZED_FILE: &'static str = {
155 const PATH: &str = dioxus_core::const_format::str_replace!(file!(), "\\\\", "/");
156 dioxus_core::const_format::str_replace!(PATH, '\\', "/")
157 };
158
159 // The key is important here - we're creating a new GlobalSignal each call to this
160 // But the key is what's keeping it stable
161 static __TEMPLATE: dioxus_signals::GlobalSignal<Option<dioxus_core::internal::HotReloadedTemplate>> = dioxus_signals::GlobalSignal::with_location(
162 || None::<dioxus_core::internal::HotReloadedTemplate>,
163 __NORMALIZED_FILE,
164 line!(),
165 column!(),
166 #index
167 );
168
169 dioxus_core::Runtime::try_current().map(|_| __TEMPLATE.read())
170 };
171 // If the template has not been hot reloaded, we always use the original template
172 // Templates nested within macros may be merged because they have the same file-line-column-index
173 // They cannot be hot reloaded, so this prevents incorrect rendering
174 #[cfg(debug_assertions)]
175 let __template_read = match __template_read.as_ref().map(|__template_read| __template_read.as_ref()) {
176 Some(Some(__template_read)) => &__template_read,
177 _ => __original_template(),
178 };
179 #[cfg(debug_assertions)]
180 let mut __dynamic_literal_pool = dioxus_core::internal::DynamicLiteralPool::new(
181 vec![ #( #dynamic_text.to_string() ),* ],
182 );
183
184 // The key needs to be created before the dynamic nodes as it might depend on a borrowed value which gets moved into the dynamic nodes
185 #[cfg(not(debug_assertions))]
186 let __key = #key_tokens;
187 // These items are used in both the debug and release expansions of rsx. Pulling them out makes the expansion
188 // slightly smaller and easier to understand. Rust analyzer also doesn't autocomplete well when it sees an ident show up twice in the expansion
189 let __dynamic_nodes: [dioxus_core::DynamicNode; #dynamic_nodes_len] = [ #( #dynamic_nodes ),* ];
190 let __dynamic_attributes: [Box<[dioxus_core::Attribute]>; #dynamic_attr_len] = [ #( #dyn_attr_printer ),* ];
191 #[doc(hidden)]
192 static __TEMPLATE_ROOTS: &[dioxus_core::TemplateNode] = &[ #( #roots ),* ];
193
194 #[cfg(debug_assertions)]
195 {
196 let mut __dynamic_value_pool = dioxus_core::internal::DynamicValuePool::new(
197 Vec::from(__dynamic_nodes),
198 Vec::from(__dynamic_attributes),
199 __dynamic_literal_pool
200 );
201 __dynamic_value_pool.render_with(__template_read)
202 }
203 #[cfg(not(debug_assertions))]
204 {
205 #[doc(hidden)] // vscode please stop showing these in symbol search
206 static ___TEMPLATE: dioxus_core::Template = dioxus_core::Template {
207 roots: __TEMPLATE_ROOTS,
208 node_paths: &[ #( #node_paths ),* ],
209 attr_paths: &[ #( #attr_paths ),* ],
210 };
211
212 // NOTE: Allocating a temporary is important to make reads within rsx drop before the value is returned
213 #[allow(clippy::let_and_return)]
214 let __vnodes = dioxus_core::VNode::new(
215 __key,
216 ___TEMPLATE,
217 Box::new(__dynamic_nodes),
218 Box::new(__dynamic_attributes),
219 );
220 __vnodes
221 }
222 })
223 });
224 }
225}
226
227impl TemplateBody {
228 /// Create a new TemplateBody from a set of nodes
229 ///
230 /// This will fill in all the necessary path information for the nodes in the template and will
231 /// overwrite data like dynamic indexes.
232 pub fn new(nodes: Vec<BodyNode>) -> Self {
233 let mut body = Self {
234 roots: vec![],
235 template_idx: DynIdx::default(),
236 node_paths: Vec::new(),
237 attr_paths: Vec::new(),
238 dynamic_text_segments: Vec::new(),
239 diagnostics: Diagnostics::new(),
240 };
241
242 // Assign paths to all nodes in the template
243 body.assign_paths_inner(&nodes);
244
245 // And then save the roots
246 body.roots = nodes;
247
248 // Finally, validate the key
249 body.validate_key();
250
251 body
252 }
253
254 /// Normalize the Template body for rendering. If the body is completely empty, insert a placeholder node
255 pub fn normalized(&self) -> Self {
256 // If the nodes are completely empty, insert a placeholder node
257 // Core expects at least one node in the template to make it easier to replace
258 if self.is_empty() {
259 // Create an empty template body with a placeholder and diagnostics + the template index from the original
260 let empty = Self::new(vec![BodyNode::RawExpr(parse_quote! {()})]);
261 let default = Self {
262 diagnostics: self.diagnostics.clone(),
263 template_idx: self.template_idx.clone(),
264 ..empty
265 };
266 return default;
267 }
268 self.clone()
269 }
270
271 pub fn is_empty(&self) -> bool {
272 self.roots.is_empty()
273 }
274
275 pub fn implicit_key(&self) -> Option<&AttributeValue> {
276 match self.roots.first() {
277 Some(BodyNode::Element(el)) => el.key(),
278 Some(BodyNode::Component(comp)) => comp.get_key(),
279 _ => None,
280 }
281 }
282
283 /// Ensure only one key and that the key is not a static str
284 ///
285 /// todo: we want to allow arbitrary exprs for keys provided they impl hash / eq
286 fn validate_key(&mut self) {
287 let key = self.implicit_key();
288
289 if let Some(attr) = key {
290 let diagnostic = match &attr {
291 AttributeValue::AttrLiteral(ifmt) => {
292 if ifmt.is_static() {
293 ifmt.span().error("Key must not be a static string. Make sure to use a formatted string like `key: \"{value}\"")
294 } else {
295 return;
296 }
297 }
298 _ => attr
299 .span()
300 .error("Key must be in the form of a formatted string like `key: \"{value}\""),
301 };
302
303 self.diagnostics.push(diagnostic);
304 }
305 }
306
307 pub fn get_dyn_node(&self, path: &[u8]) -> &BodyNode {
308 let mut node = self.roots.get(path[0] as usize).unwrap();
309 for idx in path.iter().skip(1) {
310 node = node.element_children().get(*idx as usize).unwrap();
311 }
312 node
313 }
314
315 pub fn get_dyn_attr(&self, path: &AttributePath, idx: usize) -> &Attribute {
316 match self.get_dyn_node(path) {
317 BodyNode::Element(el) => &el.merged_attributes[idx],
318 _ => unreachable!(),
319 }
320 }
321
322 pub fn dynamic_attributes(&self) -> impl DoubleEndedIterator<Item = &Attribute> {
323 self.attr_paths
324 .iter()
325 .map(|(path, idx)| self.get_dyn_attr(path, *idx))
326 }
327
328 pub fn dynamic_nodes(&self) -> impl DoubleEndedIterator<Item = &BodyNode> {
329 self.node_paths.iter().map(|path| self.get_dyn_node(path))
330 }
331
332 fn quote_roots(&self) -> impl Iterator<Item = TokenStream2> + '_ {
333 self.roots.iter().map(|node| match node {
334 BodyNode::Element(el) => quote! { #el },
335 BodyNode::Text(text) if text.is_static() => {
336 let text = text.input.to_static().unwrap();
337 quote! { dioxus_core::TemplateNode::Text { text: #text } }
338 }
339 _ => {
340 let id = node.get_dyn_idx();
341 quote! { dioxus_core::TemplateNode::Dynamic { id: #id } }
342 }
343 })
344 }
345
346 /// Iterate through the literal component properties of this rsx call in depth-first order
347 pub fn literal_component_properties(&self) -> impl Iterator<Item = &HotLiteral> + '_ {
348 self.dynamic_nodes()
349 .filter_map(|node| {
350 if let BodyNode::Component(component) = node {
351 Some(component)
352 } else {
353 None
354 }
355 })
356 .flat_map(|component| {
357 component.component_props().filter_map(|field| {
358 if let AttributeValue::AttrLiteral(literal) = &field.value {
359 Some(literal)
360 } else {
361 None
362 }
363 })
364 })
365 }
366
367 fn hot_reload_mapping(&self) -> TokenStream2 {
368 let key = if let Some(AttributeValue::AttrLiteral(HotLiteral::Fmted(key))) =
369 self.implicit_key()
370 {
371 quote! { Some(#key) }
372 } else {
373 quote! { None }
374 };
375 let dynamic_nodes = self.dynamic_nodes().map(|node| {
376 let id = node.get_dyn_idx();
377 quote! { dioxus_core::internal::HotReloadDynamicNode::Dynamic(#id) }
378 });
379 let dyn_attr_printer = self.dynamic_attributes().map(|attr| {
380 let id = attr.get_dyn_idx();
381 quote! { dioxus_core::internal::HotReloadDynamicAttribute::Dynamic(#id) }
382 });
383 let component_values = self
384 .literal_component_properties()
385 .map(|literal| literal.quote_as_hot_reload_literal());
386 quote! {
387 dioxus_core::internal::HotReloadedTemplate::new(
388 #key,
389 vec![ #( #dynamic_nodes ),* ],
390 vec![ #( #dyn_attr_printer ),* ],
391 vec![ #( #component_values ),* ],
392 __TEMPLATE_ROOTS,
393 )
394 }
395 }
396
397 /// Get the span of the first root of this template
398 pub(crate) fn first_root_span(&self) -> Span {
399 match self.roots.first() {
400 Some(root) => root.span(),
401 _ => Span::call_site(),
402 }
403 }
404}