| 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::{Markup, escape}; |
| 28 |
use crate::{Emit, class}; |
| 29 |
use makeover_layout::{Intent, Readiness, Tone}; |
| 30 |
use std::fmt::Write as _; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#[must_use] |
| 51 |
pub fn placeholder_html( |
| 52 |
state: Readiness, |
| 53 |
message: &str, |
| 54 |
action: Option<Markup<'_>>, |
| 55 |
opts: &Emit, |
| 56 |
) -> String { |
| 57 |
if state.shows_content() { |
| 58 |
return String::new(); |
| 59 |
} |
| 60 |
|
| 61 |
let name = state_name(state); |
| 62 |
let mut html = format!( |
| 63 |
"<div class=\"{}\" data-state=\"{name}\"", |
| 64 |
class("placeholder", opts) |
| 65 |
); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
if state.tone() != Tone::Neutral { |
| 71 |
let _ = write!(html, " data-tone=\"{}\"", state.tone().token()); |
| 72 |
} |
| 73 |
if state.tone() == Tone::Danger { |
| 74 |
html.push_str(" role=\"alert\""); |
| 75 |
} else { |
| 76 |
html.push_str(" role=\"status\" aria-live=\"polite\""); |
| 77 |
} |
| 78 |
|
| 79 |
let _ = write!( |
| 80 |
html, |
| 81 |
"><p class=\"{}\">{}</p>", |
| 82 |
class("placeholder-text", opts), |
| 83 |
escape(message) |
| 84 |
); |
| 85 |
if let Some(Markup(markup)) = action { |
| 86 |
let _ = write!( |
| 87 |
html, |
| 88 |
"<div class=\"{}\">{markup}</div>", |
| 89 |
class("placeholder-action", opts) |
| 90 |
); |
| 91 |
} |
| 92 |
html.push_str("</div>"); |
| 93 |
html |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
fn state_name(state: Readiness) -> &'static str { |
| 103 |
match state { |
| 104 |
Readiness::Ready => "ready", |
| 105 |
Readiness::Pending => "pending", |
| 106 |
Readiness::Empty => "empty", |
| 107 |
Readiness::Failed => "failed", |
| 108 |
_ => "unknown", |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
#[cfg(test)] |
| 113 |
mod tests { |
| 114 |
use super::*; |
| 115 |
|
| 116 |
#[test] |
| 117 |
fn the_state_that_shows_content_draws_no_stand_in() { |
| 118 |
|
| 119 |
|
| 120 |
assert!(placeholder_html(Readiness::Ready, "x", None, &Emit::default()).is_empty()); |
| 121 |
} |
| 122 |
|
| 123 |
#[test] |
| 124 |
fn an_empty_region_is_not_announced_as_a_fault() { |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
let empty = placeholder_html(Readiness::Empty, "No projects yet", None, &Emit::default()); |
| 129 |
assert!(empty.contains(r#"role="status""#)); |
| 130 |
assert!(!empty.contains("data-tone")); |
| 131 |
|
| 132 |
let failed = placeholder_html( |
| 133 |
Readiness::Failed, |
| 134 |
"Failed to load events", |
| 135 |
None, |
| 136 |
&Emit::default(), |
| 137 |
); |
| 138 |
assert!(failed.contains(r#"role="alert""#)); |
| 139 |
assert!(failed.contains(r#"data-tone="danger""#)); |
| 140 |
} |
| 141 |
|
| 142 |
#[test] |
| 143 |
fn the_message_is_escaped_and_the_action_is_not() { |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
let html = placeholder_html( |
| 148 |
Readiness::Empty, |
| 149 |
"No <b>projects</b> yet", |
| 150 |
Some(Markup("<button>Add one</button>")), |
| 151 |
&Emit::default(), |
| 152 |
); |
| 153 |
assert!(html.contains("<b>")); |
| 154 |
assert!(!html.contains("<b>")); |
| 155 |
assert!(html.contains("<button>Add one</button>")); |
| 156 |
} |
| 157 |
|
| 158 |
#[test] |
| 159 |
fn a_state_with_no_action_emits_no_action_container() { |
| 160 |
|
| 161 |
|
| 162 |
let html = placeholder_html(Readiness::Empty, "Nothing here", None, &Emit::default()); |
| 163 |
assert!(!html.contains("placeholder-action")); |
| 164 |
} |
| 165 |
|
| 166 |
#[test] |
| 167 |
fn pending_draws_the_same_anatomy_as_the_other_two() { |
| 168 |
|
| 169 |
|
| 170 |
let html = placeholder_html(Readiness::Pending, "Loading", None, &Emit::default()); |
| 171 |
assert!(html.contains(r#"data-state="pending""#)); |
| 172 |
assert!(html.contains("Loading")); |
| 173 |
} |
| 174 |
|
| 175 |
#[test] |
| 176 |
fn the_prefix_reaches_every_class() { |
| 177 |
let opts = Emit { |
| 178 |
class_prefix: "mo-", |
| 179 |
..Emit::default() |
| 180 |
}; |
| 181 |
let html = placeholder_html( |
| 182 |
Readiness::Empty, |
| 183 |
"None", |
| 184 |
Some(Markup("<button>Go</button>")), |
| 185 |
&opts, |
| 186 |
); |
| 187 |
assert!(html.contains(r#"class="mo-placeholder""#)); |
| 188 |
assert!(html.contains(r#"class="mo-placeholder-text""#)); |
| 189 |
assert!(html.contains(r#"class="mo-placeholder-action""#)); |
| 190 |
} |
| 191 |
} |
| 192 |
|