fun_html/
attr.rs

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
//! Common attributes
//!
//! Note that you may create your own attribute by using [`Attribute::new`] or [`Attribute::new_flag`]
//! Or by leveraging on of the `From` implementation on [`Attribute`]

use alloc::{borrow::Cow, string::String};

use crate::Attribute;

impl<T: Into<Cow<'static, str>>> From<(&'static str, T)> for Attribute {
    fn from((key, value): (&'static str, T)) -> Self {
        Attribute::new(key, value)
    }
}

/// `id` attribute
pub fn id(id: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("id", id)
}

/// `class` attribute
///
/// It takes a list of clases and join them together
///
/// ## Example
///
/// ```
/// # use fun_html::attr::class;
///
/// assert_eq!(
///   class(["foo", "bar", "baz"]).to_string(),
///   r#"class="foo bar baz""#,
/// );
/// ```
pub fn class<'a>(classes: impl IntoIterator<Item = &'a str>) -> Attribute {
    let mut values = String::new();
    let mut iter = classes.into_iter();
    if let Some(value) = iter.next() {
        values.push_str(value);
    }
    for value in iter {
        values.push(' ');
        values.push_str(value);
    }
    Attribute::new("class", values)
}

/// `lang` attribute (usually on `html` element)
pub fn lang(lang: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("lang", lang)
}

/// Represent an anchor target
#[derive(Debug, Clone)]
pub enum AnchorTarget {
    /// `_blank`
    Blank,
    /// `_self`
    Self_,
    /// `_parent`
    Parent,
    /// `_top`
    Top,
    /// Frame name
    Frame(Cow<'static, str>),
}

/// `target` attribute for `<a>`
pub fn target(target: AnchorTarget) -> Attribute {
    Attribute::new(
        "target",
        match target {
            AnchorTarget::Blank => "_blank".into(),
            AnchorTarget::Self_ => "_self".into(),
            AnchorTarget::Parent => "_parent".into(),
            AnchorTarget::Top => "_top".into(),
            AnchorTarget::Frame(name) => name,
        },
    )
}

/// Alias for `target(Target::Blank)`
pub fn target_blank() -> Attribute {
    target(AnchorTarget::Blank)
}

/// `href` attribute
pub fn href(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("href", value)
}

/// `rel` attribute
pub fn rel(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("rel", value)
}

/// `src` attribute
pub fn src(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("src", value)
}

/// `alt` attribute
pub fn alt(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("alt", value)
}

/// `width` attribute
pub fn width(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("width", value)
}

/// `height` attribute
pub fn height(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("height", value)
}

/// `width` attribute with an `i32` value
pub fn width_int(value: i32) -> Attribute {
    Attribute::new_int("width", value)
}

/// `height` attribute with an `i32` value
pub fn height_int(value: i32) -> Attribute {
    Attribute::new_int("height", value)
}

/// `cols` attribute
pub fn cols(value: i32) -> Attribute {
    Attribute::new_int("cols", value)
}

/// `rows` attribute
pub fn rows(value: i32) -> Attribute {
    Attribute::new_int("rows", value)
}

/// `type` attribute
pub fn type_(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("type", value)
}

/// `type="text"` (text input)
pub fn type_text() -> Attribute {
    Attribute::new("type", "text")
}

/// `type="password"` (password input)
pub fn type_password() -> Attribute {
    Attribute::new("type", "password")
}

/// `type="number"` (number input)
pub fn type_number() -> Attribute {
    Attribute::new("type", "number")
}

/// `type="tel"` (phone number input)
pub fn type_tel() -> Attribute {
    Attribute::new("type", "tel")
}

/// `type="file"` (file input)
pub fn type_file() -> Attribute {
    Attribute::new("type", "file")
}

/// `type="checkbox"` (checkbox input)
pub fn type_checkbox() -> Attribute {
    Attribute::new("type", "checkbox")
}

/// `type="radio"` (radio input)
pub fn type_radio() -> Attribute {
    Attribute::new("type", "radio")
}

/// `type="range"` (range input)
pub fn type_range() -> Attribute {
    Attribute::new("type", "range")
}

/// `type="email"` (email input)
pub fn type_email() -> Attribute {
    Attribute::new("type", "email")
}

/// `type="date"` (date input)
pub fn type_date() -> Attribute {
    Attribute::new("type", "date")
}

/// `type="month"` (month input)
pub fn type_month() -> Attribute {
    Attribute::new("type", "month")
}

/// `type="hidden"` (hidden input)
pub fn type_hidden() -> Attribute {
    Attribute::new("type", "hidden")
}

/// `type="reset"` (reset button)
pub fn type_reset() -> Attribute {
    Attribute::new("type", "reset")
}

/// `type="submit"` (reset button)
pub fn type_submit() -> Attribute {
    Attribute::new("type", "submit")
}

/// `integrity` attribute
pub fn integrity(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("integrity", value)
}

/// `defer` attribute
pub fn defer() -> Attribute {
    Attribute::new_flag("defer")
}

/// `async` attribute
pub fn async_() -> Attribute {
    Attribute::new_flag("async")
}

/// `crossorigin="anonymous"`
pub fn crossorigin_anonymous() -> Attribute {
    Attribute::new("crossorigin", "anonymous")
}

/// `crossorigin="use-credentials"`
pub fn crossorigin_use_credentials() -> Attribute {
    Attribute::new("crossorigin", "use-credentials")
}

/// `download` flag attribute
pub fn download() -> Attribute {
    Attribute::new_flag("download")
}

/// `download` attribute with a file name argument
pub fn download_with_name(name: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("download", name)
}

/// `charset` attribute
pub fn charset(charset: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("charset", charset)
}

/// Alias for `charset("UTF-8")`
pub fn charset_utf_8() -> Attribute {
    charset("UTF-8")
}

/// `name` attribute
pub fn name(name: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("name", name)
}

/// `content` attribute
pub fn content(content: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("content", content)
}

/// `action` attribute
pub fn action(action: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("action", action)
}

/// `method_get` attribute
pub fn method_get() -> Attribute {
    Attribute::new("method", "get")
}

/// `method_get` attribute
pub fn method_post() -> Attribute {
    Attribute::new("method", "post")
}

/// `for` attribute
pub fn for_(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("for", value)
}

/// `value` attribute
pub fn value(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("value", value)
}

/// `required` attribute
pub fn required() -> Attribute {
    Attribute::new_flag("required")
}

/// `pattern` attribute
pub fn pattern(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("pattern", value)
}

/// `min` attribute
pub fn min(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("min", value)
}

/// `max` attribute
pub fn max(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("max", value)
}

/// `minlength` attribute
pub fn minlength(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("minlength", value)
}

/// `maxlength` attribute
pub fn maxlength(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("maxlength", value)
}

/// `multiple` attribute
pub fn multiple() -> Attribute {
    Attribute::new_flag("multiple")
}

/// `placeholder` attribute
pub fn placeholder(value: impl Into<Cow<'static, str>>) -> Attribute {
    Attribute::new("placeholder", value)
}