hypertext 0.12.1

A blazing fast type-checked HTML macro crate.
Documentation
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! Attribute traits for various frameworks.
//!
//! To use these attributes, you need to enable the corresponding feature in
//! your `Cargo.toml` file. For example, to use [`HtmxAttributes`], you would
//! enable the `htmx` feature:
//!
//! ```toml
//! [dependencies]
//! hypertext = { version = "*", features = ["htmx"] }
//! ```
//!
//! Then you can use the attributes in your code after bringing the trait into
//! scope via [`prelude::*`](crate::prelude):
//!
//! ```rust
//! # #[cfg(feature = "htmx")] {
//! use hypertext::prelude::*;
//!
//! # assert_eq!(
//! maud! {
//!     a hx-get="/about" { "About" }
//! }
//! # .render().as_inner(), r#"<a hx-get="/about">About</a>"#);
//! # }
//! ```
#![allow(non_upper_case_globals)]
#[cfg(feature = "mathml")]
pub use super::mathml::MathMlGlobalAttributes;
use crate::validation::{Attribute, Element};
#[allow(unused_imports)]
use crate::validation::{AttributeNamespace, AttributeSymbol};

/// Global HTML attributes.
///
/// This trait must be in scope to use standard HTML attributes such as
/// [`class`](Self::class) and [`id`](Self::id). This trait is implemented
/// by every HTML element specified in
/// [`hypertext_elements`](crate::validation::hypertext_elements).
///
/// # Usage With Custom Elements
///
/// ```
/// use hypertext::prelude::*;
///
/// mod hypertext_elements {
///     #![expect(non_camel_case_types)]
///
///     pub use hypertext::validation::hypertext_elements::*;
///     use hypertext::validation::{Element, Normal, attributes::GlobalAttributes};
///
///     pub struct custom_element;
///
///     impl Element for custom_element {
///         type Kind = Normal;
///     }
///
///     impl GlobalAttributes for custom_element {}
/// }
///
/// assert_eq!(
///     maud! { custom-element #my-element title="abc" { "Hello, world!" } }
///         .render()
///         .as_inner(),
///     r#"<custom-element id="my-element" title="abc">Hello, world!</custom-element>"#
/// );
/// ```
pub trait GlobalAttributes: Element {
    /// Used as a guide for creating a keyboard shortcut that activates or
    /// focuses the element.
    const access_key: Attribute = Attribute;

    /// The autocapitalization behavior to use when the text is edited
    /// through non-keyboard methods.
    const autocapitalize: Attribute = Attribute;

    /// Indicates whether the element should be automatically focused when
    /// the page is loaded.
    const autofocus: Attribute = Attribute;

    /// The class of the element.
    #[doc(alias = ".")]
    const class: Attribute = Attribute;

    /// Whether the element is editable.
    const contenteditable: Attribute = Attribute;

    /// The text directionality of the element.
    const dir: Attribute = Attribute;

    /// Whether the element is draggable.
    const draggable: Attribute = Attribute;

    /// A hint as to what the `enter` key should do.
    const enterkeyhint: Attribute = Attribute;

    /// Whether the element is hidden from view.
    const hidden: Attribute = Attribute;

    /// A unique identifier for the element.
    #[doc(alias = "#")]
    const id: Attribute = Attribute;

    /// Mark an element and its children as inert, disabling interaction.
    const inert: Attribute = Attribute;

    /// Specifies what kind of input mechanism would be most helpful for
    /// users entering content.
    const inputmode: Attribute = Attribute;

    /// Specify which element this is a custom variant of.
    const is: Attribute = Attribute;

    /// A global identifier for the item.
    const itemid: Attribute = Attribute;

    /// A property that the item has.
    const itemprop: Attribute = Attribute;

    /// A list of additional elements to crawl to find the name-value pairs
    /// of the item.
    const itemref: Attribute = Attribute;

    /// Creates a new item, a group of name-value pairs.
    const itemscope: Attribute = Attribute;

    /// The item types of the item.
    const itemtype: Attribute = Attribute;

    /// The language of the element.
    const lang: Attribute = Attribute;

    /// A cryptographic nonce ("number used once") which can be used by
    /// Content Security Policy to determine whether or not a given
    /// fetch will be allowed to proceed.
    const nonce: Attribute = Attribute;

    /// When specified, the element won't be rendered until it becomes
    /// shown, at which point it will be rendered on top of other
    /// page content.
    const popover: Attribute = Attribute;

    /// The slot the element is inserted in.
    const slot: Attribute = Attribute;

    /// Whether the element is spellchecked or not.
    const spellcheck: Attribute = Attribute;

    /// The CSS styling to apply to the element.
    const style: Attribute = Attribute;

    /// Customize the index of the element for sequential focus navigation.
    const tabindex: Attribute = Attribute;

    /// A text description of the element.
    const title: Attribute = Attribute;

    /// Whether the element is to be translated when the page is localized.
    const translate: Attribute = Attribute;
}

/// [ARIA](https://www.w3.org/TR/wai-aria/) attributes.
#[expect(missing_docs)]
pub trait AriaAttributes: GlobalAttributes {
    const role: Attribute = Attribute;
    const aria_activedescendant: Attribute = Attribute;
    const aria_atomic: Attribute = Attribute;
    const aria_autocomplete: Attribute = Attribute;
    const aria_braillelabel: Attribute = Attribute;
    const aria_brailleroledescription: Attribute = Attribute;
    const aria_busy: Attribute = Attribute;
    const aria_checked: Attribute = Attribute;
    const aria_colcount: Attribute = Attribute;
    const aria_colindex: Attribute = Attribute;
    const aria_colindextext: Attribute = Attribute;
    const aria_colspan: Attribute = Attribute;
    const aria_controls: Attribute = Attribute;
    const aria_current: Attribute = Attribute;
    const aria_describedby: Attribute = Attribute;
    const aria_description: Attribute = Attribute;
    const aria_details: Attribute = Attribute;
    const aria_disabled: Attribute = Attribute;
    const aria_dropeffect: Attribute = Attribute;
    const aria_errormessage: Attribute = Attribute;
    const aria_expanded: Attribute = Attribute;
    const aria_flowto: Attribute = Attribute;
    const aria_grabbed: Attribute = Attribute;
    const aria_haspopup: Attribute = Attribute;
    const aria_hidden: Attribute = Attribute;
    const aria_invalid: Attribute = Attribute;
    const aria_keyshortcuts: Attribute = Attribute;
    const aria_label: Attribute = Attribute;
    const aria_labelledby: Attribute = Attribute;
    const aria_level: Attribute = Attribute;
    const aria_live: Attribute = Attribute;
    const aria_modal: Attribute = Attribute;
    const aria_multiline: Attribute = Attribute;
    const aria_multiselectable: Attribute = Attribute;
    const aria_orientation: Attribute = Attribute;
    const aria_owns: Attribute = Attribute;
    const aria_placeholder: Attribute = Attribute;
    const aria_posinset: Attribute = Attribute;
    const aria_pressed: Attribute = Attribute;
    const aria_readonly: Attribute = Attribute;
    const aria_relevant: Attribute = Attribute;
    const aria_required: Attribute = Attribute;
    const aria_roledescription: Attribute = Attribute;
    const aria_rowcount: Attribute = Attribute;
    const aria_rowindex: Attribute = Attribute;
    const aria_rowindextext: Attribute = Attribute;
    const aria_rowspan: Attribute = Attribute;
    const aria_selected: Attribute = Attribute;
    const aria_setsize: Attribute = Attribute;
    const aria_sort: Attribute = Attribute;
    const aria_valuemax: Attribute = Attribute;
    const aria_valuemin: Attribute = Attribute;
    const aria_valuenow: Attribute = Attribute;
    const aria_valuetext: Attribute = Attribute;
}

impl<T: GlobalAttributes> AriaAttributes for T {}

/// Event handler attributes.
#[expect(missing_docs)]
pub trait EventHandlerAttributes: GlobalAttributes {
    const onabort: Attribute = Attribute;
    const onautocomplete: Attribute = Attribute;
    const onautocompleteerror: Attribute = Attribute;
    const onblur: Attribute = Attribute;
    const oncancel: Attribute = Attribute;
    const oncanplay: Attribute = Attribute;
    const oncanplaythrough: Attribute = Attribute;
    const onchange: Attribute = Attribute;
    const onclick: Attribute = Attribute;
    const onclose: Attribute = Attribute;
    const oncontextmenu: Attribute = Attribute;
    const oncuechange: Attribute = Attribute;
    const ondblclick: Attribute = Attribute;
    const ondrag: Attribute = Attribute;
    const ondragend: Attribute = Attribute;
    const ondragenter: Attribute = Attribute;
    const ondragleave: Attribute = Attribute;
    const ondragover: Attribute = Attribute;
    const ondragstart: Attribute = Attribute;
    const ondrop: Attribute = Attribute;
    const ondurationchange: Attribute = Attribute;
    const onemptied: Attribute = Attribute;
    const onended: Attribute = Attribute;
    const onerror: Attribute = Attribute;
    const onfocus: Attribute = Attribute;
    const oninput: Attribute = Attribute;
    const oninvalid: Attribute = Attribute;
    const onkeydown: Attribute = Attribute;
    const onkeypress: Attribute = Attribute;
    const onkeyup: Attribute = Attribute;
    const onload: Attribute = Attribute;
    const onloadeddata: Attribute = Attribute;
    const onloadedmetadata: Attribute = Attribute;
    const onloadstart: Attribute = Attribute;
    const onmousedown: Attribute = Attribute;
    const onmouseenter: Attribute = Attribute;
    const onmouseleave: Attribute = Attribute;
    const onmousemove: Attribute = Attribute;
    const onmouseout: Attribute = Attribute;
    const onmouseover: Attribute = Attribute;
    const onmouseup: Attribute = Attribute;
    const onmousewheel: Attribute = Attribute;
    const onpause: Attribute = Attribute;
    const onplay: Attribute = Attribute;
    const onplaying: Attribute = Attribute;
    const onprogress: Attribute = Attribute;
    const onratechange: Attribute = Attribute;
    const onreset: Attribute = Attribute;
    const onresize: Attribute = Attribute;
    const onscroll: Attribute = Attribute;
    const onseeked: Attribute = Attribute;
    const onseeking: Attribute = Attribute;
    const onselect: Attribute = Attribute;
    const onshow: Attribute = Attribute;
    const onsort: Attribute = Attribute;
    const onstalled: Attribute = Attribute;
    const onsubmit: Attribute = Attribute;
    const onsuspend: Attribute = Attribute;
    const ontimeupdate: Attribute = Attribute;
    const ontoggle: Attribute = Attribute;
    const onvolumechange: Attribute = Attribute;
    const onwaiting: Attribute = Attribute;
}

impl<T: GlobalAttributes> EventHandlerAttributes for T {}

/// Attributes for use with [htmx](https://htmx.org/).
#[cfg(feature = "htmx")]
pub trait HtmxAttributes: GlobalAttributes {
    /// Issues a `GET` to the specified URL
    const hx_get: Attribute = Attribute;

    /// Issues a `POST` to the specified URL
    const hx_post: Attribute = Attribute;

    /// Handle events with inline scripts on elements
    const hx_on: AttributeNamespace = AttributeNamespace;

    /// Push a URL into the browser location bar to create history
    const hx_push_url: Attribute = Attribute;

    /// Select content to swap in from a response
    const hx_select: Attribute = Attribute;

    /// Select content to swap in from a response, somewhere other than the
    /// target (out of band)
    const hx_select_oob: Attribute = Attribute;

    /// Controls how content will swap in (`outerHTML`, `beforeend`,
    /// `afterend`, …)
    const hx_swap: Attribute = Attribute;

    /// Mark element to swap in from a response (out of band)
    const hx_swap_oob: Attribute = Attribute;

    /// Specifies the target element to be swapped
    const hx_target: Attribute = Attribute;

    /// Specifies the event that triggers the request
    const hx_trigger: Attribute = Attribute;

    /// Add values to submit with the request (JSON format)
    const hx_vals: Attribute = Attribute;

    /// Add [progressive enhancement](https://en.wikipedia.org/wiki/Progressive_enhancement) for links and forms
    const hx_boost: Attribute = Attribute;

    /// Shows a `confirm()` dialog before issuing a request
    const hx_confirm: Attribute = Attribute;

    /// Issues a `DELETE` to the specified URL
    const hx_delete: Attribute = Attribute;
    /// Disables htmx processing for the given node and any children nodes
    const hx_disable: Attribute = Attribute;

    /// Adds the `disabled` attribute to the specified elements while a
    /// request is in flight
    const hx_disabled_elt: Attribute = Attribute;

    /// Control and disable automatic attribute inheritance for child nodes
    const hx_disinherit: Attribute = Attribute;

    /// Changes the request encoding type
    const hx_encoding: Attribute = Attribute;

    /// Extensions to use for this element
    const hx_ext: Attribute = Attribute;
    /// Adds to the headers that will be submitted with the request
    const hx_headers: Attribute = Attribute;

    /// Prevent sensitive data being saved to the history cache
    const hx_history: Attribute = Attribute;

    /// The element to snapshot and restore during history navigation
    const hx_history_elt: Attribute = Attribute;

    /// Include additional data in requests
    const hx_include: Attribute = Attribute;

    /// The element to put the `htmx-request` class on during the request
    const hx_indicator: Attribute = Attribute;
    /// Control and enable automatic attribute inheritance for child nodes
    /// if it has been disabled by default
    const hx_inherit: Attribute = Attribute;

    /// Filters the parameters that will be submitted with a request
    const hx_params: Attribute = Attribute;

    /// Issues a `PATCH` to the specified URL
    const hx_patch: Attribute = Attribute;

    /// Specifies elements to keep unchanged between requests
    const hx_preserve: Attribute = Attribute;

    /// Shows a `prompt()` before submitting a request
    const hx_prompt: Attribute = Attribute;

    /// Issues a `PUT` to the specified URL
    const hx_put: Attribute = Attribute;

    /// Replace the URL in the browser location bar
    const hx_replace_url: Attribute = Attribute;

    /// Configures various aspects of the request
    const hx_request: Attribute = Attribute;

    /// Control how requests made by different elements are synchronized
    const hx_sync: Attribute = Attribute;

    /// Force elements to validate themselves before a request
    const hx_validate: Attribute = Attribute;

    /// Adds values dynamically to the parameters to submit with the request
    /// (deprecated, please use [`hx-vals`](Self::hx_vals))
    #[deprecated = "use `hx-vals` instead"]
    const hx_vars: Attribute = Attribute;
}

#[cfg(feature = "htmx")]
impl<T: GlobalAttributes> HtmxAttributes for T {}

#[cfg(feature = "hyperscript")]
/// Attributes for use with [hyperscript](https://hyperscript.org/).
pub trait HyperscriptAttributes: GlobalAttributes {
    /// The `_` hyperscript tag
    const __: Attribute = Attribute;
}

#[cfg(feature = "hyperscript")]
impl<T: GlobalAttributes> HyperscriptAttributes for T {}

/// Attributes for use with [Alpine.js](https://alpinejs.dev/).
#[cfg(feature = "alpine")]
pub trait AlpineJsAttributes: GlobalAttributes {
    /// Declare a new Alpine component and its data for a block of HTML
    const x_data: Attribute = Attribute;

    /// Dynamically set HTML attributes on an element
    const x_bind: AttributeNamespace = AttributeNamespace;

    /// Dynamically set HTML attributes on an element
    ///
    /// Shorthand for [`x-bind`](Self::x_bind).
    #[doc(alias = ":")]
    const _colon: AttributeSymbol = AttributeSymbol;

    /// Listen for browser events on an element
    const x_on: AttributeNamespace = AttributeNamespace;

    /// Listen for browser events on an element
    ///
    /// Shorthand for [`x-on`](Self::x_on).
    #[doc(alias = "@")]
    const _at: AttributeSymbol = AttributeSymbol;

    /// Set the text content of an element
    const x_text: Attribute = Attribute;

    /// Set the inner HTML of an element
    const x_html: Attribute = Attribute;

    /// Synchronize a piece of data with an input element
    const x_model: Attribute = Attribute;

    /// Toggle the visibility of an element
    const x_show: Attribute = Attribute;

    /// Transition an element in and out using CSS transitions
    const x_transition: Attribute = Attribute;

    /// Repeat a block of HTML based on a data set
    const x_for: Attribute = Attribute;

    /// Conditionally add/remove a block of HTML from the page entirely
    const x_if: Attribute = Attribute;

    /// Run code when an element is initialized by Alpine
    const x_init: Attribute = Attribute;

    /// Execute a script each time const one: Attribute = Attribute; of its
    /// dependencies change
    const x_effect: Attribute = Attribute;

    /// Reference elements directly by their specified keys using the $refs
    /// magic property
    const x_ref: Attribute = Attribute;

    /// Hide a block of HTML until after Alpine is finished initializing its
    /// contents
    const x_cloak: Attribute = Attribute;

    /// Prevent a block of HTML from being initialized by Alpine
    const x_ignore: Attribute = Attribute;
}

#[cfg(feature = "alpine")]
impl<T: GlobalAttributes> AlpineJsAttributes for T {}