Mejora el renderizado condicional de Favicon

This commit is contained in:
Manuel Cillero 2022-07-30 20:56:26 +02:00
parent 9611a80275
commit a09f3aba7b

View file

@ -1,7 +1,7 @@
use crate::html::{html, Markup, PreEscaped}; use crate::html::{html, Markup};
#[derive(Default)] #[derive(Default)]
pub struct Favicon(Vec<String>); pub struct Favicon(Vec<Markup>);
impl Favicon { impl Favicon {
pub fn new() -> Self { pub fn new() -> Self {
@ -11,66 +11,66 @@ impl Favicon {
// Favicon BUILDER. // Favicon BUILDER.
pub fn with_icon(self, image: &str) -> Self { pub fn with_icon(self, image: &str) -> Self {
self.add_item("icon", image, "", "") self.add_icon_item("icon", image, None, None)
} }
pub fn with_icon_for_sizes(self, image: &str, sizes: &str) -> Self { pub fn with_icon_for_sizes(self, image: &str, sizes: &str) -> Self {
self.add_item("icon", image, sizes, "") self.add_icon_item("icon", image, Some(sizes), None)
} }
pub fn with_apple_touch_icon(self, image: &str, sizes: &str) -> Self { pub fn with_apple_touch_icon(self, image: &str, sizes: &str) -> Self {
self.add_item("apple-touch-icon", image, sizes, "") self.add_icon_item("apple-touch-icon", image, Some(sizes), None)
} }
pub fn with_mask_icon(self, image: &str, color: &str) -> Self { pub fn with_mask_icon(self, image: &str, color: &str) -> Self {
self.add_item("mask-icon", image, "", color) self.add_icon_item("mask-icon", image, None, Some(color))
} }
pub fn with_manifest(self, file: &str) -> Self { pub fn with_manifest(self, file: &str) -> Self {
self.add_item("manifest", file, "", "") self.add_icon_item("manifest", file, None, None)
} }
pub fn with_theme_color(mut self, color: &str) -> Self { pub fn with_theme_color(mut self, color: &str) -> Self {
self.0 self.0.push(html! { meta name="theme-color" content=(color); });
.push(format!("<meta name=\"theme-color\" content=\"{}\">", color));
self self
} }
pub fn with_ms_tile_color(mut self, color: &str) -> Self { pub fn with_ms_tile_color(mut self, color: &str) -> Self {
self.0.push(format!( self.0.push(html! { meta name="msapplication-TileColor" content=(color); });
"<meta name=\"msapplication-TileColor\" content=\"{}\">",
color
));
self self
} }
pub fn with_ms_tile_image(mut self, image: &str) -> Self { pub fn with_ms_tile_image(mut self, image: &str) -> Self {
self.0.push(format!( self.0.push(html! { meta name="msapplication-TileImage" content=(image); });
"<meta name=\"msapplication-TileImage\" content=\"{}\">",
image
));
self self
} }
fn add_item(mut self, rel: &str, source: &str, sizes: &str, color: &str) -> Self { fn add_icon_item(
let mut link: String = format!("<link rel=\"{}\"", rel); mut self,
if let Some(i) = source.rfind('.') { icon_rel: &str,
link = match source[i..].to_owned().to_lowercase().as_str() { icon_source: &str,
".gif" => format!("{} type=\"image/gif\"", link), icon_sizes: Option<&str>,
".ico" => format!("{} type=\"image/x-icon\"", link), icon_color: Option<&str>,
".jpg" => format!("{} type=\"image/jpg\"", link), ) -> Self {
".png" => format!("{} type=\"image/png\"", link), let icon_type = match icon_source.rfind('.') {
".svg" => format!("{} type=\"image/svg+xml\"", link), Some(i) => match icon_source[i..].to_owned().to_lowercase().as_str() {
_ => link, ".gif" => Some("image/gif"),
".ico" => Some("image/x-icon"),
".jpg" => Some("image/jpg"),
".png" => Some("image/png"),
".svg" => Some("image/svg+xml"),
_ => None,
},
_ => None,
}; };
} self.0.push(html! {
if !sizes.is_empty() { link
link = format!("{} sizes=\"{}\"", link, sizes); rel=(icon_rel)
} type=[(icon_type)]
if !color.is_empty() { sizes=[(icon_sizes)]
link = format!("{} color=\"{}\"", link, color); color=[(icon_color)]
} href=(icon_source);
self.0.push(format!("{} href=\"{}\">", link, source)); });
self self
} }
@ -79,7 +79,7 @@ impl Favicon {
pub(crate) fn render(&self) -> Markup { pub(crate) fn render(&self) -> Markup {
html! { html! {
@for item in &self.0 { @for item in &self.0 {
(PreEscaped(item)) (item)
} }
} }
} }