1use crate::{RootRender, VdomWeak};
7/// A node is either a text node or an element.
13 /// An element potentially with attributes and children.
14 Element(ElementNode<'a>),
17/// Text nodes are just a string of text. They cannot have attributes or
19#[derive(Debug, Clone)]
20pub struct TextNode<'a> {
21 pub(crate) text: &'a str,
24/// Elements have a tag name, zero or more attributes, and zero or more
26#[derive(Debug, Clone)]
27pub struct ElementNode<'a> {
28 pub(crate) tag_name: &'a str,
29 pub(crate) listeners: &'a [Listener<'a>],
30 pub(crate) attributes: &'a [Attribute<'a>],
31 pub(crate) children: &'a [Node<'a>],
32 pub(crate) namespace: Option<&'a str>,
35/// An event listener callback function.
37/// It takes three parameters:
39/// 1. The virtual DOM's root rendering component.
40/// 2. A capability to scheduler virtual DOM re-rendering.
41/// 3. The event that occurred.
42pub type ListenerCallback<'a> =
43 &'a (dyn Fn(&mut dyn RootRender, VdomWeak, web_sys::Event) + 'static);
46pub struct Listener<'a> {
47 /// The type of event to listen for.
48 pub(crate) event: &'a str,
49 /// The callback to invoke when the event happens.
50 pub(crate) callback: ListenerCallback<'a>,
53/// An attribute on a DOM node, such as `id="my-thing"` or
54/// `href="https://example.com"`.
55#[derive(Clone, Debug)]
56pub struct Attribute<'a> {
57 pub(crate) name: &'a str,
58 pub(crate) 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))
73impl<'a> Attribute<'a> {
74 /// Get this attribute's name, such as `"id"` in `<div id="my-thing" />`.
76 pub fn name(&self) -> &'a str {
80 /// The attribute value, such as `"my-thing"` in `<div id="my-thing" />`.
82 pub fn value(&self) -> &'a str {
86 /// Certain attributes are considered "volatile" and can change via user
87 /// input that we can't see when diffing against the old virtual DOM. For
88 /// these attributes, we want to always re-set the attribute on the physical
89 /// DOM node, even if the old and new virtual DOM nodes have the same value.
91 pub(crate) fn is_volatile(&self) -> bool {
93 "value" | "checked" | "selected" => true,
100 /// Construct a new Node of type element with given tag name and children
102 pub(crate) fn element<Listeners, Attributes, Children>(
105 listeners: Listeners,
106 attributes: Attributes,
108 namespace: Option<&'a str>,
111 Listeners: 'a + AsRef<[Listener<'a>]>,
112 Attributes: 'a + AsRef<[Attribute<'a>]>,
113 Children: 'a + AsRef<[Node<'a>]>,
115 let children: &'a Children = bump.alloc(children);
116 let children: &'a [Node<'a>] = children.as_ref();
118 let listeners: &'a Listeners = bump.alloc(listeners);
119 let listeners: &'a [Listener<'a>] = listeners.as_ref();
121 let attributes: &'a Attributes = bump.alloc(attributes);
122 let attributes: &'a [Attribute<'a>] = attributes.as_ref();
124 Node::Element(ElementNode {
133 /// Is this node a text node?
134 pub fn is_text(&self) -> bool {
136 Node::Text(_) => true,
141 /// Is this node an element?
142 pub fn is_element(&self) -> bool {
144 Node::Element { .. } => true,
149 /// Construct a new text node with the given text.
151 pub(crate) fn text(text: &'a str) -> Node<'a> {
152 Node::Text(TextNode { text })
156/// A node can become an iterator that yields the node itself once.
158/// This implementation of `IntoIterator` mostly exists to improve the
159/// `typed-html` ergonomics, where the macro invokes `.into_iter()` on the child
160/// contents of a tag. By implementing `IntoIterator` here, we avoid having to
161/// do nasty shenanigans like `<p>vec![$contents]</p>` instead of plain old
162/// `<p>$contents</p>`.
163impl<'a> IntoIterator for Node<'a> {
164 type Item = Node<'a>;
165 type IntoIter = iter::Once<Self::Item>;
167 fn into_iter(self) -> Self::IntoIter {
172impl<'a> TextNode<'a> {
173 /// Get this text node's text content.
174 pub fn text(&self) -> &'a str {
179impl<'a> ElementNode<'a> {
180 /// Get this element's tag name.
181 pub fn tag_name(&self) -> &'a str {
185 /// Get this element's attributes.
186 pub fn attributes(&self) -> &'a [Attribute<'a>] {
190 /// Get this element's attributes.
191 pub fn children(&self) -> &'a [Node<'a>] {
195 /// Get this element's namespace.
196 pub fn namespace(&self) -> Option<&'a str> {
201union CallbackFatPtr<'a> {
202 callback: ListenerCallback<'a>,
208 pub(crate) fn get_callback_parts(&self) -> (u32, u32) {
210 mem::size_of::<ListenerCallback>(),
211 mem::size_of::<CallbackFatPtr>()
215 let fat = CallbackFatPtr {
216 callback: self.callback,
218 let (a, b) = fat.parts;
219 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