Skip to main content

faker_rust/default/
html.rs

1//! HTML generator
2
3use crate::base::sample;
4use crate::locale::fetch_locale;
5
6/// Generate a random HTML tag
7pub fn tag() -> String {
8    fetch_locale("html.tags", "en")
9        .map(|v| sample(&v))
10        .unwrap_or_else(|| sample(FALLBACK_TAGS).to_string())
11}
12
13/// Generate a random HTML attribute
14pub fn attribute() -> String {
15    fetch_locale("html.attributes", "en")
16        .map(|v| sample(&v))
17        .unwrap_or_else(|| sample(FALLBACK_ATTRIBUTES).to_string())
18}
19
20/// Generate a random HTML color
21pub fn color() -> String {
22    let colors = vec![
23        "#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF", "#00FFFF",
24        "#000000", "#FFFFFF", "#808080", "#FFA500", "#800080", "#008000",
25    ];
26    sample(&colors).to_string()
27}
28
29/// Generate a random HTML entity
30pub fn entity() -> String {
31    fetch_locale("html.entities", "en")
32        .map(|v| sample(&v))
33        .unwrap_or_else(|| sample(FALLBACK_ENTITIES).to_string())
34}
35
36// Fallback data
37const FALLBACK_TAGS: &[&str] = &[
38    "div", "span", "p", "a", "img", "ul", "ol", "li", "h1", "h2", "h3",
39    "h4", "h5", "h6", "table", "tr", "td", "th", "form", "input", "button",
40    "select", "option", "textarea", "label", "header", "footer", "nav",
41    "section", "article", "aside", "main", "canvas", "video", "audio",
42];
43
44const FALLBACK_ATTRIBUTES: &[&str] = &[
45    "class", "id", "style", "href", "src", "alt", "title", "type",
46    "name", "value", "placeholder", "disabled", "checked", "selected",
47    "readonly", "required", "multiple", "autofocus", "autocomplete",
48];
49
50const FALLBACK_ENTITIES: &[&str] = &[
51    "&", "<", ">", """, "'", " ",
52    "©", "®", "™", "€", "£", "¥",
53];
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_tag() {
61        assert!(!tag().is_empty());
62    }
63
64    #[test]
65    fn test_attribute() {
66        assert!(!attribute().is_empty());
67    }
68
69    #[test]
70    fn test_color() {
71        assert!(!color().is_empty());
72    }
73
74    #[test]
75    fn test_entity() {
76        assert!(!entity().is_empty());
77    }
78}