on 25dcb1c help sign in
dodrio/src/node.rs
190 lines 5.0 kB history raw
1use crate::{RootRender, VdomWeak};
2use bumpalo::Bump;
3use std::fmt;
4use std::mem;
6/// A node is either a text node or an element.
7#[derive(Debug, Clone)]
8pub enum Node<'a> {
9 /// A text node.
10 Text(TextNode<'a>),
12 /// An element potentially with attributes and children.
13 Element(ElementNode<'a>),
16/// Text nodes are just a string of text. They cannot have attributes or
17/// children.
18#[derive(Debug, Clone)]
19pub struct TextNode<'a> {
20 pub(crate) text: &'a str,
23/// Elements have a tag name, zero or more attributes, and zero or more
24/// children.
25#[derive(Debug, Clone)]
26pub struct ElementNode<'a> {
27 pub(crate) tag_name: &'a str,
28 pub(crate) listeners: &'a [Listener<'a>],
29 pub(crate) attributes: &'a [Attribute<'a>],
30 pub(crate) children: &'a [Node<'a>],
33/// An event listener callback function.
34///
35/// It takes three parameters:
36///
37/// 1. The virtual DOM's root rendering component.
38/// 2. A capability to scheduler virtual DOM re-rendering.
39/// 3. The event that ocurred.
40pub type ListenerCallback<'a> =
41 &'a (dyn Fn(&mut dyn RootRender, VdomWeak, web_sys::Event) + 'static);
43/// An event listener.
44pub struct Listener<'a> {
45 /// The type of event to listen for.
46 pub event: &'a str,
47 /// The callback to invoke when the event happens.
48 pub callback: ListenerCallback<'a>,
51/// An attribute on a DOM node, such as `id="my-thing"` or
52/// `href="https://example.com"`.
53#[derive(Clone, Debug)]
54pub struct Attribute<'a> {
55 /// The attribute name, such as `id`.
56 pub name: &'a str,
57 /// The attribute value, such as `"my-thing"`.
58 pub value: &'a str,
61impl fmt::Debug for Listener<'_> {
62 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63 let (a, b) = self.get_callback_parts();
64 let a = a as *mut u32;
65 let b = b as *mut u32;
66 f.debug_struct("Listener")
67 .field("event", &self.event)
68 .field("callback", &(a, b))
69 .finish()
70 }
73impl<'a> Node<'a> {
74 /// Construct a new element node with the given tag name and children.
75 #[inline]
76 pub fn element<Listeners, Attributes, Children>(
77 bump: &'a Bump,
78 tag_name: &'a str,
79 listeners: Listeners,
80 attributes: Attributes,
81 children: Children,
82 ) -> Node<'a>
83 where
84 Listeners: 'a + AsRef<[Listener<'a>]>,
85 Attributes: 'a + AsRef<[Attribute<'a>]>,
86 Children: 'a + AsRef<[Node<'a>]>,
87 {
88 let children: &'a Children = bump.alloc(children);
89 let children: &'a [Node<'a>] = children.as_ref();
91 let listeners: &'a Listeners = bump.alloc(listeners);
92 let listeners: &'a [Listener<'a>] = listeners.as_ref();
94 let attributes: &'a Attributes = bump.alloc(attributes);
95 let attributes: &'a [Attribute<'a>] = attributes.as_ref();
97 Node::Element(ElementNode {
98 tag_name,
99 listeners,
100 attributes,
101 children,
102 })
103 }
105 /// Is this node a text node?
106 pub fn is_text(&self) -> bool {
107 match self {
108 Node::Text(_) => true,
109 _ => false,
110 }
111 }
113 /// Is this node an element?
114 pub fn is_element(&self) -> bool {
115 match self {
116 Node::Element { .. } => true,
117 _ => false,
118 }
119 }
121 /// Construct a new text node with the given text.
122 #[inline]
123 pub fn text(text: &'a str) -> Node<'a> {
124 Node::Text(TextNode { text })
125 }
128impl<'a> TextNode<'a> {
129 /// Get this text node's text content.
130 pub fn text(&self) -> &'a str {
131 self.text
132 }
135impl<'a> ElementNode<'a> {
136 /// Get this element's tag name.
137 pub fn tag_name(&self) -> &'a str {
138 self.tag_name
139 }
141 /// Get this element's attributes.
142 pub fn attributes(&self) -> &'a [Attribute<'a>] {
143 self.attributes
144 }
146 /// Get this element's attributes.
147 pub fn children(&self) -> &'a [Node<'a>] {
148 self.children
149 }
152union CallbackFatPtr<'a> {
153 callback: ListenerCallback<'a>,
154 parts: (u32, u32),
157impl Listener<'_> {
158 #[inline]
159 pub(crate) fn get_callback_parts(&self) -> (u32, u32) {
160 assert_eq!(
161 mem::size_of::<ListenerCallback>(),
162 mem::size_of::<CallbackFatPtr>()
163 );
165 unsafe {
166 let fat = CallbackFatPtr {
167 callback: self.callback,
168 };
169 let (a, b) = fat.parts;
170 debug_assert!(a != 0);
171 (a, b)
172 }
173 }
176/// Utility function for creating event listeners and downcasting the component
177/// to its `RootRender` concrete type.
178pub fn on<'a, F>(bump: &'a Bump, event: &'a str, callback: F) -> Listener<'a>
179where
180 F: Fn(&mut dyn RootRender, VdomWeak, web_sys::Event) + 'static,
182 Listener {
183 event,
184 callback: bump.alloc(
185 move |component: &mut dyn RootRender, vdom: VdomWeak, event: web_sys::Event| {
186 callback(component, vdom, event);
187 },
188 ),
189 }