pub fn sanitize_string<T: Into<String>>(input: T) -> String {
let a: String = input.into();
a.replace('<', "<").replace('>', "&rt;")
}
pub trait AllocatingSanitizer {
fn sanitize(&mut self);
}
impl AllocatingSanitizer for String {
fn sanitize(&mut self) {
*self = self.replace('<', "<").replace('<', "&rt;");
}
}
pub fn remove_html_tags<T: Into<String>>(input: T) -> String {
let mut start: Option<usize> = None;
let mut it: usize = 0;
let mut input: String = input.into();
while it < input.len() {
match input[it..(it + 1)].as_ref() {
"<" => {
if start == None {
start = Some(it);
}
}
">" => {
if let Some(loc) = start {
input.drain(loc..(it + 1));
it = loc;
start = None;
continue;
}
}
_ => {}
}
it += 1;
}
input
}
#[cfg(test)]
mod tests_allocating {
use super::*;
#[test]
fn sanitize_string_0() {
let s = "<h1>hi!</h1>".to_string();
assert_eq!("<h1&rt;hi!</h1&rt;".to_string(), sanitize_string(s));
}
#[test]
fn remove_html_tags_0() {
let s = "<h1>hi!</h1>".to_string();
assert_eq!("hi!".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_1() {
let s = "<h1>>hi!</h1>".to_string();
assert_eq!(">hi!".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_2() {
let s = "<h1<>>hi!</h1>".to_string();
assert_eq!(">hi!".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_3() {
let s = "<h1<p>>hi!</h1>".to_string();
assert_eq!(">hi!".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_4() {
let s = "<h1 onclick=\"alert(0)\">hi!</h1>".to_string();
assert_eq!("hi!".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_5() {
let s = "<h1 onclick=\"alert(0)\"><p>hi!</p></h1>".to_string();
assert_eq!("hi!".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_6() {
let s = "<h1<<<<<<<>>>>>>>hi!</h1>".to_string();
assert_eq!(">>>>>>hi!".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_7() {
let s = "<<<<<<<hi!".to_string();
assert_eq!("<<<<<<<hi!".to_string(), remove_html_tags(s));
}
}