| 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 |
use crate::form::escape_into; |
| 27 |
use crate::{Emit, push_class}; |
| 28 |
use makeover_layout::{Figure, Intent, Tone}; |
| 29 |
use std::fmt::Write as _; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
pub const FIGURE_CLASSES: &[&str] = &[ |
| 37 |
"figures", |
| 38 |
"figure", |
| 39 |
"figure-value", |
| 40 |
"figure-caption", |
| 41 |
"figure-change", |
| 42 |
]; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
#[must_use] |
| 52 |
pub fn figure_text(figure: &Figure<'_>) -> String { |
| 53 |
figure.change.map_or_else( |
| 54 |
|| format!("{}: {}", figure.caption, figure.value), |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|change| format!("{}: {}, {change}", figure.caption, figure.value), |
| 59 |
) |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#[must_use] |
| 75 |
pub fn figure_html(figure: &Figure<'_>, opts: &Emit) -> String { |
| 76 |
let mut html = String::new(); |
| 77 |
figure_html_into(figure, opts, &mut html); |
| 78 |
html |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
pub fn figure_html_into(figure: &Figure<'_>, opts: &Emit, out: &mut String) { |
| 88 |
out.push_str("<div class=\""); |
| 89 |
push_class(out, "figure", opts); |
| 90 |
out.push_str("\" aria-label=\""); |
| 91 |
escape_into(figure.caption, out); |
| 92 |
out.push_str(": "); |
| 93 |
escape_into(figure.value, out); |
| 94 |
if let Some(change) = figure.change { |
| 95 |
out.push_str(", "); |
| 96 |
escape_into(change, out); |
| 97 |
} |
| 98 |
out.push('"'); |
| 99 |
|
| 100 |
|
| 101 |
if figure.tone != Tone::Neutral { |
| 102 |
let _ = write!(out, " data-tone=\"{}\"", figure.tone.token()); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
out.push_str("><span class=\""); |
| 108 |
push_class(out, "figure-value", opts); |
| 109 |
out.push_str("\" aria-hidden=\"true\">"); |
| 110 |
escape_into(figure.value, out); |
| 111 |
out.push_str("</span><span class=\""); |
| 112 |
push_class(out, "figure-caption", opts); |
| 113 |
out.push_str("\" aria-hidden=\"true\">"); |
| 114 |
escape_into(figure.caption, out); |
| 115 |
out.push_str("</span>"); |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
if let Some(change) = figure.change { |
| 122 |
out.push_str("<span class=\""); |
| 123 |
push_class(out, "figure-change", opts); |
| 124 |
out.push_str("\" aria-hidden=\"true\">"); |
| 125 |
escape_into(change, out); |
| 126 |
out.push_str("</span>"); |
| 127 |
} |
| 128 |
out.push_str("</div>"); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
#[must_use] |
| 138 |
pub fn figures_html(figures: &[Figure<'_>], opts: &Emit) -> String { |
| 139 |
let mut html = String::new(); |
| 140 |
figures_html_into(figures, opts, &mut html); |
| 141 |
html |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
pub fn figures_html_into(figures: &[Figure<'_>], opts: &Emit, out: &mut String) { |
| 149 |
out.push_str("<div class=\""); |
| 150 |
push_class(out, "figures", opts); |
| 151 |
out.push_str("\">"); |
| 152 |
for figure in figures { |
| 153 |
figure_html_into(figure, opts, out); |
| 154 |
} |
| 155 |
out.push_str("</div>"); |
| 156 |
} |
| 157 |
|
| 158 |
#[cfg(test)] |
| 159 |
mod tests { |
| 160 |
use super::*; |
| 161 |
use crate::form::escape; |
| 162 |
|
| 163 |
#[test] |
| 164 |
fn the_noun_reaches_a_reader_before_the_number() { |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
let figure = Figure::new("17", "Current Streak"); |
| 169 |
assert_eq!(figure_text(&figure), "Current Streak: 17"); |
| 170 |
|
| 171 |
let html = figure_html(&figure, &Emit::default()); |
| 172 |
assert!(html.contains(r#"aria-label="Current Streak: 17""#)); |
| 173 |
|
| 174 |
assert_eq!(html.matches(r#"aria-hidden="true""#).count(), 2); |
| 175 |
} |
| 176 |
|
| 177 |
#[test] |
| 178 |
fn a_change_is_its_own_span_and_reaches_a_reader_through_the_name() { |
| 179 |
|
| 180 |
|
| 181 |
let figure = Figure::new("1,204", "Views").change("+12.5%"); |
| 182 |
assert_eq!(figure_text(&figure), "Views: 1,204, +12.5%"); |
| 183 |
|
| 184 |
let html = figure_html(&figure, &Emit::default()); |
| 185 |
assert!(html.contains("figure-change"), "{html}"); |
| 186 |
assert!(html.contains(">+12.5%<"), "{html}"); |
| 187 |
assert_eq!(html.matches(r#"aria-hidden="true""#).count(), 3); |
| 188 |
|
| 189 |
|
| 190 |
let plain = figure_html(&Figure::new("17", "Total"), &Emit::default()); |
| 191 |
assert!(!plain.contains("figure-change"), "{plain}"); |
| 192 |
} |
| 193 |
|
| 194 |
#[test] |
| 195 |
fn a_change_carries_no_tone_of_its_own() { |
| 196 |
|
| 197 |
|
| 198 |
let html = figure_html( |
| 199 |
&Figure::new("1,204", "Views") |
| 200 |
.change("-4%") |
| 201 |
.tone(Tone::Danger), |
| 202 |
&Emit::default(), |
| 203 |
); |
| 204 |
assert_eq!(html.matches("data-tone").count(), 1, "{html}"); |
| 205 |
} |
| 206 |
|
| 207 |
#[test] |
| 208 |
fn a_change_is_text_and_cannot_become_markup() { |
| 209 |
let html = figure_html( |
| 210 |
&Figure::new("1", "Views").change("<img src=x onerror=alert(1)>"), |
| 211 |
&Emit::default(), |
| 212 |
); |
| 213 |
assert!(!html.contains("<img"), "{html}"); |
| 214 |
} |
| 215 |
|
| 216 |
#[test] |
| 217 |
fn an_untoned_figure_emits_no_tone_attribute() { |
| 218 |
|
| 219 |
|
| 220 |
let plain = figure_html(&Figure::new("17", "Total"), &Emit::default()); |
| 221 |
assert!(!plain.contains("data-tone")); |
| 222 |
|
| 223 |
let toned = figure_html( |
| 224 |
&Figure::new("0", "Current Streak").tone(Tone::Warning), |
| 225 |
&Emit::default(), |
| 226 |
); |
| 227 |
assert!(toned.contains(r#"data-tone="warning""#)); |
| 228 |
} |
| 229 |
|
| 230 |
#[test] |
| 231 |
fn a_value_and_a_caption_are_escaped_like_every_other_string() { |
| 232 |
|
| 233 |
let html = figure_html(&Figure::new("<b>3</b>", "a & b"), &Emit::default()); |
| 234 |
assert!(html.contains("a & b")); |
| 235 |
assert!(html.contains("<b>")); |
| 236 |
assert!(!html.contains("<b>")); |
| 237 |
} |
| 238 |
|
| 239 |
#[test] |
| 240 |
fn a_strip_is_the_unit_because_a_renderer_cannot_infer_a_set() { |
| 241 |
let html = figures_html( |
| 242 |
&[ |
| 243 |
Figure::new("17", "Current Streak"), |
| 244 |
Figure::new("84%", "Completion Rate"), |
| 245 |
], |
| 246 |
&Emit::default(), |
| 247 |
); |
| 248 |
assert!(html.starts_with(r#"<div class="figures">"#)); |
| 249 |
assert_eq!(html.matches(r#"class="figure""#).count(), 2); |
| 250 |
} |
| 251 |
|
| 252 |
#[test] |
| 253 |
fn an_empty_strip_renders_as_an_empty_strip() { |
| 254 |
|
| 255 |
|
| 256 |
let html = figures_html(&[], &Emit::default()); |
| 257 |
assert_eq!(html, r#"<div class="figures"></div>"#); |
| 258 |
assert!(!html.contains("figure-")); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
#[test] |
| 265 |
fn a_streamed_figure_is_the_figure_the_other_form_returns() { |
| 266 |
let opts = Emit { |
| 267 |
class_prefix: "mo-", |
| 268 |
..Emit::default() |
| 269 |
}; |
| 270 |
for figure in [ |
| 271 |
Figure::new("17", "Total"), |
| 272 |
Figure::new("<b>3</b>", "a & b").tone(Tone::Danger), |
| 273 |
Figure::new("1,204", "Views & co").change("+12.5% <up>"), |
| 274 |
] { |
| 275 |
let mut streamed = String::new(); |
| 276 |
figure_html_into(&figure, &opts, &mut streamed); |
| 277 |
assert_eq!(streamed, figure_html(&figure, &opts)); |
| 278 |
assert!( |
| 279 |
streamed.contains(&format!("aria-label=\"{}\"", escape(&figure_text(&figure)))), |
| 280 |
"{streamed}" |
| 281 |
); |
| 282 |
|
| 283 |
let mut strip = String::new(); |
| 284 |
figures_html_into(&[figure], &opts, &mut strip); |
| 285 |
assert_eq!(strip, figures_html(&[figure], &opts)); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
#[test] |
| 290 |
fn the_prefix_reaches_every_class() { |
| 291 |
|
| 292 |
|
| 293 |
let opts = Emit { |
| 294 |
class_prefix: "mo-", |
| 295 |
..Emit::default() |
| 296 |
}; |
| 297 |
let html = figures_html(&[Figure::new("17", "Total")], &opts); |
| 298 |
assert!(html.contains(r#"class="mo-figures""#)); |
| 299 |
assert!(html.contains(r#"class="mo-figure""#)); |
| 300 |
assert!(html.contains(r#"class="mo-figure-value""#)); |
| 301 |
assert!(html.contains(r#"class="mo-figure-caption""#)); |
| 302 |
} |
| 303 |
} |
| 304 |
|