on c2cb76c help sign in
dodrio/src/node.rs
201 lines 5.4 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>],
31 pub(crate) namespace: Option<&'a str>,
34/// An event listener callback function.
35///
36/// It takes three parameters:
37///
38/// 1. The virtual DOM's root rendering component.
39/// 2. A capability to scheduler virtual DOM re-rendering.
40/// 3. The event that ocurred.
41pub type ListenerCallback<'a> =
42 &'a (dyn Fn(&mut dyn RootRender, VdomWeak, web_sys::Event) + 'static);
44/// An event listener.
45pub struct Listener<'a> {
46 /// The type of event to listen for.
47 pub(crate) event: &'a str,
48 /// The callback to invoke when the event happens.
49 pub(crate) callback: ListenerCallback<'a>,
52/// An attribute on a DOM node, such as `id="my-thing"` or
53/// `href="https://example.com"`.
54#[derive(Clone, Debug)]
55pub struct Attribute<'a> {
56 pub(crate) name: &'a str,
57 pub(crate) value: &'a str,
60impl fmt::Debug for Listener<'_> {
61 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62 let (a, b) = self.get_callback_parts();
63 let a = a as *mut u32;
64 let b = b as *mut u32;
65 f.debug_struct("Listener")
66 .field("event", &self.event)
67 .field("callback", &(a, b))
68 .finish()
69 }
72impl<'a> Attribute<'a> {
73 /// Get this attribute's name, such as `"id"` in `<div id="my-thing" />`.
74 #[inline]
75 pub fn name(&self) -> &'a str {
76 self.name
77 }
79 /// The attribute value, such as `"my-thing"` in `<div id="my-thing" />`.
80 #[inline]
81 pub fn value(&self) -> &'a str {
82 self.value
83 }
85 /// Certain attributes are considered "volatile" and can change via user
86 /// input that we can't see when diffing against the old virtual DOM. For
87 /// these attributes, we want to always re-set the attribute on the physical
88 /// DOM node, even if the old and new virtual DOM nodes have the same value.
89 #[inline]
90 pub(crate) fn is_volatile(&self) -> bool {
91 match self.name {
92 "value" | "checked" | "selected" => true,
93 _ => false,
94 }
95 }
98impl<'a> Node<'a> {
99 /// Construct a new Node of type element with given tag name and children
100 #[inline]
101 pub(crate) fn element<Listeners, Attributes, Children>(
102 bump: &'a Bump,
103 tag_name: &'a str,
104 listeners: Listeners,
105 attributes: Attributes,
106 children: Children,
107 namespace: Option<&'a str>,
108 ) -> Node<'a>
109 where
110 Listeners: 'a + AsRef<[Listener<'a>]>,
111 Attributes: 'a + AsRef<[Attribute<'a>]>,
112 Children: 'a + AsRef<[Node<'a>]>,
113 {
114 let children: &'a Children = bump.alloc(children);
115 let children: &'a [Node<'a>] = children.as_ref();
117 let listeners: &'a Listeners = bump.alloc(listeners);
118 let listeners: &'a [Listener<'a>] = listeners.as_ref();
120 let attributes: &'a Attributes = bump.alloc(attributes);
121 let attributes: &'a [Attribute<'a>] = attributes.as_ref();
123 Node::Element(ElementNode {
124 tag_name,
125 listeners,
126 attributes,
127 children,
128 namespace,
129 })
130 }
132 /// Is this node a text node?
133 pub fn is_text(&self) -> bool {
134 match self {
135 Node::Text(_) => true,
136 _ => false,
137 }
138 }
140 /// Is this node an element?
141 pub fn is_element(&self) -> bool {
142 match self {
143 Node::Element { .. } => true,
144 _ => false,
145 }
146 }
148 /// Construct a new text node with the given text.
149 #[inline]
150 pub(crate) fn text(text: &'a str) -> Node<'a> {
151 Node::Text(TextNode { text })
152 }
155impl<'a> TextNode<'a> {
156 /// Get this text node's text content.
157 pub fn text(&self) -> &'a str {
158 self.text
159 }
162impl<'a> ElementNode<'a> {
163 /// Get this element's tag name.
164 pub fn tag_name(&self) -> &'a str {
165 self.tag_name
166 }
168 /// Get this element's attributes.
169 pub fn attributes(&self) -> &'a [Attribute<'a>] {
170 self.attributes
171 }
173 /// Get this element's attributes.
174 pub fn children(&self) -> &'a [Node<'a>] {
175 self.children
176 }
179union CallbackFatPtr<'a> {
180 callback: ListenerCallback<'a>,
181 parts: (u32, u32),
184impl Listener<'_> {
185 #[inline]
186 pub(crate) fn get_callback_parts(&self) -> (u32, u32) {
187 assert_eq!(
188 mem::size_of::<ListenerCallback>(),
189 mem::size_of::<CallbackFatPtr>()
190 );
192 unsafe {
193 let fat = CallbackFatPtr {
194 callback: self.callback,
195 };
196 let (a, b) = fat.parts;
197 debug_assert!(a != 0);
198 (a, b)
199 }
200 }