html_types/node.rs
1use crate::attributes;
2use crate::tag::Tag;
3use crate::text::Text;
4use attributes::Attribute;
5use derive_more::{From, Into};
6use std::collections::HashMap;
7///! A loose representation of the tree structure HTML follows
8///! This can still be used to generate 'invalid' html.
9///! Eg a br tag with children would be possible. However most browsers
10///! are incredibly lax, as is HTML somewhat, so this is at least highly useable
11///! If you know what tags you need
12///!
13///! This file attempts to be the full set of tools needed, so there is no usage
14///! Of traits allowing further 'custom' types. It is more Data driven in it's approach
15///!
16///! Note: All datastructures here are fairly public, allowing them to be manipulated
17///! as desired
18
19type Void = ();
20type Normal<'a> = Vec<Node<'a>>;
21pub type Attributes<'a> = HashMap<Attribute<'a>, Option<attributes::Value<'a>>>;
22
23/// Describes all potential shapes of a html element
24/// Note that there are only three kinds, text nodes, comment nodes, and element nodes
25/// but an element node can be void, or have children
26#[derive(Clone, From)]
27pub enum Node<'a> {
28 Text(Text),
29 Comment(Comment),
30 Element(Element<'a, Normal<'a>>),
31 Void(Element<'a, Void>),
32}
33
34/// A Html comment node. <!---- Text --->
35#[derive(From, Into, Clone)]
36pub struct Comment(String);
37
38/// The html element type. This is the most common
39/// Note: if children is None, then it is handled as an empty
40/// element, this is different than having no children
41#[derive(Clone)]
42pub struct Element<'a, T>
43where
44 T: ElementType,
45{
46 pub name: Tag<'a>,
47 pub attributes: Attributes<'a>,
48 pub children: T,
49}
50
51/// Represents the different element types.
52/// Note: This is sealed so cannot be implemented other this crate
53/// Implementations
54pub trait ElementType: private::Sealed + Default {}
55
56mod private {
57 impl super::ElementType for super::Void {}
58 impl<'a> super::ElementType for super::Normal<'a> {}
59 pub trait Sealed {}
60 impl Sealed for () {}
61 impl<'a> Sealed for Vec<super::Node<'a>> {}
62}