1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
use crate::mjml::body::prelude::*; use crate::mjml::error::Error; use crate::mjml::prelude::*; use crate::util::condition::conditional_tag; use crate::util::prelude::*; use crate::util::{Context, Header, Size, Style, Tag}; use crate::Options; use log::debug; use roxmltree::Node; use std::collections::HashMap; #[derive(Clone, Debug)] pub struct MJDivider { options: Options, attributes: HashMap<String, String>, context: Option<Context>, } impl MJDivider { pub fn parse<'a, 'b>(node: Node<'a, 'b>, opts: &Options) -> Result<MJDivider, Error> { Ok(MJDivider { options: opts.clone(), attributes: get_node_attributes(&node), context: None, }) } fn get_style_p(&self) -> Style { let mut res = Style::new(); res.set( "border-top", format!( "{} {} {}", self.get_attribute("border-style").unwrap(), self.get_attribute("border-width").unwrap(), self.get_attribute("border-color").unwrap() ), ); res.set("font-size", "1"); res.set("margin", "0px auto"); res.maybe_set("width", self.get_attribute("width")); res } fn get_style_outlook(&self) -> Style { let mut res = self.get_style_p(); res.set("width", self.get_outlook_width().to_string()); res } fn get_outlook_width(&self) -> Size { let container_width = match self.get_container_width() { Some(value) => value, None => Size::Percent(100.0), }; let padding_horizontal = self.get_padding_horizontal_width(); let width = match self.get_size_attribute("width") { Some(value) => value, None => Size::Percent(100.0), }; match width { Size::Percent(value) => { Size::Pixel((container_width.value() * value) / 100.0 - padding_horizontal.value()) } Size::Pixel(value) => Size::Pixel(value), Size::Raw(_) => Size::Pixel(container_width.value() - padding_horizontal.value()), } } fn render_after(&self) -> String { let table = Tag::new("table") .set_attribute("align", "center") .set_attribute("border", 0) .set_attribute("cellpadding", 0) .set_attribute("cellspacing", 0) .insert_style(self.get_style_outlook().inner()) .set_attribute("role", "presentation") .set_attribute("width", self.get_outlook_width()); let tr = Tag::new("tr"); let td = Tag::new("td") .set_style("height", 0) .set_style("line-height", 0); conditional_tag(table.render(tr.render(td.render(" ")))) } } impl Component for MJDivider { fn context(&self) -> Option<&Context> { self.context.as_ref() } fn set_context(&mut self, ctx: Context) { self.context = Some(ctx.clone()); } fn render(&self, _header: &Header) -> Result<String, Error> { let mut res = vec![]; res.push( Tag::new("p") .insert_style(self.get_style_p().inner()) .render(""), ); res.push(self.render_after()); Ok(res.join("")) } } impl ComponentWithAttributes for MJDivider { fn default_attribute(&self, key: &str) -> Option<String> { debug!("default_attribute {}", key); match key { "border-color" => Some("#000000".into()), "border-style" => Some("solid".into()), "border-width" => Some("4px".into()), "padding" => Some("10px 25px".into()), "width" => Some("100%".into()), _ => None, } } fn source_attributes(&self) -> Option<&HashMap<String, String>> { Some(&self.attributes) } } impl BodyComponent for MJDivider { fn get_style(&self, name: &str) -> Style { match name { "p" => self.get_style_p(), "outlook" => self.get_style_outlook(), _ => Style::new(), } } } impl BodyContainedComponent for MJDivider {} impl ComponentWithSizeAttribute for MJDivider {} impl BodyComponentWithBorder for MJDivider {} impl BodyComponentWithPadding for MJDivider {} impl BodyComponentWithBoxWidths for MJDivider {} #[cfg(test)] pub mod tests { use crate::tests::compare_render; #[test] fn base() { compare_render( include_str!("../../../test/mj-divider.mjml"), include_str!("../../../test/mj-divider.html"), ); } #[test] fn with_border() { compare_render( include_str!("../../../test/mj-divider-border.mjml"), include_str!("../../../test/mj-divider-border.html"), ); } #[test] fn with_container_background_color() { compare_render( include_str!("../../../test/mj-divider-container-background-color.mjml"), include_str!("../../../test/mj-divider-container-background-color.html"), ); } #[test] fn with_css_class() { compare_render( include_str!("../../../test/mj-divider-class.mjml"), include_str!("../../../test/mj-divider-class.html"), ); } #[test] fn with_padding() { compare_render( include_str!("../../../test/mj-divider-padding.mjml"), include_str!("../../../test/mj-divider-padding.html"), ); } #[test] fn with_width() { compare_render( include_str!("../../../test/mj-divider-width.mjml"), include_str!("../../../test/mj-divider-width.html"), ); } }