| 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 |
use crate::form::escape; |
| 28 |
use crate::{Emit, class}; |
| 29 |
use makeover_layout::{Figure, Intent, Tone}; |
| 30 |
use std::fmt::Write as _; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#[must_use] |
| 40 |
pub fn figure_text(figure: &Figure<'_>) -> String { |
| 41 |
format!("{}: {}", figure.caption, figure.value) |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#[must_use] |
| 57 |
pub fn figure_html(figure: &Figure<'_>, opts: &Emit) -> String { |
| 58 |
let mut html = format!( |
| 59 |
"<div class=\"{}\" aria-label=\"{}\"", |
| 60 |
class("figure", opts), |
| 61 |
escape(&figure_text(figure)) |
| 62 |
); |
| 63 |
|
| 64 |
|
| 65 |
if figure.tone != Tone::Neutral { |
| 66 |
let _ = write!(html, " data-tone=\"{}\"", figure.tone.token()); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
let _ = write!( |
| 72 |
html, |
| 73 |
"><span class=\"{}\" aria-hidden=\"true\">{}</span>\ |
| 74 |
<span class=\"{}\" aria-hidden=\"true\">{}</span></div>", |
| 75 |
class("figure-value", opts), |
| 76 |
escape(figure.value), |
| 77 |
class("figure-caption", opts), |
| 78 |
escape(figure.caption), |
| 79 |
); |
| 80 |
html |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
#[must_use] |
| 90 |
pub fn figures_html(figures: &[Figure<'_>], opts: &Emit) -> String { |
| 91 |
let mut html = format!("<div class=\"{}\">", class("figures", opts)); |
| 92 |
for figure in figures { |
| 93 |
html.push_str(&figure_html(figure, opts)); |
| 94 |
} |
| 95 |
html.push_str("</div>"); |
| 96 |
html |
| 97 |
} |
| 98 |
|
| 99 |
#[cfg(test)] |
| 100 |
mod tests { |
| 101 |
use super::*; |
| 102 |
|
| 103 |
#[test] |
| 104 |
fn the_noun_reaches_a_reader_before_the_number() { |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
let figure = Figure::new("17", "Current Streak"); |
| 109 |
assert_eq!(figure_text(&figure), "Current Streak: 17"); |
| 110 |
|
| 111 |
let html = figure_html(&figure, &Emit::default()); |
| 112 |
assert!(html.contains(r#"aria-label="Current Streak: 17""#)); |
| 113 |
|
| 114 |
assert_eq!(html.matches(r#"aria-hidden="true""#).count(), 2); |
| 115 |
} |
| 116 |
|
| 117 |
#[test] |
| 118 |
fn an_untoned_figure_emits_no_tone_attribute() { |
| 119 |
|
| 120 |
|
| 121 |
let plain = figure_html(&Figure::new("17", "Total"), &Emit::default()); |
| 122 |
assert!(!plain.contains("data-tone")); |
| 123 |
|
| 124 |
let toned = figure_html( |
| 125 |
&Figure::new("0", "Current Streak").tone(Tone::Warning), |
| 126 |
&Emit::default(), |
| 127 |
); |
| 128 |
assert!(toned.contains(r#"data-tone="warning""#)); |
| 129 |
} |
| 130 |
|
| 131 |
#[test] |
| 132 |
fn a_value_and_a_caption_are_escaped_like_every_other_string() { |
| 133 |
|
| 134 |
let html = figure_html(&Figure::new("<b>3</b>", "a & b"), &Emit::default()); |
| 135 |
assert!(html.contains("a & b")); |
| 136 |
assert!(html.contains("<b>")); |
| 137 |
assert!(!html.contains("<b>")); |
| 138 |
} |
| 139 |
|
| 140 |
#[test] |
| 141 |
fn a_strip_is_the_unit_because_a_renderer_cannot_infer_a_set() { |
| 142 |
let html = figures_html( |
| 143 |
&[ |
| 144 |
Figure::new("17", "Current Streak"), |
| 145 |
Figure::new("84%", "Completion Rate"), |
| 146 |
], |
| 147 |
&Emit::default(), |
| 148 |
); |
| 149 |
assert!(html.starts_with(r#"<div class="figures">"#)); |
| 150 |
assert_eq!(html.matches(r#"class="figure""#).count(), 2); |
| 151 |
} |
| 152 |
|
| 153 |
#[test] |
| 154 |
fn an_empty_strip_renders_as_an_empty_strip() { |
| 155 |
|
| 156 |
|
| 157 |
let html = figures_html(&[], &Emit::default()); |
| 158 |
assert_eq!(html, r#"<div class="figures"></div>"#); |
| 159 |
assert!(!html.contains("figure-")); |
| 160 |
} |
| 161 |
|
| 162 |
#[test] |
| 163 |
fn the_prefix_reaches_every_class() { |
| 164 |
|
| 165 |
|
| 166 |
let opts = Emit { |
| 167 |
class_prefix: "mo-", |
| 168 |
..Emit::default() |
| 169 |
}; |
| 170 |
let html = figures_html(&[Figure::new("17", "Total")], &opts); |
| 171 |
assert!(html.contains(r#"class="mo-figures""#)); |
| 172 |
assert!(html.contains(r#"class="mo-figure""#)); |
| 173 |
assert!(html.contains(r#"class="mo-figure-value""#)); |
| 174 |
assert!(html.contains(r#"class="mo-figure-caption""#)); |
| 175 |
} |
| 176 |
} |
| 177 |
|