From e91ca7cbedd77d29130d553ecc4850bca5d920c0 Mon Sep 17 00:00:00 2001 From: Sunshine Date: Sun, 9 Mar 2025 22:46:57 -0100 Subject: [PATCH] add support for embedding apple-touch-icons --- src/html.rs | 21 +++++++++++++-------- tests/html/{is_icon.rs => is_favicon.rs} | 23 ++++++++++++++--------- tests/html/mod.rs | 2 +- tests/html/parse_link_type.rs | 9 +++++++-- 4 files changed, 35 insertions(+), 20 deletions(-) rename tests/html/{is_icon.rs => is_favicon.rs} (82%) diff --git a/src/html.rs b/src/html.rs index 1f350fc1..8a928fad 100644 --- a/src/html.rs +++ b/src/html.rs @@ -25,8 +25,9 @@ use crate::url::{ #[derive(PartialEq, Eq)] pub enum LinkType { Alternate, + AppleTouchIcon, DnsPrefetch, - Icon, + Favicon, Preload, Stylesheet, } @@ -36,7 +37,7 @@ struct SrcSetItem<'a> { descriptor: &'a str, } -const ICON_VALUES: &'static [&str] = &["icon", "shortcut icon"]; +const FAVICON_VALUES: &'static [&str] = &["icon", "shortcut icon"]; const WHITESPACES: &'static [char] = &['\t', '\n', '\x0c', '\r', ' ']; @@ -398,7 +399,7 @@ pub fn has_favicon(handle: &Handle) -> bool { match name.local.as_ref() { "link" => { if let Some(attr_value) = get_node_attr(handle, "rel") { - if is_icon(attr_value.trim()) { + if is_favicon(attr_value.trim()) { found_favicon = true; } } @@ -438,8 +439,8 @@ pub fn html_to_dom(data: &Vec, document_encoding: String) -> RcDom { .unwrap() } -pub fn is_icon(attr_value: &str) -> bool { - ICON_VALUES.contains(&attr_value.to_lowercase().as_str()) +pub fn is_favicon(attr_value: &str) -> bool { + FAVICON_VALUES.contains(&attr_value.to_lowercase().as_str()) } pub fn parse_link_type(link_attr_rel_value: &str) -> Vec { @@ -454,8 +455,10 @@ pub fn parse_link_type(link_attr_rel_value: &str) -> Vec { types.push(LinkType::Preload); } else if link_attr_rel_type.eq_ignore_ascii_case("stylesheet") { types.push(LinkType::Stylesheet); - } else if is_icon(&link_attr_rel_type) { - types.push(LinkType::Icon); + } else if is_favicon(&link_attr_rel_type) { + types.push(LinkType::Favicon); + } else if link_attr_rel_type.eq_ignore_ascii_case("apple-touch-icon") { + types.push(LinkType::AppleTouchIcon); } } @@ -771,7 +774,9 @@ pub fn walk_and_embed_assets( let link_node_types: Vec = parse_link_type(&get_node_attr(node, "rel").unwrap_or(String::from(""))); - if link_node_types.contains(&LinkType::Icon) { + if link_node_types.contains(&LinkType::Favicon) + || link_node_types.contains(&LinkType::AppleTouchIcon) + { // Find and resolve LINK's href attribute if let Some(link_attr_href_value) = get_node_attr(node, "href") { if !options.no_images && !link_attr_href_value.is_empty() { diff --git a/tests/html/is_icon.rs b/tests/html/is_favicon.rs similarity index 82% rename from tests/html/is_icon.rs rename to tests/html/is_favicon.rs index a29f136c..0bfc1167 100644 --- a/tests/html/is_icon.rs +++ b/tests/html/is_favicon.rs @@ -7,21 +7,21 @@ #[cfg(test)] mod passing { - use monolith::html; + use monolith::html::is_favicon; #[test] fn icon() { - assert!(html::is_icon("icon")); + assert!(is_favicon("icon")); } #[test] fn shortcut_icon_capitalized() { - assert!(html::is_icon("Shortcut Icon")); + assert!(is_favicon("Shortcut Icon")); } #[test] fn icon_uppercase() { - assert!(html::is_icon("ICON")); + assert!(is_favicon("ICON")); } } @@ -34,25 +34,30 @@ mod passing { #[cfg(test)] mod failing { - use monolith::html; + use monolith::html::is_favicon; + + #[test] + fn apple_touch_icon() { + assert!(!is_favicon("apple-touch-icon")); + } #[test] fn mask_icon() { - assert!(!html::is_icon("mask-icon")); + assert!(!is_favicon("mask-icon")); } #[test] fn fluid_icon() { - assert!(!html::is_icon("fluid-icon")); + assert!(!is_favicon("fluid-icon")); } #[test] fn stylesheet() { - assert!(!html::is_icon("stylesheet")); + assert!(!is_favicon("stylesheet")); } #[test] fn empty_string() { - assert!(!html::is_icon("")); + assert!(!is_favicon("")); } } diff --git a/tests/html/mod.rs b/tests/html/mod.rs index 91e4354c..8aebfed6 100644 --- a/tests/html/mod.rs +++ b/tests/html/mod.rs @@ -8,7 +8,7 @@ mod get_charset; mod get_node_attr; mod get_node_name; mod has_favicon; -mod is_icon; +mod is_favicon; mod parse_link_type; mod serialize_document; mod set_node_attr; diff --git a/tests/html/parse_link_type.rs b/tests/html/parse_link_type.rs index 230b427b..a7a81ca4 100644 --- a/tests/html/parse_link_type.rs +++ b/tests/html/parse_link_type.rs @@ -11,12 +11,12 @@ mod passing { #[test] fn icon() { - assert!(html::parse_link_type("icon").contains(&html::LinkType::Icon)); + assert!(html::parse_link_type("icon").contains(&html::LinkType::Favicon)); } #[test] fn shortcut_icon_capitalized() { - assert!(html::parse_link_type("Shortcut Icon").contains(&html::LinkType::Icon)); + assert!(html::parse_link_type("Shortcut Icon").contains(&html::LinkType::Favicon)); } #[test] @@ -28,6 +28,11 @@ mod passing { fn preload_stylesheet() { assert!(html::parse_link_type("preload stylesheet").contains(&html::LinkType::Stylesheet)); } + + #[test] + fn apple_touch_icon() { + assert!(html::parse_link_type("apple-touch-icon").contains(&html::LinkType::AppleTouchIcon)); + } } // ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗