1use crate::{RootRender, VdomWeak};
6/// A node is either a text node or an element.
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
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
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.
35/// It takes three parameters:
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);
44pub struct Listener<'a> {
45 /// The type of event to listen for.
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`.
57 /// The attribute value, such as `"my-thing"`.
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))
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.
79 pub(crate) fn is_volatile(&self) -> bool {
81 "value" | "checked" | "selected" => true,
88 /// Construct a new element node with the given tag name and children.
90 pub(crate) fn element<Listeners, Attributes, Children>(
94 attributes: Attributes,
98 Listeners: 'a + AsRef<[Listener<'a>]>,
99 Attributes: 'a + AsRef<[Attribute<'a>]>,
100 Children: 'a + AsRef<[Node<'a>]>,
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 {
119 /// Is this node a text node?
120 pub fn is_text(&self) -> bool {
122 Node::Text(_) => true,
127 /// Is this node an element?
128 pub fn is_element(&self) -> bool {
130 Node::Element { .. } => true,
135 /// Construct a new text node with the given text.
137 pub(crate) fn text(text: &'a str) -> Node<'a> {
138 Node::Text(TextNode { text })
142impl<'a> TextNode<'a> {
143 /// Get this text node's text content.
144 pub fn text(&self) -> &'a str {
149impl<'a> ElementNode<'a> {
150 /// Get this element's tag name.
151 pub fn tag_name(&self) -> &'a str {
155 /// Get this element's attributes.
156 pub fn attributes(&self) -> &'a [Attribute<'a>] {
160 /// Get this element's attributes.
161 pub fn children(&self) -> &'a [Node<'a>] {
166union CallbackFatPtr<'a> {
167 callback: ListenerCallback<'a>,
173 pub(crate) fn get_callback_parts(&self) -> (u32, u32) {
175 mem::size_of::<ListenerCallback>(),
176 mem::size_of::<CallbackFatPtr>()
180 let fat = CallbackFatPtr {
181 callback: self.callback,
183 let (a, b) = fat.parts;
184 debug_assert!(a != 0);
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>
194 F: Fn(&mut dyn RootRender, VdomWeak, web_sys::Event) + 'static,
198 callback: bump.alloc(
199 move |component: &mut dyn RootRender, vdom: VdomWeak, event: web_sys::Event| {
200 callback(component, vdom, event);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list