| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
use quasi_axum::Serves as _; |
| 72 |
use quasi_declare::declare; |
| 73 |
use quasi_router::screen::Document; |
| 74 |
use quasi_router::{Chrome, Screen}; |
| 75 |
use quasi_webview::{Shell, Webview}; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
const CHROME_CSS: &str = "\ |
| 89 |
<style>\ |
| 90 |
html,body{margin:0;padding:0}\ |
| 91 |
body{font-family:system-ui,-apple-system,\"Segoe UI\",sans-serif}\ |
| 92 |
#chrome-top,#chrome-bottom{background:#161616;color:#e9e9e9;font-size:.85rem;line-height:1.4}\ |
| 93 |
#chrome-top{display:flex;justify-content:space-between;align-items:center;gap:1rem;\ |
| 94 |
padding:.55rem 1rem;border-bottom:1px solid #2c2c2c}\ |
| 95 |
#chrome-top a,#chrome-bottom a{color:#e9e9e9;text-decoration:none}\ |
| 96 |
#chrome-top a:hover,#chrome-bottom a:hover{text-decoration:underline}\ |
| 97 |
#chrome-top a:first-child{font-weight:600;letter-spacing:.01em}\ |
| 98 |
#chrome-bottom{padding:1rem;text-align:center;border-top:1px solid #2c2c2c;color:#9a9a9a}\ |
| 99 |
.mnw-buy,.mnw-item{margin:1.5rem 0;padding:1rem;border:1px solid currentColor;border-radius:4px}\ |
| 100 |
.mnw-buy-cta,.mnw-item-cta{display:inline-block;margin-top:.5rem;padding:.4rem .9rem;\ |
| 101 |
border:1px solid currentColor;border-radius:4px;font-weight:600}\ |
| 102 |
.mnw-buy-cta a,.mnw-item-cta a{color:inherit;text-decoration:none}\ |
| 103 |
.mnw-files ul{margin:0;padding:0;list-style:none}\ |
| 104 |
.mnw-files{margin:1.5rem 0}\ |
| 105 |
.mnw-files li{padding:.35rem 0;border-bottom:1px solid currentColor}\ |
| 106 |
.mnw-price{font-weight:600}\ |
| 107 |
</style>"; |
| 108 |
|
| 109 |
|
| 110 |
pub struct File { |
| 111 |
|
| 112 |
pub title: String, |
| 113 |
|
| 114 |
pub url: String, |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
pub struct UserView { |
| 119 |
|
| 120 |
pub page_title: String, |
| 121 |
|
| 122 |
pub apex_url: String, |
| 123 |
|
| 124 |
pub canonical_url: String, |
| 125 |
|
| 126 |
pub creator_label: String, |
| 127 |
|
| 128 |
pub canvas_id: String, |
| 129 |
|
| 130 |
pub sanitized_css: String, |
| 131 |
|
| 132 |
pub sanitized_html: String, |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
pub struct ProjectView { |
| 137 |
|
| 138 |
pub page: UserView, |
| 139 |
|
| 140 |
pub price_label: String, |
| 141 |
|
| 142 |
pub buy_url: String, |
| 143 |
|
| 144 |
pub files: Vec<File>, |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
pub struct ItemView { |
| 154 |
|
| 155 |
pub page_title: String, |
| 156 |
|
| 157 |
pub apex_url: String, |
| 158 |
|
| 159 |
pub canonical_url: String, |
| 160 |
|
| 161 |
pub creator_label: String, |
| 162 |
|
| 163 |
pub canvas_id: String, |
| 164 |
|
| 165 |
pub sanitized_css: String, |
| 166 |
|
| 167 |
pub item_title: String, |
| 168 |
|
| 169 |
pub item_description: Option<String>, |
| 170 |
|
| 171 |
pub price_label: String, |
| 172 |
|
| 173 |
pub buy_url: String, |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
#[must_use] |
| 183 |
pub fn document(screen: &Screen) -> String { |
| 184 |
let shell = Shell::default() |
| 185 |
.without_scripts() |
| 186 |
.with_chrome(Chrome::new()) |
| 187 |
.with_head_first(CHROME_CSS); |
| 188 |
Webview::new().with_shell(shell).screen(screen) |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
const SCOPE_CLASS: &str = "user-canvas"; |
| 198 |
|
| 199 |
impl UserView { |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
fn scope_id(&self) -> String { |
| 206 |
format!("uc-{}", self.canvas_id) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
impl ItemView { |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
fn has_description(&self) -> bool { |
| 217 |
self.item_description.is_some() |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
fn description(&self) -> &str { |
| 226 |
self.item_description.as_deref().unwrap_or_default() |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
declare! { |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
shape top(apex_url: &str, canonical_url: &str) -> Slot; |
| 237 |
|
| 238 |
region "chrome-top" as Band { |
| 239 |
link "makenot.work" to leaving apex_url; |
| 240 |
link "View on makenot.work" to leaving canonical_url; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
declare! { |
| 245 |
|
| 246 |
shape footer(creator_label: &str, canonical_url: &str) -> Slot; |
| 247 |
|
| 248 |
region "chrome-bottom" as Band { |
| 249 |
link "{creator_label} on makenot.work" to leaving canonical_url; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
declare! { |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
#[must_use] |
| 260 |
pub shape user(view: &UserView) -> Screen; |
| 261 |
|
| 262 |
screen single &view.page_title { |
| 263 |
documented Document::default().styled(&view.sanitized_css); |
| 264 |
|
| 265 |
include top(&view.apex_url, &view.canonical_url); |
| 266 |
region "canvas" as Pane { |
| 267 |
canvas &view.sanitized_html { |
| 268 |
classed SCOPE_CLASS; |
| 269 |
identified view.scope_id(); |
| 270 |
} |
| 271 |
} |
| 272 |
include footer(&view.creator_label, &view.canonical_url); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
declare! { |
| 277 |
|
| 278 |
|
| 279 |
#[must_use] |
| 280 |
pub shape project(view: &ProjectView) -> Screen; |
| 281 |
|
| 282 |
screen single &view.page.page_title { |
| 283 |
documented Document::default().styled(&view.page.sanitized_css); |
| 284 |
|
| 285 |
include top(&view.page.apex_url, &view.page.canonical_url); |
| 286 |
region "canvas" as Pane { |
| 287 |
canvas &view.page.sanitized_html { |
| 288 |
classed SCOPE_CLASS; |
| 289 |
identified view.page.scope_id(); |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
include slot_block("mnw-buy", "mnw-buy-cta", &view.price_label, &view.buy_url); |
| 296 |
canvas "" unless view.files.is_empty() { |
| 297 |
classed "mnw-files"; |
| 298 |
list { |
| 299 |
for file in view.files.iter() { |
| 300 |
row &file.title { |
| 301 |
activate to leaving &file.url; |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
include footer(&view.page.creator_label, &view.page.canonical_url); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
declare! { |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
#[must_use] |
| 319 |
pub shape item(view: &ItemView) -> Screen; |
| 320 |
|
| 321 |
screen single &view.page_title { |
| 322 |
documented Document::default().styled(&view.sanitized_css); |
| 323 |
|
| 324 |
include top(&view.apex_url, &view.canonical_url); |
| 325 |
region "canvas" as Pane { |
| 326 |
canvas "" { |
| 327 |
classed "item-canvas"; |
| 328 |
identified "ic-{view.canvas_id}"; |
| 329 |
|
| 330 |
canvas "" { |
| 331 |
classed "mnw-item"; |
| 332 |
page &view.item_title; |
| 333 |
text view.description() when view.has_description(); |
| 334 |
include price(&view.price_label); |
| 335 |
include cta("mnw-item-cta", &view.buy_url); |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
include footer(&view.creator_label, &view.canonical_url); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
declare! { |
| 344 |
|
| 345 |
shape slot_block( |
| 346 |
block_class: &str, |
| 347 |
cta_class: &str, |
| 348 |
price_label: &str, |
| 349 |
buy_url: &str, |
| 350 |
) -> Node; |
| 351 |
|
| 352 |
canvas "" { |
| 353 |
classed block_class; |
| 354 |
include price(price_label); |
| 355 |
include cta(cta_class, buy_url); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
declare! { |
| 360 |
|
| 361 |
shape price(label: &str) -> Node; |
| 362 |
|
| 363 |
canvas "" { |
| 364 |
classed "mnw-price"; |
| 365 |
text label; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
declare! { |
| 370 |
|
| 371 |
shape cta(class: &str, buy_url: &str) -> Node; |
| 372 |
|
| 373 |
canvas "" { |
| 374 |
classed class; |
| 375 |
link "Get on makenot.work" to leaving buy_url; |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
#[cfg(test)] |
| 380 |
mod tests { |
| 381 |
use super::*; |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
fn body(html: &str) -> &str { |
| 390 |
&html[html.find("<body").expect("a document has a body")..] |
| 391 |
} |
| 392 |
|
| 393 |
fn a_user() -> UserView { |
| 394 |
UserView { |
| 395 |
page_title: "Ada - makenot.work".into(), |
| 396 |
apex_url: "https://makenot.work".into(), |
| 397 |
canonical_url: "https://makenot.work/u/ada".into(), |
| 398 |
creator_label: "Ada".into(), |
| 399 |
canvas_id: "11111111".into(), |
| 400 |
sanitized_css: ".user-canvas#uc-11111111 p{color:red}".into(), |
| 401 |
sanitized_html: "<h1>Ada</h1>".into(), |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
#[test] |
| 406 |
fn the_canvas_carries_the_scope_the_sanitiser_rewrote_every_rule_for() { |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
let html = document(&user(&a_user())); |
| 412 |
|
| 413 |
assert!( |
| 414 |
html.contains("<div class=\"user-canvas\" id=\"uc-11111111\"><h1>Ada</h1></div>"), |
| 415 |
"{html}" |
| 416 |
); |
| 417 |
} |
| 418 |
|
| 419 |
#[test] |
| 420 |
fn the_creators_stylesheet_is_in_the_head_after_the_platforms() { |
| 421 |
|
| 422 |
|
| 423 |
let html = document(&user(&a_user())); |
| 424 |
let chrome = html.find("#chrome-top").expect("the strip is styled"); |
| 425 |
let creator = html |
| 426 |
.find(".user-canvas#uc-11111111 p{color:red}") |
| 427 |
.expect("the creator's sheet is written"); |
| 428 |
let head_end = html.find("</head>").expect("the head closes"); |
| 429 |
|
| 430 |
assert!(chrome < creator, "{html}"); |
| 431 |
assert!(creator < head_end, "{html}"); |
| 432 |
} |
| 433 |
|
| 434 |
#[test] |
| 435 |
fn the_strip_is_a_sibling_of_the_canvas_and_not_an_ancestor() { |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
let html = document(&user(&a_user())); |
| 440 |
let body = body(&html); |
| 441 |
let top = body.find("id=\"chrome-top\"").expect("the top strip"); |
| 442 |
let canvas = body.find("class=\"user-canvas\"").expect("the canvas"); |
| 443 |
let bottom = body.find("id=\"chrome-bottom\"").expect("the bottom strip"); |
| 444 |
|
| 445 |
assert!(top < canvas && canvas < bottom, "{body}"); |
| 446 |
|
| 447 |
assert!(!body[canvas..bottom].contains("id=\"chrome-"), "{body}"); |
| 448 |
} |
| 449 |
|
| 450 |
#[test] |
| 451 |
fn no_script_reaches_a_page_whose_csp_forbids_every_one() { |
| 452 |
|
| 453 |
let html = document(&user(&a_user())); |
| 454 |
|
| 455 |
assert!(!html.contains("<script"), "{html}"); |
| 456 |
assert!(!html.contains("<link"), "{html}"); |
| 457 |
} |
| 458 |
|
| 459 |
#[test] |
| 460 |
fn every_way_off_this_page_replaces_it_rather_than_opening_a_tab() { |
| 461 |
|
| 462 |
|
| 463 |
let html = document(&user(&a_user())); |
| 464 |
|
| 465 |
assert!(!html.contains("target=\"_blank\""), "{html}"); |
| 466 |
assert!(html.contains("href=\"https://makenot.work\""), "{html}"); |
| 467 |
} |
| 468 |
|
| 469 |
#[test] |
| 470 |
fn the_published_system_slot_names_survive_the_conversion() { |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
let view = ProjectView { |
| 475 |
page: a_user(), |
| 476 |
price_label: "$12".into(), |
| 477 |
buy_url: "https://makenot.work/p/thing".into(), |
| 478 |
files: vec![File { |
| 479 |
title: "one.wav".into(), |
| 480 |
url: "https://makenot.work/i/1".into(), |
| 481 |
}], |
| 482 |
}; |
| 483 |
let html = document(&project(&view)); |
| 484 |
|
| 485 |
assert!(html.contains("class=\"mnw-buy\""), "{html}"); |
| 486 |
assert!(html.contains("class=\"mnw-buy-cta\""), "{html}"); |
| 487 |
assert!(html.contains("class=\"mnw-price\""), "{html}"); |
| 488 |
assert!(html.contains("class=\"mnw-files\""), "{html}"); |
| 489 |
} |
| 490 |
|
| 491 |
#[test] |
| 492 |
fn the_platform_blocks_sit_inside_the_creators_scope_after_their_markup() { |
| 493 |
|
| 494 |
|
| 495 |
let view = ProjectView { |
| 496 |
page: a_user(), |
| 497 |
price_label: "$12".into(), |
| 498 |
buy_url: "https://makenot.work/p/thing".into(), |
| 499 |
files: Vec::new(), |
| 500 |
}; |
| 501 |
let html = document(&project(&view)); |
| 502 |
let canvas = html |
| 503 |
.find("class=\"user-canvas\"") |
| 504 |
.expect("the canvas opens"); |
| 505 |
let markup = html.find("<h1>Ada</h1>").expect("the creator's markup"); |
| 506 |
let buy = html.find("class=\"mnw-buy\"").expect("the buy block"); |
| 507 |
|
| 508 |
assert!(canvas < markup && markup < buy, "{html}"); |
| 509 |
} |
| 510 |
|
| 511 |
#[test] |
| 512 |
fn a_project_with_no_published_items_draws_no_file_list() { |
| 513 |
|
| 514 |
|
| 515 |
let view = ProjectView { |
| 516 |
page: a_user(), |
| 517 |
price_label: "Free".into(), |
| 518 |
buy_url: "https://makenot.work/p/thing".into(), |
| 519 |
files: Vec::new(), |
| 520 |
}; |
| 521 |
|
| 522 |
assert!(!body(&document(&project(&view))).contains("mnw-files")); |
| 523 |
} |
| 524 |
|
| 525 |
#[test] |
| 526 |
fn an_item_page_wears_the_parents_stylesheet_under_the_item_canvas() { |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
let html = document(&item(&ItemView { |
| 531 |
page_title: "Track - makenot.work".into(), |
| 532 |
apex_url: "https://makenot.work".into(), |
| 533 |
canonical_url: "https://makenot.work/i/9".into(), |
| 534 |
creator_label: "Ada".into(), |
| 535 |
canvas_id: "22222222".into(), |
| 536 |
sanitized_css: ".item-canvas#ic-22222222 p{color:red}".into(), |
| 537 |
item_title: "Track".into(), |
| 538 |
item_description: Some("A track.".into()), |
| 539 |
price_label: "$3".into(), |
| 540 |
buy_url: "https://makenot.work/i/9".into(), |
| 541 |
})); |
| 542 |
|
| 543 |
assert!( |
| 544 |
html.contains("<div class=\"item-canvas\" id=\"ic-22222222\">"), |
| 545 |
"{html}" |
| 546 |
); |
| 547 |
assert!(html.contains("class=\"mnw-item\""), "{html}"); |
| 548 |
assert!(html.contains("class=\"mnw-item-cta\""), "{html}"); |
| 549 |
assert!(!html.contains("user-canvas"), "{html}"); |
| 550 |
} |
| 551 |
|
| 552 |
#[test] |
| 553 |
fn an_item_with_no_description_draws_none() { |
| 554 |
let html = document(&item(&ItemView { |
| 555 |
page_title: "Track - makenot.work".into(), |
| 556 |
apex_url: "https://makenot.work".into(), |
| 557 |
canonical_url: "https://makenot.work/i/9".into(), |
| 558 |
creator_label: "Ada".into(), |
| 559 |
canvas_id: "22222222".into(), |
| 560 |
sanitized_css: String::new(), |
| 561 |
item_title: "Track".into(), |
| 562 |
item_description: None, |
| 563 |
price_label: "$3".into(), |
| 564 |
buy_url: "https://makenot.work/i/9".into(), |
| 565 |
})); |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
let drawn = body(&html); |
| 570 |
let heading = drawn.find("<h1").expect("the item is named"); |
| 571 |
let price = drawn.find("mnw-price").expect("the price is drawn"); |
| 572 |
assert!(!drawn[heading..price].contains("class=\"text\""), "{drawn}"); |
| 573 |
} |
| 574 |
|
| 575 |
#[test] |
| 576 |
fn a_locked_creator_gets_the_strip_and_an_empty_canvas() { |
| 577 |
|
| 578 |
|
| 579 |
let mut view = a_user(); |
| 580 |
view.sanitized_html = String::new(); |
| 581 |
view.sanitized_css = String::new(); |
| 582 |
let html = document(&user(&view)); |
| 583 |
|
| 584 |
assert!( |
| 585 |
html.contains("<div class=\"user-canvas\" id=\"uc-11111111\">"), |
| 586 |
"{html}" |
| 587 |
); |
| 588 |
assert!(html.contains("id=\"chrome-top\""), "{html}"); |
| 589 |
assert!(html.contains("id=\"chrome-bottom\""), "{html}"); |
| 590 |
} |
| 591 |
} |
| 592 |
|