| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use quasi_router::Node; |
| 22 |
use quasi_router::screen::Lexeme; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#[must_use] |
| 29 |
pub fn line_html(runs: &[Lexeme], language: Option<&str>) -> String { |
| 30 |
use quasi_axum::Serves as _; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
quasi_webview::Webview::new().fragment(&Node::Code { |
| 35 |
runs: runs.to_vec(), |
| 36 |
language: language.map(str::to_owned), |
| 37 |
inline: true, |
| 38 |
}) |
| 39 |
} |
| 40 |
|
| 41 |
#[cfg(test)] |
| 42 |
mod tests { |
| 43 |
use super::*; |
| 44 |
use makeover_layout::Syntax; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#[test] |
| 49 |
fn a_classified_line_arrives_whole() { |
| 50 |
let html = line_html( |
| 51 |
&[ |
| 52 |
Lexeme::new("fn", Syntax::Keyword), |
| 53 |
Lexeme::plain(" main() {}"), |
| 54 |
], |
| 55 |
Some("rust"), |
| 56 |
); |
| 57 |
assert!(html.contains("lex-keyword"), "{html}"); |
| 58 |
assert!(html.contains(" main() {}"), "{html}"); |
| 59 |
assert_eq!(html.matches("<span").count(), 1, "{html}"); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#[test] |
| 66 |
fn an_unclassified_line_is_still_the_line() { |
| 67 |
let html = line_html(&[Lexeme::plain("hello world")], None); |
| 68 |
assert!(html.contains("hello world"), "{html}"); |
| 69 |
assert!(!html.contains("<span"), "{html}"); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#[test] |
| 75 |
fn a_blank_line_still_renders() { |
| 76 |
let html = line_html(&[], None); |
| 77 |
assert!(html.contains("<code"), "{html}"); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
#[test] |
| 83 |
fn source_cannot_smuggle_markup() { |
| 84 |
let html = line_html(&[Lexeme::plain("<script>alert(1)</script>")], None); |
| 85 |
assert!(!html.contains("<script>"), "{html}"); |
| 86 |
assert!(html.contains("<script>"), "{html}"); |
| 87 |
} |
| 88 |
} |
| 89 |
|