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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
use super::{AttributeName, Namespace, Tag};
use crate::dom::SkipDiff;
use crate::vdom::Attribute;
use crate::vdom::AttributeValue;
use crate::vdom::Element;
use crate::vdom::Leaf;
use crate::vdom::Value;
use derive_where::derive_where;
use std::fmt;
use std::fmt::{Debug, Formatter};
/// represents a node in a virtual dom
/// A node could be an element which can contain one or more children of nodes.
/// A node could also be just a text node which contains a string
///
/// Much of the types are Generics
///
/// Namespace - is the type for the namespace, this will be &'static str when used in html based virtual dom implementation
/// Tag - is the type for the element tag, this will be &'static str when used in html based virtual
/// dom impmenentation
/// AttributeName - is the type for the attribute name, this will be &'static str when used in html based
/// virtual dom implementation
/// AttributeValue - is the type for the value of the attribute, this will be String, f64, or just another
/// generics that suits the implementing library which used mt-dom for just dom-diffing purposes
#[derive_where(Clone, Debug, PartialEq, Eq)]
pub enum Node<MSG> {
/// Element variant of a virtual node
Element(Element<MSG>),
/// A Leaf node
Leaf(Leaf<MSG>),
}
#[derive(Debug, Copy, Clone)]
pub enum Error {
AddChildrenNotAllowed,
AttributesNotAllowed,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
match self {
Self::AddChildrenNotAllowed => {
write!(f, "Adding children on this node variant is not allowed")
}
Self::AttributesNotAllowed => {
write!(
f,
"Adding or setting attibutes on this node variant is not allowed"
)
}
}
}
}
///TODO: use core::error when it will go out of nightly
impl std::error::Error for Error {}
impl<MSG> Node<MSG> {
/// consume self and return the element if it is an element variant
/// None if it is a text node
pub fn take_element(self) -> Option<Element<MSG>> {
match self {
Node::Element(element) => Some(element),
_ => None,
}
}
/// returns a reference to the Leaf if the node is a Leaf variant
pub fn leaf(&self) -> Option<&Leaf<MSG>> {
match self {
Node::Leaf(leaf) => Some(leaf),
_ => None,
}
}
/// returns true if the node is an element variant
pub fn is_element(&self) -> bool {
matches!(self, Node::Element(_))
}
/// returns true if the node is a Leaf
pub fn is_leaf(&self) -> bool {
matches!(self, Node::Leaf(_))
}
/// returns true if this is a text node
pub fn is_text(&self) -> bool {
match self {
Self::Leaf(leaf) => leaf.is_text(),
_ => false,
}
}
/// return the text if this is text node leaf
pub fn as_text(&self) -> Option<&str> {
match self {
Self::Leaf(ref leaf) => leaf.as_text(),
_ => None,
}
}
/// return the html entity if it is a symbol variant
pub fn as_symbol(&self) -> Option<&str> {
match self {
Self::Leaf(Leaf::Symbol(symbol)) => Some(symbol),
_ => None,
}
}
/// Get a mutable reference to the element, if this node is an element node
pub fn element_mut(&mut self) -> Option<&mut Element<MSG>> {
match *self {
Node::Element(ref mut element) => Some(element),
_ => None,
}
}
/// returns a reference to the element if this is an element node
pub fn element_ref(&self) -> Option<&Element<MSG>> {
match *self {
Node::Element(ref element) => Some(element),
_ => None,
}
}
/// Consume a mutable self and add a children to this node it if is an element
/// will have no effect if it is a text node.
/// This is used in building the nodes in a builder pattern
pub fn with_children(mut self, children: impl IntoIterator<Item = Node<MSG>>) -> Self {
if let Some(element) = self.element_mut() {
element.add_children(children);
} else {
panic!("Can not add children to a text node");
}
self
}
/// add children but not consume self
pub fn add_children(
&mut self,
children: impl IntoIterator<Item = Node<MSG>>,
) -> Result<(), Error> {
if let Some(element) = self.element_mut() {
element.add_children(children);
Ok(())
} else {
Err(Error::AddChildrenNotAllowed)
}
}
/// add attributes to the node and returns itself
/// this is used in view building
pub fn with_attributes(mut self, attributes: impl IntoIterator<Item = Attribute<MSG>>) -> Self {
if let Some(elm) = self.element_mut() {
elm.add_attributes(attributes);
} else {
panic!("Can not add attributes to a text node");
}
self
}
/// add attributes using a mutable reference to self
pub fn add_attributes(
&mut self,
attributes: impl IntoIterator<Item = Attribute<MSG>>,
) -> Result<(), Error> {
if let Some(elm) = self.element_mut() {
elm.add_attributes(attributes);
Ok(())
} else {
Err(Error::AttributesNotAllowed)
}
}
/// get the attributes of this node
/// returns None if it is a text node
pub fn attributes(&self) -> Option<&[Attribute<MSG>]> {
match self {
Node::Element(element) => Some(element.attributes()),
Node::Leaf(leaf) => leaf.attributes(),
}
}
/// returns the tag of this node if it is an element
/// otherwise None if it is a text node
pub fn tag(&self) -> Option<&Tag> {
if let Some(e) = self.element_ref() {
Some(&e.tag)
} else {
None
}
}
/// return the children of this node if it is an element
/// returns None if it is a text node
pub fn children(&self) -> &[Node<MSG>] {
match self {
Self::Element(elm) => elm.children(),
Self::Leaf(Leaf::StatefulComponent(comp)) => &comp.children,
_ => &[],
}
}
/// Return the count of the children of this node
pub fn children_count(&self) -> usize {
self.children().len()
}
/// return the children of this node if it is an element
/// returns None if it is a text node
pub fn children_mut(&mut self) -> Option<&mut [Node<MSG>]> {
if let Some(element) = self.element_mut() {
Some(element.children_mut())
} else {
None
}
}
/// Removes an child node from this element and returns it.
///
/// The removed child is replaced by the last child of the element's children.
///
/// # Panics
/// Panics if this is a text node
///
pub fn swap_remove_child(&mut self, index: usize) -> Node<MSG> {
match self {
Node::Element(element) => element.swap_remove_child(index),
_ => panic!("text has no child"),
}
}
/// Swaps the 2 child node in this element
///
/// # Arguments
/// * a - The index of the first child node
/// * b - The index of the second child node
///
/// # Panics
/// Panics if both `a` and `b` are out of bounds
/// Panics if this is a text node
pub fn swap_children(&mut self, a: usize, b: usize) {
match self {
Node::Element(element) => element.swap_children(a, b),
_ => panic!("text has no child"),
}
}
/// Returns the total number of nodes on this node tree, that is counting the direct and
/// indirect child nodes of this node.
pub fn node_count(&self) -> usize {
1 + self.descendant_node_count()
}
/// only count the descendant node
pub fn descendant_node_count(&self) -> usize {
let mut cnt = 0;
if let Node::Element(element) = self {
for child in element.children().iter() {
cnt += child.node_count();
}
}
cnt
}
/// remove the existing attributes and set with the new value
pub fn set_attributes(
&mut self,
attributes: impl IntoIterator<Item = Attribute<MSG>>,
) -> Result<(), Error> {
if let Some(elm) = self.element_mut() {
elm.set_attributes(attributes);
Ok(())
} else {
Err(Error::AttributesNotAllowed)
}
}
/// merge to existing attributes if the attribute name already exist
pub fn merge_attributes(
mut self,
attributes: impl IntoIterator<Item = Attribute<MSG>>,
) -> Self {
if let Some(elm) = self.element_mut() {
elm.merge_attributes(attributes);
}
self
}
/// return the attribute values of this node which match the attribute name `name`
pub fn attribute_value(&self, name: &AttributeName) -> Option<Vec<&AttributeValue<MSG>>> {
match self {
Self::Element(elm) => elm.attribute_value(name),
Self::Leaf(leaf) => leaf.attribute_value(name),
}
}
/// get the first value of the attribute which has the name `att_name` of this node
pub fn first_value(&self, att_name: &AttributeName) -> Option<&Value> {
self.attribute_value(att_name)
.and_then(|att_values| att_values.first().and_then(|v| v.get_simple()))
}
/// return the skip diff if this node has one
pub fn skip_diff(&self) -> Option<SkipDiff> {
match self {
Self::Leaf(Leaf::TemplatedView(view)) => Some((view.skip_diff)()),
_ => None,
}
}
///
pub fn unwrap_template(self) -> Node<MSG> {
match self {
Self::Leaf(Leaf::TemplatedView(view)) => *view.view,
_ => self,
}
}
///
pub fn unwrap_template_ref(&self) -> &Node<MSG> {
match self {
Self::Leaf(Leaf::TemplatedView(view)) => &view.view,
_ => self,
}
}
/// returns true if this node is a templated view
pub fn is_template(&self) -> bool {
matches!(self, Self::Leaf(Leaf::TemplatedView(_)))
}
}
/// create a virtual node with tag, attrs and children
/// # Example
/// ```rust
/// use sauron::{Node,vdom::element,attr};
///
/// let div:Node<()> = element(
/// "div",
/// vec![attr("class", "container")],
/// vec![],
/// );
/// ```
#[inline]
pub fn element<MSG>(
tag: Tag,
attrs: impl IntoIterator<Item = Attribute<MSG>>,
children: impl IntoIterator<Item = Node<MSG>>,
) -> Node<MSG> {
element_ns(None, tag, attrs, children, false)
}
/// create a virtual node with namespace, tag, attrs and children
/// # Example
/// ```rust
/// use sauron::{Node, vdom::element_ns,attr};
///
/// let svg: Node<()> = element_ns(
/// Some("http://www.w3.org/2000/svg"),
/// "svg",
/// vec![attr("width","400"), attr("height","400")],
/// vec![],
/// false
/// );
/// ```
pub fn element_ns<MSG>(
namespace: Option<Namespace>,
tag: Tag,
attrs: impl IntoIterator<Item = Attribute<MSG>>,
children: impl IntoIterator<Item = Node<MSG>>,
self_closing: bool,
) -> Node<MSG> {
Node::Element(Element::new(namespace, tag, attrs, children, self_closing))
}
/// create a leaf node
pub fn leaf<MSG>(leaf: impl Into<Leaf<MSG>>) -> Node<MSG> {
Node::Leaf(leaf.into())
}
/// create a node list
pub fn node_list<MSG>(nodes: impl IntoIterator<Item = Node<MSG>>) -> Node<MSG> {
Node::Leaf(Leaf::NodeList(nodes.into_iter().collect()))
}
/// create fragment node
pub fn fragment<MSG>(nodes: impl IntoIterator<Item = Node<MSG>>) -> Node<MSG> {
Node::Leaf(Leaf::Fragment(nodes.into_iter().collect()))
}