html_node_core/node/
unsafe_text.rs

1use std::fmt::{self, Display, Formatter};
2
3/// An unsafe text node.
4///
5/// # Warning
6///
7/// [`UnsafeText`] is not escaped when rendered, and as such, can allow
8/// for XSS attacks. Use with caution!
9#[derive(Debug, Clone, PartialEq, Eq)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct UnsafeText {
12    /// The text of the node.
13    pub text: String,
14}
15
16impl Display for UnsafeText {
17    /// Unescaped text.
18    ///
19    /// This string is **not** HTML encoded!
20    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
21        write!(f, "{}", self.text)
22    }
23}
24
25impl<T> From<T> for UnsafeText
26where
27    T: Into<String>,
28{
29    /// Create a new unsafe text element from anything
30    /// that can be converted into a string.
31    fn from(text: T) -> Self {
32        Self { text: text.into() }
33    }
34}