diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-08 16:19:08 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-08 16:19:08 +0100 |
commit | a3844f8ce58f16b6787f356e1adf5c7f59855ac2 (patch) | |
tree | 219c2d1b0a4f3d25cc454d3cda3e23aea8cebf52 | |
parent | 06bd6ef634de9a6569f15eb5043540e545371d68 (diff) | |
download | askama-a3844f8ce58f16b6787f356e1adf5c7f59855ac2.tar.gz askama-a3844f8ce58f16b6787f356e1adf5c7f59855ac2.tar.bz2 askama-a3844f8ce58f16b6787f356e1adf5c7f59855ac2.zip |
Fix some bugs in whitespace handling
Diffstat (limited to '')
-rw-r--r-- | askama/src/generator.rs | 29 | ||||
-rw-r--r-- | testing/templates/comparison.html | 4 |
2 files changed, 24 insertions, 9 deletions
diff --git a/askama/src/generator.rs b/askama/src/generator.rs index 8441bca..fbfec83 100644 --- a/askama/src/generator.rs +++ b/askama/src/generator.rs @@ -124,9 +124,13 @@ impl<'a> Generator<'a> { fn flush_ws(&mut self, ws: &WS) { if self.next_ws.is_some() && !ws.0 { let val = self.next_ws.unwrap(); - self.write(&format!("writer.write_str({:#?}).unwrap();", val)); - self.next_ws = None; + if !val.is_empty() { + self.write(&format!("writer.write_str({:#?}).unwrap();", + val)); + } + } else if self.next_ws.is_some() { } + self.next_ws = None; } fn prepare_ws(&mut self, ws: &WS) { @@ -138,13 +142,24 @@ impl<'a> Generator<'a> { self.prepare_ws(ws); } - fn write_lit(&mut self, lws: &str, val: &str, rws: &'a str) { + fn write_lit(&mut self, lws: &'a str, val: &str, rws: &'a str) { assert!(self.next_ws.is_none()); - if !self.skip_ws { - self.write(&format!("writer.write_str({:#?}).unwrap();", lws)); + if !lws.is_empty() { + if self.skip_ws { + self.skip_ws = false; + } else if val.is_empty() { + assert!(rws.is_empty()); + self.next_ws = Some(lws); + } else { + self.write(&format!("writer.write_str({:#?}).unwrap();", lws)); + } + } + if !val.is_empty() { + self.write(&format!("writer.write_str({:#?}).unwrap();", val)); + } + if !rws.is_empty() { + self.next_ws = Some(rws); } - self.write(&format!("writer.write_str({:#?}).unwrap();", val)); - self.next_ws = Some(rws); } fn write_expr(&mut self, ws: &WS, s: &Expr) { diff --git a/testing/templates/comparison.html b/testing/templates/comparison.html index 71a4a7b..ea22546 100644 --- a/testing/templates/comparison.html +++ b/testing/templates/comparison.html @@ -30,9 +30,9 @@ {%- else -%} f {%- endif %} -{% if a <= b %} +{% if a <= b -%} t -{% endif -%} +{%- endif -%} {% if c <= b -%} t {%- else -%} |