Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ use crate::url::{
#[derive(PartialEq, Eq)]
pub enum LinkType {
Alternate,
AppleTouchIcon,
DnsPrefetch,
Icon,
Favicon,
Preload,
Stylesheet,
}
Expand All @@ -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', ' '];

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -438,8 +439,8 @@ pub fn html_to_dom(data: &Vec<u8>, 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<LinkType> {
Expand All @@ -454,8 +455,10 @@ pub fn parse_link_type(link_attr_rel_value: &str) -> Vec<LinkType> {
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);
}
}

Expand Down Expand Up @@ -771,7 +774,9 @@ pub fn walk_and_embed_assets(
let link_node_types: Vec<LinkType> =
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() {
Expand Down
23 changes: 14 additions & 9 deletions tests/html/is_icon.rs → tests/html/is_favicon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

Expand All @@ -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(""));
}
}
2 changes: 1 addition & 1 deletion tests/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions tests/html/parse_link_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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));
}
}

// ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗
Expand Down