Skip to content

Commit

Permalink
fix(mrml-core): make sure <br> can be parsed in mj-raw
Browse files Browse the repository at this point in the history
Signed-off-by: Jérémie Drouet <jeremie.drouet@gmail.com>
  • Loading branch information
jdrouet committed Apr 4, 2024
1 parent 509c063 commit 2aaf1b8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
23 changes: 2 additions & 21 deletions packages/mrml-core/src/mj_body/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,13 @@ use crate::mj_text::NAME as MJ_TEXT;
use crate::mj_wrapper::NAME as MJ_WRAPPER;
use crate::node::Node;
use crate::prelude::parser::{
parse_attributes_map, Error, MrmlCursor, MrmlParser, MrmlToken, ParseChildren, ParseElement,
parse_attributes_map, should_ignore_children, Error, MrmlCursor, MrmlParser, MrmlToken,
ParseChildren, ParseElement,
};
#[cfg(feature = "async")]
use crate::prelude::parser::{AsyncMrmlParser, AsyncParseChildren, AsyncParseElement};
use crate::text::Text;

fn should_ignore_children(tag: &str) -> bool {
matches!(
tag,
"area"
| "base"
| "br"
| "col"
| "embed"
| "hr"
| "img"
| "input"
| "link"
| "meta"
| "param"
| "source"
| "track"
| "wbr"
)
}

impl<'opts> ParseElement<Node<MjBodyChild>> for MrmlParser<'opts> {
fn parse<'a>(
&self,
Expand Down
11 changes: 6 additions & 5 deletions packages/mrml-core/src/mj_raw/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use xmlparser::StrSpan;
use super::{MjRaw, MjRawChild};
use crate::comment::Comment;
use crate::node::Node;
#[cfg(feature = "async")]
use crate::prelude::parser::{AsyncMrmlParser, AsyncParseChildren, AsyncParseElement};
use crate::prelude::parser::{
Error, MrmlCursor, MrmlParser, MrmlToken, ParseAttributes, ParseChildren, ParseElement,
should_ignore_children, Error, MrmlCursor, MrmlParser, MrmlToken, ParseAttributes,
ParseChildren, ParseElement,
};
#[cfg(feature = "async")]
use crate::prelude::parser::{AsyncMrmlParser, AsyncParseChildren, AsyncParseElement};
use crate::text::Text;

impl<'opts> ParseElement<Node<MjRawChild>> for MrmlParser<'opts> {
Expand All @@ -18,7 +19,7 @@ impl<'opts> ParseElement<Node<MjRawChild>> for MrmlParser<'opts> {
) -> Result<Node<MjRawChild>, Error> {
let attributes = self.parse_attributes(cursor)?;
let ending = cursor.assert_element_end()?;
if ending.empty {
if ending.empty || should_ignore_children(tag.as_str()) {
return Ok(Node {
tag: tag.to_string(),
attributes,
Expand Down Expand Up @@ -48,7 +49,7 @@ impl AsyncParseElement<Node<MjRawChild>> for AsyncMrmlParser {
) -> Result<Node<MjRawChild>, Error> {
let attributes = self.parse_attributes(cursor)?;
let ending = cursor.assert_element_end()?;
if ending.empty {
if ending.empty || should_ignore_children(tag.as_str()) {
return Ok(Node {
tag: tag.to_string(),
attributes,
Expand Down
20 changes: 20 additions & 0 deletions packages/mrml-core/src/prelude/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ pub mod memory_loader;
pub mod multi_loader;
pub mod noop_loader;

pub(crate) fn should_ignore_children(tag: &str) -> bool {
matches!(
tag,
"area"
| "base"
| "br"
| "col"
| "embed"
| "hr"
| "img"
| "input"
| "link"
| "meta"
| "param"
| "source"
| "track"
| "wbr"
)
}

#[derive(Clone, Debug, Default)]
pub struct Span {
pub start: usize,
Expand Down
21 changes: 21 additions & 0 deletions packages/mrml-core/tests/issue-402.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![cfg(feature = "parse")]

#[test]
fn should_parse_br_in_mj_raw() {
let template = r#"<mjml>
<mj-body>
<mj-raw>something<br>else</mj-raw>
</mj-body>
</mjml>"#;
let _ = mrml::parse(template).unwrap();
}

#[test]
fn should_parse_br_in_mj_wrapper() {
let template = r#"<mjml>
<mj-body>
<mj-wrapper>something<br>else</mj-wrapper>
</mj-body>
</mjml>"#;
let _ = mrml::parse(template).unwrap();
}

0 comments on commit 2aaf1b8

Please sign in to comment.