From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/askama_escape/src/lib.rs | 186 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 vendor/askama_escape/src/lib.rs (limited to 'vendor/askama_escape/src/lib.rs') diff --git a/vendor/askama_escape/src/lib.rs b/vendor/askama_escape/src/lib.rs new file mode 100644 index 000000000..4643390ca --- /dev/null +++ b/vendor/askama_escape/src/lib.rs @@ -0,0 +1,186 @@ +#![no_std] +#![deny(elided_lifetimes_in_paths)] + +#[cfg(test)] +extern crate std; + +use core::fmt::{self, Display, Formatter, Write}; +use core::str; + +pub struct MarkupDisplay +where + E: Escaper, + T: Display, +{ + value: DisplayValue, + escaper: E, +} + +impl MarkupDisplay +where + E: Escaper, + T: Display, +{ + pub fn new_unsafe(value: T, escaper: E) -> Self { + Self { + value: DisplayValue::Unsafe(value), + escaper, + } + } + + pub fn new_safe(value: T, escaper: E) -> Self { + Self { + value: DisplayValue::Safe(value), + escaper, + } + } + + pub fn mark_safe(mut self) -> MarkupDisplay { + self.value = match self.value { + DisplayValue::Unsafe(t) => DisplayValue::Safe(t), + _ => self.value, + }; + self + } +} + +impl Display for MarkupDisplay +where + E: Escaper, + T: Display, +{ + fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { + match self.value { + DisplayValue::Unsafe(ref t) => write!( + EscapeWriter { + fmt, + escaper: &self.escaper + }, + "{}", + t + ), + DisplayValue::Safe(ref t) => t.fmt(fmt), + } + } +} + +pub struct EscapeWriter<'a, E, W> { + fmt: W, + escaper: &'a E, +} + +impl<'a, E, W> Write for EscapeWriter<'a, E, W> +where + W: Write, + E: Escaper, +{ + fn write_str(&mut self, s: &str) -> fmt::Result { + self.escaper.write_escaped(&mut self.fmt, s) + } +} + +pub fn escape(string: &str, escaper: E) -> Escaped<'_, E> +where + E: Escaper, +{ + Escaped { string, escaper } +} + +pub struct Escaped<'a, E> +where + E: Escaper, +{ + string: &'a str, + escaper: E, +} + +impl<'a, E> Display for Escaped<'a, E> +where + E: Escaper, +{ + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + self.escaper.write_escaped(fmt, self.string) + } +} + +pub struct Html; + +macro_rules! escaping_body { + ($start:ident, $i:ident, $fmt:ident, $bytes:ident, $quote:expr) => {{ + if $start < $i { + $fmt.write_str(unsafe { str::from_utf8_unchecked(&$bytes[$start..$i]) })?; + } + $fmt.write_str($quote)?; + $start = $i + 1; + }}; +} + +impl Escaper for Html { + fn write_escaped(&self, mut fmt: W, string: &str) -> fmt::Result + where + W: Write, + { + let bytes = string.as_bytes(); + let mut start = 0; + for (i, b) in bytes.iter().enumerate() { + if b.wrapping_sub(b'"') <= FLAG { + match *b { + b'<' => escaping_body!(start, i, fmt, bytes, "<"), + b'>' => escaping_body!(start, i, fmt, bytes, ">"), + b'&' => escaping_body!(start, i, fmt, bytes, "&"), + b'"' => escaping_body!(start, i, fmt, bytes, """), + b'\'' => escaping_body!(start, i, fmt, bytes, "'"), + _ => (), + } + } + } + if start < bytes.len() { + fmt.write_str(unsafe { str::from_utf8_unchecked(&bytes[start..]) }) + } else { + Ok(()) + } + } +} + +pub struct Text; + +impl Escaper for Text { + fn write_escaped(&self, mut fmt: W, string: &str) -> fmt::Result + where + W: Write, + { + fmt.write_str(string) + } +} + +#[derive(Debug, PartialEq)] +enum DisplayValue +where + T: Display, +{ + Safe(T), + Unsafe(T), +} + +pub trait Escaper { + fn write_escaped(&self, fmt: W, string: &str) -> fmt::Result + where + W: Write; +} + +const FLAG: u8 = b'>' - b'"'; + +#[cfg(test)] +mod tests { + use super::*; + use std::string::ToString; + + #[test] + fn test_escape() { + assert_eq!(escape("", Html).to_string(), ""); + assert_eq!(escape("<&>", Html).to_string(), "<&>"); + assert_eq!(escape("bla&", Html).to_string(), "bla&"); + assert_eq!(escape("