Skip to content

Commit

Permalink
Require braces around element bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
lambda-fairy committed Jun 16, 2018
1 parent b9279f7 commit 41aea5f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
20 changes: 7 additions & 13 deletions maud/tests/basic_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ fn simple_elements() {
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();
Expand All @@ -73,7 +67,7 @@ fn simple_attributes() {
let s = html! {
link rel="stylesheet" href="styles.css";
section id="midriff" {
p class="hotpink" "Hello!"
p class="hotpink" { "Hello!" }
}
}.into_string();
assert_eq!(s, concat!(
Expand All @@ -83,7 +77,7 @@ fn simple_attributes() {

#[test]
fn empty_attributes() {
let s = html!(div readonly? input type="checkbox" checked?;).into_string();
let s = html!(div readonly? { input type="checkbox" checked?; }).into_string();
assert_eq!(s, r#"<div readonly><input type="checkbox" checked></div>"#);
}

Expand Down Expand Up @@ -112,7 +106,7 @@ fn toggle_empty_attributes_braces() {

#[test]
fn colons_in_names() {
let s = html!(pon-pon:controls-alpha a on:click="yay()" "Yay!").into_string();
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>"#,
Expand Down Expand Up @@ -151,14 +145,14 @@ fn classes_shorthand() {

#[test]
fn hyphens_in_class_names() {
let s = html!(p.rocks-these.are--my--rocks "yes").into_string();
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 toggle_classes() {
fn test(is_cupcake: bool, is_muffin: bool) -> Markup {
html!(p.cupcake[is_cupcake].muffin[is_muffin] "Testing!")
html!(p.cupcake[is_cupcake].muffin[is_muffin] { "Testing!" })
}
assert_eq!(test(true, true).into_string(), r#"<p class="cupcake muffin">Testing!</p>"#);
assert_eq!(test(false, true).into_string(), r#"<p class=" muffin">Testing!</p>"#);
Expand All @@ -169,14 +163,14 @@ fn toggle_classes() {
#[test]
fn toggle_classes_braces() {
struct Maud { rocks: bool }
let s = html!(p.rocks[Maud { rocks: true }.rocks] "Awesome!").into_string();
let s = html!(p.rocks[Maud { rocks: true }.rocks] { "Awesome!" }).into_string();
assert_eq!(s, r#"<p class="rocks">Awesome!</p>"#);
}

#[test]
fn mixed_classes() {
fn test(is_muffin: bool) -> Markup {
html!(p.cupcake.muffin[is_muffin].lamington "Testing!")
html!(p.cupcake.muffin[is_muffin].lamington { "Testing!" })
}
assert_eq!(test(true).into_string(), r#"<p class="cupcake lamington muffin">Testing!</p>"#);
assert_eq!(test(false).into_string(), r#"<p class="cupcake lamington">Testing!</p>"#);
Expand Down
22 changes: 14 additions & 8 deletions maud/tests/control_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ fn if_let() {
fn while_expr() {
let mut numbers = (0..3).into_iter().peekable();
let s = html! {
ul @while numbers.peek().is_some() {
li (numbers.next().unwrap())
ul {
@while numbers.peek().is_some() {
li { (numbers.next().unwrap()) }
}
}
}.into_string();
assert_eq!(s, "<ul><li>0</li><li>1</li><li>2</li></ul>");
Expand All @@ -56,8 +58,10 @@ fn while_let_expr() {
let mut numbers = (0..3).into_iter();
#[cfg_attr(feature = "cargo-clippy", allow(while_let_on_iterator))]
let s = html! {
ul @while let Some(n) = numbers.next() {
li (n)
ul {
@while let Some(n) = numbers.next() {
li { (n) }
}
}
}.into_string();
assert_eq!(s, "<ul><li>0</li><li>1</li><li>2</li></ul>");
Expand All @@ -67,8 +71,10 @@ fn while_let_expr() {
fn for_expr() {
let ponies = ["Apple Bloom", "Scootaloo", "Sweetie Belle"];
let s = html! {
ul @for pony in &ponies {
li (pony)
ul {
@for pony in &ponies {
li { (pony) }
}
}
}.into_string();
assert_eq!(s, concat!(
Expand All @@ -85,7 +91,7 @@ fn match_expr() {
let s = html! {
@match input {
Some(value) => {
div (value)
div { (value) }
},
None => {
"oh noes"
Expand All @@ -102,7 +108,7 @@ fn match_expr_without_delims() {
let s = html! {
@match input {
Some(value) => (value),
None => span "oh noes",
None => span { "oh noes" },
}
}.into_string();
assert_eq!(s, output);
Expand Down
2 changes: 1 addition & 1 deletion maud_extras/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub struct Title<T: AsRef<str>>(pub T);
impl<T: AsRef<str>> Render for Title<T> {
fn render(&self) -> Markup {
html! {
title (self.0.as_ref())
title { (self.0.as_ref()) }
}
}
}
Expand Down
17 changes: 12 additions & 5 deletions maud_macros/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,18 @@ impl Parser {
_ => {
match self.markup()? {
ast::Markup::Block(block) => ast::ElementBody::Block { block },
markup => ast::ElementBody::Block {
block: ast::Block {
markups: vec![markup],
outer_span: Span::call_site(),
},
markup => {
let markup_span = markup.span();
markup_span
.error("element body must be wrapped in braces")
.help("see https://github.com/lfairy/maud/pull/137 for details")
.emit();
ast::ElementBody::Block {
block: ast::Block {
markups: vec![markup],
outer_span: markup_span,
},
}
},
}
},
Expand Down

0 comments on commit 41aea5f

Please sign in to comment.