sanitize_html/rules/
mod.rs1pub mod pattern;
4pub mod predefined;
5
6use self::pattern::Pattern;
7use std::collections::HashMap;
8use std::collections::HashSet;
9
10pub struct Element {
12 pub name: String,
14 pub attributes: HashMap<String, Pattern>,
16 pub mandatory_attributes: HashMap<String, String>,
19}
20
21impl Element {
22 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 pub fn attribute(mut self, attribute: &str, pattern: Pattern) -> Self {
33 self.attributes.insert(attribute.to_owned(), pattern);
34 self
35 }
36
37 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 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#[derive(Default)]
55pub struct Rules {
56 pub allow_comments: bool,
58 pub allowed_elements: HashMap<String, Element>,
60 pub delete_elements: HashSet<String>,
62 pub space_elements: HashSet<String>,
64 pub rename_elements: HashMap<String, String>,
66}
67
68impl Rules {
69 pub fn new() -> Self {
71 Self::default()
72 }
73
74 pub fn allow_comments(mut self, allow_comments: bool) -> Self {
76 self.allow_comments = allow_comments;
77 self
78 }
79
80 pub fn element(mut self, element: Element) -> Self {
82 self.allowed_elements.insert(element.name.clone(), element);
83 self
84 }
85
86 pub fn delete(mut self, element_name: &str) -> Self {
88 self.delete_elements.insert(element_name.to_owned());
89 self
90 }
91
92 pub fn space(mut self, element_name: &str) -> Self {
94 self.space_elements.insert(element_name.to_owned());
95 self
96 }
97
98 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}