fun_html/
attr.rs

1//! Common attributes
2//!
3//! Note that you may create your own attribute by using [`Attribute::new`] or [`Attribute::new_flag`]
4//! Or by leveraging on of the `From` implementation on [`Attribute`]
5
6use alloc::{borrow::Cow, string::String};
7
8use crate::Attribute;
9
10impl<T: Into<Cow<'static, str>>> From<(&'static str, T)> for Attribute {
11    fn from((key, value): (&'static str, T)) -> Self {
12        Attribute::new(key, value)
13    }
14}
15
16/// Do not render any attribute. Useful for conditional rendering.
17///
18/// # Example
19///
20/// ```
21/// use fun_html::{attr, elt};
22///
23/// let name: Option<&str> = None;
24/// let element = elt::div([
25///   // Note, `unwrap_or_default()` would have the same effect here
26///   name.map(attr::name).unwrap_or(attr::none()),
27/// ], []);
28/// ```
29pub fn none() -> Attribute {
30    Attribute(crate::AttributeInner::None)
31}
32
33/// `id` attribute
34pub fn id(id: impl Into<Cow<'static, str>>) -> Attribute {
35    Attribute::new("id", id)
36}
37
38/// `class` attribute
39///
40/// It takes a list of clases and join them together
41///
42/// ## Example
43///
44/// ```
45/// # use fun_html::attr::class;
46///
47/// assert_eq!(
48///   class(["foo", "bar", "baz"]).to_string(),
49///   r#"class="foo bar baz""#,
50/// );
51/// ```
52pub fn class<'a>(classes: impl IntoIterator<Item = &'a str>) -> Attribute {
53    let mut values = String::new();
54    let mut iter = classes.into_iter();
55    if let Some(value) = iter.next() {
56        values.push_str(value);
57    }
58    for value in iter {
59        values.push(' ');
60        values.push_str(value);
61    }
62    Attribute::new("class", values)
63}
64
65/// `lang` attribute (usually on `html` element)
66pub fn lang(lang: impl Into<Cow<'static, str>>) -> Attribute {
67    Attribute::new("lang", lang)
68}
69
70/// Represent an anchor target
71#[derive(Debug, Clone)]
72pub enum AnchorTarget {
73    /// `_blank`
74    Blank,
75    /// `_self`
76    Self_,
77    /// `_parent`
78    Parent,
79    /// `_top`
80    Top,
81    /// Frame name
82    Frame(Cow<'static, str>),
83}
84
85/// `target` attribute for `<a>`
86pub fn target(target: AnchorTarget) -> Attribute {
87    Attribute::new(
88        "target",
89        match target {
90            AnchorTarget::Blank => "_blank".into(),
91            AnchorTarget::Self_ => "_self".into(),
92            AnchorTarget::Parent => "_parent".into(),
93            AnchorTarget::Top => "_top".into(),
94            AnchorTarget::Frame(name) => name,
95        },
96    )
97}
98
99/// Alias for `target(Target::Blank)`
100pub fn target_blank() -> Attribute {
101    target(AnchorTarget::Blank)
102}
103
104/// `href` attribute
105pub fn href(value: impl Into<Cow<'static, str>>) -> Attribute {
106    Attribute::new("href", value)
107}
108
109/// `rel` attribute
110pub fn rel(value: impl Into<Cow<'static, str>>) -> Attribute {
111    Attribute::new("rel", value)
112}
113
114/// `src` attribute
115pub fn src(value: impl Into<Cow<'static, str>>) -> Attribute {
116    Attribute::new("src", value)
117}
118
119/// `alt` attribute
120pub fn alt(value: impl Into<Cow<'static, str>>) -> Attribute {
121    Attribute::new("alt", value)
122}
123
124/// `width` attribute
125pub fn width(value: impl Into<Cow<'static, str>>) -> Attribute {
126    Attribute::new("width", value)
127}
128
129/// `height` attribute
130pub fn height(value: impl Into<Cow<'static, str>>) -> Attribute {
131    Attribute::new("height", value)
132}
133
134/// `width` attribute with an `i32` value
135pub fn width_int(value: i32) -> Attribute {
136    Attribute::new_int("width", value)
137}
138
139/// `height` attribute with an `i32` value
140pub fn height_int(value: i32) -> Attribute {
141    Attribute::new_int("height", value)
142}
143
144/// `style` attribute
145pub fn style(value: impl Into<Cow<'static, str>>) -> Attribute {
146    Attribute::new("style", value)
147}
148
149/// `cols` attribute
150pub fn cols(value: i32) -> Attribute {
151    Attribute::new_int("cols", value)
152}
153
154/// `rows` attribute
155pub fn rows(value: i32) -> Attribute {
156    Attribute::new_int("rows", value)
157}
158
159/// `type` attribute
160pub fn type_(value: impl Into<Cow<'static, str>>) -> Attribute {
161    Attribute::new("type", value)
162}
163
164/// `type="text"` (text input)
165pub fn type_text() -> Attribute {
166    Attribute::new("type", "text")
167}
168
169/// `type="password"` (password input)
170pub fn type_password() -> Attribute {
171    Attribute::new("type", "password")
172}
173
174/// `type="number"` (number input)
175pub fn type_number() -> Attribute {
176    Attribute::new("type", "number")
177}
178
179/// `type="tel"` (phone number input)
180pub fn type_tel() -> Attribute {
181    Attribute::new("type", "tel")
182}
183
184/// `type="file"` (file input)
185pub fn type_file() -> Attribute {
186    Attribute::new("type", "file")
187}
188
189/// `type="checkbox"` (checkbox input)
190pub fn type_checkbox() -> Attribute {
191    Attribute::new("type", "checkbox")
192}
193
194/// `type="radio"` (radio input)
195pub fn type_radio() -> Attribute {
196    Attribute::new("type", "radio")
197}
198
199/// `type="range"` (range input)
200pub fn type_range() -> Attribute {
201    Attribute::new("type", "range")
202}
203
204/// `type="email"` (email input)
205pub fn type_email() -> Attribute {
206    Attribute::new("type", "email")
207}
208
209/// `type="date"` (date input)
210pub fn type_date() -> Attribute {
211    Attribute::new("type", "date")
212}
213
214/// `type="month"` (month input)
215pub fn type_month() -> Attribute {
216    Attribute::new("type", "month")
217}
218
219/// `type="hidden"` (hidden input)
220pub fn type_hidden() -> Attribute {
221    Attribute::new("type", "hidden")
222}
223
224/// `type="reset"` (reset button)
225pub fn type_reset() -> Attribute {
226    Attribute::new("type", "reset")
227}
228
229/// `type="submit"` (reset button)
230pub fn type_submit() -> Attribute {
231    Attribute::new("type", "submit")
232}
233
234/// `integrity` attribute
235pub fn integrity(value: impl Into<Cow<'static, str>>) -> Attribute {
236    Attribute::new("integrity", value)
237}
238
239/// `defer` attribute
240pub fn defer() -> Attribute {
241    Attribute::new_flag("defer")
242}
243
244/// `async` attribute
245pub fn async_() -> Attribute {
246    Attribute::new_flag("async")
247}
248
249/// `crossorigin="anonymous"`
250pub fn crossorigin_anonymous() -> Attribute {
251    Attribute::new("crossorigin", "anonymous")
252}
253
254/// `crossorigin="use-credentials"`
255pub fn crossorigin_use_credentials() -> Attribute {
256    Attribute::new("crossorigin", "use-credentials")
257}
258
259/// `download` flag attribute
260pub fn download() -> Attribute {
261    Attribute::new_flag("download")
262}
263
264/// `download` attribute with a file name argument
265pub fn download_with_name(name: impl Into<Cow<'static, str>>) -> Attribute {
266    Attribute::new("download", name)
267}
268
269/// `charset` attribute
270pub fn charset(charset: impl Into<Cow<'static, str>>) -> Attribute {
271    Attribute::new("charset", charset)
272}
273
274/// Alias for `charset("UTF-8")`
275#[deprecated(since = "1.5.0", note = "renamed to 'meta_charset_utf8'")]
276pub fn charset_utf_8() -> Attribute {
277    charset("UTF-8")
278}
279
280/// Alias for `charset("UTF-8")`
281pub fn charset_utf8() -> Attribute {
282    charset("UTF-8")
283}
284
285/// `name` attribute
286pub fn name(name: impl Into<Cow<'static, str>>) -> Attribute {
287    Attribute::new("name", name)
288}
289
290/// `content` attribute
291pub fn content(content: impl Into<Cow<'static, str>>) -> Attribute {
292    Attribute::new("content", content)
293}
294
295/// `action` attribute
296pub fn action(action: impl Into<Cow<'static, str>>) -> Attribute {
297    Attribute::new("action", action)
298}
299
300/// `method_get` attribute
301pub fn method_get() -> Attribute {
302    Attribute::new("method", "get")
303}
304
305/// `method_get` attribute
306pub fn method_post() -> Attribute {
307    Attribute::new("method", "post")
308}
309
310/// `for` attribute
311pub fn for_(value: impl Into<Cow<'static, str>>) -> Attribute {
312    Attribute::new("for", value)
313}
314
315/// `value` attribute
316pub fn value(value: impl Into<Cow<'static, str>>) -> Attribute {
317    Attribute::new("value", value)
318}
319
320/// `required` attribute
321pub fn required() -> Attribute {
322    Attribute::new_flag("required")
323}
324/// `autofocus` attribute
325pub fn autofocus() -> Attribute {
326    Attribute::new_flag("autofocus")
327}
328
329/// `autocomplete` attribute
330pub fn autocomplete(type_: impl Into<Cow<'static, str>>) -> Attribute {
331    Attribute::new("autocomplete", type_)
332}
333
334/// `autocomplete="on"` attribute
335pub fn autocomplete_on() -> Attribute {
336    Attribute::new("autocomplete", "on")
337}
338
339/// `autocomplete="on"` attribute
340pub fn autocomplete_off() -> Attribute {
341    Attribute::new("autocomplete", "off")
342}
343
344/// `disabled` attribute
345pub fn disabled() -> Attribute {
346    Attribute::new_flag("disabled")
347}
348
349/// `pattern` attribute
350pub fn pattern(value: impl Into<Cow<'static, str>>) -> Attribute {
351    Attribute::new("pattern", value)
352}
353
354/// `min` attribute
355pub fn min(value: impl Into<Cow<'static, str>>) -> Attribute {
356    Attribute::new("min", value)
357}
358
359/// `max` attribute
360pub fn max(value: impl Into<Cow<'static, str>>) -> Attribute {
361    Attribute::new("max", value)
362}
363
364/// `minlength` attribute
365pub fn minlength(value: impl Into<Cow<'static, str>>) -> Attribute {
366    Attribute::new("minlength", value)
367}
368
369/// `minlength` attribute using a `u16` value
370pub fn minlength_u16(value: u16) -> Attribute {
371    Attribute::new_int("minlength", value.into())
372}
373
374/// `maxlength` attribute
375pub fn maxlength(value: impl Into<Cow<'static, str>>) -> Attribute {
376    Attribute::new("maxlength", value)
377}
378
379/// `maxlength` attribute using a `u16` value
380pub fn maxlength_u16(value: u16) -> Attribute {
381    Attribute::new_int("maxlength", value.into())
382}
383
384/// `multiple` attribute
385pub fn multiple() -> Attribute {
386    Attribute::new_flag("multiple")
387}
388
389/// `placeholder` attribute
390pub fn placeholder(value: impl Into<Cow<'static, str>>) -> Attribute {
391    Attribute::new("placeholder", value)
392}
393
394/// `role` attribute with an aribtrary string value
395pub fn role_str(value: impl Into<Cow<'static, str>>) -> Attribute {
396    Attribute::new("role", value)
397}