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