From 467f4ade19fa34983de7e6f6d81c6b4d5ff140fe Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 10 Jan 2019 17:19:28 +0100 Subject: Specify a trait that handles the output format's escaping --- askama_derive/src/generator.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'askama_derive/src/generator.rs') diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 16a08bb..f6c83ec 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -732,14 +732,14 @@ impl<'a> Generator<'a> { } Writable::Expr(s) => { use self::DisplayWrap::*; - use super::input::EscapeMode::*; let mut expr_buf = Buffer::new(0); let wrapped = self.visit_expr(&mut expr_buf, s); - let expression = match (wrapped, &self.input.escaping) { - (Wrapped, &Html) | (Wrapped, &None) | (Unwrapped, &None) => expr_buf.buf, - (Unwrapped, &Html) => { - format!("::askama::MarkupDisplay::from(&{})", expr_buf.buf) - } + let expression = match wrapped { + Wrapped => expr_buf.buf, + Unwrapped => format!( + "::askama::MarkupDisplay::new_unsafe(&{}, {})", + expr_buf.buf, self.input.escaping + ), }; let id = expr_cache.entry(expression.clone()).or_insert_with(|| { @@ -876,7 +876,12 @@ impl<'a> Generator<'a> { return DisplayWrap::Unwrapped; } - if filters::BUILT_IN_FILTERS.contains(&name) { + if name == "escape" || name == "safe" || name == "e" || name == "json" { + buf.write(&format!( + "::askama::filters::{}({}, &", + name, self.input.escaping + )); + } else if filters::BUILT_IN_FILTERS.contains(&name) { buf.write(&format!("::askama::filters::{}(&", name)); } else { buf.write(&format!("filters::{}(&", name)); -- cgit