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
//! A blazing fast type-checked HTML macro crate.
//!
//! # Features
//!
//! ## Speed
//!
//! The macros generate code that is as fast as writing HTML to a string by
//! hand, and intelligently combines what would be multiple `push_str` calls
//! into one if there is no dynamic content between them.
//!
//! The entire crate is `#![no_std]` compatible, and allocation is completely
//! optional if you don't use any dynamic content.
//!
//! The crate gives extreme importance to lazy rendering and minimizing
//! allocation, so it will only render the HTML to a string when you finally
//! call [`Renderable::render`] at the end. This makes composing nested HTML
//! elements extremely cheap.
//!
//! ## Type-Checking
//!
//! All macro invocations are validated at compile time, so you can't ever
//! misspell an element/attribute or use invalid attributes.
//!
//! It does this by looking in your current namespace, or a module named
//! `hypertext_elements` (all the valid HTML elements are defined in this crate
//! already in [`hypertext_elements`](validation::hypertext_elements), but it
//! doesn't hard-code this module so you can define your own elements).
//!
//! It then imports each element you use in your macro invocation, and then
//! attempts to access the corresponding associated type for each attribute you
//! use.
//!
//! # Example
//!
//! ```rust
//! use hypertext::prelude::*;
//! # use hypertext::{Lazy, context::Node, Buffer, validation::{Element, ElementKind, Attribute, Normal}};
//!
//! # assert_eq!(
//! maud! {
//! div #main title="Main Div" {
//! h1.important {
//! "Hello, world!"
//! }
//!
//! @for i in 1..=3 {
//! p.{ "p-" (i) } {
//! "This is paragraph number " (i)
//! }
//! }
//! }
//! }
//! # .render(),
//!
//! // expands to (roughly):
//!
//! Lazy::<_, Node>::dangerously_create(move |buffer: &mut Buffer| {
//! const _: fn() = || {
//! use hypertext_elements::*;
//! fn check_element<K: ElementKind>(_: impl Element<Kind = K>) {}
//!
//! check_element::<Normal>(h1);
//! let _: Attribute = <h1>::class;
//! check_element::<Normal>(p);
//! let _: Attribute = <p>::class;
//! check_element::<Normal>(div);
//! let _: Attribute = <div>::id;
//! let _: Attribute = <div>::title;
//! };
//! buffer
//! .dangerously_get_string()
//! .push_str(r#"<div id="main" title="Main Div">"#);
//! {
//! buffer
//! .dangerously_get_string()
//! .push_str(r#"<h1 class="important">Hello, world!</h1>"#);
//! for i in 1..=3 {
//! buffer.dangerously_get_string().push_str("<p class=\"p-");
//! i.render_to(buffer.as_attribute_buffer());
//! buffer
//! .dangerously_get_string()
//! .push_str(r#"">This is paragraph number "#);
//! i.render_to(buffer);
//! buffer.dangerously_get_string().push_str("</p>");
//! }
//! }
//! buffer.dangerously_get_string().push_str("</div>");
//! })
//! # .render());
//! ```
//!
//! This approach is also extremely extensible, as you can define your own
//! traits to add attributes for your favourite libraries! In fact, this is
//! exactly what [`GlobalAttributes`] does, and why it is required in the above
//! example, as it defines the attributes that can be used on any element, for
//! example [`id`], [`class`], and [`title`]. This library comes with built-in
//! support for many popular frontend attribute-based frameworks in
//! [`validation::attributes`], such as [`HtmxAttributes`] and
//! [`AlpineJsAttributes`]
//!
//! Here's an example of how you could define your own attributes for use with
//! the wonderful frontend library [htmx](https://htmx.org):
//!
//! ```rust
//! use hypertext::{
//! prelude::*,
//! validation::{Attribute, AttributeNamespace},
//! };
//!
//! trait HtmxAttributes: GlobalAttributes {
//! const hx_get: Attribute = Attribute;
//! const hx_on: AttributeNamespace = AttributeNamespace;
//! // ...
//! }
//!
//! impl<T: GlobalAttributes> HtmxAttributes for T {}
//!
//! assert_eq!(
//! maud! {
//! div hx-get="/api/endpoint" hx-on:click="alert('Hello, world!')" {
//! // ^^^^^^ note that it converts `-` to `_` for you during checking!
//! "Hello, world!"
//! }
//! }
//! .render()
//! .as_inner(),
//! r#"<div hx-get="/api/endpoint" hx-on:click="alert('Hello, world!')">Hello, world!</div>"#,
//! );
//! ```
//!
//! Wrapping an attribue name in quotes will bypass the type-checking, so you
//! can use any attribute you want, even if it doesn't exist in the current
//! context.
//!
//! ```rust
//! use hypertext::prelude::*;
//!
//! assert_eq!(
//! maud! {
//! div "custom-attribute"="value" { "Hello, world!" }
//! }
//! .render()
//! .as_inner(),
//! r#"<div custom-attribute="value">Hello, world!</div>"#,
//! );
//! ```
//!
//! This library also supports component structs, which are simply structs that
//! implement [`Renderable`]. If an element name is capitalized, it will be
//! treated as a component, with attributes representing the struct fields. The
//! [`#[component]`](component) macro can be used to easily turn functions into
//! components.
//!
//! ```rust
//! use hypertext::{Buffer, prelude::*};
//!
//! struct Repeater<R: Renderable> {
//! count: usize,
//! children: R,
//! }
//!
//! impl<R: Renderable> Renderable for Repeater<R> {
//! fn render_to(&self, buffer: &mut Buffer) {
//! maud! {
//! @for i in 0..self.count {
//! (self.children)
//! }
//! }
//! .render_to(buffer);
//! }
//! }
//!
//! assert_eq!(
//! maud! {
//! div {
//! Repeater count=3 {
//! // children are passed as a `Lazy` to the `children` field
//! p { "Hi!" }
//! }
//! }
//! }
//! .render()
//! .as_inner(),
//! "<div><p>Hi!</p><p>Hi!</p><p>Hi!</p></div>"
//! );
//! ```
//!
//! [`GlobalAttributes`]: validation::attributes::GlobalAttributes
//! [`id`]: validation::attributes::GlobalAttributes::id
//! [`class`]: validation::attributes::GlobalAttributes::class
//! [`title`]: validation::attributes::GlobalAttributes::title
//! [`HtmxAttributes`]: validation::attributes::HtmxAttributes
//! [`AlpineJsAttributes`]: validation::attributes::AlpineJsAttributes
use ;
pub use *;
use ;
pub use *;
/// A raw pre-escaped string.
///
/// For [`Raw<T, Node>`] (a.k.a. [`Raw<T>`]), this must contain complete HTML
/// nodes. If rendering string-like types, the value must escape `&` to `&`,
/// `<` to `<`, and `>` to `>`.
///
/// For [`Raw<T, AttributeValue>`] (a.k.a. [`RawAttribute<T>`]), this must
/// contain an attribute value which will eventually be surrounded by double
/// quotes. The value must escape `&` to `&`, `<` to `<`, `>` to `>`,
/// and `"` to `"`.
///
/// This is useful for rendering raw HTML, but should be used with caution
/// as it can lead to XSS vulnerabilities if used incorrectly. If you are
/// unsure, render the string itself, as its [`Renderable`] implementation will
/// escape any dangerous characters.
///
/// # Example
///
/// ```rust
/// use hypertext::{Raw, prelude::*};
///
/// fn get_some_html() -> String {
/// // get html from some source, such as a CMS
/// "<h2>Some HTML from the CMS</h2>".into()
/// }
///
/// assert_eq!(
/// maud! {
/// h1 { "My Document!" }
/// // XSS SAFETY: The CMS sanitizes the HTML before returning it.
/// (Raw::dangerously_create(get_some_html()))
/// }
/// .render()
/// .as_inner(),
/// "<h1>My Document!</h1><h2>Some HTML from the CMS</h2>"
/// )
/// ```
/// A raw pre-escaped attribute value.
///
/// This is a type alias for [`Raw<T, Attribute>`].
pub type RawAttribute<T> = ;
/// A rendered HTML string.
///
/// This type is returned by [`Renderable::render`] ([`Rendered<String>`]), as
/// well as [`Raw<T>::rendered`] ([`Rendered<T>`]).
///
/// This type intentionally does **not** implement [`Renderable`] to discourage
/// anti-patterns such as rendering to a string then embedding that HTML string
/// into another page. To do this, you should use [`RenderableExt::memoize`], or
/// use [`Raw`] directly.
;
/// Workaround for [`const_precise_live_drops`](https://github.com/rust-lang/rust/issues/73255) being unstable.
///
/// # Safety
///
/// - `$self` must be a struct with exactly 1 non-zero-sized field
/// - `$field` must be the name/index of that field
pub use const_precise_live_drops_hack;