vy_core/
escape.rs

1use alloc::string::String;
2use core::fmt::Write as _;
3
4use crate::Render;
5
6/// Escapes all special HTML characters in `input` and writes the result into
7/// `buf`.
8#[inline]
9pub fn escape_into(input: &str, buf: &mut String) {
10    for c in input.chars() {
11        match c {
12            '&' => buf.push_str("&"),
13            '<' => buf.push_str("&lt;"),
14            '>' => buf.push_str("&gt;"),
15            '"' => buf.push_str("&quot;"),
16            _ => buf.push(c),
17        };
18    }
19}
20
21/// A type that is assumed to be pre-escaped and shouldn't require further
22/// escaping.
23pub struct PreEscaped<T: ?Sized>(pub T);
24
25impl Render for PreEscaped<&str> {
26    #[inline]
27    fn render_to(self, buf: &mut String) {
28        buf.push_str(self.0);
29    }
30}
31
32impl Render for PreEscaped<String> {
33    #[inline]
34    fn render_to(self, buf: &mut String) {
35        buf.push_str(&self.0);
36    }
37}
38
39impl Render for PreEscaped<core::fmt::Arguments<'_>> {
40    #[inline]
41    fn render_to(self, buf: &mut String) {
42        let _ = buf.write_fmt(self.0);
43    }
44}