Skip to content

Commit

Permalink
feat(mrml-core): read attributes from mj-attributes > mj-class
Browse files Browse the repository at this point in the history
Signed-off-by: Jérémie Drouet <[email protected]>
  • Loading branch information
jdrouet committed May 27, 2021
1 parent 3ee20f9 commit 3eb0b5c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/mrml-core/src/mj_attributes_class/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Parser for MJAttributesClassParser {

fn parse_attribute<'a>(&mut self, name: StrSpan<'a>, value: StrSpan<'a>) -> Result<(), Error> {
if name.as_str() == "name" {
self.0.name = name.to_string();
self.0.name = value.to_string();
} else {
self.0
.attributes
Expand Down
12 changes: 11 additions & 1 deletion packages/mrml-core/src/mj_head/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,17 @@ mod tests {
use crate::prelude::render::Options;

#[test]
fn basic() {
fn attributes_basic() {
let opts = Options::default();
let template = include_str!("../../resources/compare/success/mj-attributes.mjml");
let expected = include_str!("../../resources/compare/success/mj-attributes.html");
let root = MJML::parse(template.to_string()).unwrap();
let result = root.render(&opts).unwrap();
compare(expected, result.as_str());
}

#[test]
fn style_basic() {
let opts = Options::default();
let template = include_str!("../../resources/compare/success/mj-style.mjml");
let expected = include_str!("../../resources/compare/success/mj-style.html");
Expand Down
14 changes: 13 additions & 1 deletion packages/mrml-core/src/prelude/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,24 @@ pub trait Render<'header> {
return Some(value.clone());
}
let header = self.header();
if let Some(value) = self
.attributes()
.and_then(|attrs| attrs.get("mj-class"))
.and_then(|mj_classes| {
mj_classes
.split(' ')
.map(|mj_class| mj_class.trim())
.filter_map(|mj_class| header.attribute_class(&mj_class, key))
.next()
})
{
return Some(value.to_string());
}
if let Some(tag) = self.tag() {
if let Some(value) = header.attribute_element(tag, key) {
return Some(value.to_string());
}
}
// TODO handle classes
if let Some(value) = header.attribute_all(key) {
return Some(value.to_string());
}
Expand Down
3 changes: 2 additions & 1 deletion resources/compare/success/mj-attributes.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!-- FILE: resources/compare/success/mj-attributes.mjml -->
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">

Expand Down Expand Up @@ -105,4 +106,4 @@
</div>
</body>

</html>
</html>

0 comments on commit 3eb0b5c

Please sign in to comment.