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