diff options
Diffstat (limited to 'askama_shared')
| -rw-r--r-- | askama_shared/Cargo.toml | 1 | ||||
| -rw-r--r-- | askama_shared/src/escaping.rs | 100 | ||||
| -rw-r--r-- | askama_shared/src/filters/mod.rs | 2 | ||||
| -rw-r--r-- | askama_shared/src/lib.rs | 4 | 
4 files changed, 4 insertions, 103 deletions
| diff --git a/askama_shared/Cargo.toml b/askama_shared/Cargo.toml index c81ab9f..d2499c0 100644 --- a/askama_shared/Cargo.toml +++ b/askama_shared/Cargo.toml @@ -16,6 +16,7 @@ rocket = []  actix-web = []  [dependencies] +askama_escape = { version = "0.1.0", path = "../askama_escape" }  num-traits = "0.2.6"  serde = "1.0"  serde_derive = "1.0" diff --git a/askama_shared/src/escaping.rs b/askama_shared/src/escaping.rs deleted file mode 100644 index b967f1f..0000000 --- a/askama_shared/src/escaping.rs +++ /dev/null @@ -1,100 +0,0 @@ -use std::fmt::{self, Display, Formatter}; -use std::str; - -#[derive(Debug, PartialEq)] -pub enum MarkupDisplay<T> -where -    T: Display, -{ -    Safe(T), -    Unsafe(T), -} - -impl<T> MarkupDisplay<T> -where -    T: Display, -{ -    pub fn mark_safe(self) -> MarkupDisplay<T> { -        match self { -            MarkupDisplay::Unsafe(t) => MarkupDisplay::Safe(t), -            _ => self, -        } -    } -} - -impl<T> From<T> for MarkupDisplay<T> -where -    T: Display, -{ -    fn from(t: T) -> MarkupDisplay<T> { -        MarkupDisplay::Unsafe(t) -    } -} - -impl<T> Display for MarkupDisplay<T> -where -    T: Display, -{ -    fn fmt(&self, f: &mut Formatter) -> fmt::Result { -        match *self { -            MarkupDisplay::Unsafe(ref t) => escape(&t.to_string()).fmt(f), -            MarkupDisplay::Safe(ref t) => t.fmt(f), -        } -    } -} - -pub fn escape(s: &str) -> Escaped { -    Escaped { -        bytes: s.as_bytes(), -    } -} - -macro_rules! escaping_body { -    ($start:ident, $i:ident, $fmt:ident, $_self:ident, $quote:expr) => {{ -        if $start < $i { -            $fmt.write_str(unsafe { str::from_utf8_unchecked(&$_self.bytes[$start..$i]) })?; -        } -        $fmt.write_str($quote)?; -        $start = $i + 1; -    }}; -} - -pub struct Escaped<'a> { -    bytes: &'a [u8], -} - -impl<'a> ::std::fmt::Display for Escaped<'a> { -    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { -        let mut start = 0; -        for (i, b) in self.bytes.iter().enumerate() { -            if b.wrapping_sub(b'"') <= FLAG { -                match *b { -                    b'<' => escaping_body!(start, i, fmt, self, "<"), -                    b'>' => escaping_body!(start, i, fmt, self, ">"), -                    b'&' => escaping_body!(start, i, fmt, self, "&"), -                    b'"' => escaping_body!(start, i, fmt, self, """), -                    b'\'' => escaping_body!(start, i, fmt, self, "'"), -                    b'/' => escaping_body!(start, i, fmt, self, "/"), -                    _ => (), -                } -            } -        } -        fmt.write_str(unsafe { str::from_utf8_unchecked(&self.bytes[start..]) })?; -        Ok(()) -    } -} - -const FLAG: u8 = b'>' - b'"'; - -#[cfg(test)] -mod tests { -    use super::*; -    #[test] -    fn test_escape() { -        assert_eq!(escape("").to_string(), ""); -        assert_eq!(escape("<&>").to_string(), "<&>"); -        assert_eq!(escape("bla&").to_string(), "bla&"); -        assert_eq!(escape("<foo").to_string(), "<foo"); -        assert_eq!(escape("bla&h").to_string(), "bla&h"); -    } -} diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs index d5c4bd0..35166ff 100644 --- a/askama_shared/src/filters/mod.rs +++ b/askama_shared/src/filters/mod.rs @@ -10,13 +10,13 @@ mod json;  #[cfg(feature = "serde-json")]  pub use self::json::json; +use askama_escape::MarkupDisplay;  use error::Error::Fmt;  use num_traits::cast::NumCast;  use num_traits::Signed;  use std::fmt;  use super::Result; -use escaping::MarkupDisplay;  // This is used by the code generator to decide whether a named filter is part of  // Askama or should refer to a local `filters` module. It should contain all the diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index 56b7868..9e2ba6c 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -1,5 +1,6 @@  #![cfg_attr(feature = "cargo-clippy", allow(unused_parens))] +extern crate askama_escape;  extern crate num_traits;  extern crate serde;  #[macro_use] @@ -13,10 +14,9 @@ use std::fs;  use std::path::{Path, PathBuf};  mod error; -mod escaping; +pub use askama_escape::MarkupDisplay;  pub use error::{Error, Result}; -pub use escaping::MarkupDisplay;  use std::collections::BTreeMap; | 
