server: strip element sizing attributes and inline style

This commit is contained in:
Bill Thiede 2025-04-09 19:15:31 -07:00
parent 79ed24135f
commit b5f24ba1f2
2 changed files with 79 additions and 29 deletions

View File

@ -441,6 +441,38 @@ pub fn sanitize_html(
}
};
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
element!("a[href]", |el| {
el.set_attribute("target", "_blank").unwrap();
@ -913,3 +945,21 @@ async fn clean_title(title: &str) -> Result<String, ServerError> {
}
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(())
}
}

View File

@ -1,18 +1,18 @@
html {
background-color: black;
background-color: black;
}
.mail-thread a,
.news-post a {
color: var(--color-link) !important;
text-decoration: underline;
color: var(--color-link) !important;
text-decoration: underline;
}
.mail-thread br,
.news-post br {
display: block;
margin-top: 1em;
content: " ";
display: block;
margin-top: 1em;
content: " ";
}
.mail-thread h1,
@ -23,61 +23,61 @@ html {
.news-post h2,
.news-post h3,
.news-post h4 {
margin-top: 1em !important;
margin-bottom: 1em !important;
margin-top: 1em !important;
margin-bottom: 1em !important;
}
.mail-thread p,
.news-post p {
margin-bottom: 1em;
margin-bottom: 1em;
}
.mail-thread pre,
.news-post pre {
font-family: monospace;
background-color: #eee !important;
padding: 0.5em;
white-space: break-spaces;
font-family: monospace;
background-color: #eee !important;
padding: 0.5em;
white-space: break-spaces;
}
.mail-thread code,
.news-post code {
font-family: monospace;
white-space: break-spaces;
font-family: monospace;
white-space: break-spaces;
}
.mail-thread blockquote {
padding-left: 1em;
border-left: 2px solid #ddd;
padding-left: 1em;
border-left: 2px solid #ddd;
}
.mail-thread ol,
.mail-thread ul {
margin-left: 2em;
margin-left: 2em;
}
.mail-thread .noreply-news-bloomberg-com img.logo-image {
width: initial !important;
max-width: 100% !important;
.mail-thread .noreply-news-bloomberg-com a {
background-color: initial !important;
}
.mail-thread .noreply-news-bloomberg-com h2 {
margin: 0 !important;
padding: 0 !important;
margin: 0 !important;
padding: 0 !important;
}
/* Hackaday figures have unreadable black on dark grey */
.news-post figcaption.wp-caption-text {
background-color: initial !important;
background-color: initial !important;
}
.news-post.site-nautilus .article-ad,
.news-post.site-nautilus .primis-ad {
display: none !important;
display: none !important;
}
.news-post.site-slashdot .story-byline {
display: block !important;
height: initial !important;
overflow: auto !important;
position: static !important;
display: block !important;
height: initial !important;
overflow: auto !important;
position: static !important;
}