our own message html processing to open links in new tab

This commit is contained in:
2025-02-11 23:17:39 -07:00
parent 0b928c171f
commit 0462340694
5 changed files with 220 additions and 11 deletions
+1 -1
View File
@@ -212,9 +212,9 @@ pub fn Channel(id: ChannelId) -> Element {
summary {
span {
role: "button",
prevent_default: "onclick",
ondoubleclick: move |evt| {
evt.stop_propagation();
evt.prevent_default();
net.send(EnterChannel { channel: id, user })
},
"{state.name}"
+4 -2
View File
@@ -12,6 +12,7 @@ use futures::SinkExt as _;
use futures::StreamExt as _;
use futures_channel::mpsc::UnboundedSender;
pub use imp::spawn;
use msghtml::process_message_html;
use mumble_protocol::control::msgs;
use mumble_protocol::control::ControlCodec;
use mumble_protocol::control::ControlPacket;
@@ -30,6 +31,7 @@ use tracing::info;
pub mod app;
pub mod imp;
mod msghtml;
pub static CONFIG: Lazy<GuiConfig> = Lazy::new(|| imp::load_config().unwrap_or_default());
@@ -375,7 +377,7 @@ fn accept_packet(
} else {
None
},
dangerous_html: html_purifier::purifier(&text, Default::default()),
dangerous_html: process_message_html(&text),
raw: text,
});
}
@@ -387,7 +389,7 @@ fn accept_packet(
let text = u.get_welcome_text().to_string();
server.chat.push(Chat {
sender: None,
dangerous_html: html_purifier::purifier(&text, Default::default()),
dangerous_html: process_message_html(&text),
raw: text,
});
}
+110
View File
@@ -0,0 +1,110 @@
// 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;
}