diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-04-12 19:49:16 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-04-12 19:49:51 +0200 |
commit | e46e2d6b02394eea8ffe7ac723469b9b114e234c (patch) | |
tree | c0c0d05e736a8a474f1f01cd1a1f8770a8e8bd46 | |
parent | e03df3d9690e228a7a076107d64057c28d63cdfa (diff) | |
download | askama-e46e2d6b02394eea8ffe7ac723469b9b114e234c.tar.gz askama-e46e2d6b02394eea8ffe7ac723469b9b114e234c.tar.bz2 askama-e46e2d6b02394eea8ffe7ac723469b9b114e234c.zip |
Handle a lack of whitespace after match block (fixes #76)
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/generator.rs | 10 | ||||
-rw-r--r-- | askama_shared/src/parser.rs | 9 | ||||
-rw-r--r-- | testing/templates/match-no-ws.html | 1 | ||||
-rw-r--r-- | testing/tests/matches.rs | 12 |
4 files changed, 24 insertions, 8 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index bec3437..8a112c8 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -415,11 +415,13 @@ impl<'a> Generator<'a> { self.writeln("}"); } - fn write_match(&mut self, state: &'a State, ws1: &WS, expr: &Expr, inter: &'a str, arms: - &'a [When], ws2: &WS) { + fn write_match(&mut self, state: &'a State, ws1: &WS, expr: &Expr, inter: Option<&'a str>, + arms: &'a [When], ws2: &WS) { self.flush_ws(ws1); - if !inter.is_empty() { - self.next_ws = Some(inter); + if let Some(inter) = inter { + if !inter.is_empty() { + self.next_ws = Some(inter); + } } self.write("match "); diff --git a/askama_shared/src/parser.rs b/askama_shared/src/parser.rs index 14d1fe8..402801f 100644 --- a/askama_shared/src/parser.rs +++ b/askama_shared/src/parser.rs @@ -58,7 +58,7 @@ pub enum Node<'a> { LetDecl(WS, Target<'a>), Let(WS, Target<'a>, Expr<'a>), Cond(Vec<(WS, Option<Expr<'a>>, Vec<Node<'a>>)>, WS), - Match(WS, Expr<'a>, &'a str, Vec<When<'a>>, WS), + Match(WS, Expr<'a>, Option<&'a str>, Vec<When<'a>>, WS), Loop(WS, Target<'a>, Expr<'a>, Vec<Node<'a>>, WS), Extends(Expr<'a>), BlockDef(WS, &'a str, Vec<Node<'a>>, WS), @@ -480,7 +480,7 @@ named!(block_match<Node>, do_parse!( expr: ws!(expr_any) >> nws1: opt!(tag_s!("-")) >> tag_s!("%}") >> - inter: take_content >> + inter: opt!(take_content) >> arms: many1!(when_block) >> else_arm: opt!(match_else_block) >> ws!(tag_s!("{%")) >> @@ -493,13 +493,14 @@ named!(block_match<Node>, do_parse!( arms.push(arm); } let inter = match inter { - Node::Lit(lws, val, rws) => { + Some(Node::Lit(lws, val, rws)) => { assert!(val.is_empty(), "only whitespace allowed between match and first when, found {}", val); assert!(rws.is_empty(), "only whitespace allowed between match and first when, found {}", rws); - lws + Some(lws) }, + None => None, _ => panic!("only literals allowed between match and first when"), }; Node::Match( diff --git a/testing/templates/match-no-ws.html b/testing/templates/match-no-ws.html new file mode 100644 index 0000000..2ab0bf3 --- /dev/null +++ b/testing/templates/match-no-ws.html @@ -0,0 +1 @@ +{% match foo %}{% when Some with (bar) %}{{ bar }}{% when None %}{% endmatch %} diff --git a/testing/tests/matches.rs b/testing/tests/matches.rs index 8f4b05c..9fae70c 100644 --- a/testing/tests/matches.rs +++ b/testing/tests/matches.rs @@ -82,3 +82,15 @@ fn test_match_custom_enum() { let s = MatchCustomEnumTemplate { color: Color::Rgb(160, 0, 255) }; assert_eq!(s.render().unwrap(), "\n\nColorful: #A000FF\n"); } + +#[derive(Template)] +#[template(path = "match-no-ws.html")] +struct MatchNoWhitespace { + foo: Option<usize>, +} + +#[test] +fn test_match_no_whitespace() { + let s = MatchNoWhitespace { foo: Some(1) }; + assert_eq!(s.render().unwrap(), "1"); +} |