Add support for inline images
This commit is contained in:
@@ -4,7 +4,7 @@ pub mod nm;
|
||||
|
||||
use css_inline::{CSSInliner, InlineError, InlineOptions};
|
||||
use linkify::{LinkFinder, LinkKind};
|
||||
use log::error;
|
||||
use log::{error, info};
|
||||
use lol_html::{element, errors::RewritingError, rewrite_str, RewriteStrSettings};
|
||||
use maplit::{hashmap, hashset};
|
||||
use thiserror::Error;
|
||||
@@ -43,7 +43,9 @@ pub fn linkify_html(text: &str) -> String {
|
||||
parts.join("")
|
||||
}
|
||||
|
||||
pub fn sanitize_html(html: &str) -> Result<String, SanitizeError> {
|
||||
// html contains the content to be cleaned, and cid_prefix is used to resolve mixed part image
|
||||
// referrences
|
||||
pub fn sanitize_html(html: &str, cid_prefix: &str) -> Result<String, SanitizeError> {
|
||||
let element_content_handlers = vec![
|
||||
// Open links in new tab
|
||||
element!("a[href]", |el| {
|
||||
@@ -51,6 +53,17 @@ pub fn sanitize_html(html: &str) -> Result<String, SanitizeError> {
|
||||
|
||||
Ok(())
|
||||
}),
|
||||
// Replace mixed part CID images with URL
|
||||
element!("img[src]", |el| {
|
||||
let src = el
|
||||
.get_attribute("src")
|
||||
.expect("src was required")
|
||||
.replace("cid:", cid_prefix);
|
||||
|
||||
el.set_attribute("src", &src)?;
|
||||
|
||||
Ok(())
|
||||
}),
|
||||
// Only secure image URLs
|
||||
element!("img[src]", |el| {
|
||||
let src = el
|
||||
@@ -225,19 +238,19 @@ pub fn sanitize_html(html: &str) -> Result<String, SanitizeError> {
|
||||
],
|
||||
];
|
||||
|
||||
let clean_html = ammonia::Builder::default()
|
||||
.tags(tags)
|
||||
.tag_attributes(tag_attributes)
|
||||
.generic_attributes(attributes)
|
||||
.clean(&inlined_html)
|
||||
.to_string();
|
||||
//let clean_html = inlined_html;
|
||||
|
||||
Ok(rewrite_str(
|
||||
&clean_html,
|
||||
let rewritten_html = rewrite_str(
|
||||
&inlined_html,
|
||||
RewriteStrSettings {
|
||||
element_content_handlers,
|
||||
..RewriteStrSettings::default()
|
||||
},
|
||||
)?)
|
||||
)?;
|
||||
let clean_html = ammonia::Builder::default()
|
||||
.tags(tags)
|
||||
.tag_attributes(tag_attributes)
|
||||
.generic_attributes(attributes)
|
||||
.clean(&rewritten_html)
|
||||
.to_string();
|
||||
|
||||
Ok(clean_html)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user