aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared
diff options
context:
space:
mode:
Diffstat (limited to 'askama_shared')
-rw-r--r--askama_shared/src/filters/json.rs13
-rw-r--r--askama_shared/src/filters/mod.rs35
2 files changed, 28 insertions, 20 deletions
diff --git a/askama_shared/src/filters/json.rs b/askama_shared/src/filters/json.rs
index 8cdd4e6..ba7e61b 100644
--- a/askama_shared/src/filters/json.rs
+++ b/askama_shared/src/filters/json.rs
@@ -1,5 +1,5 @@
use crate::error::{Error, Result};
-use askama_escape::MarkupDisplay;
+use askama_escape::{Escaper, MarkupDisplay};
use serde::Serialize;
/// Serialize to JSON (requires `serde_json` feature)
@@ -8,9 +8,9 @@ use serde::Serialize;
///
/// This will panic if `S`'s implementation of `Serialize` decides to fail,
/// or if `T` contains a map with non-string keys.
-pub fn json<S: Serialize>(s: &S) -> Result<MarkupDisplay<String>> {
+pub fn json<E: Escaper, S: Serialize>(e: E, s: &S) -> Result<MarkupDisplay<E, String>> {
match serde_json::to_string_pretty(s) {
- Ok(s) => Ok(MarkupDisplay::Safe(s)),
+ Ok(s) => Ok(MarkupDisplay::new_safe(s, e)),
Err(e) => Err(Error::from(e)),
}
}
@@ -18,13 +18,14 @@ pub fn json<S: Serialize>(s: &S) -> Result<MarkupDisplay<String>> {
#[cfg(test)]
mod tests {
use super::*;
+ use askama_escape::Html;
#[test]
fn test_json() {
- assert_eq!(json(&true).unwrap().to_string(), "true");
- assert_eq!(json(&"foo").unwrap().to_string(), r#""foo""#);
+ assert_eq!(json(Html, &true).unwrap().to_string(), "true");
+ assert_eq!(json(Html, &"foo").unwrap().to_string(), r#""foo""#);
assert_eq!(
- json(&vec!["foo", "bar"]).unwrap().to_string(),
+ json(Html, &vec!["foo", "bar"]).unwrap().to_string(),
r#"[
"foo",
"bar"
diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs
index 85ff8b2..ea702db 100644
--- a/askama_shared/src/filters/mod.rs
+++ b/askama_shared/src/filters/mod.rs
@@ -11,7 +11,7 @@ mod json;
pub use self::json::json;
use crate::error::Error::Fmt;
-use askama_escape::MarkupDisplay;
+use askama_escape::{Escaper, MarkupDisplay};
use humansize::{file_size_opts, FileSize};
use num_traits::cast::NumCast;
use num_traits::Signed;
@@ -52,31 +52,38 @@ pub const BUILT_IN_FILTERS: [&str; 22] = [
///
/// Use this is you want to allow markup in an expression, or if you know
/// that the expression's contents don't need to be escaped.
-pub fn safe<D, I>(v: I) -> Result<MarkupDisplay<D>>
+///
+/// Askama will automatically insert the first (`Escaper`) argument,
+/// so this filter only takes a single argument of any type that implements
+/// `Display`.
+pub fn safe<E, T>(e: E, v: T) -> Result<MarkupDisplay<E, T>>
where
- D: fmt::Display,
- MarkupDisplay<D>: From<I>,
+ E: Escaper,
+ T: fmt::Display,
{
- let res: MarkupDisplay<D> = v.into();
- Ok(res.mark_safe())
+ Ok(MarkupDisplay::new_safe(v, e))
}
/// Escapes `&`, `<` and `>` in strings
-pub fn escape<D, I>(i: I) -> Result<MarkupDisplay<D>>
+///
+/// Askama will automatically insert the first (`Escaper`) argument,
+/// so this filter only takes a single argument of any type that implements
+/// `Display`.
+pub fn escape<E, T>(e: E, v: T) -> Result<MarkupDisplay<E, T>>
where
- D: fmt::Display,
- MarkupDisplay<D>: From<I>,
+ E: Escaper,
+ T: fmt::Display,
{
- Ok(i.into())
+ Ok(MarkupDisplay::new_unsafe(v, e))
}
/// Alias for the `escape()` filter
-pub fn e<D, I>(i: I) -> Result<MarkupDisplay<D>>
+pub fn e<E, T>(e: E, v: T) -> Result<MarkupDisplay<E, T>>
where
- D: fmt::Display,
- MarkupDisplay<D>: From<I>,
+ E: Escaper,
+ T: fmt::Display,
{
- escape(i)
+ escape(e, v)
}
/// Returns adequate string representation (in KB, ..) of number of bytes