Skip to main content

verso/reader/
sanitize.rs

1use ammonia::Builder;
2use std::collections::HashSet;
3
4/// Strip dangerous tags and attributes from EPUB HTML before any rendering.
5pub fn clean(html: &str) -> String {
6    let mut allowed_tags: HashSet<&str> = HashSet::new();
7    for t in [
8        "a",
9        "p",
10        "br",
11        "span",
12        "div",
13        "em",
14        "strong",
15        "b",
16        "i",
17        "u",
18        "s",
19        "small",
20        "sup",
21        "sub",
22        "h1",
23        "h2",
24        "h3",
25        "h4",
26        "h5",
27        "h6",
28        "blockquote",
29        "cite",
30        "q",
31        "code",
32        "pre",
33        "kbd",
34        "samp",
35        "ul",
36        "ol",
37        "li",
38        "dl",
39        "dt",
40        "dd",
41        "hr",
42        "img",
43        "figure",
44        "figcaption",
45        "table",
46        "thead",
47        "tbody",
48        "tfoot",
49        "tr",
50        "th",
51        "td",
52    ] {
53        allowed_tags.insert(t);
54    }
55
56    Builder::default()
57        .tags(allowed_tags)
58        .strip_comments(true)
59        .link_rel(Some("noopener noreferrer"))
60        .clean(html)
61        .to_string()
62}