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();
let mut last_closing: Option<usize> = None;
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;
last_closing=Some(loc);
continue;
}
if let Some(last_c) = last_closing {
input.drain(last_c..(it+1));
it=last_c;
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 sanitize_string_empty_input() {
let s = "".to_string();
assert_eq!("".to_string(), sanitize_string(s));
}
#[test]
fn sanitize_string_no_tags() {
let s = "hello world".to_string();
assert_eq!("hello world".to_string(), sanitize_string(s));
}
#[test]
fn sanitize_string_only_html_tags() {
let s = "<h1><h2><p></p></h2></h1>".to_string();
assert_eq!("<h1&rt;<h2&rt;<p&rt;</p&rt;</h2&rt;</h1&rt;".to_string(), sanitize_string(s));
}
#[test]
fn sanitize_string_nested_html_tags() {
let s = "<h1><p>hello</p></h1>".to_string();
assert_eq!("<h1&rt;<p&rt;hello</p&rt;</h1&rt;".to_string(), sanitize_string(s));
}
#[test]
fn sanitize_string_incomplete_tags() {
let s = "<p>hello".to_string();
assert_eq!("<p&rt;hello".to_string(), sanitize_string(s));
}
#[test]
fn sanitize_string_tags_and_special_chars() {
let s = "<p>&hello</p>".to_string();
assert_eq!("<p&rt;&hello</p&rt;".to_string(), sanitize_string(s));
}
#[test]
fn sanitize_string_multiple_tags() {
let s = "<p>hello</p><h1>world</h1>".to_string();
assert_eq!("<p&rt;hello</p&rt;<h1&rt;world</h1&rt;".to_string(), sanitize_string(s));
}
#[test]
fn sanitize_string_tags_at_beginning_and_end() {
let s = "<h1>hello</h1><p>world</p>".to_string();
assert_eq!("<h1&rt;hello</h1&rt;<p&rt;world</p&rt;".to_string(), sanitize_string(s));
}
#[test]
fn sanitize_string_mixed_case_tags() {
let s = "<P>Hello</P>".to_string();
assert_eq!("<P&rt;Hello</P&rt;".to_string(), sanitize_string(s));
}
#[test]
fn sanitize_string_numbers_and_symbols_in_tags() {
let s = "<h1_!@#$>hello</h1_!@#$>".to_string();
assert_eq!("<h1_!@#$&rt;hello</h1_!@#$&rt;".to_string(), sanitize_string(s));
}
#[test]
fn sanitize_string_already_sanitized() {
let s = "<p&rt;hello</p&rt;".to_string();
assert_eq!("<p&rt;hello</p&rt;".to_string(), sanitize_string(s));
}
#[test]
fn sanitize_string_only_opening_tags() {
let s = "<p><h1><div>".to_string();
assert_eq!("<p&rt;<h1&rt;<div&rt;".to_string(), sanitize_string(s));
}
#[test]
fn sanitize_string_only_closing_tags() {
let s = "</p></h1></div>".to_string();
assert_eq!("</p&rt;</h1&rt;</div&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));
}
#[test]
fn remove_html_tags_empty_string() {
let s = "".to_string();
assert_eq!("".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_no_tags() {
let s = "hello world".to_string();
assert_eq!("hello world".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_only_html_tags() {
let s = "<h1><p></p></h1>".to_string();
assert_eq!("".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_tags_no_content() {
let s = "<a></a>".to_string();
assert_eq!("".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_incomplete_tag_at_end() {
let s = "hello<world".to_string();
assert_eq!("hello<world".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_incomplete_tag_at_beginning() {
let s = "world>hello".to_string();
assert_eq!("world>hello".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_only_opening_tag() {
let s = "<h1>".to_string();
assert_eq!("".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_only_closing_tag() {
let s = "</h1>".to_string();
assert_eq!("".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_self_closing_tags() {
let s = "<img src='test.png'/>".to_string();
assert_eq!("".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_attributes_with_special_chars() {
let s = "<a title='<hello>'>link</a>".to_string();
assert_eq!("link".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_multiple_nested_sibling_tags() {
let s = "<div><p><span>text1</span></p><b>text2</b></div>".to_string();
assert_eq!("text1text2".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_tags_with_surrounding_whitespace() {
let s = " <p>hello</p> ".to_string();
assert_eq!(" hello ".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_mixed_valid_invalid_tags() {
let s = "<p>valid</p><invalid>invalid content<p>more valid</p>".to_string();
assert_eq!("validinvalid contentmore valid".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_attribute_with_multiple_gt() {
let s = "<a title='>>hello>>'>link</a>".to_string();
assert_eq!("link".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_attribute_with_gt_and_other_chars() {
let s = "<a data-info='value > more'>text</a>".to_string();
assert_eq!("text".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_multiple_attributes_with_gt() {
let s = "<img src='>' alt='>'>".to_string();
assert_eq!("".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_gt_in_unquoted_attribute_like_context() {
let s = "<div data=foo>bar>content</div>".to_string();
assert_eq!("content".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_standalone_br() {
let s = "<br>".to_string();
assert_eq!("".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_standalone_closing_br() {
let s = "</br>".to_string();
assert_eq!("".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_text_with_standalone_opening_tag() {
let s = "Hello <p> world".to_string();
assert_eq!("Hello world".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_text_with_standalone_closing_tag() {
let s = "Hello </p> world".to_string();
assert_eq!("Hello world".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_mixed_incomplete_tags() {
let s = "<a><b><c".to_string();
assert_eq!("<c".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_valid_tag_with_incomplete_tag_inside() {
let s = "<p>Hello <world</p>".to_string();
assert_eq!("Hello ".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_incomplete_tag_around_valid_tag() {
let s = "<start <p>Hello</p> end>".to_string();
assert_eq!("".to_string(), remove_html_tags(s));
}
#[test]
fn remove_html_tags_multiple_gt_in_text_not_attributes() {
let s = "text > more text >> even more".to_string();
assert_eq!("text > more text >> even more".to_string(), remove_html_tags(s));
}
#[test]
fn allocating_sanitizer_sanitize_empty_string() {
let mut s = "".to_string();
s.sanitize();
assert_eq!("".to_string(), s);
}
#[test]
fn allocating_sanitizer_sanitize_no_tags() {
let mut s = "hello world".to_string();
s.sanitize();
assert_eq!("hello world".to_string(), s);
}
#[test]
fn allocating_sanitizer_sanitize_only_html_tags() {
let mut s = "<h1><h2><p></p></h2></h1>".to_string();
s.sanitize();
assert_eq!("<h1&rt;<h2&rt;<p&rt;</p&rt;</h2&rt;</h1&rt;".to_string(), s);
}
#[test]
fn allocating_sanitizer_sanitize_nested_html_tags() {
let mut s = "<h1><p>hello</p></h1>".to_string();
s.sanitize();
assert_eq!("<h1&rt;<p&rt;hello</p&rt;</h1&rt;".to_string(), s);
}
#[test]
fn allocating_sanitizer_sanitize_incomplete_tags() {
let mut s = "<p>hello".to_string();
s.sanitize();
assert_eq!("<p&rt;hello".to_string(), s);
}
#[test]
fn allocating_sanitizer_sanitize_tags_and_special_chars() {
let mut s = "<p>&hello</p>".to_string();
s.sanitize();
assert_eq!("<p&rt;&hello</p&rt;".to_string(), s);
}
#[test]
fn allocating_sanitizer_sanitize_multiple_tags() {
let mut s = "<p>hello</p><h1>world</h1>".to_string();
s.sanitize();
assert_eq!("<p&rt;hello</p&rt;<h1&rt;world</h1&rt;".to_string(), s);
}
#[test]
fn allocating_sanitizer_sanitize_tags_at_beginning_and_end() {
let mut s = "<h1>hello</h1><p>world</p>".to_string();
s.sanitize();
assert_eq!("<h1&rt;hello</h1&rt;<p&rt;world</p&rt;".to_string(), s);
}
#[test]
fn allocating_sanitizer_sanitize_mixed_case_tags() {
let mut s = "<P>Hello</P>".to_string();
s.sanitize();
assert_eq!("<P&rt;Hello</P&rt;".to_string(), s);
}
#[test]
fn allocating_sanitizer_sanitize_numbers_and_symbols_in_tags() {
let mut s = "<h1_!@#$>hello</h1_!@#$>".to_string();
s.sanitize();
assert_eq!("<h1_!@#$&rt;hello</h1_!@#$&rt;".to_string(), s);
}
#[test]
fn allocating_sanitizer_sanitize_already_sanitized() {
let mut s = "<p&rt;hello</p&rt;".to_string();
s.sanitize();
assert_eq!("<p&rt;hello</p&rt;".to_string(), s);
}
#[test]
fn allocating_sanitizer_sanitize_only_opening_tags() {
let mut s = "<p><h1><div>".to_string();
s.sanitize();
assert_eq!("<p&rt;<h1&rt;<div&rt;".to_string(), s);
}
#[test]
fn allocating_sanitizer_sanitize_only_closing_tags() {
let mut s = "</p></h1></div>".to_string();
s.sanitize();
assert_eq!("</p&rt;</h1&rt;</div&rt;".to_string(), s);
}
}