Skip to content

Commit 9e26046

Browse files
committed
add support for embedding apple-touch-icons
1 parent 677c704 commit 9e26046

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

src/html.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ use crate::url::{
2525
#[derive(PartialEq, Eq)]
2626
pub enum LinkType {
2727
Alternate,
28+
AppleTouchIcon,
2829
DnsPrefetch,
29-
Icon,
30+
Favicon,
3031
Preload,
3132
Stylesheet,
3233
}
@@ -36,7 +37,7 @@ struct SrcSetItem<'a> {
3637
descriptor: &'a str,
3738
}
3839

39-
const ICON_VALUES: &'static [&str] = &["icon", "shortcut icon"];
40+
const FAVICON_VALUES: &'static [&str] = &["icon", "shortcut icon"];
4041

4142
const WHITESPACES: &'static [char] = &['\t', '\n', '\x0c', '\r', ' '];
4243

@@ -398,7 +399,7 @@ pub fn has_favicon(handle: &Handle) -> bool {
398399
match name.local.as_ref() {
399400
"link" => {
400401
if let Some(attr_value) = get_node_attr(handle, "rel") {
401-
if is_icon(attr_value.trim()) {
402+
if is_favicon(attr_value.trim()) {
402403
found_favicon = true;
403404
}
404405
}
@@ -438,8 +439,8 @@ pub fn html_to_dom(data: &Vec<u8>, document_encoding: String) -> RcDom {
438439
.unwrap()
439440
}
440441

441-
pub fn is_icon(attr_value: &str) -> bool {
442-
ICON_VALUES.contains(&attr_value.to_lowercase().as_str())
442+
pub fn is_favicon(attr_value: &str) -> bool {
443+
FAVICON_VALUES.contains(&attr_value.to_lowercase().as_str())
443444
}
444445

445446
pub fn parse_link_type(link_attr_rel_value: &str) -> Vec<LinkType> {
@@ -454,8 +455,10 @@ pub fn parse_link_type(link_attr_rel_value: &str) -> Vec<LinkType> {
454455
types.push(LinkType::Preload);
455456
} else if link_attr_rel_type.eq_ignore_ascii_case("stylesheet") {
456457
types.push(LinkType::Stylesheet);
457-
} else if is_icon(&link_attr_rel_type) {
458-
types.push(LinkType::Icon);
458+
} else if is_favicon(&link_attr_rel_type) {
459+
types.push(LinkType::Favicon);
460+
} else if link_attr_rel_type.eq_ignore_ascii_case("apple-touch-icon") {
461+
types.push(LinkType::AppleTouchIcon);
459462
}
460463
}
461464

@@ -771,7 +774,9 @@ pub fn walk_and_embed_assets(
771774
let link_node_types: Vec<LinkType> =
772775
parse_link_type(&get_node_attr(node, "rel").unwrap_or(String::from("")));
773776

774-
if link_node_types.contains(&LinkType::Icon) {
777+
if link_node_types.contains(&LinkType::Favicon)
778+
|| link_node_types.contains(&LinkType::AppleTouchIcon)
779+
{
775780
// Find and resolve LINK's href attribute
776781
if let Some(link_attr_href_value) = get_node_attr(node, "href") {
777782
if !options.no_images && !link_attr_href_value.is_empty() {
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77

88
#[cfg(test)]
99
mod passing {
10-
use monolith::html;
10+
use monolith::html::is_favicon;
1111

1212
#[test]
1313
fn icon() {
14-
assert!(html::is_icon("icon"));
14+
assert!(is_favicon("icon"));
1515
}
1616

1717
#[test]
1818
fn shortcut_icon_capitalized() {
19-
assert!(html::is_icon("Shortcut Icon"));
19+
assert!(is_favicon("Shortcut Icon"));
2020
}
2121

2222
#[test]
2323
fn icon_uppercase() {
24-
assert!(html::is_icon("ICON"));
24+
assert!(is_favicon("ICON"));
2525
}
2626
}
2727

@@ -34,25 +34,30 @@ mod passing {
3434

3535
#[cfg(test)]
3636
mod failing {
37-
use monolith::html;
37+
use monolith::html::is_favicon;
38+
39+
#[test]
40+
fn apple_touch_icon() {
41+
assert!(!is_favicon("apple-touch-icon"));
42+
}
3843

3944
#[test]
4045
fn mask_icon() {
41-
assert!(!html::is_icon("mask-icon"));
46+
assert!(!is_favicon("mask-icon"));
4247
}
4348

4449
#[test]
4550
fn fluid_icon() {
46-
assert!(!html::is_icon("fluid-icon"));
51+
assert!(!is_favicon("fluid-icon"));
4752
}
4853

4954
#[test]
5055
fn stylesheet() {
51-
assert!(!html::is_icon("stylesheet"));
56+
assert!(!is_favicon("stylesheet"));
5257
}
5358

5459
#[test]
5560
fn empty_string() {
56-
assert!(!html::is_icon(""));
61+
assert!(!is_favicon(""));
5762
}
5863
}

tests/html/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod get_charset;
88
mod get_node_attr;
99
mod get_node_name;
1010
mod has_favicon;
11-
mod is_icon;
11+
mod is_favicon;
1212
mod parse_link_type;
1313
mod serialize_document;
1414
mod set_node_attr;

tests/html/parse_link_type.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ mod passing {
1111

1212
#[test]
1313
fn icon() {
14-
assert!(html::parse_link_type("icon").contains(&html::LinkType::Icon));
14+
assert!(html::parse_link_type("icon").contains(&html::LinkType::Favicon));
1515
}
1616

1717
#[test]
1818
fn shortcut_icon_capitalized() {
19-
assert!(html::parse_link_type("Shortcut Icon").contains(&html::LinkType::Icon));
19+
assert!(html::parse_link_type("Shortcut Icon").contains(&html::LinkType::Favicon));
2020
}
2121

2222
#[test]
@@ -28,6 +28,11 @@ mod passing {
2828
fn preload_stylesheet() {
2929
assert!(html::parse_link_type("preload stylesheet").contains(&html::LinkType::Stylesheet));
3030
}
31+
32+
#[test]
33+
fn apple_touch_icon() {
34+
assert!(html::parse_link_type("apple-touch-icon").contains(&html::LinkType::AppleTouchIcon));
35+
}
3136
}
3237

3338
// ███████╗ █████╗ ██╗██╗ ██╗███╗ ██╗ ██████╗

0 commit comments

Comments
 (0)