server: strip element sizing attributes and inline style
This commit is contained in:
parent
79ed24135f
commit
b5f24ba1f2
@ -441,6 +441,38 @@ pub fn sanitize_html(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
let mut element_content_handlers = vec![
|
let mut element_content_handlers = vec![
|
||||||
|
// Remove width and height attributes on elements
|
||||||
|
element!("[width],[height]", |el| {
|
||||||
|
println!("width or height {el:?}");
|
||||||
|
el.remove_attribute("width");
|
||||||
|
el.remove_attribute("height");
|
||||||
|
Ok(())
|
||||||
|
}),
|
||||||
|
// Remove width and height values from inline styles
|
||||||
|
element!("[style]", |el| {
|
||||||
|
println!("style {el:?}");
|
||||||
|
let style = el.get_attribute("style").unwrap();
|
||||||
|
let style = style
|
||||||
|
.split(";")
|
||||||
|
.filter(|s| {
|
||||||
|
println!("s {s}");
|
||||||
|
let Some((k, _)) = s.split_once(':') else {
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
match k {
|
||||||
|
"width" | "max-width" | "min-width" | "height" | "max-height"
|
||||||
|
| "min-height" => false,
|
||||||
|
_ => true,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(";");
|
||||||
|
println!("style: {style}");
|
||||||
|
if let Err(e) = el.set_attribute("style", &style) {
|
||||||
|
error!("Failed to set style attribute: {e}");
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}),
|
||||||
// Open links in new tab
|
// Open links in new tab
|
||||||
element!("a[href]", |el| {
|
element!("a[href]", |el| {
|
||||||
el.set_attribute("target", "_blank").unwrap();
|
el.set_attribute("target", "_blank").unwrap();
|
||||||
@ -913,3 +945,21 @@ async fn clean_title(title: &str) -> Result<String, ServerError> {
|
|||||||
}
|
}
|
||||||
Ok(title)
|
Ok(title)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::{SanitizeHtml, Transformer};
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn strip_sizes() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let ss = SanitizeHtml {
|
||||||
|
cid_prefix: "",
|
||||||
|
base_url: &None,
|
||||||
|
};
|
||||||
|
let input = r#"<p width=16 height=16 style="color:blue;width:16px;height:16px;">This el has width and height attributes and inline styles</p>"#;
|
||||||
|
let want = r#"<p style="color:blue;">This el has width and height attributes and inline styles</p>"#;
|
||||||
|
let got = ss.transform(&None, input).await?;
|
||||||
|
assert_eq!(got, want);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -56,10 +56,10 @@ html {
|
|||||||
margin-left: 2em;
|
margin-left: 2em;
|
||||||
}
|
}
|
||||||
|
|
||||||
.mail-thread .noreply-news-bloomberg-com img.logo-image {
|
.mail-thread .noreply-news-bloomberg-com a {
|
||||||
width: initial !important;
|
background-color: initial !important;
|
||||||
max-width: 100% !important;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.mail-thread .noreply-news-bloomberg-com h2 {
|
.mail-thread .noreply-news-bloomberg-com h2 {
|
||||||
margin: 0 !important;
|
margin: 0 !important;
|
||||||
padding: 0 !important;
|
padding: 0 !important;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user