euv_core/vdom/cast/impl.rs
1use super::*;
2
3/// Converts a `Vec<VirtualNode>` into a `VirtualNode::Fragment`.
4///
5/// This enables using a `Vec<VirtualNode>` directly in the `html!` macro
6/// without manually wrapping it in `VirtualNode::Fragment(...)`.
7///
8/// # Returns
9///
10/// - `VirtualNode` - A `VirtualNode::Fragment` containing the nodes, or
11/// `VirtualNode::Empty` if the vector is empty.
12impl From<Vec<VirtualNode>> for VirtualNode {
13 /// Lifts a `Vec<VirtualNode>` into a single [`VirtualNode`] via wrapping or unwrapping.
14 ///
15 /// # Arguments
16 ///
17 /// - `Vec<VirtualNode>` - Input value to convert from.
18 fn from(nodes: Vec<VirtualNode>) -> Self {
19 if nodes.is_empty() {
20 VirtualNode::Empty
21 } else {
22 VirtualNode::Fragment(nodes)
23 }
24 }
25}
26
27/// Converts a borrowed `[VirtualNode]` slice into a `VirtualNode`.
28///
29/// OPT 21: this lets call sites that hold `node.get_children()` (a
30/// `&[VirtualNode]`) hand the children straight to the `html!` macro's
31/// `(expr).into()` dispatch without first cloning into a `Vec`. The
32/// slice is materialised exactly once at the conversion site — same
33/// number of clones as the old `get_child_node` helper performed
34/// (zero for empty, one for single child, N for fragment) but moved
35/// out of the helper so any caller that just wants to peek at the
36/// children pays nothing.
37impl From<&[VirtualNode]> for VirtualNode {
38 /// Lifts a borrowed slice into a [`VirtualNode`].
39 ///
40 /// # Arguments
41 ///
42 /// - `&[VirtualNode]` - Input value to convert from.
43 fn from(children: &[VirtualNode]) -> Self {
44 match children {
45 [] => VirtualNode::Empty,
46 [only] => only.clone(),
47 _ => VirtualNode::Fragment(children.to_vec()),
48 }
49 }
50}
51
52/// Converts an `Option<VirtualNode>` into a `VirtualNode`.
53///
54/// `Some(node)` returns the inner node, `None` returns `VirtualNode::Empty`.
55///
56/// # Returns
57///
58/// - `VirtualNode` - The inner node if `Some`, otherwise `VirtualNode::Empty`.
59impl From<Option<VirtualNode>> for VirtualNode {
60 /// Lifts an `Option<VirtualNode>` into a [`VirtualNode`] (unwraps or falls back to empty).
61 ///
62 /// # Arguments
63 ///
64 /// - `Option<VirtualNode>` - Input value to convert from.
65 fn from(node: Option<VirtualNode>) -> Self {
66 match node {
67 Some(node) => node,
68 None => VirtualNode::Empty,
69 }
70 }
71}
72
73/// Converts an `Option<Vec<VirtualNode>>` into a `VirtualNode`.
74///
75/// `Some(vec)` converts the vector into a `VirtualNode::Fragment` (or `Empty`
76/// if the vector is empty), `None` returns `VirtualNode::Empty`.
77///
78/// # Returns
79///
80/// - `VirtualNode` - A `VirtualNode::Fragment` if `Some` with nodes,
81/// `VirtualNode::Empty` if `None` or the vector is empty.
82impl From<Option<Vec<VirtualNode>>> for VirtualNode {
83 /// Lifts an `Option<Vec<VirtualNode>>` into a [`VirtualNode`] (unwraps or falls back to empty).
84 ///
85 /// # Arguments
86 ///
87 /// - `Option<Vec<VirtualNode>>` - Input value to convert from.
88 fn from(nodes: Option<Vec<VirtualNode>>) -> Self {
89 match nodes {
90 Some(nodes) => nodes.into(),
91 None => VirtualNode::Empty,
92 }
93 }
94}
95
96/// Wraps a `FnMut(&mut HookContext) -> VirtualNode` closure into a `DynamicNode`.
97///
98/// This enables writing `{move |_: &mut HookContext| html! { ... }}` directly in HTML markup
99/// without explicit `DynamicNode` construction.
100impl<F> From<F> for VirtualNode
101where
102 F: FnMut(&mut HookContext) -> VirtualNode + 'static,
103{
104 /// Wraps this closure into a `VirtualNode::Dynamic` with a fresh hook context.
105 ///
106 /// # Returns
107 ///
108 /// - `VirtualNode` - A dynamic virtual node wrapping this closure.
109 ///
110 /// # Arguments
111 ///
112 /// - `F` - Input value to convert from.
113 fn from(render_fn: F) -> Self {
114 VirtualNode::create_dynamic(render_fn)
115 }
116}
117
118/// Converts a `String` into a text virtual node.
119impl From<String> for VirtualNode {
120 /// Converts this string into a text virtual node.
121 ///
122 /// # Returns
123 ///
124 /// - `VirtualNode` - A text virtual node.
125 ///
126 /// # Arguments
127 ///
128 /// - `String` - Input value to convert from.
129 fn from(text: String) -> Self {
130 VirtualNode::Text(TextNode::new(Cow::Owned(text), None))
131 }
132}
133
134/// Converts a `&str` into a text virtual node.
135impl From<&str> for VirtualNode {
136 /// Converts this string slice into a text virtual node.
137 ///
138 /// The `&str` may not be `'static`, so we route through `Cow::Owned`
139 /// (small heap allocation) — runtime-evaluated text typically comes
140 /// here via `format!`, interpolated messages, or formatted signal
141 /// values. Macro-generated literal text takes the `Cow::Borrowed`
142 /// fast path in [`TextNode`] instead.
143 ///
144 /// # Returns
145 ///
146 /// - `VirtualNode` - A text virtual node.
147 ///
148 /// # Arguments
149 ///
150 /// - `&str` - Input value to convert from.
151 fn from(text: &str) -> Self {
152 VirtualNode::Text(TextNode::new(Cow::Owned(text.to_owned()), None))
153 }
154}
155
156/// Converts a `Cow` of text into a text virtual node.
157///
158/// A component that names a control with text it holds at run time keeps
159/// the text in a cow, so a borrowed literal and an owned string both reach
160/// the tree the same way.
161///
162/// # Returns
163///
164/// - `VirtualNode` - A text virtual node.
165///
166/// # Arguments
167///
168/// - `Cow<'static, str>` - Input value to convert from.
169impl From<Cow<'static, str>> for VirtualNode {
170 /// Converts this cow into a text virtual node.
171 ///
172 /// # Returns
173 ///
174 /// - `VirtualNode` - A text virtual node.
175 ///
176 /// # Arguments
177 ///
178 /// - `Cow<'static, str>` - Input value to convert from.
179 fn from(text: Cow<'static, str>) -> Self {
180 VirtualNode::Text(TextNode::new(text, None))
181 }
182}
183
184/// Converts an `i32` into a text virtual node.
185impl From<i32> for VirtualNode {
186 /// Converts this integer into a text virtual node.
187 ///
188 /// OPT 29: `i32::to_string()` is unavoidable, so the Cow wraps an
189 /// `Owned` variant. (`Cow::Owned(text)` is implicit via the
190 /// `String: Into<Cow<'static, str>>` bound, but spelled out for
191 /// consistency.)
192 ///
193 /// # Returns
194 ///
195 /// - `VirtualNode` - A text virtual node.
196 ///
197 /// # Arguments
198 ///
199 /// - `i32` - Input value to convert from.
200 fn from(value: i32) -> Self {
201 VirtualNode::Text(TextNode::new(Cow::Owned(value.to_string()), None))
202 }
203}
204
205/// Converts a `usize` into a text virtual node.
206impl From<usize> for VirtualNode {
207 /// Converts this unsigned integer into a text virtual node.
208 ///
209 /// # Returns
210 ///
211 /// - `VirtualNode` - A text virtual node.
212 ///
213 /// # Arguments
214 ///
215 /// - `usize` - Input value to convert from.
216 fn from(value: usize) -> Self {
217 VirtualNode::Text(TextNode::new(Cow::Owned(value.to_string()), None))
218 }
219}
220
221/// Converts a `bool` into a text virtual node.
222impl From<bool> for VirtualNode {
223 /// Converts this boolean into a text virtual node.
224 ///
225 /// # Returns
226 ///
227 /// - `VirtualNode` - A text virtual node.
228 ///
229 /// # Arguments
230 ///
231 /// - `bool` - Input value to convert from.
232 fn from(value: bool) -> Self {
233 VirtualNode::Text(TextNode::new(Cow::Owned(value.to_string()), None))
234 }
235}
236
237/// Converts a signal into a reactive text virtual node.
238impl<T> From<Signal<T>> for VirtualNode
239where
240 T: Clone + PartialEq + Display + 'static,
241{
242 /// Converts this signal into a reactive text virtual node.
243 ///
244 /// # Returns
245 ///
246 /// - `VirtualNode` - A reactive text virtual node.
247 ///
248 /// # Arguments
249 ///
250 /// - `Signal<T>` - Input value to convert from.
251 fn from(signal: Signal<T>) -> Self {
252 signal.as_reactive_text()
253 }
254}
255
256/// Converts a signal into a reactive text node with listener wiring.
257impl<T> AsReactiveText for Signal<T>
258where
259 T: Clone + PartialEq + Display + 'static,
260{
261 /// Creates a reactive text node that auto-updates when the signal changes.
262 ///
263 /// The returned `TextNode` carries a binder closure instead of an
264 /// intermediate bridge signal. The binder runs exactly once per DOM text
265 /// node at materialization time: it subscribes the source signal directly
266 /// to that node (converting `T` to `String` on every change). The
267 /// listener self-unsubscribes the first time it observes the node as
268 /// detached — a removed text node never comes back (patch materialises
269 /// a fresh `Text` per mount), so without self-unsubscribe the listener
270 /// would live on the source signal's list forever and pay one
271 /// `is_connected` crossing per set per dead node. Mid-notification
272 /// unsubscribe is deferred through `removed_listener_ids`, so the
273 /// self-removal is re-entrancy-safe. A kept text node keeps its single
274 /// subscription across any number of re-renders of its parent.
275 ///
276 /// # Returns
277 ///
278 /// - `VirtualNode` - A text virtual node with reactive signal binding.
279 fn as_reactive_text(&self) -> VirtualNode {
280 let source: Signal<T> = *self;
281 let binder: Rc<dyn Fn(&Text)> = Rc::new(move |text: &Text| {
282 let text_node: Text = text.clone();
283 let subscription_id: Rc<Cell<usize>> = Rc::new(Cell::new(usize::MAX));
284 let listener_id: Rc<Cell<usize>> = subscription_id.clone();
285 let id: usize = source.subscribe(move || {
286 if !text_node.is_connected() {
287 source.unsubscribe(listener_id.get());
288 return;
289 }
290 let value: String = source.with(|current: &T| current.to_string());
291 text_node.set_text_content(Some(&value));
292 });
293 subscription_id.set(id);
294 });
295 VirtualNode::Text(TextNode::new(
296 Cow::Owned(source.with(|current: &T| current.to_string())),
297 Some(binder),
298 ))
299 }
300}
301
302/// Constructs an `EventAdapter` that wraps any event-compatible value.
303impl<T> EventAdapter<T> {
304 /// Returns the inner wrapped value, consuming the adapter.
305 ///
306 /// # Returns
307 ///
308 /// - `T` - The inner value.
309 pub(crate) fn into_inner(self) -> T {
310 let Self { inner }: Self = self;
311 inner
312 }
313}
314
315/// Adapts a `FnMut(Event)` closure into an `AttributeValue::Event`.
316///
317/// Wraps the closure into a `NativeEventHandler` and returns it as an
318/// event attribute value. This replaces the `__EventWrapper<F>` type
319/// that was previously generated inline by the `html!` macro.
320impl<F> EventAdapter<F>
321where
322 F: FnMut(Event) + 'static,
323{
324 /// Converts the wrapped closure into an event `AttributeValue`.
325 ///
326 /// # Arguments
327 ///
328 /// - `&'static str` - The event name string to associate with the handler.
329 ///
330 /// # Returns
331 ///
332 /// - `AttributeValue` - An `AttributeValue::Event` wrapping the handler.
333 pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
334 AttributeValue::Event(NativeEventHandler::create(event_name, self.into_inner()))
335 }
336}
337
338/// Converts an event with a specific event name into an `AttributeValue`.
339impl<F> From<EventNamedAdapter<F>> for AttributeValue
340where
341 F: FnMut(Event) + 'static,
342{
343 /// Converts the wrapped closure with event name into an event `AttributeValue`.
344 ///
345 /// # Returns
346 ///
347 /// - `AttributeValue` - An `AttributeValue::Event` wrapping the handler.
348 ///
349 /// # Arguments
350 ///
351 /// - `EventNamedAdapter<F>` - Input value to convert from.
352 fn from(adapter: EventNamedAdapter<F>) -> Self {
353 AttributeValue::Event(NativeEventHandler::create(
354 adapter.get_event_name(),
355 adapter.inner,
356 ))
357 }
358}
359
360/// Converts an event named adapter with `NativeEventHandler` into an `AttributeValue`.
361impl From<EventNamedAdapter<NativeEventHandler>> for AttributeValue {
362 /// Converts the wrapped handler with event name into an event `AttributeValue`.
363 ///
364 /// # Returns
365 ///
366 /// - `AttributeValue` - An `AttributeValue::Event` wrapping the handler.
367 ///
368 /// # Arguments
369 ///
370 /// - `EventNamedAdapter<NativeEventHandler>` - Input value to convert from.
371 fn from(mut adapter: EventNamedAdapter<NativeEventHandler>) -> Self {
372 let event_name: &'static str = adapter.get_event_name();
373 adapter.get_mut_inner().set_event_name(event_name);
374 AttributeValue::Event(adapter.inner)
375 }
376}
377
378/// Converts an event named adapter with optional shared closure into an `AttributeValue`.
379///
380/// `Some(callback)` becomes `AttributeValue::Event` by wrapping the shared closure
381/// into a `NativeEventHandler` with the adapter's event name, and `None` becomes
382/// `AttributeValue::Text(String::new())`.
383impl From<EventNamedAdapter<Option<Rc<dyn Fn(Event)>>>> for AttributeValue {
384 /// Converts the wrapped optional shared closure with event name into an event `AttributeValue`.
385 ///
386 /// # Returns
387 ///
388 /// - `AttributeValue` - An event attribute if `Some`, otherwise an empty text attribute.
389 ///
390 /// # Arguments
391 ///
392 /// - `EventNamedAdapter<Option<Rc<dyn Fn(Event)>>>` - Input value to convert from.
393 fn from(adapter: EventNamedAdapter<Option<Rc<dyn Fn(Event)>>>) -> Self {
394 let event_name: &'static str = adapter.get_event_name();
395 match adapter.inner {
396 Some(callback) => AttributeValue::Event(NativeEventHandler::create(
397 event_name,
398 move |event: Event| {
399 callback(event);
400 },
401 )),
402 None => AttributeValue::Text(String::new()),
403 }
404 }
405}
406
407/// Adapts an owned `NativeEventHandler` into an `AttributeValue::Event` directly.
408///
409/// When the user already provides a `NativeEventHandler`, the handler is
410/// re-wrapped with the given `event_name` to ensure the DOM event listener
411/// is bound to the correct event type (e.g., "click" rather than "onclick").
412impl EventAdapter<NativeEventHandler> {
413 /// Converts the wrapped handler into an event `AttributeValue`.
414 ///
415 /// Re-wraps the handler with the provided `event_name` so that the
416 /// DOM event listener uses the correct event type string.
417 ///
418 /// # Arguments
419 ///
420 /// - `&'static str` - The event name to bind the handler to.
421 ///
422 /// # Returns
423 ///
424 /// - `AttributeValue` - An `AttributeValue::Event` containing the re-wrapped handler.
425 pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
426 let mut handler: NativeEventHandler = self.into_inner();
427 handler.set_event_name(event_name);
428 AttributeValue::Event(handler)
429 }
430}
431
432/// Adapts an `Option<NativeEventHandler>` into an `AttributeValue`.
433///
434/// `Some(handler)` becomes `AttributeValue::Event(handler)` re-wrapped with the
435/// given event name, and `None` becomes `AttributeValue::Text(String::new())`.
436impl EventAdapter<Option<NativeEventHandler>> {
437 /// Converts the wrapped optional handler into an attribute value.
438 ///
439 /// Re-wraps a `Some` handler with the provided `event_name` so that the
440 /// DOM event listener uses the correct event type string.
441 ///
442 /// # Arguments
443 ///
444 /// - `&'static str` - The event name to bind the handler to.
445 ///
446 /// # Returns
447 ///
448 /// - `AttributeValue` - An event attribute if `Some`, otherwise an empty text attribute.
449 pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
450 match self.into_inner() {
451 Some(handler) => EventNamedAdapter::new(handler, event_name).into(),
452 None => AttributeValue::Text(String::new()),
453 }
454 }
455}
456
457/// Adapts an `Option<Rc<dyn Fn(Event)>>` into an `AttributeValue`.
458///
459/// `Some(callback)` becomes `AttributeValue::Event` by wrapping the shared closure
460/// into a `NativeEventHandler`, and `None` becomes `AttributeValue::Text(String::new())`.
461/// This supports component Props that use `Option<Rc<dyn Fn(Event)>>` for event callbacks.
462impl EventAdapter<Option<Rc<dyn Fn(Event)>>> {
463 /// Converts the wrapped optional shared closure into an attribute value.
464 ///
465 /// # Arguments
466 ///
467 /// - `&'static str` - The event name to bind the handler to.
468 ///
469 /// # Returns
470 ///
471 /// - `AttributeValue` - An event attribute if `Some`, otherwise an empty text attribute.
472 pub fn into_attribute(self, event_name: &'static str) -> AttributeValue {
473 match self.into_inner() {
474 Some(callback) => AttributeValue::Event(NativeEventHandler::create(
475 event_name,
476 move |event: Event| {
477 callback(event);
478 },
479 )),
480 None => AttributeValue::Text(String::new()),
481 }
482 }
483}
484
485/// Constructs an `AttrValueAdapter` that wraps any attribute-compatible value.
486impl<T> AttrValueAdapter<T> {
487 /// Returns the inner wrapped value, consuming the adapter.
488 ///
489 /// # Returns
490 ///
491 /// - `T` - The inner value.
492 pub(crate) fn into_inner(self) -> T {
493 let Self { inner }: Self = self;
494 inner
495 }
496}
497
498/// Constructs an `InnerHtmlAdapter` that wraps an `inner_html:` payload.
499impl<T> InnerHtmlAdapter<T> {
500 /// Returns the inner wrapped value, consuming the adapter.
501 ///
502 /// Mirrors [`AttrValueAdapter::into_inner`] so the html! macro can
503 /// use the same "wrap-then-into-inner" pattern for both adapter
504 /// kinds without diverging call sites.
505 ///
506 /// # Returns
507 ///
508 /// - `T` - The inner value.
509 pub(crate) fn into_inner(self) -> T {
510 let Self { inner }: Self = self;
511 inner
512 }
513}
514
515/// Adapts a `FnMut(Event)` closure into a callback `AttributeValue`.
516///
517/// This handles the case where a closure is used as a component callback prop.
518/// The closure is converted via `IntoCallbackAttribute::into_callback_attribute()`.
519impl<F> AttrValueAdapter<F>
520where
521 F: FnMut(Event) + 'static,
522{
523 /// Converts the wrapped closure into a callback `AttributeValue`.
524 ///
525 /// # Returns
526 ///
527 /// - `AttributeValue` - An event attribute value wrapping the adapted closure.
528 pub fn into_callback(self) -> AttributeValue {
529 self.into_inner().into()
530 }
531
532 /// Converts the wrapped closure into a callback `AttributeValue` with a
533 /// custom event name for component props.
534 ///
535 /// # Arguments
536 ///
537 /// - `&'static str` - The custom attribute name (e.g., "on-increment", "on-change").
538 ///
539 /// # Returns
540 ///
541 /// - `AttributeValue` - An event attribute value with the custom name.
542 pub fn into_callback_named(self, name: &'static str) -> AttributeValue {
543 AttributeValue::Event(NativeEventHandler::create(name, self.into_inner()))
544 }
545}
546
547/// Converts a named callback adapter into an `AttributeValue`.
548impl<F> From<CallbackNamedAdapter<F>> for AttributeValue
549where
550 F: FnMut(Event) + 'static,
551{
552 /// Converts the wrapped closure with custom name into a callback `AttributeValue`.
553 ///
554 /// # Returns
555 ///
556 /// - `AttributeValue` - An event attribute value with the custom name.
557 ///
558 /// # Arguments
559 ///
560 /// - `CallbackNamedAdapter<F>` - Input value to convert from.
561 fn from(adapter: CallbackNamedAdapter<F>) -> Self {
562 AttributeValue::Event(NativeEventHandler::create(
563 adapter.get_name(),
564 adapter.inner,
565 ))
566 }
567}
568
569/// Generic-parameterised implementation for [`AttrValueAdapter`].
570/// Implements `impl AttrValueAdapter<NativeEventHandler>`.
571impl AttrValueAdapter<NativeEventHandler> {
572 /// Converts the wrapped handler into a callback `AttributeValue` with a
573 /// custom event name for component props.
574 ///
575 /// # Arguments
576 ///
577 /// - `&'static str` - The custom attribute name.
578 ///
579 /// # Returns
580 ///
581 /// - `AttributeValue` - An event attribute value with the custom name.
582 pub fn into_callback_named(self, name: &'static str) -> AttributeValue {
583 let mut handler: NativeEventHandler = self.into_inner();
584 handler.set_event_name(name);
585 AttributeValue::Event(handler)
586 }
587}
588
589/// Adapts an `Option<NativeEventHandler>` into an `AttributeValue`.
590impl AttrValueAdapter<Option<NativeEventHandler>> {
591 /// Converts the wrapped optional handler into an attribute value.
592 ///
593 /// # Returns
594 ///
595 /// - `AttributeValue` - An event attribute if `Some`, otherwise an empty text attribute.
596 pub fn into_callback(self) -> AttributeValue {
597 match self.into_inner() {
598 Some(handler) => AttrValueAdapter::new(handler).into(),
599 None => AttributeValue::Text(String::new()),
600 }
601 }
602
603 /// Converts this optional handler into a callback `AttributeValue` with a
604 /// custom event name for component props.
605 ///
606 /// # Arguments
607 ///
608 /// - `&'static str` - The custom attribute name.
609 ///
610 /// # Returns
611 ///
612 /// - `AttributeValue` - An event attribute with the custom name if `Some`,
613 /// otherwise an empty text attribute.
614 pub fn into_callback_named(self, name: &'static str) -> AttributeValue {
615 match self.into_inner() {
616 Some(handler) => AttrValueAdapter::new(handler).into_callback_named(name),
617 None => AttributeValue::Text(String::new()),
618 }
619 }
620}
621
622/// Adapts any type that implements `Into<AttributeValue>` into an `AttributeValue`.
623///
624/// This is the fallback path for non-closure attribute values (strings, signals,
625/// CSS classes, etc.).
626impl<T> From<AttrValueAdapter<T>> for AttributeValue
627where
628 T: Into<AttributeValue>,
629{
630 /// Converts the wrapped value into an `AttributeValue`.
631 ///
632 /// # Returns
633 ///
634 /// - `AttributeValue` - The reactive attribute value.
635 ///
636 /// # Arguments
637 ///
638 /// - `AttrValueAdapter<T>` - Input value to convert from.
639 fn from(adapter: AttrValueAdapter<T>) -> Self {
640 adapter.into_inner().into()
641 }
642}
643
644/// Adapts an `inner_html:` payload into the matching `AttributeValue`
645/// variant, routing through `set_inner_html` instead of the generic
646/// `Text` attribute path.
647///
648/// The two blanket impls below cover the user-visible call sites:
649///
650/// - `String` produces
651/// `AttributeValue::InnerHtml(String::from("..."))`.
652/// - `Signal<String>` produces
653/// `AttributeValue::InnerHtmlSignal(signal)`.
654///
655/// Each impl only requires its specific source type, so the compiler
656/// picks the right one based on the inferred `T` at the call site
657/// (no manual `.into()` annotation needed).
658impl From<InnerHtmlAdapter<String>> for AttributeValue {
659 /// Wraps the static `String` payload in an
660 /// `AttributeValue::InnerHtml` variant so the renderer can call
661 /// `Element::set_inner_html` on it.
662 ///
663 /// # Arguments
664 ///
665 /// - `InnerHtmlAdapter<String>` - Input value to convert from.
666 fn from(adapter: InnerHtmlAdapter<String>) -> Self {
667 AttributeValue::InnerHtml(adapter.into_inner())
668 }
669}
670
671/// `From` conversion into [`AttributeValue`].
672impl From<InnerHtmlAdapter<&str>> for AttributeValue {
673 /// Wraps the static `&str` payload by allocating a new `String`
674 /// so the renderer owns the data independently of the caller's
675 /// borrow lifetime.
676 ///
677 /// # Arguments
678 ///
679 /// - `InnerHtmlAdapter<&str>` - Input value to convert from.
680 fn from(adapter: InnerHtmlAdapter<&str>) -> Self {
681 AttributeValue::InnerHtml(adapter.into_inner().to_owned())
682 }
683}
684
685/// `From` conversion into [`AttributeValue`].
686impl From<InnerHtmlAdapter<Signal<String>>> for AttributeValue {
687 /// Wraps the reactive payload in an `AttributeValue::InnerHtmlSignal`
688 /// so the renderer subscribes to the signal and re-applies
689 /// `set_inner_html` on every change.
690 ///
691 /// # Arguments
692 ///
693 /// - `InnerHtmlAdapter<Signal<String>>` - Input value to convert from.
694 fn from(adapter: InnerHtmlAdapter<Signal<String>>) -> Self {
695 AttributeValue::InnerHtmlSignal(adapter.into_inner())
696 }
697}
698
699/// Converts a `NodeRef<T>` into an `AttributeValue::Ref`.
700///
701/// The type parameter `T` is erased at the `AttributeValue` layer (we
702/// store the underlying `JsValue` cell), so any concrete element type
703/// works. The `html!` macro relies on this to accept
704/// `html! { input { ref: my_ref } }` without the user needing to call
705/// `.into()` explicitly.
706impl<T: ?Sized> From<NodeRef<T>> for AttributeValue {
707 /// Wraps the ref cell as an `AttributeValue::Ref` so the renderer
708 /// can populate it after mount.
709 ///
710 /// # Returns
711 ///
712 /// - `AttributeValue` - A ref variant carrying the (still empty)
713 /// handle.
714 ///
715 /// # Arguments
716 ///
717 /// - `NodeRef<T>` - Input value to convert from.
718 fn from(node_ref: NodeRef<T>) -> Self {
719 // Erase the typed phantom marker to a `NodeRef<JsValue>` so the
720 // `AttributeValue::Ref` payload is uniform. The `JsValue` cell
721 // is what the renderer actually stores and sets.
722 let erased: NodeRefDyn = NodeRefDyn {
723 inner: node_ref.inner.clone(),
724 _marker: PhantomData,
725 };
726 AttributeValue::Ref(erased)
727 }
728}