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>],
31 pub(crate) namespace: Option<&'a str>,
34/// An event listener callback function.
36/// It takes three parameters:
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);
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))
72impl<'a> Attribute<'a> {
73 /// Get this attribute's name, such as `"id"` in `<div id="my-thing" />`.
75 pub fn name(&self) -> &'a str {
79 /// The attribute value, such as `"my-thing"` in `<div id="my-thing" />`.
81 pub fn value(&self) -> &'a str {
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.
90 pub(crate) fn is_volatile(&self) -> bool {
92 "value" | "checked" | "selected" => true,
99 /// Construct a new Node of type element with given tag name and children
101 pub(crate) fn element<Listeners, Attributes, Children>(
104 listeners: Listeners,
105 attributes: Attributes,
107 namespace: Option<&'a str>,
110 Listeners: 'a + AsRef<[Listener<'a>]>,
111 Attributes: 'a + AsRef<[Attribute<'a>]>,
112 Children: 'a + AsRef<[Node<'a>]>,
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 {
132 /// Is this node a text node?
133 pub fn is_text(&self) -> bool {
135 Node::Text(_) => true,
140 /// Is this node an element?
141 pub fn is_element(&self) -> bool {
143 Node::Element { .. } => true,
148 /// Construct a new text node with the given text.
150 pub(crate) fn text(text: &'a str) -> Node<'a> {
151 Node::Text(TextNode { text })
155impl<'a> TextNode<'a> {
156 /// Get this text node's text content.
157 pub fn text(&self) -> &'a str {
162impl<'a> ElementNode<'a> {
163 /// Get this element's tag name.
164 pub fn tag_name(&self) -> &'a str {
168 /// Get this element's attributes.
169 pub fn attributes(&self) -> &'a [Attribute<'a>] {
173 /// Get this element's attributes.
174 pub fn children(&self) -> &'a [Node<'a>] {
179union CallbackFatPtr<'a> {
180 callback: ListenerCallback<'a>,
186 pub(crate) fn get_callback_parts(&self) -> (u32, u32) {
188 mem::size_of::<ListenerCallback>(),
189 mem::size_of::<CallbackFatPtr>()
193 let fat = CallbackFatPtr {
194 callback: self.callback,
196 let (a, b) = fat.parts;
197 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