Files
mumble-web2/client/src/msghtml.rs
T
sam 3a9bb60605
Build Mumble Web 2 / macos_build (push) Successful in 56s
Build Mumble Web 2 / windows_build (push) Successful in 2m35s
Build Mumble Web 2 / linux_build (push) Successful in 1m21s
Build Mumble Web 2 / android_build (push) Successful in 4m45s
Split into gui and client crates (#30)
Reviewed-on: #30
Reviewed-by: restitux <restitux@ohea.xyz>
Co-authored-by: Sam Sartor <me@samsartor.com>
Co-committed-by: Sam Sartor <me@samsartor.com>
2026-05-05 03:23:22 +00:00

111 lines
2.6 KiB
Rust

// This is a fork of https://github.com/mehmetcansahin/html-purifier
use lol_html::html_content::{Comment, Element};
use lol_html::{comments, element, rewrite_str, RewriteStrSettings};
pub struct AllowedElement {
pub name: &'static str,
pub attributes: &'static [&'static str],
}
const ALLOWED: &'static [AllowedElement] = &[
AllowedElement {
name: "div",
attributes: &[],
},
AllowedElement {
name: "b",
attributes: &[],
},
AllowedElement {
name: "strong",
attributes: &[],
},
AllowedElement {
name: "i",
attributes: &[],
},
AllowedElement {
name: "em",
attributes: &[],
},
AllowedElement {
name: "u",
attributes: &[],
},
AllowedElement {
name: "a",
attributes: &["href", "title"],
},
AllowedElement {
name: "ul",
attributes: &[],
},
AllowedElement {
name: "ol",
attributes: &[],
},
AllowedElement {
name: "li",
attributes: &[],
},
AllowedElement {
name: "p",
attributes: &["style"],
},
AllowedElement {
name: "br",
attributes: &[],
},
AllowedElement {
name: "span",
attributes: &["style"],
},
AllowedElement {
name: "img",
attributes: &["width", "height", "alt", "src"],
},
];
pub fn process_message_html(input: &str) -> String {
let element_handler = |el: &mut Element| {
let find = ALLOWED.iter().find(|e| e.name.eq(&el.tag_name()));
match find {
Some(find) => {
let remove_attributes = el
.attributes()
.iter()
.filter(|e| find.attributes.iter().any(|a| a.eq(&e.name())) == false)
.map(|m| m.name())
.collect::<Vec<String>>();
for attr in remove_attributes {
el.remove_attribute(&attr);
}
}
None => {
el.remove_and_keep_content();
}
}
if el.tag_name() == "a" {
el.set_attribute("target", "_blank");
}
Ok(())
};
let comment_handler = |c: &mut Comment| {
c.remove();
Ok(())
};
let output = rewrite_str(
input,
RewriteStrSettings {
element_content_handlers: vec![
element!("*", element_handler),
comments!("*", comment_handler),
],
..RewriteStrSettings::default()
},
)
.unwrap();
return output;
}