| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
use makeover_layout as layout; |
| 49 |
use quasi_axum::Serves as _; |
| 50 |
use quasi_router::screen::{Act, Picture, Row}; |
| 51 |
use quasi_router::{Action, Chrome, Node, RegionKind, Screen, Slot}; |
| 52 |
use quasi_webview::{Shell, Webview}; |
| 53 |
|
| 54 |
use crate::templates::{EMBED_GEOMETRY_CSS, EMBED_TYPOGRAPHY_CSS, embed_theme_css}; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
const LAYOUT_CSS: &str = include_str!("../../static/layout.css"); |
| 63 |
|
| 64 |
|
| 65 |
const REGION: &str = "embed"; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
const DOCUMENT_CSS: &str = "\ |
| 85 |
* { margin: 0; padding: 0; box-sizing: border-box; } |
| 86 |
body { |
| 87 |
font-family: var(--font-sans); |
| 88 |
background: var(--surface-page); |
| 89 |
color: var(--content); |
| 90 |
} |
| 91 |
main.list-detail { |
| 92 |
display: block; |
| 93 |
min-height: 100vh; |
| 94 |
padding: var(--step-base) var(--gap-section); |
| 95 |
} |
| 96 |
body.embed-button .picture-img { flex: none; width: 40px; height: 40px; } |
| 97 |
body.embed-tip .picture-img { flex: none; width: 32px; height: 32px; border-radius: 50%; } |
| 98 |
"; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
#[must_use] |
| 107 |
pub fn document(screen: &Screen, body_class: Option<&str>) -> String { |
| 108 |
let mut shell = Shell::default() |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
.without_htmx() |
| 122 |
.without_hyperscript() |
| 123 |
.without_clock() |
| 124 |
.without_fill() |
| 125 |
.without_reveal() |
| 126 |
.without_repeat() |
| 127 |
.without_copy() |
| 128 |
.with_chrome(Chrome::new()) |
| 129 |
.with_head_first(head_first()); |
| 130 |
shell.body_class = body_class.map(str::to_owned); |
| 131 |
Webview::new().with_shell(shell).screen(screen) |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
fn head_first() -> String { |
| 140 |
format!( |
| 141 |
"<style>{}{}{}{}{}</style>", |
| 142 |
embed_theme_css(), |
| 143 |
EMBED_GEOMETRY_CSS, |
| 144 |
EMBED_TYPOGRAPHY_CSS, |
| 145 |
LAYOUT_CSS, |
| 146 |
DOCUMENT_CSS, |
| 147 |
) |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
fn one(title: &str, node: Node) -> Screen { |
| 152 |
Screen::list_detail(title, false).with(Slot::new(REGION, RegionKind::Pane).with(node)) |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
fn cover(url: &str) -> Node { |
| 161 |
let mut picture = Picture::new(url, ""); |
| 162 |
picture.fit = layout::Fit::Cover; |
| 163 |
Node::Image(picture) |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
pub struct ItemView { |
| 171 |
|
| 172 |
pub title: String, |
| 173 |
|
| 174 |
pub price: String, |
| 175 |
|
| 176 |
pub button_text: String, |
| 177 |
|
| 178 |
pub purchase_url: String, |
| 179 |
|
| 180 |
pub cover_image_url: Option<String>, |
| 181 |
|
| 182 |
pub creator_display_name: String, |
| 183 |
|
| 184 |
pub profile_url: String, |
| 185 |
|
| 186 |
pub description_excerpt: String, |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
fn buy(view: &ItemView) -> Act { |
| 195 |
Act::new(&view.button_text, Action::external(&view.purchase_url)) |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
#[must_use] |
| 204 |
pub fn item_button(view: &ItemView) -> Screen { |
| 205 |
let mut row = Row::new(""); |
| 206 |
if let Some(url) = &view.cover_image_url { |
| 207 |
row = row.part(layout::RowPart::Primary, cover(url)); |
| 208 |
} |
| 209 |
let row = row |
| 210 |
.part(layout::RowPart::Primary, Node::text(&view.title)) |
| 211 |
.meta(&view.price) |
| 212 |
.part(layout::RowPart::Actions, Node::Act(buy(view))); |
| 213 |
one(&view.title, Node::list([row])) |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
#[must_use] |
| 226 |
pub fn item_card(view: &ItemView) -> Screen { |
| 227 |
let mut slot = Slot::new(REGION, RegionKind::Pane); |
| 228 |
if let Some(url) = &view.cover_image_url { |
| 229 |
slot = slot.with(cover(url)); |
| 230 |
} |
| 231 |
slot = slot.with(Node::section(&view.title)).with(Node::Link { |
| 232 |
text: format!("by {}", view.creator_display_name), |
| 233 |
action: Action::external(&view.profile_url), |
| 234 |
}); |
| 235 |
if !view.description_excerpt.is_empty() { |
| 236 |
slot = slot.with(Node::text(&view.description_excerpt)); |
| 237 |
} |
| 238 |
let slot = slot |
| 239 |
.with(Node::text(&view.price)) |
| 240 |
.with(Node::Act(buy(view))); |
| 241 |
Screen::list_detail(&view.title, false).with(slot) |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
#[must_use] |
| 256 |
pub fn item_player(view: &ItemView) -> Screen { |
| 257 |
let mut slot = Slot::new(REGION, RegionKind::Pane); |
| 258 |
if let Some(url) = &view.cover_image_url { |
| 259 |
slot = slot.with(cover(url)); |
| 260 |
} |
| 261 |
let slot = slot |
| 262 |
.with(Node::section(&view.title)) |
| 263 |
.with(Node::text(format!("by {}", view.creator_display_name))) |
| 264 |
.with(Node::text(&view.price)) |
| 265 |
.with(Node::Act(buy(view))); |
| 266 |
|
| 267 |
Screen::list_detail(&view.title, false) |
| 268 |
.with(slot) |
| 269 |
.with(Slot::new( |
| 270 |
PLAYER_REGION, |
| 271 |
RegionKind::Bespoke { |
| 272 |
name: "media-transport".into(), |
| 273 |
}, |
| 274 |
)) |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
pub const PLAYER_REGION: &str = "transport"; |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
#[must_use] |
| 287 |
pub fn player_document(view: &ItemView, preview_url: &str) -> String { |
| 288 |
let mut shell = Shell::default() |
| 289 |
.without_htmx() |
| 290 |
.without_hyperscript() |
| 291 |
.without_clock() |
| 292 |
.without_fill() |
| 293 |
.without_reveal() |
| 294 |
.without_repeat() |
| 295 |
.without_copy() |
| 296 |
.with_chrome(Chrome::new()) |
| 297 |
.with_head_first(format!("{}<style>{PLAYER_CSS}</style>", head_first())); |
| 298 |
shell.body_class = Some("embed-player".to_owned()); |
| 299 |
Webview::new() |
| 300 |
.with_shell(shell) |
| 301 |
.with_fill(PLAYER_REGION, player_markup(preview_url)) |
| 302 |
.screen(&item_player(view)) |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
#[must_use] |
| 315 |
pub fn player_markup(preview_url: &str) -> String { |
| 316 |
format!( |
| 317 |
r#"<div class="transport" data-preview-url="{}"> |
| 318 |
<button class="play-btn" id="play">▶</button> |
| 319 |
<div class="progress-bar" id="progress-bar"><div class="progress-fill" id="progress"></div></div> |
| 320 |
<span class="time" id="time">0:00</span> |
| 321 |
</div> |
| 322 |
<span class="preview-label">Preview</span> |
| 323 |
<script src="/static/embed-item-player.js?v=0623" defer></script>"#, |
| 324 |
crate::helpers::escape_html(preview_url) |
| 325 |
) |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
pub const PLAYER_CSS: &str = "\ |
| 335 |
.transport { display: flex; align-items: center; gap: var(--step-base); } |
| 336 |
.play-btn { |
| 337 |
width: 32px; height: 32px; border-radius: 50%; |
| 338 |
background: var(--action); color: var(--content-on-action); border: none; |
| 339 |
cursor: pointer; display: flex; align-items: center; justify-content: center; |
| 340 |
flex: none; |
| 341 |
} |
| 342 |
.play-btn:hover { background: var(--action-hover); } |
| 343 |
.progress-bar { |
| 344 |
flex: 1; height: 4px; background: var(--surface-sunken); |
| 345 |
border-radius: 2px; cursor: pointer; position: relative; |
| 346 |
} |
| 347 |
.progress-fill { height: 100%; background: var(--action); border-radius: 2px; width: 0%; } |
| 348 |
.time { font-family: var(--font-mono); color: var(--content-muted); white-space: nowrap; } |
| 349 |
.preview-label { color: var(--content-muted); } |
| 350 |
"; |
| 351 |
|
| 352 |
|
| 353 |
pub struct ProjectView { |
| 354 |
|
| 355 |
pub title: String, |
| 356 |
|
| 357 |
pub creator_display_name: String, |
| 358 |
|
| 359 |
pub profile_url: String, |
| 360 |
|
| 361 |
pub project_url: String, |
| 362 |
|
| 363 |
pub cover_image_url: Option<String>, |
| 364 |
|
| 365 |
pub description_excerpt: String, |
| 366 |
|
| 367 |
pub item_count: usize, |
| 368 |
|
| 369 |
pub category_label: String, |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
#[must_use] |
| 374 |
pub fn project_card(view: &ProjectView) -> Screen { |
| 375 |
let mut slot = Slot::new(REGION, RegionKind::Pane); |
| 376 |
if let Some(url) = &view.cover_image_url { |
| 377 |
slot = slot.with(cover(url)); |
| 378 |
} |
| 379 |
slot = slot.with(Node::section(&view.title)).with(Node::Link { |
| 380 |
text: format!("by {}", view.creator_display_name), |
| 381 |
action: Action::external(&view.profile_url), |
| 382 |
}); |
| 383 |
if !view.description_excerpt.is_empty() { |
| 384 |
slot = slot.with(Node::text(&view.description_excerpt)); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
let slot = slot |
| 389 |
.with(Node::text(format!( |
| 390 |
"{} {} \u{b7} {}", |
| 391 |
view.item_count, |
| 392 |
if view.item_count == 1 { |
| 393 |
"item" |
| 394 |
} else { |
| 395 |
"items" |
| 396 |
}, |
| 397 |
view.category_label |
| 398 |
))) |
| 399 |
.with(Node::Act(Act::new( |
| 400 |
"View project", |
| 401 |
Action::external(&view.project_url), |
| 402 |
))); |
| 403 |
Screen::list_detail(&view.title, false).with(slot) |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
pub struct TipView { |
| 408 |
|
| 409 |
pub display_name: String, |
| 410 |
|
| 411 |
pub username: String, |
| 412 |
|
| 413 |
pub tip_url: String, |
| 414 |
|
| 415 |
pub avatar_url: Option<String>, |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
#[must_use] |
| 420 |
pub fn tip_button(view: &TipView) -> Screen { |
| 421 |
let mut row = Row::new(""); |
| 422 |
if let Some(url) = &view.avatar_url { |
| 423 |
row = row.part(layout::RowPart::Primary, cover(url)); |
| 424 |
} |
| 425 |
let row = row |
| 426 |
.part( |
| 427 |
layout::RowPart::Primary, |
| 428 |
Node::text(format!("Support @{}", view.username)), |
| 429 |
) |
| 430 |
.part( |
| 431 |
layout::RowPart::Actions, |
| 432 |
Node::Act(Act::new("Support", Action::external(&view.tip_url))), |
| 433 |
); |
| 434 |
one(&format!("Support {}", view.display_name), Node::list([row])) |
| 435 |
} |
| 436 |
|
| 437 |
#[cfg(test)] |
| 438 |
mod tests { |
| 439 |
use super::*; |
| 440 |
|
| 441 |
fn item() -> ItemView { |
| 442 |
ItemView { |
| 443 |
title: "Item".into(), |
| 444 |
price: "$9".into(), |
| 445 |
button_text: "Buy".into(), |
| 446 |
purchase_url: "https://makenot.work/buy/one".into(), |
| 447 |
cover_image_url: Some("https://makenot.work/cover.png".into()), |
| 448 |
creator_display_name: "Creator".into(), |
| 449 |
profile_url: "https://makenot.work/u/creator".into(), |
| 450 |
description_excerpt: "About it.".into(), |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
fn hex_literals(css: &str) -> Vec<String> { |
| 455 |
css.split('#') |
| 456 |
.skip(1) |
| 457 |
.map(|tail| { |
| 458 |
tail.chars() |
| 459 |
.take_while(char::is_ascii_hexdigit) |
| 460 |
.collect::<String>() |
| 461 |
}) |
| 462 |
.filter(|run| run.len() == 3 || run.len() == 6) |
| 463 |
.map(|run| format!("#{run}")) |
| 464 |
.collect() |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
#[test] |
| 472 |
fn this_host_writes_no_colour_of_its_own() { |
| 473 |
for (name, css) in [("document", DOCUMENT_CSS), ("player", PLAYER_CSS)] { |
| 474 |
let found = hex_literals(css); |
| 475 |
assert!( |
| 476 |
found.is_empty(), |
| 477 |
"{name} writes its own colour: {found:?}. Use the token instead; \ |
| 478 |
a literal here drifts from the theme and nothing will report it.", |
| 479 |
); |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
#[test] |
| 485 |
fn an_embed_document_carries_the_whole_design_system() { |
| 486 |
let html = document(&item_button(&item()), Some("embed-button")); |
| 487 |
assert!(html.contains("<style>"), "{html}"); |
| 488 |
|
| 489 |
|
| 490 |
assert!(html.contains(":root"), "{html}"); |
| 491 |
assert!(html.contains("--font-sans"), "{html}"); |
| 492 |
assert!(html.contains(".row-primary"), "{html}"); |
| 493 |
|
| 494 |
assert!(!html.contains("<link"), "{html}"); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
#[test] |
| 500 |
fn an_embed_document_carries_no_script() { |
| 501 |
let html = document(&item_button(&item()), Some("embed-button")); |
| 502 |
assert!(!html.contains("<script"), "{html}"); |
| 503 |
assert!(!html.contains("htmx"), "{html}"); |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
#[test] |
| 509 |
fn every_control_is_a_link_out() { |
| 510 |
let html = document(&item_button(&item()), Some("embed-button")); |
| 511 |
assert!( |
| 512 |
html.contains(r#"href="https://makenot.work/buy/one""#), |
| 513 |
"{html}" |
| 514 |
); |
| 515 |
assert!(html.contains(r#"rel="noopener noreferrer""#), "{html}"); |
| 516 |
assert!(!html.contains("hx-get"), "{html}"); |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
#[test] |
| 523 |
fn a_title_cannot_open_a_tag() { |
| 524 |
let mut view = item(); |
| 525 |
view.title = "<script>alert(1)</script>".into(); |
| 526 |
let html = document(&item_button(&view), Some("embed-button")); |
| 527 |
assert!(!html.contains("<script>alert"), "{html}"); |
| 528 |
assert!(html.contains("<script>"), "{html}"); |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
#[test] |
| 534 |
fn the_player_keeps_its_transport_in_a_bespoke_region() { |
| 535 |
let html = player_document(&item(), "https://makenot.work/p.mp3"); |
| 536 |
assert!(html.contains(r#"id="play""#), "{html}"); |
| 537 |
assert!(html.contains("embed-item-player.js"), "{html}"); |
| 538 |
assert!(html.contains(r#"data-bespoke="media-transport""#), "{html}"); |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
assert!(html.contains(r#"<h2 class="heading">Item</h2>"#), "{html}"); |
| 543 |
assert!(html.contains(">Buy</a>"), "{html}"); |
| 544 |
|
| 545 |
|
| 546 |
assert_eq!(html.matches("<script").count(), 1, "{html}"); |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
#[test] |
| 552 |
fn a_preview_url_cannot_break_out_of_its_attribute() { |
| 553 |
let markup = player_markup("\" onload=alert(1) x=\""); |
| 554 |
assert!(!markup.contains("\" onload"), "{markup}"); |
| 555 |
} |
| 556 |
} |
| 557 |
|