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.
46 pub(crate) event: &'a str,
47 /// The callback to invoke when the event happens.
48 pub(crate) 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 pub(crate) name: &'a str,
56 pub(crate) value: &'a str,
59impl fmt::Debug for Listener<'_> {
60 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61 let (a, b) = self.get_callback_parts();
62 let a = a as *mut u32;
63 let b = b as *mut u32;
64 f.debug_struct("Listener")
65 .field("event", &self.event)
66 .field("callback", &(a, b))
71impl<'a> Attribute<'a> {
72 /// Get this attribute's name, such as `"id"` in `<div id="my-thing" />`.
74 pub fn name(&self) -> &'a str {
78 /// The attribute value, such as `"my-thing"` in `<div id="my-thing" />`.
80 pub fn value(&self) -> &'a str {
84 /// Certain attributes are considered "volatile" and can change via user
85 /// input that we can't see when diffing against the old virtual DOM. For
86 /// these attributes, we want to always re-set the attribute on the physical
87 /// DOM node, even if the old and new virtual DOM nodes have the same value.
89 pub(crate) fn is_volatile(&self) -> bool {
91 "value" | "checked" | "selected" => true,
98 /// Construct a new element node with the given tag name and children.
100 pub(crate) fn element<Listeners, Attributes, Children>(
103 listeners: Listeners,
104 attributes: Attributes,
108 Listeners: 'a + AsRef<[Listener<'a>]>,
109 Attributes: 'a + AsRef<[Attribute<'a>]>,
110 Children: 'a + AsRef<[Node<'a>]>,
112 let children: &'a Children = bump.alloc(children);
113 let children: &'a [Node<'a>] = children.as_ref();
115 let listeners: &'a Listeners = bump.alloc(listeners);
116 let listeners: &'a [Listener<'a>] = listeners.as_ref();
118 let attributes: &'a Attributes = bump.alloc(attributes);
119 let attributes: &'a [Attribute<'a>] = attributes.as_ref();
121 Node::Element(ElementNode {
129 /// Is this node a text node?
130 pub fn is_text(&self) -> bool {
132 Node::Text(_) => true,
137 /// Is this node an element?
138 pub fn is_element(&self) -> bool {
140 Node::Element { .. } => true,
145 /// Construct a new text node with the given text.
147 pub(crate) fn text(text: &'a str) -> Node<'a> {
148 Node::Text(TextNode { text })
152impl<'a> TextNode<'a> {
153 /// Get this text node's text content.
154 pub fn text(&self) -> &'a str {
159impl<'a> ElementNode<'a> {
160 /// Get this element's tag name.
161 pub fn tag_name(&self) -> &'a str {
165 /// Get this element's attributes.
166 pub fn attributes(&self) -> &'a [Attribute<'a>] {
170 /// Get this element's attributes.
171 pub fn children(&self) -> &'a [Node<'a>] {
176union CallbackFatPtr<'a> {
177 callback: ListenerCallback<'a>,
183 pub(crate) fn get_callback_parts(&self) -> (u32, u32) {
185 mem::size_of::<ListenerCallback>(),
186 mem::size_of::<CallbackFatPtr>()
190 let fat = CallbackFatPtr {
191 callback: self.callback,
193 let (a, b) = fat.parts;
194 debug_assert!(a != 0);
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