hypertext/validation/
attributes.rs

1//! Attribute traits for various frameworks.
2//!
3//! To use these attributes, you need to enable the corresponding feature in
4//! your `Cargo.toml` file. For example, to use [`HtmxAttributes`], you would
5//! enable the `htmx` feature:
6//!
7//! ```toml
8//! [dependencies]
9//! hypertext = { version = "*", features = ["htmx"] }
10//! ```
11//!
12//! Then you can use the attributes in your code after bringing the trait into
13//! scope via [`prelude::*`](crate::prelude):
14//!
15//! ```rust
16//! # #[cfg(feature = "htmx")] {
17//! use hypertext::prelude::*;
18//!
19//! # assert_eq!(
20//! maud! {
21//!     a hx-get="/about" { "About" }
22//! }
23//! # .render().as_inner(), r#"<a hx-get="/about">About</a>"#);
24//! # }
25//! ```
26#![allow(non_upper_case_globals)]
27#[cfg(feature = "mathml")]
28pub use super::mathml::MathMlGlobalAttributes;
29use crate::validation::{Attribute, Element};
30#[allow(unused_imports)]
31use crate::validation::{AttributeNamespace, AttributeSymbol};
32
33/// Global HTML attributes.
34///
35/// This trait must be in scope to use standard HTML attributes such as
36/// [`class`](Self::class) and [`id`](Self::id). This trait is implemented
37/// by every HTML element specified in
38/// [`hypertext_elements`](crate::validation::hypertext_elements).
39///
40/// # Usage With Custom Elements
41///
42/// ```
43/// use hypertext::prelude::*;
44///
45/// mod hypertext_elements {
46///     #![expect(non_camel_case_types)]
47///
48///     pub use hypertext::validation::hypertext_elements::*;
49///     use hypertext::validation::{Element, Normal, attributes::GlobalAttributes};
50///
51///     pub struct custom_element;
52///
53///     impl Element for custom_element {
54///         type Kind = Normal;
55///     }
56///
57///     impl GlobalAttributes for custom_element {}
58/// }
59///
60/// assert_eq!(
61///     maud! { custom-element #my-element title="abc" { "Hello, world!" } }
62///         .render()
63///         .as_inner(),
64///     r#"<custom-element id="my-element" title="abc">Hello, world!</custom-element>"#
65/// );
66/// ```
67pub trait GlobalAttributes: Element {
68    /// Used as a guide for creating a keyboard shortcut that activates or
69    /// focuses the element.
70    const access_key: Attribute = Attribute;
71
72    /// The autocapitalization behavior to use when the text is edited
73    /// through non-keyboard methods.
74    const autocapitalize: Attribute = Attribute;
75
76    /// Indicates whether the element should be automatically focused when
77    /// the page is loaded.
78    const autofocus: Attribute = Attribute;
79
80    /// The class of the element.
81    #[doc(alias = ".")]
82    const class: Attribute = Attribute;
83
84    /// Whether the element is editable.
85    const contenteditable: Attribute = Attribute;
86
87    /// The text directionality of the element.
88    const dir: Attribute = Attribute;
89
90    /// Whether the element is draggable.
91    const draggable: Attribute = Attribute;
92
93    /// A hint as to what the `enter` key should do.
94    const enterkeyhint: Attribute = Attribute;
95
96    /// Whether the element is hidden from view.
97    const hidden: Attribute = Attribute;
98
99    /// A unique identifier for the element.
100    #[doc(alias = "#")]
101    const id: Attribute = Attribute;
102
103    /// Mark an element and its children as inert, disabling interaction.
104    const inert: Attribute = Attribute;
105
106    /// Specifies what kind of input mechanism would be most helpful for
107    /// users entering content.
108    const inputmode: Attribute = Attribute;
109
110    /// Specify which element this is a custom variant of.
111    const is: Attribute = Attribute;
112
113    /// A global identifier for the item.
114    const itemid: Attribute = Attribute;
115
116    /// A property that the item has.
117    const itemprop: Attribute = Attribute;
118
119    /// A list of additional elements to crawl to find the name-value pairs
120    /// of the item.
121    const itemref: Attribute = Attribute;
122
123    /// Creates a new item, a group of name-value pairs.
124    const itemscope: Attribute = Attribute;
125
126    /// The item types of the item.
127    const itemtype: Attribute = Attribute;
128
129    /// The language of the element.
130    const lang: Attribute = Attribute;
131
132    /// A cryptographic nonce ("number used once") which can be used by
133    /// Content Security Policy to determine whether or not a given
134    /// fetch will be allowed to proceed.
135    const nonce: Attribute = Attribute;
136
137    /// When specified, the element won't be rendered until it becomes
138    /// shown, at which point it will be rendered on top of other
139    /// page content.
140    const popover: Attribute = Attribute;
141
142    /// The slot the element is inserted in.
143    const slot: Attribute = Attribute;
144
145    /// Whether the element is spellchecked or not.
146    const spellcheck: Attribute = Attribute;
147
148    /// The CSS styling to apply to the element.
149    const style: Attribute = Attribute;
150
151    /// Customize the index of the element for sequential focus navigation.
152    const tabindex: Attribute = Attribute;
153
154    /// A text description of the element.
155    const title: Attribute = Attribute;
156
157    /// Whether the element is to be translated when the page is localized.
158    const translate: Attribute = Attribute;
159}
160
161/// [ARIA](https://www.w3.org/TR/wai-aria/) attributes.
162#[expect(missing_docs)]
163pub trait AriaAttributes: GlobalAttributes {
164    const role: Attribute = Attribute;
165    const aria_activedescendant: Attribute = Attribute;
166    const aria_atomic: Attribute = Attribute;
167    const aria_autocomplete: Attribute = Attribute;
168    const aria_braillelabel: Attribute = Attribute;
169    const aria_brailleroledescription: Attribute = Attribute;
170    const aria_busy: Attribute = Attribute;
171    const aria_checked: Attribute = Attribute;
172    const aria_colcount: Attribute = Attribute;
173    const aria_colindex: Attribute = Attribute;
174    const aria_colindextext: Attribute = Attribute;
175    const aria_colspan: Attribute = Attribute;
176    const aria_controls: Attribute = Attribute;
177    const aria_current: Attribute = Attribute;
178    const aria_describedby: Attribute = Attribute;
179    const aria_description: Attribute = Attribute;
180    const aria_details: Attribute = Attribute;
181    const aria_disabled: Attribute = Attribute;
182    const aria_dropeffect: Attribute = Attribute;
183    const aria_errormessage: Attribute = Attribute;
184    const aria_expanded: Attribute = Attribute;
185    const aria_flowto: Attribute = Attribute;
186    const aria_grabbed: Attribute = Attribute;
187    const aria_haspopup: Attribute = Attribute;
188    const aria_hidden: Attribute = Attribute;
189    const aria_invalid: Attribute = Attribute;
190    const aria_keyshortcuts: Attribute = Attribute;
191    const aria_label: Attribute = Attribute;
192    const aria_labelledby: Attribute = Attribute;
193    const aria_level: Attribute = Attribute;
194    const aria_live: Attribute = Attribute;
195    const aria_modal: Attribute = Attribute;
196    const aria_multiline: Attribute = Attribute;
197    const aria_multiselectable: Attribute = Attribute;
198    const aria_orientation: Attribute = Attribute;
199    const aria_owns: Attribute = Attribute;
200    const aria_placeholder: Attribute = Attribute;
201    const aria_posinset: Attribute = Attribute;
202    const aria_pressed: Attribute = Attribute;
203    const aria_readonly: Attribute = Attribute;
204    const aria_relevant: Attribute = Attribute;
205    const aria_required: Attribute = Attribute;
206    const aria_roledescription: Attribute = Attribute;
207    const aria_rowcount: Attribute = Attribute;
208    const aria_rowindex: Attribute = Attribute;
209    const aria_rowindextext: Attribute = Attribute;
210    const aria_rowspan: Attribute = Attribute;
211    const aria_selected: Attribute = Attribute;
212    const aria_setsize: Attribute = Attribute;
213    const aria_sort: Attribute = Attribute;
214    const aria_valuemax: Attribute = Attribute;
215    const aria_valuemin: Attribute = Attribute;
216    const aria_valuenow: Attribute = Attribute;
217    const aria_valuetext: Attribute = Attribute;
218}
219
220impl<T: GlobalAttributes> AriaAttributes for T {}
221
222/// Event handler attributes.
223#[expect(missing_docs)]
224pub trait EventHandlerAttributes: GlobalAttributes {
225    const onabort: Attribute = Attribute;
226    const onautocomplete: Attribute = Attribute;
227    const onautocompleteerror: Attribute = Attribute;
228    const onblur: Attribute = Attribute;
229    const oncancel: Attribute = Attribute;
230    const oncanplay: Attribute = Attribute;
231    const oncanplaythrough: Attribute = Attribute;
232    const onchange: Attribute = Attribute;
233    const onclick: Attribute = Attribute;
234    const onclose: Attribute = Attribute;
235    const oncontextmenu: Attribute = Attribute;
236    const oncuechange: Attribute = Attribute;
237    const ondblclick: Attribute = Attribute;
238    const ondrag: Attribute = Attribute;
239    const ondragend: Attribute = Attribute;
240    const ondragenter: Attribute = Attribute;
241    const ondragleave: Attribute = Attribute;
242    const ondragover: Attribute = Attribute;
243    const ondragstart: Attribute = Attribute;
244    const ondrop: Attribute = Attribute;
245    const ondurationchange: Attribute = Attribute;
246    const onemptied: Attribute = Attribute;
247    const onended: Attribute = Attribute;
248    const onerror: Attribute = Attribute;
249    const onfocus: Attribute = Attribute;
250    const oninput: Attribute = Attribute;
251    const oninvalid: Attribute = Attribute;
252    const onkeydown: Attribute = Attribute;
253    const onkeypress: Attribute = Attribute;
254    const onkeyup: Attribute = Attribute;
255    const onload: Attribute = Attribute;
256    const onloadeddata: Attribute = Attribute;
257    const onloadedmetadata: Attribute = Attribute;
258    const onloadstart: Attribute = Attribute;
259    const onmousedown: Attribute = Attribute;
260    const onmouseenter: Attribute = Attribute;
261    const onmouseleave: Attribute = Attribute;
262    const onmousemove: Attribute = Attribute;
263    const onmouseout: Attribute = Attribute;
264    const onmouseover: Attribute = Attribute;
265    const onmouseup: Attribute = Attribute;
266    const onmousewheel: Attribute = Attribute;
267    const onpause: Attribute = Attribute;
268    const onplay: Attribute = Attribute;
269    const onplaying: Attribute = Attribute;
270    const onprogress: Attribute = Attribute;
271    const onratechange: Attribute = Attribute;
272    const onreset: Attribute = Attribute;
273    const onresize: Attribute = Attribute;
274    const onscroll: Attribute = Attribute;
275    const onseeked: Attribute = Attribute;
276    const onseeking: Attribute = Attribute;
277    const onselect: Attribute = Attribute;
278    const onshow: Attribute = Attribute;
279    const onsort: Attribute = Attribute;
280    const onstalled: Attribute = Attribute;
281    const onsubmit: Attribute = Attribute;
282    const onsuspend: Attribute = Attribute;
283    const ontimeupdate: Attribute = Attribute;
284    const ontoggle: Attribute = Attribute;
285    const onvolumechange: Attribute = Attribute;
286    const onwaiting: Attribute = Attribute;
287}
288
289impl<T: GlobalAttributes> EventHandlerAttributes for T {}
290
291/// Attributes for use with [htmx](https://htmx.org/).
292#[cfg(feature = "htmx")]
293pub trait HtmxAttributes: GlobalAttributes {
294    /// Issues a `GET` to the specified URL
295    const hx_get: Attribute = Attribute;
296
297    /// Issues a `POST` to the specified URL
298    const hx_post: Attribute = Attribute;
299
300    /// Handle events with inline scripts on elements
301    const hx_on: AttributeNamespace = AttributeNamespace;
302
303    /// Push a URL into the browser location bar to create history
304    const hx_push_url: Attribute = Attribute;
305
306    /// Select content to swap in from a response
307    const hx_select: Attribute = Attribute;
308
309    /// Select content to swap in from a response, somewhere other than the
310    /// target (out of band)
311    const hx_select_oob: Attribute = Attribute;
312
313    /// Controls how content will swap in (`outerHTML`, `beforeend`,
314    /// `afterend`, …)
315    const hx_swap: Attribute = Attribute;
316
317    /// Mark element to swap in from a response (out of band)
318    const hx_swap_oob: Attribute = Attribute;
319
320    /// Specifies the target element to be swapped
321    const hx_target: Attribute = Attribute;
322
323    /// Specifies the event that triggers the request
324    const hx_trigger: Attribute = Attribute;
325
326    /// Add values to submit with the request (JSON format)
327    const hx_vals: Attribute = Attribute;
328
329    /// Add [progressive enhancement](https://en.wikipedia.org/wiki/Progressive_enhancement) for links and forms
330    const hx_boost: Attribute = Attribute;
331
332    /// Shows a `confirm()` dialog before issuing a request
333    const hx_confirm: Attribute = Attribute;
334
335    /// Issues a `DELETE` to the specified URL
336    const hx_delete: Attribute = Attribute;
337    /// Disables htmx processing for the given node and any children nodes
338    const hx_disable: Attribute = Attribute;
339
340    /// Adds the `disabled` attribute to the specified elements while a
341    /// request is in flight
342    const hx_disabled_elt: Attribute = Attribute;
343
344    /// Control and disable automatic attribute inheritance for child nodes
345    const hx_disinherit: Attribute = Attribute;
346
347    /// Changes the request encoding type
348    const hx_encoding: Attribute = Attribute;
349
350    /// Extensions to use for this element
351    const hx_ext: Attribute = Attribute;
352    /// Adds to the headers that will be submitted with the request
353    const hx_headers: Attribute = Attribute;
354
355    /// Prevent sensitive data being saved to the history cache
356    const hx_history: Attribute = Attribute;
357
358    /// The element to snapshot and restore during history navigation
359    const hx_history_elt: Attribute = Attribute;
360
361    /// Include additional data in requests
362    const hx_include: Attribute = Attribute;
363
364    /// The element to put the `htmx-request` class on during the request
365    const hx_indicator: Attribute = Attribute;
366    /// Control and enable automatic attribute inheritance for child nodes
367    /// if it has been disabled by default
368    const hx_inherit: Attribute = Attribute;
369
370    /// Filters the parameters that will be submitted with a request
371    const hx_params: Attribute = Attribute;
372
373    /// Issues a `PATCH` to the specified URL
374    const hx_patch: Attribute = Attribute;
375
376    /// Specifies elements to keep unchanged between requests
377    const hx_preserve: Attribute = Attribute;
378
379    /// Shows a `prompt()` before submitting a request
380    const hx_prompt: Attribute = Attribute;
381
382    /// Issues a `PUT` to the specified URL
383    const hx_put: Attribute = Attribute;
384
385    /// Replace the URL in the browser location bar
386    const hx_replace_url: Attribute = Attribute;
387
388    /// Configures various aspects of the request
389    const hx_request: Attribute = Attribute;
390
391    /// Control how requests made by different elements are synchronized
392    const hx_sync: Attribute = Attribute;
393
394    /// Force elements to validate themselves before a request
395    const hx_validate: Attribute = Attribute;
396
397    /// Adds values dynamically to the parameters to submit with the request
398    /// (deprecated, please use [`hx-vals`](Self::hx_vals))
399    #[deprecated = "use `hx-vals` instead"]
400    const hx_vars: Attribute = Attribute;
401}
402
403#[cfg(feature = "htmx")]
404impl<T: GlobalAttributes> HtmxAttributes for T {}
405
406#[cfg(feature = "hyperscript")]
407/// Attributes for use with [hyperscript](https://hyperscript.org/).
408pub trait HyperscriptAttributes: GlobalAttributes {
409    /// The `_` hyperscript tag
410    const __: Attribute = Attribute;
411}
412
413#[cfg(feature = "hyperscript")]
414impl<T: GlobalAttributes> HyperscriptAttributes for T {}
415
416/// Attributes for use with [Alpine.js](https://alpinejs.dev/).
417#[cfg(feature = "alpine")]
418pub trait AlpineJsAttributes: GlobalAttributes {
419    /// Declare a new Alpine component and its data for a block of HTML
420    const x_data: Attribute = Attribute;
421
422    /// Dynamically set HTML attributes on an element
423    const x_bind: AttributeNamespace = AttributeNamespace;
424
425    /// Dynamically set HTML attributes on an element
426    ///
427    /// Shorthand for [`x-bind`](Self::x_bind).
428    #[doc(alias = ":")]
429    const _colon: AttributeSymbol = AttributeSymbol;
430
431    /// Listen for browser events on an element
432    const x_on: AttributeNamespace = AttributeNamespace;
433
434    /// Listen for browser events on an element
435    ///
436    /// Shorthand for [`x-on`](Self::x_on).
437    #[doc(alias = "@")]
438    const _at: AttributeSymbol = AttributeSymbol;
439
440    /// Set the text content of an element
441    const x_text: Attribute = Attribute;
442
443    /// Set the inner HTML of an element
444    const x_html: Attribute = Attribute;
445
446    /// Synchronize a piece of data with an input element
447    const x_model: Attribute = Attribute;
448
449    /// Toggle the visibility of an element
450    const x_show: Attribute = Attribute;
451
452    /// Transition an element in and out using CSS transitions
453    const x_transition: Attribute = Attribute;
454
455    /// Repeat a block of HTML based on a data set
456    const x_for: Attribute = Attribute;
457
458    /// Conditionally add/remove a block of HTML from the page entirely
459    const x_if: Attribute = Attribute;
460
461    /// Run code when an element is initialized by Alpine
462    const x_init: Attribute = Attribute;
463
464    /// Execute a script each time const one: Attribute = Attribute; of its
465    /// dependencies change
466    const x_effect: Attribute = Attribute;
467
468    /// Reference elements directly by their specified keys using the $refs
469    /// magic property
470    const x_ref: Attribute = Attribute;
471
472    /// Hide a block of HTML until after Alpine is finished initializing its
473    /// contents
474    const x_cloak: Attribute = Attribute;
475
476    /// Prevent a block of HTML from being initialized by Alpine
477    const x_ignore: Attribute = Attribute;
478}
479
480#[cfg(feature = "alpine")]
481impl<T: GlobalAttributes> AlpineJsAttributes for T {}