| 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 |
|
| 37 |
use quasi_declare::declare; |
| 38 |
use quasi_router::RegionKind; |
| 39 |
|
| 40 |
|
| 41 |
pub const NAME: &str = "carousel"; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
fn kind() -> RegionKind { |
| 50 |
RegionKind::Widget { |
| 51 |
name: NAME.to_string(), |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 61 |
pub struct Size { |
| 62 |
|
| 63 |
pub width: u32, |
| 64 |
|
| 65 |
pub height: u32, |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 74 |
pub struct Frame { |
| 75 |
|
| 76 |
pub src: String, |
| 77 |
|
| 78 |
pub alt: String, |
| 79 |
|
| 80 |
pub caption: Option<String>, |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
pub intrinsic: Option<Size>, |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
pub defer: bool, |
| 100 |
} |
| 101 |
|
| 102 |
impl Frame { |
| 103 |
|
| 104 |
pub fn new(src: impl Into<String>, alt: impl Into<String>) -> Self { |
| 105 |
Self { |
| 106 |
src: src.into(), |
| 107 |
alt: alt.into(), |
| 108 |
caption: None, |
| 109 |
intrinsic: None, |
| 110 |
defer: false, |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
#[must_use] |
| 116 |
pub const fn intrinsic(mut self, width: u32, height: u32) -> Self { |
| 117 |
self.intrinsic = Some(Size { width, height }); |
| 118 |
self |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
#[must_use] |
| 123 |
pub fn caption(mut self, caption: impl Into<String>) -> Self { |
| 124 |
self.caption = Some(caption.into()); |
| 125 |
self |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
#[derive(Debug, Clone, PartialEq, Eq, Default)] |
| 142 |
pub struct Gallery { |
| 143 |
|
| 144 |
pub frames: Vec<Frame>, |
| 145 |
} |
| 146 |
|
| 147 |
impl Gallery { |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
pub fn new(frames: impl IntoIterator<Item = Frame>) -> Self { |
| 154 |
Self { |
| 155 |
frames: frames |
| 156 |
.into_iter() |
| 157 |
.enumerate() |
| 158 |
.map(|(i, frame)| Frame { |
| 159 |
defer: i > 0, |
| 160 |
..frame |
| 161 |
}) |
| 162 |
.collect(), |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
impl FromIterator<Frame> for Gallery { |
| 168 |
fn from_iter<I: IntoIterator<Item = Frame>>(frames: I) -> Self { |
| 169 |
Self::new(frames) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
declare! { |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
pub shape frames(gallery: &Gallery) -> Vec<Node>; |
| 181 |
|
| 182 |
for frame in gallery.frames.iter() { |
| 183 |
picture &frame.src &frame.alt { |
| 184 |
fit Natural; |
| 185 |
lazy when frame.defer; |
| 186 |
for size in frame.intrinsic.iter() { |
| 187 |
intrinsic size.width size.height; |
| 188 |
} |
| 189 |
for line in frame.caption.iter() { |
| 190 |
caption line; |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
declare! { |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
pub shape carousel(id: &str, gallery: &Gallery) -> Slot; |
| 217 |
|
| 218 |
region id as kind() { |
| 219 |
showing_one 0; |
| 220 |
extend frames(gallery); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
#[cfg(test)] |
| 225 |
mod tests { |
| 226 |
use super::*; |
| 227 |
use quasi_http::Serves as _; |
| 228 |
|
| 229 |
fn render(id: &str, items: Vec<Frame>) -> String { |
| 230 |
let gallery = Gallery::new(items); |
| 231 |
quasi_webview::Webview::new().fragment(&quasi_router::Node::Region(carousel(id, &gallery))) |
| 232 |
} |
| 233 |
|
| 234 |
fn three() -> Vec<Frame> { |
| 235 |
vec![ |
| 236 |
Frame::new("/a.png", "The library, mid-import").caption("Library"), |
| 237 |
Frame::new("/b.png", "A project page with two items"), |
| 238 |
Frame::new("/c.png", "The payouts table"), |
| 239 |
] |
| 240 |
} |
| 241 |
|
| 242 |
#[test] |
| 243 |
fn the_name_is_on_the_region_for_a_renderer_that_knows_it() { |
| 244 |
let html = render("landing-shots", three()); |
| 245 |
assert!(html.contains(r#"data-widget="carousel""#), "{html}"); |
| 246 |
assert!(html.contains(r#"id="landing-shots""#), "{html}"); |
| 247 |
} |
| 248 |
|
| 249 |
#[test] |
| 250 |
fn every_frame_is_in_the_markup_and_not_only_the_first() { |
| 251 |
|
| 252 |
|
| 253 |
let html = render("g", three()); |
| 254 |
for src in ["/a.png", "/b.png", "/c.png"] { |
| 255 |
assert!(html.contains(src), "{src} missing from {html}"); |
| 256 |
} |
| 257 |
let first = html.find("/a.png").unwrap(); |
| 258 |
let second = html.find("/b.png").unwrap(); |
| 259 |
let third = html.find("/c.png").unwrap(); |
| 260 |
assert!(first < second && second < third, "frames out of order"); |
| 261 |
} |
| 262 |
|
| 263 |
#[test] |
| 264 |
fn a_frame_says_what_it_shows_to_someone_not_looking_at_it() { |
| 265 |
let html = render("g", three()); |
| 266 |
assert!(html.contains(r#"alt="The library, mid-import""#), "{html}"); |
| 267 |
assert!(html.contains(r#"alt="The payouts table""#), "{html}"); |
| 268 |
} |
| 269 |
|
| 270 |
#[test] |
| 271 |
fn a_captioned_frame_is_a_figure_and_a_bare_one_is_not() { |
| 272 |
let html = render("g", three()); |
| 273 |
|
| 274 |
assert_eq!(html.matches("<figure").count(), 1, "{html}"); |
| 275 |
assert!(html.contains("Library</figcaption>"), "{html}"); |
| 276 |
} |
| 277 |
|
| 278 |
#[test] |
| 279 |
fn a_frame_that_knows_its_size_reserves_its_space() { |
| 280 |
|
| 281 |
|
| 282 |
let html = render( |
| 283 |
"g", |
| 284 |
vec![Frame::new("/a.png", "Alpha").intrinsic(5120, 3412)], |
| 285 |
); |
| 286 |
assert!(html.contains(r#"width="5120" height="3412""#), "{html}"); |
| 287 |
} |
| 288 |
|
| 289 |
#[test] |
| 290 |
fn a_frame_that_does_not_know_its_size_says_nothing() { |
| 291 |
|
| 292 |
|
| 293 |
let html = render("g", vec![Frame::new("/a.png", "Alpha")]); |
| 294 |
assert!(!html.contains("width="), "{html}"); |
| 295 |
assert!(!html.contains("height="), "{html}"); |
| 296 |
} |
| 297 |
|
| 298 |
#[test] |
| 299 |
fn the_visible_frame_is_fetched_now_and_the_rest_can_wait() { |
| 300 |
|
| 301 |
|
| 302 |
let html = render("g", three()); |
| 303 |
assert_eq!( |
| 304 |
html.matches("loading=\"lazy\"").count(), |
| 305 |
2, |
| 306 |
"expected the two offscreen frames only: {html}" |
| 307 |
); |
| 308 |
|
| 309 |
|
| 310 |
let upto_second = &html[..html.find("/b.png").unwrap()]; |
| 311 |
assert!( |
| 312 |
!upto_second.contains("loading=\"lazy\""), |
| 313 |
"the visible frame must not be deferred: {html}" |
| 314 |
); |
| 315 |
} |
| 316 |
|
| 317 |
#[test] |
| 318 |
fn a_screenshot_keeps_its_own_shape() { |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
let html = render("g", three()); |
| 323 |
assert!(!html.contains("data-fit"), "{html}"); |
| 324 |
} |
| 325 |
|
| 326 |
#[test] |
| 327 |
fn a_hostile_source_cannot_break_out_of_the_attribute() { |
| 328 |
let html = render( |
| 329 |
"g", |
| 330 |
vec![Frame::new(r#"x" onerror="alert(1)"#, "</title><script>")], |
| 331 |
); |
| 332 |
assert!(!html.contains("onerror=\"alert"), "{html}"); |
| 333 |
assert!(!html.contains("<script>"), "{html}"); |
| 334 |
} |
| 335 |
|
| 336 |
#[test] |
| 337 |
fn the_empty_case_is_a_region_with_nothing_in_it() { |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
let html = render("g", Vec::new()); |
| 342 |
assert!(html.contains(r#"data-widget="carousel""#), "{html}"); |
| 343 |
assert!(!html.contains("<img"), "{html}"); |
| 344 |
} |
| 345 |
|
| 346 |
#[test] |
| 347 |
fn the_gallery_decides_which_frame_waits_and_the_description_only_reads_it() { |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
let gallery = Gallery::new(three()); |
| 352 |
let waiting: Vec<bool> = gallery.frames.iter().map(|frame| frame.defer).collect(); |
| 353 |
assert_eq!(waiting, vec![false, true, true]); |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
assert!(Gallery::new(Vec::new()).frames.is_empty()); |
| 358 |
} |
| 359 |
|
| 360 |
#[test] |
| 361 |
fn the_chrome_is_derived_and_no_renderer_was_told_what_a_carousel_is() { |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
let html = render("shots", three()); |
| 367 |
|
| 368 |
assert!(html.contains("data-showing=\"one\""), "{html}"); |
| 369 |
assert!(html.contains("data-shows=\"previous\""), "{html}"); |
| 370 |
assert!(html.contains("data-shows=\"next\""), "{html}"); |
| 371 |
assert!(html.contains(">1 / 3</span>"), "{html}"); |
| 372 |
|
| 373 |
|
| 374 |
assert!(!html.contains("dot"), "{html}"); |
| 375 |
} |
| 376 |
} |
| 377 |
|