Skip to content

Commit

Permalink
fix: keep whitespaces when need (#442)
Browse files Browse the repository at this point in the history
* test: make sure of the expected result

Signed-off-by: Jérémie Drouet <[email protected]>

* fix: trim new lines and allow empty strings between open and close

Signed-off-by: Jérémie Drouet <[email protected]>

---------

Signed-off-by: Jérémie Drouet <[email protected]>
  • Loading branch information
jdrouet authored Jun 26, 2024
1 parent 9815582 commit b28caff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/mrml-core/src/prelude/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ impl<'a> MrmlCursor<'a> {
.next()
.map(|res| res.map_err(Error::from).and_then(MrmlToken::try_from))
.and_then(|token| match token {
Ok(MrmlToken::Text(inner)) if inner.text.trim().is_empty() => {
Ok(MrmlToken::Text(inner))
if inner.text.starts_with('\n') && inner.text.trim().is_empty() =>
{
self.read_next_token()
}
other => Some(other),
Expand Down Expand Up @@ -343,6 +345,9 @@ impl<'a> MrmlCursor<'a> {
pub(crate) fn assert_element_close(&mut self) -> Result<ElementClose<'a>, Error> {
match self.next_token() {
Some(Ok(MrmlToken::ElementClose(inner))) => Ok(inner),
Some(Ok(MrmlToken::Text(inner))) if inner.text.trim().is_empty() => {
self.assert_element_close()
}
Some(Ok(other)) => Err(Error::UnexpectedToken(other.span())),
Some(Err(inner)) => Err(inner),
None => Err(Error::EndOfStream),
Expand Down
24 changes: 24 additions & 0 deletions packages/mrml-core/tests/issue-439.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#![cfg(all(feature = "parse", feature = "render"))]

#[test]
fn should_preserve_whitespaces() {
let template = r#"<mjml>
<mj-body>
<mj-text>
<p>
<em>foo</em> <strong>bar</strong>
</p>
</mj-text>
</mj-body>
</mjml>"#;

let root = mrml::parse(template).unwrap();
let html = root
.render(&mrml::prelude::render::RenderOptions::default())
.unwrap();
assert!(
html.contains("<p><em>foo</em> <strong>bar</strong></p>"),
"{}",
html
);
}

0 comments on commit b28caff

Please sign in to comment.