| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
use makeover_layout::Fit; |
| 37 |
use quasi_router::{Node, Picture, Slot}; |
| 38 |
|
| 39 |
|
| 40 |
pub const NAME: &str = "carousel"; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 48 |
pub struct Frame { |
| 49 |
|
| 50 |
pub src: String, |
| 51 |
|
| 52 |
pub alt: String, |
| 53 |
|
| 54 |
pub caption: Option<String>, |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
pub intrinsic: Option<(u32, u32)>, |
| 62 |
} |
| 63 |
|
| 64 |
impl Frame { |
| 65 |
|
| 66 |
pub fn new(src: impl Into<String>, alt: impl Into<String>) -> Self { |
| 67 |
Self { |
| 68 |
src: src.into(), |
| 69 |
alt: alt.into(), |
| 70 |
caption: None, |
| 71 |
intrinsic: None, |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
#[must_use] |
| 77 |
pub const fn intrinsic(mut self, width: u32, height: u32) -> Self { |
| 78 |
self.intrinsic = Some((width, height)); |
| 79 |
self |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
#[must_use] |
| 84 |
pub fn caption(mut self, caption: impl Into<String>) -> Self { |
| 85 |
self.caption = Some(caption.into()); |
| 86 |
self |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
pub fn frames(frames: impl IntoIterator<Item = Frame>) -> impl Iterator<Item = Node> { |
| 96 |
frames.into_iter().enumerate().map(|(i, frame)| { |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
let mut picture = Picture::new(frame.src, frame.alt).fit(Fit::Natural); |
| 102 |
if let Some((w, h)) = frame.intrinsic { |
| 103 |
picture = picture.intrinsic(w, h); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
if i > 0 { |
| 116 |
picture = picture.lazy(); |
| 117 |
} |
| 118 |
Node::Image(match frame.caption { |
| 119 |
Some(caption) => picture.caption(caption), |
| 120 |
None => picture, |
| 121 |
}) |
| 122 |
}) |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
pub fn carousel(id: &str, items: impl IntoIterator<Item = Frame>) -> Slot { |
| 142 |
Slot::widget(id, NAME).extend(frames(items)).showing_one(0) |
| 143 |
} |
| 144 |
|
| 145 |
#[cfg(test)] |
| 146 |
mod tests { |
| 147 |
use super::*; |
| 148 |
use quasi_http::Serves as _; |
| 149 |
|
| 150 |
fn render(id: &str, items: Vec<Frame>) -> String { |
| 151 |
quasi_webview::Webview::new().fragment(&Node::Region(carousel(id, items))) |
| 152 |
} |
| 153 |
|
| 154 |
fn three() -> Vec<Frame> { |
| 155 |
vec![ |
| 156 |
Frame::new("/a.png", "The library, mid-import").caption("Library"), |
| 157 |
Frame::new("/b.png", "A project page with two items"), |
| 158 |
Frame::new("/c.png", "The payouts table"), |
| 159 |
] |
| 160 |
} |
| 161 |
|
| 162 |
#[test] |
| 163 |
fn the_name_is_on_the_region_for_a_renderer_that_knows_it() { |
| 164 |
let html = render("landing-shots", three()); |
| 165 |
assert!(html.contains(r#"data-widget="carousel""#), "{html}"); |
| 166 |
assert!(html.contains(r#"id="landing-shots""#), "{html}"); |
| 167 |
} |
| 168 |
|
| 169 |
#[test] |
| 170 |
fn every_frame_is_in_the_markup_and_not_only_the_first() { |
| 171 |
|
| 172 |
|
| 173 |
let html = render("g", three()); |
| 174 |
for src in ["/a.png", "/b.png", "/c.png"] { |
| 175 |
assert!(html.contains(src), "{src} missing from {html}"); |
| 176 |
} |
| 177 |
let first = html.find("/a.png").unwrap(); |
| 178 |
let second = html.find("/b.png").unwrap(); |
| 179 |
let third = html.find("/c.png").unwrap(); |
| 180 |
assert!(first < second && second < third, "frames out of order"); |
| 181 |
} |
| 182 |
|
| 183 |
#[test] |
| 184 |
fn a_frame_says_what_it_shows_to_someone_not_looking_at_it() { |
| 185 |
let html = render("g", three()); |
| 186 |
assert!(html.contains(r#"alt="The library, mid-import""#), "{html}"); |
| 187 |
assert!(html.contains(r#"alt="The payouts table""#), "{html}"); |
| 188 |
} |
| 189 |
|
| 190 |
#[test] |
| 191 |
fn a_captioned_frame_is_a_figure_and_a_bare_one_is_not() { |
| 192 |
let html = render("g", three()); |
| 193 |
|
| 194 |
assert_eq!(html.matches("<figure").count(), 1, "{html}"); |
| 195 |
assert!(html.contains("Library</figcaption>"), "{html}"); |
| 196 |
} |
| 197 |
|
| 198 |
#[test] |
| 199 |
fn a_frame_that_knows_its_size_reserves_its_space() { |
| 200 |
|
| 201 |
|
| 202 |
let html = render( |
| 203 |
"g", |
| 204 |
vec![Frame::new("/a.png", "Alpha").intrinsic(5120, 3412)], |
| 205 |
); |
| 206 |
assert!(html.contains(r#"width="5120" height="3412""#), "{html}"); |
| 207 |
} |
| 208 |
|
| 209 |
#[test] |
| 210 |
fn a_frame_that_does_not_know_its_size_says_nothing() { |
| 211 |
|
| 212 |
|
| 213 |
let html = render("g", vec![Frame::new("/a.png", "Alpha")]); |
| 214 |
assert!(!html.contains("width="), "{html}"); |
| 215 |
assert!(!html.contains("height="), "{html}"); |
| 216 |
} |
| 217 |
|
| 218 |
#[test] |
| 219 |
fn the_visible_frame_is_fetched_now_and_the_rest_can_wait() { |
| 220 |
|
| 221 |
|
| 222 |
let html = render("g", three()); |
| 223 |
assert_eq!( |
| 224 |
html.matches("loading=\"lazy\"").count(), |
| 225 |
2, |
| 226 |
"expected the two offscreen frames only: {html}" |
| 227 |
); |
| 228 |
|
| 229 |
|
| 230 |
let upto_second = &html[..html.find("/b.png").unwrap()]; |
| 231 |
assert!( |
| 232 |
!upto_second.contains("loading=\"lazy\""), |
| 233 |
"the visible frame must not be deferred: {html}" |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
#[test] |
| 238 |
fn a_screenshot_keeps_its_own_shape() { |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
let html = render("g", three()); |
| 243 |
assert!(!html.contains("data-fit"), "{html}"); |
| 244 |
} |
| 245 |
|
| 246 |
#[test] |
| 247 |
fn a_hostile_source_cannot_break_out_of_the_attribute() { |
| 248 |
let html = render( |
| 249 |
"g", |
| 250 |
vec![Frame::new(r#"x" onerror="alert(1)"#, "</title><script>")], |
| 251 |
); |
| 252 |
assert!(!html.contains("onerror=\"alert"), "{html}"); |
| 253 |
assert!(!html.contains("<script>"), "{html}"); |
| 254 |
} |
| 255 |
|
| 256 |
#[test] |
| 257 |
fn the_empty_case_is_a_region_with_nothing_in_it() { |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
let html = render("g", Vec::new()); |
| 262 |
assert!(html.contains(r#"data-widget="carousel""#), "{html}"); |
| 263 |
assert!(!html.contains("<img"), "{html}"); |
| 264 |
} |
| 265 |
|
| 266 |
#[test] |
| 267 |
fn the_chrome_is_derived_and_no_renderer_was_told_what_a_carousel_is() { |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
let html = render("shots", three()); |
| 273 |
|
| 274 |
assert!(html.contains("data-showing=\"one\""), "{html}"); |
| 275 |
assert!(html.contains("data-shows=\"previous\""), "{html}"); |
| 276 |
assert!(html.contains("data-shows=\"next\""), "{html}"); |
| 277 |
assert!(html.contains(">1 / 3</span>"), "{html}"); |
| 278 |
|
| 279 |
|
| 280 |
assert!(!html.contains("dot"), "{html}"); |
| 281 |
} |
| 282 |
} |
| 283 |
|