diff options
author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2023-03-06 21:51:49 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-03-09 13:35:00 +0100 |
commit | ca083f59fcf151725b4f86719674a153fded4eb5 (patch) | |
tree | 0e1d95b151204e87e0ddca0c2b233d6f36f12ddd | |
parent | c795edfd8de5b77f27ad6c8a25568213dcc09bb6 (diff) | |
download | askama-ca083f59fcf151725b4f86719674a153fded4eb5.tar.gz askama-ca083f59fcf151725b4f86719674a153fded4eb5.tar.bz2 askama-ca083f59fcf151725b4f86719674a153fded4eb5.zip |
Fix handling of trailing whitespace characters
-rw-r--r-- | askama_derive/src/generator.rs | 12 | ||||
-rw-r--r-- | askama_derive/src/parser/node.rs | 11 |
2 files changed, 19 insertions, 4 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index d8093c3..cad7909 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -686,6 +686,11 @@ impl<'a> Generator<'a> { } if AstLevel::Top == level { + // Handle any pending whitespace. + if self.next_ws.is_some() { + self.flush_ws(Ws(Some(self.skip_ws.into()), None)); + } + size_hint += self.write_buf_writable(buf)?; } Ok(size_hint) @@ -1247,9 +1252,7 @@ impl<'a> Generator<'a> { assert!(self.next_ws.is_none()); if !lws.is_empty() { match self.skip_ws { - WhitespaceHandling::Suppress => { - self.skip_ws = WhitespaceHandling::Preserve; - } + WhitespaceHandling::Suppress => {} _ if val.is_empty() => { assert!(rws.is_empty()); self.next_ws = Some(lws); @@ -1260,12 +1263,13 @@ impl<'a> Generator<'a> { .push(Writable::Lit(match lws.contains('\n') { true => "\n", false => " ", - })) + })); } } } if !val.is_empty() { + self.skip_ws = WhitespaceHandling::Preserve; self.buf_writable.push(Writable::Lit(val)); } diff --git a/askama_derive/src/parser/node.rs b/askama_derive/src/parser/node.rs index 9f8b37b..fc6860e 100644 --- a/askama_derive/src/parser/node.rs +++ b/askama_derive/src/parser/node.rs @@ -14,6 +14,7 @@ use super::{ tag_block_end, tag_block_start, tag_comment_end, tag_comment_start, tag_expr_end, tag_expr_start, take_content, ws, Expr, State, }; +use crate::config::WhitespaceHandling; #[derive(Debug, PartialEq)] pub(crate) enum Node<'a> { @@ -55,6 +56,16 @@ pub(crate) enum Whitespace { Minimize, } +impl From<WhitespaceHandling> for Whitespace { + fn from(ws: WhitespaceHandling) -> Self { + match ws { + WhitespaceHandling::Suppress => Whitespace::Suppress, + WhitespaceHandling::Preserve => Whitespace::Preserve, + WhitespaceHandling::Minimize => Whitespace::Minimize, + } + } +} + #[derive(Debug, PartialEq)] pub(crate) struct Loop<'a> { pub(crate) ws1: Ws, |