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