From 28b5cf1d340fab25db26755bb9bb3e58163f4ad3 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 12 Apr 2020 16:02:26 -0400 Subject: [PATCH] Fix handling of surrounding whitespace --- src/parser/parser.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 37e6569..41d13bd 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -4,12 +4,15 @@ use nom::bytes::complete::is_a; use nom::bytes::complete::is_not; use nom::bytes::complete::tag; use nom::bytes::complete::take_until; +use nom::bytes::complete::take_until_parser_matches; +use nom::character::complete::line_ending; +use nom::character::complete::multispace0; use nom::character::complete::one_of; use nom::character::complete::space1; +use nom::combinator::all_consuming; use nom::combinator::map; use nom::combinator::opt; use nom::combinator::recognize; -use nom::combinator::rest; use nom::combinator::value; use nom::combinator::verify; use nom::multi::many0; @@ -471,7 +474,13 @@ fn filter(i: &str) -> IResult<&str, Filter> { /// Any text that is not a Dust element fn span(i: &str) -> IResult<&str, Span> { - let (remaining, body) = verify(alt((take_until("{"), rest)), |s: &str| s.len() > 0)(i)?; + let (remaining, body) = verify( + alt(( + take_until("{"), + take_until_parser_matches(all_consuming(eof_whitespace)), + )), + |s: &str| s.len() > 0, + )(i)?; Ok((remaining, Span { contents: body })) } @@ -489,7 +498,8 @@ fn body(i: &str) -> IResult<&str, Body> { } pub fn template(i: &str) -> IResult<&str, Template> { - let (remaining, contents) = body(i)?; + // DustJS ignores all preceding whitespace (tabs, newlines, spaces) but only ignores trailing newlines + let (remaining, contents) = delimited(multispace0, body, eof_whitespace)(i)?; Ok((remaining, Template { contents: contents })) } @@ -501,6 +511,10 @@ fn quoted_string(i: &str) -> IResult<&str, String> { )(i) } +fn eof_whitespace(i: &str) -> IResult<&str, Vec<&str>> { + many0(line_ending)(i) +} + #[cfg(test)] mod tests { use super::*;