Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trailing block whitespace removal not working #90

Merged
merged 1 commit into from Sep 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 52 additions & 21 deletions minijinja/src/lexer.rs
Expand Up @@ -381,29 +381,37 @@ fn whitespace_filter<'a, I: Iterator<Item = Result<(Token<'a>, Span), Error>>>(
let mut iter = iter.peekable();
let mut remove_leading_ws = false;
// TODO: this does not update spans
std::iter::from_fn(move || match iter.next() {
Some(Ok((Token::TemplateData(mut data), span))) => {
if remove_leading_ws {
remove_leading_ws = false;
data = data.trim_start();
std::iter::from_fn(move || loop {
return match iter.next() {
Some(Ok((Token::TemplateData(mut data), span))) => {
if remove_leading_ws {
remove_leading_ws = false;
data = data.trim_start();
}
if matches!(
iter.peek(),
Some(Ok((Token::VariableStart(true), _)))
| Some(Ok((Token::BlockStart(true), _)))
) {
data = data.trim_end();
}
// if we trim down template data completely, skip to the
// next token
if data.is_empty() {
continue;
}
Some(Ok((Token::TemplateData(data), span)))
}
if matches!(
iter.peek(),
Some(Ok((Token::VariableStart(true), _))) | Some(Ok((Token::BlockStart(true), _)))
) {
data = data.trim_end();
rv @ Some(Ok((Token::VariableEnd(true), _)))
| rv @ Some(Ok((Token::BlockEnd(true), _))) => {
remove_leading_ws = true;
rv
}
Some(Ok((Token::TemplateData(data), span)))
}
rv @ Some(Ok((Token::VariableEnd(true), _)))
| rv @ Some(Ok((Token::BlockStart(true), _))) => {
remove_leading_ws = true;
rv
}
other => {
remove_leading_ws = false;
other
}
other => {
remove_leading_ws = false;
other
}
};
})
}

Expand Down Expand Up @@ -435,6 +443,29 @@ fn test_whitespace_filter() {
"###);
}

#[test]
fn test_block_filter() {
let input = "{% for item in seq -%}\n {{ item }}{% endfor %}";
let tokens: Result<Vec<_>, _> = tokenize(input, false).collect();
let tokens = tokens.unwrap().into_iter().map(|x| x.0).collect::<Vec<_>>();
insta::assert_debug_snapshot!(&tokens, @r###"
[
BLOCK_END(false),
IDENT(for),
IDENT(item),
IDENT(in),
IDENT(seq),
BLOCK_END(true),
VARIABLE_START(false),
IDENT(item),
VARIABLE_END(false),
BLOCK_END(false),
IDENT(endfor),
BLOCK_END(false),
]
"###);
}

#[test]
fn test_find_marker() {
assert!(find_marker("{").is_none());
Expand Down