Repository: jdrouet/mrml (language: rust)

Issue:
<mj-font> tag not valid anymore?
Hey @jdrouet, I was updating [the Elixir mjml_nif](https://github.com/adoptoposs/mjml_nif) package to use mrml v2.1.1 and saw an issue with rendering previously valid MJML templates that included an `<mj-font>` tag. (Also see https://github.com/adoptoposs/mjml_nif/issues/119)

It looks like if I remove the `<mj-font>` tag from the template and instead pass the `fonts` render option, then rendering works again as expected.

Is the `<mj-font>` tag not handled anymore in v2.1.1 or should either way of configuring fonts in the MJML template work?




Discussion / hints:
it should, I'll take a look at that, thanks for noticing!
After looking at it, `<mj-font {...attrs} />` is the expected behavior. But I can add the behavior for `<mj-font {...attrs}></mj-font>`.
@jdrouet Ooh, alright. I just checked again with the self-closing tag and it looks like the issue is how the Elixir template engine renders an embedded template: It turns `<mj-font ... />` into `<mj-font ...></mj-font>` (https://github.com/phoenixframework/phoenix_live_view/issues/1596), which is now an invalid format when passed to mrml.

Is there other self-closing tags that are handled similarily? I could see e.g. `<mj-divider />` and `<mj-image />` work  and do not throw errors, even though they got changed to `<mj-divider ...></mj-divider>` and `<mj-image ...></mj-image>` respectively in Elixir before passing it to mrml.

Fix patch:
diff --git a/packages/mrml-core/src/mj_font/parse.rs b/packages/mrml-core/src/mj_font/parse.rs
index e6642880..29ee15a4 100644
--- a/packages/mrml-core/src/mj_font/parse.rs
+++ b/packages/mrml-core/src/mj_font/parse.rs
@@ -38,7 +38,7 @@ fn parse(cursor: &mut MrmlCursor<'_>) -> Result<MjFont, Error> {
     let attributes = parse_attributes(cursor)?;
     let ending = cursor.assert_element_end()?;
     if !ending.empty {
-        return Err(Error::InvalidFormat(ending.span.into()));
+        cursor.assert_element_close()?;
     }
 
     Ok(MjFont { attributes })


Test patch:
diff --git a/packages/mrml-core/tests/issue-356.rs b/packages/mrml-core/tests/issue-356.rs
new file mode 100644
index 00000000..373ff84c
--- /dev/null
+++ b/packages/mrml-core/tests/issue-356.rs
@@ -0,0 +1,30 @@
+#![cfg(feature = "parse")]
+
+#[test]
+fn should_parse_mjfont() {
+    let template = r#"<mjml>
+    <mj-head>
+        <mj-font href="https://fonts.googleapis.com/css?family=Inter:normal&display=swap" name="Inter" />
+        <mj-font href="https://fonts.googleapis.com/css?family=Inter:bold&display=swap" name="InterBold">
+        </mj-font>
+    </mj-head>
+    <mj-body>
+        <div>Hello World</div>
+    </mj-body>
+</mjml>"#;
+    let _ = mrml::parse(template).unwrap();
+}
+
+#[test]
+fn should_parse_mjimage() {
+    let template = r#"<mjml>
+    <mj-body>
+        <div>Hello World</div>
+        <mj-image src="/other.png" />
+        <mj-image src="/wherever.png">  </mj-image>
+        <mj-image src="/wherever.png">
+        </mj-image>
+    </mj-body>
+</mjml>"#;
+    let _ = mrml::parse(template).unwrap();
+}