Fix handling of surrounding whitespace

This commit is contained in:
Tom Alexander 2020-04-12 16:02:26 -04:00
parent 4856fb6d11
commit 28b5cf1d34
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -4,12 +4,15 @@ use nom::bytes::complete::is_a;
use nom::bytes::complete::is_not; use nom::bytes::complete::is_not;
use nom::bytes::complete::tag; use nom::bytes::complete::tag;
use nom::bytes::complete::take_until; 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::one_of;
use nom::character::complete::space1; use nom::character::complete::space1;
use nom::combinator::all_consuming;
use nom::combinator::map; use nom::combinator::map;
use nom::combinator::opt; use nom::combinator::opt;
use nom::combinator::recognize; use nom::combinator::recognize;
use nom::combinator::rest;
use nom::combinator::value; use nom::combinator::value;
use nom::combinator::verify; use nom::combinator::verify;
use nom::multi::many0; use nom::multi::many0;
@ -471,7 +474,13 @@ fn filter(i: &str) -> IResult<&str, Filter> {
/// Any text that is not a Dust element /// Any text that is not a Dust element
fn span(i: &str) -> IResult<&str, Span> { 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 })) Ok((remaining, Span { contents: body }))
} }
@ -489,7 +498,8 @@ fn body(i: &str) -> IResult<&str, Body> {
} }
pub fn template(i: &str) -> IResult<&str, Template> { 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 })) Ok((remaining, Template { contents: contents }))
} }
@ -501,6 +511,10 @@ fn quoted_string(i: &str) -> IResult<&str, String> {
)(i) )(i)
} }
fn eof_whitespace(i: &str) -> IResult<&str, Vec<&str>> {
many0(line_ending)(i)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;