hypertext/validation/
mod.rs

1//! Types and traits used for validation of HTML elements and attributes.
2pub mod attributes;
3pub mod hypertext_elements;
4#[cfg(feature = "mathml")]
5mod mathml;
6
7/// A marker trait for type-checked elements.
8pub trait Element {
9    /// The kind of this element.
10    type Kind: ElementKind;
11}
12/// A marker trait to represent the kind of an element.
13///
14/// This can be either [`Normal`] or [`Void`]. A [`Normal`] element will always
15/// have a closing tag, and can have children. A [`Void`] element will never
16/// have a closing tag, and cannot have children.
17pub trait ElementKind: sealed::Sealed {}
18
19/// A marker type to represent a normal element.
20///
21/// This element has a closing tag and can have children.
22///
23/// # Example
24///
25/// ```html
26/// <div>
27///   Hello, world!
28/// </div>
29/// ```
30#[derive(Debug, Clone, Copy)]
31pub struct Normal;
32
33impl ElementKind for Normal {}
34
35/// A marker type to represent a void element.
36///
37/// This element does not have a closing tag and cannot have children.
38///
39/// # Example
40///
41/// ```html
42/// <img src="image.png" alt="An image">
43/// ```
44#[derive(Debug, Clone, Copy)]
45pub struct Void;
46
47impl ElementKind for Void {}
48
49mod sealed {
50    use super::{Normal, Void};
51
52    pub trait Sealed {}
53    impl Sealed for Normal {}
54    impl Sealed for Void {}
55}
56
57/// A standard attribute.
58#[derive(Debug, Clone, Copy)]
59pub struct Attribute;
60
61/// An attribute namespace.
62#[derive(Debug, Clone, Copy)]
63pub struct AttributeNamespace;
64
65/// An attribute prefixed by a symbol.
66#[derive(Debug, Clone, Copy)]
67pub struct AttributeSymbol;
68
69/// Define custom elements.
70///
71/// This macro should be called from within a module named `hypertext_elements`.
72///
73/// # Example
74///
75/// ```rust
76/// mod hypertext_elements {
77///     use hypertext::define_elements;
78///     // Re-export all standard HTML elements
79///     pub use hypertext::validation::hypertext_elements::*;
80///
81///     define_elements! {
82///         /// A custom web component that greets the user.
83///         simple_greeting {
84///             /// The name of the person to greet.
85///             name
86///         }
87///
88///         /// An element representing a coordinate.
89///         coordinate {
90///             /// The x coordinate.
91///             x
92///
93///             /// The y coordinate.
94///             y
95///         }
96///     }
97/// }
98///
99/// // Now, you can use the custom elements like this:
100///
101/// use hypertext::prelude::*;
102///
103/// assert_eq!(
104///     maud! {
105///         simple-greeting name="Alice" {
106///             coordinate x=1 y=2 {}
107///         }
108///     }
109///     .render()
110///     .as_inner(),
111///     r#"<simple-greeting name="Alice"><coordinate x="1" y="2"></coordinate></simple-greeting>"#,
112/// )
113/// ```
114#[macro_export]
115macro_rules! define_elements {
116    {
117        $(
118            $(#[$meta:meta])*
119            $name:ident $(
120                {
121                    $(
122                        $(#[$attr_meta:meta])*
123                        $attr:ident
124                    )*
125                }
126            )?
127        )*
128    } => {
129        $(
130            $(#[$meta])*
131            #[expect(
132                non_camel_case_types,
133                reason = "camel case types will be interpreted as components"
134            )]
135            #[derive(::core::fmt::Debug, ::core::clone::Clone, ::core::marker::Copy)]
136            pub struct $name;
137
138            $(
139                #[allow(non_upper_case_globals)]
140                impl $name {
141                    $(
142                        $(#[$attr_meta])*
143                        pub const $attr: $crate::validation::Attribute = $crate::validation::Attribute;
144                    )*
145                }
146            )?
147
148            impl $crate::validation::Element for $name {
149                type Kind = $crate::validation::Normal;
150            }
151
152            impl $crate::validation::attributes::GlobalAttributes for $name {}
153        )*
154    }
155}
156
157/// Define custom void elements.
158///
159/// This macro should be called from within a module named `hypertext_elements`.
160///
161/// # Example
162/// ```rust
163/// mod hypertext_elements {
164///     // Re-export all standard HTML elements
165///     use hypertext::define_void_elements;
166///     pub use hypertext::validation::hypertext_elements::*;
167///
168///     define_void_elements! {
169///         /// A custom void element that greets the user.
170///         simple_greeting {
171///             /// The name of the person to greet.
172///             name
173///         }
174///     }
175/// }
176///
177/// // Now, you can use the custom elements like this:
178///
179/// use hypertext::prelude::*;
180///
181/// assert_eq!(
182///     maud! {
183///         simple-greeting name="Alice";
184///     }
185///     .render()
186///     .as_inner(),
187///     r#"<simple-greeting name="Alice">"#,
188/// )
189/// ```
190#[macro_export]
191macro_rules! define_void_elements {
192    {
193        $(
194            $(#[$meta:meta])*
195            $name:ident $(
196                {
197                    $(
198                        $(#[$attr_meta:meta])*
199                        $attr:ident
200                    )*
201                }
202            )?
203        )*
204    } => {
205        $(
206            $(#[$meta])*
207            #[expect(
208                non_camel_case_types,
209                reason = "camel case types will be interpreted as components"
210            )]
211            #[derive(::core::fmt::Debug, ::core::clone::Clone, ::core::marker::Copy)]
212            pub struct $name;
213
214            $(
215                #[allow(non_upper_case_globals)]
216                impl $name {
217                    $(
218                        $(#[$attr_meta])*
219                        pub const $attr: $crate::validation::Attribute = $crate::validation::Attribute;
220                    )*
221                }
222            )?
223
224            impl $crate::validation::Element for $name {
225                type Kind = $crate::validation::Void;
226            }
227
228            impl $crate::validation::attributes::GlobalAttributes for $name {}
229        )*
230    }
231}