-
Notifications
You must be signed in to change notification settings - Fork 991
/
Copy pathlib.rs
48 lines (43 loc) · 1.69 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
mod elasticlunr;
mod fuse;
use libs::ammonia;
use libs::once_cell::sync::Lazy;
use std::collections::{HashMap, HashSet};
pub use elasticlunr::{build_index as build_elasticlunr, ELASTICLUNR_JS};
pub use fuse::build_index as build_fuse;
static AMMONIA: Lazy<ammonia::Builder<'static>> = Lazy::new(|| {
let mut clean_content = HashSet::new();
clean_content.insert("script");
clean_content.insert("style");
clean_content.insert("pre");
let mut builder = ammonia::Builder::new();
builder
.tags(HashSet::new())
.tag_attributes(HashMap::new())
.generic_attributes(HashSet::new())
.link_rel(None)
.allowed_classes(HashMap::new())
.clean_content_tags(clean_content);
builder
});
/// uses ammonia to clean the body, and truncates it to `truncate_content_length`
pub fn clean_and_truncate_body(truncate_content_length: Option<usize>, body: &str) -> String {
let mut clean = AMMONIA.clean(body).to_string();
if let Some(new_len) = truncate_content_length {
clean.truncate(clean.char_indices().nth(new_len).map(|(i, _)| i).unwrap_or(clean.len()))
}
clean
}
#[cfg(test)]
#[test]
fn clean_and_truncate_body_test() {
assert_eq!(clean_and_truncate_body(None, "hello world"), "hello world");
assert_eq!(
clean_and_truncate_body(None, "hello <script>alert('xss')</script> world"),
"hello world"
);
assert_eq!(clean_and_truncate_body(Some(100), "hello"), "hello");
assert_eq!(clean_and_truncate_body(Some(2), "hello"), "he");
assert_eq!(clean_and_truncate_body(Some(6), "hello \u{202E} world"), "hello ");
assert_eq!(clean_and_truncate_body(Some(7), "hello \u{202E} world"), "hello \u{202e}");
}