dioxus_native_core/
node.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//! Items related to Nodes in the RealDom

use rustc_hash::{FxHashMap, FxHashSet};
use shipyard::Component;
use std::{
    any::Any,
    fmt::{Debug, Display},
};

/// A element node in the RealDom
#[derive(Debug, Clone, Default)]
pub struct ElementNode<V: FromAnyValue = ()> {
    /// The [tag](https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName) of the element
    pub tag: String,
    /// The [namespace](https://developer.mozilla.org/en-US/docs/Web/API/Element/namespaceURI) of the element
    pub namespace: Option<String>,
    /// The attributes of the element
    pub attributes: FxHashMap<OwnedAttributeDiscription, OwnedAttributeValue<V>>,
    /// The events the element is listening for
    pub listeners: FxHashSet<String>,
}

impl ElementNode {
    /// Create a new element node
    pub fn new(tag: impl Into<String>, namespace: impl Into<Option<String>>) -> Self {
        Self {
            tag: tag.into(),
            namespace: namespace.into(),
            attributes: Default::default(),
            listeners: Default::default(),
        }
    }
}

/// A text node in the RealDom
#[derive(Debug, Clone, Default)]
pub struct TextNode {
    /// The text of the node
    pub text: String,
    /// The events the node is listening for
    pub listeners: FxHashSet<String>,
}

impl TextNode {
    /// Create a new text node
    pub fn new(text: String) -> Self {
        Self {
            text,
            listeners: Default::default(),
        }
    }
}

/// A type of node with data specific to the node type.
#[derive(Debug, Clone, Component)]
pub enum NodeType<V: FromAnyValue = ()> {
    /// A text node
    Text(TextNode),
    /// An element node
    Element(ElementNode<V>),
    /// A placeholder node. This can be used as a cheaper placeholder for a node that will be created later
    Placeholder,
}

impl<V: FromAnyValue, S: Into<String>> From<S> for NodeType<V> {
    fn from(text: S) -> Self {
        Self::Text(TextNode::new(text.into()))
    }
}

impl<V: FromAnyValue> From<TextNode> for NodeType<V> {
    fn from(text: TextNode) -> Self {
        Self::Text(text)
    }
}

impl<V: FromAnyValue> From<ElementNode<V>> for NodeType<V> {
    fn from(element: ElementNode<V>) -> Self {
        Self::Element(element)
    }
}

/// A discription of an attribute on a DOM node, such as `id` or `href`.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct OwnedAttributeDiscription {
    /// The name of the attribute.
    pub name: String,
    /// The namespace of the attribute used to identify what kind of attribute it is.
    ///
    /// For renderers that use HTML, this can be used to identify if the attribute is a style attribute.
    /// Instead of parsing the style attribute every time a style is changed, you can set an attribute with the `style` namespace.
    pub namespace: Option<String>,
}

impl From<String> for OwnedAttributeDiscription {
    fn from(name: String) -> Self {
        Self {
            name,
            namespace: None,
        }
    }
}

impl<S: Into<String>, N: Into<String>> From<(S, N)> for OwnedAttributeDiscription {
    fn from(name: (S, N)) -> Self {
        Self {
            name: name.0.into(),
            namespace: Some(name.1.into()),
        }
    }
}

/// An attribute on a DOM node, such as `id="my-thing"` or
/// `href="https://example.com"`.
#[derive(Clone, Copy, Debug)]
pub struct OwnedAttributeView<'a, V: FromAnyValue = ()> {
    /// The discription of the attribute.
    pub attribute: &'a OwnedAttributeDiscription,

    /// The value of the attribute.
    pub value: &'a OwnedAttributeValue<V>,
}

/// The value of an attribute on a DOM node. This contains non-text values to allow users to skip parsing attribute values in some cases.
#[derive(Clone)]
pub enum OwnedAttributeValue<V: FromAnyValue = ()> {
    /// A string value. This is the most common type of attribute.
    Text(String),
    /// A floating point value.
    Float(f64),
    /// An integer value.
    Int(i64),
    /// A boolean value.
    Bool(bool),
    /// A custom value specific to the renderer
    Custom(V),
}

impl<V: FromAnyValue> From<String> for OwnedAttributeValue<V> {
    fn from(value: String) -> Self {
        Self::Text(value)
    }
}

impl<V: FromAnyValue> From<f64> for OwnedAttributeValue<V> {
    fn from(value: f64) -> Self {
        Self::Float(value)
    }
}

impl<V: FromAnyValue> From<i64> for OwnedAttributeValue<V> {
    fn from(value: i64) -> Self {
        Self::Int(value)
    }
}

impl<V: FromAnyValue> From<bool> for OwnedAttributeValue<V> {
    fn from(value: bool) -> Self {
        Self::Bool(value)
    }
}

impl<V: FromAnyValue> From<V> for OwnedAttributeValue<V> {
    fn from(value: V) -> Self {
        Self::Custom(value)
    }
}

/// Something that can be converted from a borrowed [Any] value.
pub trait FromAnyValue: Clone + 'static {
    /// Convert from an [Any] value.
    fn from_any_value(value: &dyn Any) -> Self;
}

impl FromAnyValue for () {
    fn from_any_value(_: &dyn Any) -> Self {}
}

impl<V: FromAnyValue> Debug for OwnedAttributeValue<V> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Text(arg0) => f.debug_tuple("Text").field(arg0).finish(),
            Self::Float(arg0) => f.debug_tuple("Float").field(arg0).finish(),
            Self::Int(arg0) => f.debug_tuple("Int").field(arg0).finish(),
            Self::Bool(arg0) => f.debug_tuple("Bool").field(arg0).finish(),
            Self::Custom(_) => f.debug_tuple("Any").finish(),
        }
    }
}

impl<V: FromAnyValue> Display for OwnedAttributeValue<V> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Text(arg0) => f.write_str(arg0),
            Self::Float(arg0) => f.write_str(&arg0.to_string()),
            Self::Int(arg0) => f.write_str(&arg0.to_string()),
            Self::Bool(arg0) => f.write_str(&arg0.to_string()),
            Self::Custom(_) => f.write_str("custom"),
        }
    }
}

#[cfg(feature = "dioxus")]
impl<V: FromAnyValue> From<dioxus_core::BorrowedAttributeValue<'_>> for OwnedAttributeValue<V> {
    fn from(value: dioxus_core::BorrowedAttributeValue<'_>) -> Self {
        match value {
            dioxus_core::BorrowedAttributeValue::Text(text) => Self::Text(text.to_string()),
            dioxus_core::BorrowedAttributeValue::Float(float) => Self::Float(float),
            dioxus_core::BorrowedAttributeValue::Int(int) => Self::Int(int),
            dioxus_core::BorrowedAttributeValue::Bool(bool) => Self::Bool(bool),
            dioxus_core::BorrowedAttributeValue::Any(any) => Self::Custom(V::from_any_value(any.as_any())),
            dioxus_core::BorrowedAttributeValue::None => panic!("None attribute values result in removing the attribute, not converting it to a None value.")
        }
    }
}

impl<V: FromAnyValue> OwnedAttributeValue<V> {
    /// Attempt to convert the attribute value to a string.
    pub fn as_text(&self) -> Option<&str> {
        match self {
            OwnedAttributeValue::Text(text) => Some(text),
            _ => None,
        }
    }

    /// Attempt to convert the attribute value to a float.
    pub fn as_float(&self) -> Option<f64> {
        match self {
            OwnedAttributeValue::Float(float) => Some(*float),
            OwnedAttributeValue::Int(int) => Some(*int as f64),
            _ => None,
        }
    }

    /// Attempt to convert the attribute value to an integer.
    pub fn as_int(&self) -> Option<i64> {
        match self {
            OwnedAttributeValue::Float(float) => Some(*float as i64),
            OwnedAttributeValue::Int(int) => Some(*int),
            _ => None,
        }
    }

    /// Attempt to convert the attribute value to a boolean.
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            OwnedAttributeValue::Bool(bool) => Some(*bool),
            _ => None,
        }
    }

    /// Attempt to convert the attribute value to a custom value.
    pub fn as_custom(&self) -> Option<&V> {
        match self {
            OwnedAttributeValue::Custom(custom) => Some(custom),
            _ => None,
        }
    }
}