on df5dadf help sign in
dodrio/src/node.rs
204 lines 5.5 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> Attribute<'a> {
74 /// Certain attributes are considered "volatile" and can change via user
75 /// input that we can't see when diffing against the old virtual DOM. For
76 /// these attributes, we want to always re-set the attribute on the physical
77 /// DOM node, even if the old and new virtual DOM nodes have the same value.
78 #[inline]
79 pub(crate) fn is_volatile(&self) -> bool {
80 match self.name {
81 "value" | "checked" | "selected" => true,
82 _ => false,
83 }
84 }
87impl<'a> Node<'a> {
88 /// Construct a new element node with the given tag name and children.
89 #[inline]
90 pub fn element<Listeners, Attributes, Children>(
91 bump: &'a Bump,
92 tag_name: &'a str,
93 listeners: Listeners,
94 attributes: Attributes,
95 children: Children,
96 ) -> Node<'a>
97 where
98 Listeners: 'a + AsRef<[Listener<'a>]>,
99 Attributes: 'a + AsRef<[Attribute<'a>]>,
100 Children: 'a + AsRef<[Node<'a>]>,
101 {
102 let children: &'a Children = bump.alloc(children);
103 let children: &'a [Node<'a>] = children.as_ref();
105 let listeners: &'a Listeners = bump.alloc(listeners);
106 let listeners: &'a [Listener<'a>] = listeners.as_ref();
108 let attributes: &'a Attributes = bump.alloc(attributes);
109 let attributes: &'a [Attribute<'a>] = attributes.as_ref();
111 Node::Element(ElementNode {
112 tag_name,
113 listeners,
114 attributes,
115 children,
116 })
117 }
119 /// Is this node a text node?
120 pub fn is_text(&self) -> bool {
121 match self {
122 Node::Text(_) => true,
123 _ => false,
124 }
125 }
127 /// Is this node an element?
128 pub fn is_element(&self) -> bool {
129 match self {
130 Node::Element { .. } => true,
131 _ => false,
132 }
133 }
135 /// Construct a new text node with the given text.
136 #[inline]
137 pub fn text(text: &'a str) -> Node<'a> {
138 Node::Text(TextNode { text })
139 }
142impl<'a> TextNode<'a> {
143 /// Get this text node's text content.
144 pub fn text(&self) -> &'a str {
145 self.text
146 }
149impl<'a> ElementNode<'a> {
150 /// Get this element's tag name.
151 pub fn tag_name(&self) -> &'a str {
152 self.tag_name
153 }
155 /// Get this element's attributes.
156 pub fn attributes(&self) -> &'a [Attribute<'a>] {
157 self.attributes
158 }
160 /// Get this element's attributes.
161 pub fn children(&self) -> &'a [Node<'a>] {
162 self.children
163 }
166union CallbackFatPtr<'a> {
167 callback: ListenerCallback<'a>,
168 parts: (u32, u32),
171impl Listener<'_> {
172 #[inline]
173 pub(crate) fn get_callback_parts(&self) -> (u32, u32) {
174 assert_eq!(
175 mem::size_of::<ListenerCallback>(),
176 mem::size_of::<CallbackFatPtr>()
177 );
179 unsafe {
180 let fat = CallbackFatPtr {
181 callback: self.callback,
182 };
183 let (a, b) = fat.parts;
184 debug_assert!(a != 0);
185 (a, b)
186 }
187 }
190/// Utility function for creating event listeners and downcasting the component
191/// to its `RootRender` concrete type.
192pub fn on<'a, F>(bump: &'a Bump, event: &'a str, callback: F) -> Listener<'a>
193where
194 F: Fn(&mut dyn RootRender, VdomWeak, web_sys::Event) + 'static,
196 Listener {
197 event,
198 callback: bump.alloc(
199 move |component: &mut dyn RootRender, vdom: VdomWeak, event: web_sys::Event| {
200 callback(component, vdom, event);
201 },
202 ),
203 }