After fiddling around with zero-allocation solutions, I concluded that all non-allocating approaches are too annoying to work with in realistic code. Using closures leads to yak-shaving with lifetimes; and because Iron needs to take ownership of the response body we often end up cloning the input data anyway. Removing this constraint has let me simplify the entire system, removing a net 300 lines from the library. The `html!` macro no longer takes a writer, and instead returns a `PreEscaped<String>`. This means that the result of an `html!` can be spliced directly into another `html!`, removing the need for the `impl Template` rigmarole. To rub it in, benchmarks show the new code is in fact *faster* than it was before. How lovely.
134 lines
3.4 KiB
Rust
134 lines
3.4 KiB
Rust
#![feature(plugin)]
|
|
#![plugin(maud_macros)]
|
|
|
|
extern crate maud;
|
|
|
|
#[test]
|
|
fn literals() {
|
|
let s = html!("du\tcks" "-23" "3.14\n" "geese").into_string();
|
|
assert_eq!(s, "du\tcks-233.14\ngeese");
|
|
}
|
|
|
|
#[test]
|
|
fn escaping() {
|
|
let s = html!("<flim&flam>").into_string();
|
|
assert_eq!(s, "<flim&flam>");
|
|
}
|
|
|
|
#[test]
|
|
fn semicolons() {
|
|
let s = html! {
|
|
"one";
|
|
"two";
|
|
"three";
|
|
;;;;;;;;;;;;;;;;;;;;;;;;
|
|
"four";
|
|
}.into_string();
|
|
assert_eq!(s, "onetwothreefour");
|
|
}
|
|
|
|
#[test]
|
|
fn blocks() {
|
|
let s = html! {
|
|
"hello"
|
|
{
|
|
" ducks" " geese"
|
|
}
|
|
" swans"
|
|
}.into_string();
|
|
assert_eq!(s, "hello ducks geese swans");
|
|
}
|
|
|
|
#[test]
|
|
fn simple_elements() {
|
|
let s = html!(p { b { "pickle" } "barrel" i { "kumquat" } }).into_string();
|
|
assert_eq!(s, "<p><b>pickle</b>barrel<i>kumquat</i></p>");
|
|
}
|
|
|
|
#[test]
|
|
fn nesting_elements() {
|
|
let s = html!(html body div p sup "butts").into_string();
|
|
assert_eq!(s, "<html><body><div><p><sup>butts</sup></p></div></body></html>");
|
|
}
|
|
|
|
#[test]
|
|
fn empty_elements() {
|
|
let s = html!("pinkie" br/ "pie").into_string();
|
|
assert_eq!(s, "pinkie<br>pie");
|
|
}
|
|
|
|
#[test]
|
|
fn simple_attributes() {
|
|
let s = html! {
|
|
link rel="stylesheet" href="styles.css"/
|
|
section id="midriff" {
|
|
p class="hotpink" "Hello!"
|
|
}
|
|
}.into_string();
|
|
assert_eq!(s, concat!(
|
|
r#"<link rel="stylesheet" href="styles.css">"#,
|
|
r#"<section id="midriff"><p class="hotpink">Hello!</p></section>"#));
|
|
}
|
|
|
|
#[test]
|
|
fn empty_attributes() {
|
|
let s = html!(div readonly? input type="checkbox" checked? /).into_string();
|
|
assert_eq!(s, r#"<div readonly><input type="checkbox" checked></div>"#);
|
|
}
|
|
|
|
#[test]
|
|
fn colons_in_names() {
|
|
let s = html!(pon-pon:controls-alpha a on:click="yay()" "Yay!").into_string();
|
|
assert_eq!(s, concat!(
|
|
r#"<pon-pon:controls-alpha>"#,
|
|
r#"<a on:click="yay()">Yay!</a>"#,
|
|
r#"</pon-pon:controls-alpha>"#));
|
|
}
|
|
|
|
#[test]
|
|
fn hyphens_in_element_names() {
|
|
let s = html!(custom-element {}).into_string();
|
|
assert_eq!(s, "<custom-element></custom-element>");
|
|
}
|
|
|
|
#[test]
|
|
fn hyphens_in_attribute_names() {
|
|
let s = html!(this sentence-is="false" of-course? {}).into_string();
|
|
assert_eq!(s, r#"<this sentence-is="false" of-course></this>"#);
|
|
}
|
|
|
|
#[test]
|
|
fn class_shorthand() {
|
|
let s = html!(p { "Hi, " span.name { "Lyra" } "!" }).into_string();
|
|
assert_eq!(s, r#"<p>Hi, <span class="name">Lyra</span>!</p>"#);
|
|
}
|
|
|
|
#[test]
|
|
fn class_shorthand_with_space() {
|
|
let s = html!(p { "Hi, " span .name { "Lyra" } "!" }).into_string();
|
|
assert_eq!(s, r#"<p>Hi, <span class="name">Lyra</span>!</p>"#);
|
|
}
|
|
|
|
#[test]
|
|
fn classes_shorthand() {
|
|
let s = html!(p { "Hi, " span.name.here { "Lyra" } "!" }).into_string();
|
|
assert_eq!(s, r#"<p>Hi, <span class="name here">Lyra</span>!</p>"#);
|
|
}
|
|
|
|
#[test]
|
|
fn hyphens_in_class_names() {
|
|
let s = html!(p.rocks-these.are--my--rocks "yes").into_string();
|
|
assert_eq!(s, r#"<p class="rocks-these are--my--rocks">yes</p>"#);
|
|
}
|
|
|
|
#[test]
|
|
fn ids_shorthand() {
|
|
let s = html!(p { "Hi, " span#thing { "Lyra" } "!" }).into_string();
|
|
assert_eq!(s, r#"<p>Hi, <span id="thing">Lyra</span>!</p>"#);
|
|
}
|
|
|
|
#[test]
|
|
fn classes_attrs_ids_mixed_up() {
|
|
let s = html!(p { "Hi, " span.name.here lang="en" #thing { "Lyra" } "!" }).into_string();
|
|
assert_eq!(s, "<p>Hi, <span lang=\"en\" class=\"name here\" id=\"thing\">Lyra</span>!</p>");
|
|
}
|