| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use std::sync::OnceLock; |
| 25 |
|
| 26 |
use quasi_webview::Shell; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
const V: &str = env!("STATIC_VERSION"); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
const HTMX: &str = "4.0.0-beta6"; |
| 44 |
|
| 45 |
fn parts() -> &'static quasi_webview::Parts { |
| 46 |
static PARTS: OnceLock<quasi_webview::Parts> = OnceLock::new(); |
| 47 |
PARTS.get_or_init(|| { |
| 48 |
Shell::under("/static") |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
.layered(["base", "components", "responsive"]) |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
.with_head_first( |
| 78 |
"<meta name=\"htmx-config\" content='{\"noSwap\":[204,304,\"4xx\",\"5xx\"]}'>\ |
| 79 |
<link rel=\"preload\" href=\"/static/fonts/QuasiBody.woff2\" as=\"font\" type=\"font/woff2\" crossorigin>\ |
| 80 |
<link rel=\"preload\" href=\"/static/fonts/ysrf.woff2\" as=\"font\" type=\"font/woff2\" crossorigin>", |
| 81 |
) |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
.styled(format!("/static/typography.css?v={V}")) |
| 89 |
.styled(format!("/static/geometry.css?v={V}")) |
| 90 |
.styled(format!("/static/layout.css?v={V}")) |
| 91 |
.styled(format!("/static/style.css?v={V}")) |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
.with_head(format!( |
| 113 |
"<link rel=\"icon\" href=\"/static/images/favicon.ico\" type=\"image/x-icon\">\ |
| 114 |
<script src=\"/static/hx-history-cache.min.js?v={HTMX}\" defer></script>\ |
| 115 |
<script src=\"/static/upload.js?v={V}\"></script>\ |
| 116 |
<script type=\"module\" src=\"/static/dist-{V}/core/index.js\"></script>" |
| 117 |
)) |
| 118 |
.parts() |
| 119 |
}) |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
pub fn head() -> &'static str { |
| 127 |
&parts().head |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
pub fn body_attrs() -> &'static str { |
| 135 |
&parts().body_attrs |
| 136 |
} |
| 137 |
|
| 138 |
pub use makeover_layout::Measure; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
#[must_use] |
| 158 |
pub const fn measure(measure: Measure) -> &'static str { |
| 159 |
match measure { |
| 160 |
Measure::Contained => "centered-page", |
| 161 |
Measure::Reading => "article-page", |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
_ => "padded-page", |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
#[cfg(test)] |
| 170 |
mod tests { |
| 171 |
use super::*; |
| 172 |
|
| 173 |
#[test] |
| 174 |
fn the_layer_statement_precedes_every_stylesheet() { |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
let head = head(); |
| 179 |
let stmt = head |
| 180 |
.find("@layer makeover, base, components, responsive;") |
| 181 |
.expect("the order is stated"); |
| 182 |
for sheet in ["typography.css", "geometry.css", "layout.css", "style.css"] { |
| 183 |
assert!(stmt < head.find(sheet).expect("the sheet is linked")); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
fn the_head_is_not_closed_and_carries_no_title() { |
| 189 |
|
| 190 |
|
| 191 |
assert!(!head().contains("</head>")); |
| 192 |
assert!(!head().contains("<title>")); |
| 193 |
assert!(head().starts_with("<!doctype html><html lang=\"en\">")); |
| 194 |
} |
| 195 |
|
| 196 |
#[test] |
| 197 |
fn the_fonts_are_preloaded_before_the_sheets_that_race_them() { |
| 198 |
let head = head(); |
| 199 |
assert!(head.find("QuasiBody.woff2") < head.find("style.css")); |
| 200 |
|
| 201 |
|
| 202 |
assert!(!head.contains("Lato")); |
| 203 |
assert!(!head.contains("IBMPlexMono")); |
| 204 |
} |
| 205 |
|
| 206 |
#[test] |
| 207 |
fn the_body_attributes_start_with_a_space_so_a_page_can_add_its_own() { |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
let attrs = body_attrs(); |
| 212 |
assert!(attrs.is_empty() || attrs.starts_with(' ')); |
| 213 |
|
| 214 |
|
| 215 |
assert!(!attrs.contains("class=")); |
| 216 |
} |
| 217 |
|
| 218 |
#[test] |
| 219 |
fn every_measure_keeps_the_class_the_templates_used_to_write() { |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
assert_eq!(measure(Measure::Wide), "padded-page"); |
| 224 |
assert_eq!(measure(Measure::Contained), "centered-page"); |
| 225 |
assert_eq!(measure(Measure::Reading), "article-page"); |
| 226 |
} |
| 227 |
|
| 228 |
#[test] |
| 229 |
fn no_template_still_writes_a_layout_class_by_hand() { |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
let mut offenders = Vec::new(); |
| 237 |
for entry in walk("templates") { |
| 238 |
let source = std::fs::read_to_string(&entry).expect("a template reads"); |
| 239 |
for (at, line) in source.lines().enumerate() { |
| 240 |
if !line.contains("block body_attrs") { |
| 241 |
continue; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
let derived = line.contains("crate::shell::measure("); |
| 246 |
let literal = ["padded-page", "centered-page", "article-page"] |
| 247 |
.iter() |
| 248 |
.any(|name| line.contains(name)); |
| 249 |
if literal && !derived { |
| 250 |
offenders.push(format!("{}:{}", entry.display(), at + 1)); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
assert!(offenders.is_empty(), "{offenders:?}"); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
fn walk(root: &str) -> Vec<std::path::PathBuf> { |
| 259 |
let mut found = Vec::new(); |
| 260 |
let mut stack = vec![std::path::PathBuf::from(root)]; |
| 261 |
while let Some(at) = stack.pop() { |
| 262 |
let Ok(entries) = std::fs::read_dir(&at) else { |
| 263 |
continue; |
| 264 |
}; |
| 265 |
for entry in entries.flatten() { |
| 266 |
let path = entry.path(); |
| 267 |
if path.is_dir() { |
| 268 |
stack.push(path); |
| 269 |
} else if path.extension().is_some_and(|ext| ext == "html") { |
| 270 |
found.push(path); |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
found |
| 275 |
} |
| 276 |
|
| 277 |
#[test] |
| 278 |
fn the_transport_the_head_links_is_served_from_here() { |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
assert!(head().contains("htmx.min.js")); |
| 285 |
assert!(!head().contains("idiomorph")); |
| 286 |
let vendored = concat!(env!("CARGO_MANIFEST_DIR"), "/static/htmx.min.js"); |
| 287 |
assert!(std::path::Path::new(vendored).exists()); |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
let bundle = std::fs::read_to_string(vendored).expect("the bundle reads"); |
| 292 |
assert!(bundle.contains(HTMX), "the vendored bundle is {HTMX}"); |
| 293 |
} |
| 294 |
|
| 295 |
#[test] |
| 296 |
fn every_vendored_extension_is_busted_by_the_htmx_release_it_came_from() { |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")); |
| 303 |
let mut linked = 0; |
| 304 |
let mut sources = vec![head().to_string()]; |
| 305 |
for entry in walk("templates") { |
| 306 |
sources.push(std::fs::read_to_string(&entry).expect("a template reads")); |
| 307 |
} |
| 308 |
for source in &sources { |
| 309 |
for (at, _) in source.match_indices("/static/hx-") { |
| 310 |
let tail = &source[at + "/static/".len()..]; |
| 311 |
|
| 312 |
let url = tail.split(['"', '\'']).next().expect("a quoted url"); |
| 313 |
let (name, suffix) = url.split_once("?v=").unwrap_or((url, "")); |
| 314 |
assert!( |
| 315 |
root.join("static").join(name).exists(), |
| 316 |
"{name} is linked but not vendored" |
| 317 |
); |
| 318 |
assert_eq!(suffix, HTMX, "{name} is busted by the wrong version"); |
| 319 |
linked += 1; |
| 320 |
} |
| 321 |
} |
| 322 |
assert_eq!(linked, 2, "the vendored extension count changed"); |
| 323 |
} |
| 324 |
|
| 325 |
#[test] |
| 326 |
fn the_htmx_config_is_stated_above_the_script_that_reads_it() { |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
let head = head(); |
| 331 |
let meta = head |
| 332 |
.find("name=\"htmx-config\"") |
| 333 |
.expect("the config is stated"); |
| 334 |
assert!(meta < head.find("htmx.min.js").expect("htmx is linked")); |
| 335 |
assert!(head.contains("\"noSwap\"")); |
| 336 |
} |
| 337 |
} |
| 338 |
|