sanitize_html/rules/
mod.rs

1//! Structures to define sanitization rules.
2
3pub mod pattern;
4pub mod predefined;
5
6use self::pattern::Pattern;
7use std::collections::HashMap;
8use std::collections::HashSet;
9
10/// structure to describe HTML element
11pub struct Element {
12    /// name of an element
13    pub name: String,
14    /// List of allowed attributes
15    pub attributes: HashMap<String, Pattern>,
16    /// List of mandatory atributes and their values.
17    /// These attributes will be forcibly added to element.
18    pub mandatory_attributes: HashMap<String, String>,
19}
20
21impl Element {
22    /// Creates element descriptor
23    pub fn new(name: &str) -> Self {
24        Self {
25            name: name.to_owned(),
26            attributes: HashMap::new(),
27            mandatory_attributes: HashMap::new(),
28        }
29    }
30
31    /// Adds an attribute
32    pub fn attribute(mut self, attribute: &str, pattern: Pattern) -> Self {
33        self.attributes.insert(attribute.to_owned(), pattern);
34        self
35    }
36
37    /// Adds mandatory attribute
38    pub fn mandatory_attribute(mut self, attribute: &str, value: &str) -> Self {
39        self.mandatory_attributes
40            .insert(attribute.to_owned(), value.to_owned());
41        self
42    }
43
44    /// Checks if attribute is valid
45    pub fn is_valid(&self, attribute: &str, value: &str) -> bool {
46        match self.attributes.get(attribute) {
47            None => false,
48            Some(pattern) => pattern.matches(value),
49        }
50    }
51}
52
53/// structure to describe sanitization rules
54#[derive(Default)]
55pub struct Rules {
56    /// Determines if comments are kept of stripped out of a document.
57    pub allow_comments: bool,
58    /// Allowed elements.
59    pub allowed_elements: HashMap<String, Element>,
60    /// Elements which will be removed together with their children.
61    pub delete_elements: HashSet<String>,
62    /// Elements which will be replaced by spaces (Their children will be processed recursively).
63    pub space_elements: HashSet<String>,
64    /// Elements which will be renamed.
65    pub rename_elements: HashMap<String, String>,
66}
67
68impl Rules {
69    /// Creates a new rules set.
70    pub fn new() -> Self {
71        Self::default()
72    }
73
74    /// Sets if comments are allowed
75    pub fn allow_comments(mut self, allow_comments: bool) -> Self {
76        self.allow_comments = allow_comments;
77        self
78    }
79
80    /// Adds a rule for an allowed element
81    pub fn element(mut self, element: Element) -> Self {
82        self.allowed_elements.insert(element.name.clone(), element);
83        self
84    }
85
86    /// Adds a rule to delete an element
87    pub fn delete(mut self, element_name: &str) -> Self {
88        self.delete_elements.insert(element_name.to_owned());
89        self
90    }
91
92    /// Adds a rule to replace an element with space
93    pub fn space(mut self, element_name: &str) -> Self {
94        self.space_elements.insert(element_name.to_owned());
95        self
96    }
97
98    /// Adds a rule to rename an element
99    pub fn rename(mut self, element_name: &str, to: &str) -> Self {
100        self.rename_elements
101            .insert(element_name.to_owned(), to.to_owned());
102        self
103    }
104}