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
use crate::{
attribute::{AttributeValue, IntoAttributeValue},
element::Element,
html_element::HtmlElement,
};
use pastey::paste;
/// The `hx-swap` attribute allows you to specify how the response will be swapped in relative to the
/// target of an AJAX request. If you do not specify the option, the default is
/// `htmx.config.defaultSwapStyle` (`innerHTML`).
#[derive(Debug, Clone, Copy)]
pub enum HXSwap {
/// Replace the inner HTML of the target element
InnerHtml,
/// Replace the entire target element with the response
OuterHTML,
/// Replace the text content of the target element, without parsing the response as HTML
TextContent,
/// Insert the response before the target element
BeforeBegin,
/// Insert the response before the first child of the target element
AfterBegin,
/// Insert the response after the last child of the target element
BeforeEnd,
/// Insert the response after the target element
AfterEnd,
/// Deletes the target element regardless of the response
Delete,
/// Does not append content from response (out of band items will still be processed).
None,
}
impl std::fmt::Display for HXSwap {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
HXSwap::InnerHtml => "innerHTML",
HXSwap::OuterHTML => "outerHTML",
HXSwap::TextContent => "textContent",
HXSwap::BeforeBegin => "beforebegin",
HXSwap::AfterBegin => "afterbegin",
HXSwap::BeforeEnd => "beforeend",
HXSwap::AfterEnd => "afterend",
HXSwap::Delete => "delete",
HXSwap::None => "none",
};
write!(f, "{}", s)
}
}
impl IntoAttributeValue for HXSwap {
fn into_attr(self) -> Option<AttributeValue> {
Some(AttributeValue::Value(self.to_string()))
}
}
/// The `hx-target` attribute allows you to target a different element for swapping than the one
/// issuing the AJAX request.
#[derive(Debug, Clone)]
pub enum HXTarget<'a> {
/// Which indicates that the element that the `hx-target` attribute is on is the target.
This,
/// `closest <CSS selector>` which will find the closest ancestor element or itself, that matches
/// the given CSS selector (e.g. `closest tr` will target the closest table row to the element).
Closest(&'a str),
/// `find <CSS selector>` which will find the first child descendant element that matches the
/// given CSS selector.
Find,
/// `next` which resolves to `element.nextElementSibling`
Next,
/// `next <CSS selector>` which will scan the DOM forward for the first element that matches the
/// given CSS selector. (e.g. `next .error` will target the closest following sibling element
/// with error class)
NextSelector(&'a str),
/// `previous` which resolves to `element.previousElementSibling`
Previous,
/// `previous <CSS selector>` which will scan the DOM backwards for the first element that
/// matches the given CSS selector. (e.g. `previous .error` will target the closest previous
/// sibling with error class)
PreviousSelector(&'a str),
}
impl std::fmt::Display for HXTarget<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
HXTarget::This => "this",
HXTarget::Closest(o) => {
return write!(f, "closest {}", o);
}
HXTarget::Find => "find",
HXTarget::Next => "next",
HXTarget::NextSelector(o) => {
return write!(f, "next {}", o);
}
HXTarget::Previous => "previous",
HXTarget::PreviousSelector(o) => {
return write!(f, "previous {}", o);
}
};
write!(f, "{}", s)
}
}
impl IntoAttributeValue for HXTarget<'_> {
fn into_attr(self) -> Option<AttributeValue> {
Some(AttributeValue::Value(self.to_string()))
}
}
/// Request headers sent by htmx.
#[derive(Debug, Clone, Copy)]
pub enum HtmxRequestHeader {
/// Indicates that the request is via an element using hx-boost
HXBoosted,
/// The current URL of the browser
HXCurrentURL,
/// “true” if the request is for history restoration after a miss in the local history cache
HXHistoryRestoreRequest,
/// The user response to an hx-prompt
HXPrompt,
/// Always “true”
HXRequest,
/// The id of the target element if it exists
HXTarget,
/// The name of the triggered element if it exists
HXTriggerName,
/// The id of the triggered element if it exists
HXTrigger,
}
impl std::fmt::Display for HtmxRequestHeader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
HtmxRequestHeader::HXBoosted => "HX-Boosted",
HtmxRequestHeader::HXCurrentURL => "HX-Current-URL",
HtmxRequestHeader::HXHistoryRestoreRequest => "HX-History-Restore-Request",
HtmxRequestHeader::HXPrompt => "HX-Prompt",
HtmxRequestHeader::HXRequest => "HX-Request",
HtmxRequestHeader::HXTarget => "HX-Target",
HtmxRequestHeader::HXTriggerName => "HX-Trigger-Name",
HtmxRequestHeader::HXTrigger => "HX-Trigger",
};
write!(f, "{}", s)
}
}
/// Response headers understood by htmx.
#[derive(Debug, Clone, Copy)]
pub enum HtmxResponseHeader {
/// Allows you to do a client-side redirect that does not do a full page reload
HXLocation,
/// Pushes a new url into the history stack
HXPushUrl,
/// Can be used to do a client-side redirect to a new location
HXRedirect,
/// If set to “true” the client-side will do a full refresh of the page
HXRefresh,
/// Replaces the current URL in the location bar
HXReplaceUrl,
/// Allows you to specify how the response will be swapped. See hx-swap for possible values
HXReswap,
/// A CSS selector that updates the target of the content update to a different element on the page
HXRetarget,
/// A CSS selector that allows you to choose which part of the response is used to be swapped in. Overrides an existing hx-select on the triggering element
HXReselect,
/// Allows you to trigger client-side events
HXTrigger,
/// Allows you to trigger client-side events after the settle step
HXTriggerAfterSettle,
/// Allows you to trigger client-side events after the swap step
HXTriggerAfterSwap,
}
impl std::fmt::Display for HtmxResponseHeader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
HtmxResponseHeader::HXLocation => "HX-Location",
HtmxResponseHeader::HXPushUrl => "HX-Push-Url",
HtmxResponseHeader::HXRedirect => "HX-Redirect",
HtmxResponseHeader::HXRefresh => "HX-Refresh",
HtmxResponseHeader::HXReplaceUrl => "HX-Replace-Url",
HtmxResponseHeader::HXReswap => "HX-Reswap",
HtmxResponseHeader::HXRetarget => "HX-Retarget",
HtmxResponseHeader::HXReselect => "HX-Reselect",
HtmxResponseHeader::HXTrigger => "HX-Trigger",
HtmxResponseHeader::HXTriggerAfterSettle => "HX-Trigger-After-Settle",
HtmxResponseHeader::HXTriggerAfterSwap => "HX-Trigger-After-Swap",
};
write!(f, "{}", s)
}
}
// TODO: simplify
macro_rules! set_htmx_attr {
($attr:ident = $name:expr; $eg:expr) => {
paste! {
#[doc = "Sets the `" $name "` attribute.\nExample: `" $eg "`"]
pub fn $attr(self, value: impl IntoAttributeValue) -> Self {
self.set_raw_attr($name, value)
}
}
};
($attr:ident = $name:expr) => {
paste! {
#[doc = "Sets the `" $name "` attribute."]
pub fn $attr(self, value: impl IntoAttributeValue) -> Self {
self.set_raw_attr($name, value)
}
}
};
($attr:ident) => {
paste! {
#[doc = "Sets the `" $attr "` attribute."]
pub fn $attr(self, value: impl IntoAttributeValue) -> Self {
self.set_raw_attr(stringify!([< $attr:lower >]), value)
}
}
};
($attr:ident$(=$name:expr)?$(;$eg:expr)?, $($rest:ident$(=$name_rest:expr)?$(;$eg_rest:expr)?),+) => {
set_htmx_attr!($attr$(=$name)?$(;$eg)?);
set_htmx_attr!($($rest$(=$name_rest)?$(;$eg_rest)?),+);
};
}
// TODO: add documentation for each attr
impl HtmlElement {
set_htmx_attr!(
hx_boost = "hx-boost"; r#"a().hx_boost("true")"#,
hx_confirm = "hx-confirm",
hx_delete = "hx-delete",
hx_disable = "hx-disable",
hx_disabled_elt = "hx-disabled-elt",
hx_ext = "hx-ext",
hx_get = "hx-get",
hx_headers = "hx-headers",
hx_history = "hx-history",
hx_history_elt = "hx-history-elt",
hx_include = "hx-include",
hx_indicator = "hx-indicator",
hx_inherit = "hx-inherit",
hx_params = "hx-params",
hx_patch = "hx-patch",
hx_post = "hx-post",
hx_preserve = "hx-preserve",
hx_prompt = "hx-prompt",
hx_push_url = "hx-push-url",
hx_put = "hx-put",
hx_replace_url = "hx-replace-url",
hx_request = "hx-request",
hx_select = "hx-select",
hx_select_oob = "hx-select-oob",
hx_swap = "hx-swap"; "div().hx_swap(HXSwap::OuterHTML)",
hx_swap_oob = "hx-swap-oob",
hx_sync = "hx-sync",
hx_target = "hx-target"; r#"div().hx_target(HXTarget::Closest("form"))"#,
hx_trigger = "hx-trigger",
hx_validate = "hx-validate",
hx_vals = "hx-vals"; r##"div().hx_vals(format!(r#"{{"key": "{x}"}}"#))"##,
sse_connect = "sse-connect",
sse_swap = "sse-swap",
ws_connect = "ws-connect",
ws_send = "ws-send"
);
}
#[cfg(test)]
mod test {
use super::*;
use crate::{html_element::*, render::Render};
#[test]
fn hx_attr_works() {
let token = "asdoiu12309usad";
let res = p()
.hx_get("/some_route")
.hx_swap(HXSwap::OuterHTML)
.hx_headers(format!(r#"{{"Authorization": "Bearer {}"}}"#, token))
.render();
insta::assert_snapshot!(res, @r#"<p hx-get="/some_route" hx-swap="outerHTML" hx-headers='{"Authorization": "Bearer asdoiu12309usad"}'></p>"#);
}
#[test]
fn hx_vals_works() {
let res = div().hx_vals(r#"{"myVal": "My Value"}"#).render();
insta::assert_snapshot!(res, @r#"<div hx-vals='{"myVal": "My Value"}'></div>"#);
}
}