Ignore empty src in images, accept fluid icons

This commit is contained in:
Vincent Flyson 2019-09-08 02:48:07 -04:00
parent facc992881
commit 04cbbefafa
3 changed files with 15 additions and 5 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "monolith" name = "monolith"
version = "2.0.16" version = "2.0.17"
authors = [ authors = [
"Sunshine <sunshine@uberspace.net>", "Sunshine <sunshine@uberspace.net>",
"Mahdi Robatipoor <mahdi.robatipoor@gmail.com>", "Mahdi Robatipoor <mahdi.robatipoor@gmail.com>",

View File

@ -12,7 +12,7 @@ lazy_static! {
static ref EMPTY_STRING: String = String::new(); static ref EMPTY_STRING: String = String::new();
static ref HAS_PROTOCOL: Regex = Regex::new(r"^[a-z0-9]+:").unwrap(); static ref HAS_PROTOCOL: Regex = Regex::new(r"^[a-z0-9]+:").unwrap();
static ref ICON_VALUES: Regex = Regex::new( static ref ICON_VALUES: Regex = Regex::new(
r"^icon|shortcut icon|mask-icon|apple-touch-icon$" r"^icon|shortcut icon|mask-icon|apple-touch-icon|fluid-icon$"
).unwrap(); ).unwrap();
} }
@ -181,13 +181,20 @@ pub fn walk_and_embed_assets(
"img" => { "img" => {
for attr in attrs_mut.iter_mut() { for attr in attrs_mut.iter_mut() {
if &attr.name.local == "src" { if &attr.name.local == "src" {
let value = attr.value.to_string();
// Ignore images with empty source (they're hopelessly broken)
if value == EMPTY_STRING.clone() {
continue;
}
if opt_no_images { if opt_no_images {
attr.value.clear(); attr.value.clear();
attr.value.push_slice(TRANSPARENT_PIXEL); attr.value.push_slice(TRANSPARENT_PIXEL);
} else { } else {
let src_full_url: String = resolve_url( let src_full_url: String = resolve_url(
&url, &url,
&attr.value.to_string(), &value,
) )
.unwrap_or(EMPTY_STRING.clone()); .unwrap_or(EMPTY_STRING.clone());
let img_datauri = retrieve_asset( let img_datauri = retrieve_asset(
@ -284,7 +291,7 @@ pub fn walk_and_embed_assets(
"form" => { "form" => {
for attr in attrs_mut.iter_mut() { for attr in attrs_mut.iter_mut() {
if &attr.name.local == "action" { if &attr.name.local == "action" {
// Do not touch action props which are set to a URL // Don't modify action that's already a full URL
if is_valid_url(&attr.value) { if is_valid_url(&attr.value) {
continue; continue;
} }
@ -300,12 +307,12 @@ pub fn walk_and_embed_assets(
for attr in attrs_mut.iter_mut() { for attr in attrs_mut.iter_mut() {
if &attr.name.local == "src" { if &attr.name.local == "src" {
let value = attr.value.to_string(); let value = attr.value.to_string();
// Ignore iframes with empty source (they cause infinite loops) // Ignore iframes with empty source (they cause infinite loops)
if value == EMPTY_STRING.clone() { if value == EMPTY_STRING.clone() {
continue; continue;
} }
let src_full_url: String = resolve_url(&url, &value) let src_full_url: String = resolve_url(&url, &value)
.unwrap_or(EMPTY_STRING.clone()); .unwrap_or(EMPTY_STRING.clone());
let iframe_data = retrieve_asset( let iframe_data = retrieve_asset(
@ -392,6 +399,8 @@ mod tests {
assert_eq!(is_icon("icon"), true); assert_eq!(is_icon("icon"), true);
assert_eq!(is_icon("Shortcut Icon"), true); assert_eq!(is_icon("Shortcut Icon"), true);
assert_eq!(is_icon("ICON"), true); assert_eq!(is_icon("ICON"), true);
assert_eq!(is_icon("mask-icon"), true);
assert_eq!(is_icon("fluid-icon"), true);
assert_eq!(is_icon("stylesheet"), false); assert_eq!(is_icon("stylesheet"), false);
assert_eq!(is_icon(""), false); assert_eq!(is_icon(""), false);
} }

View File

@ -91,6 +91,7 @@ mod tests {
fn test_is_valid_url() { fn test_is_valid_url() {
assert!(is_valid_url("https://www.rust-lang.org/")); assert!(is_valid_url("https://www.rust-lang.org/"));
assert!(is_valid_url("http://kernel.org")); assert!(is_valid_url("http://kernel.org"));
assert!(!is_valid_url("//kernel.org"));
assert!(!is_valid_url("./index.html")); assert!(!is_valid_url("./index.html"));
assert!(!is_valid_url("some-local-page.htm")); assert!(!is_valid_url("some-local-page.htm"));
assert!(!is_valid_url("ftp://1.2.3.4/www/index.html")); assert!(!is_valid_url("ftp://1.2.3.4/www/index.html"));