diff --git a/CHANGELOG.md b/CHANGELOG.md index 476d63c3..36dbdb03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ All notable changes to MiniJinja are documented here. iterators that were projected from objects. #663 - The `|items` filter will no longer allocate a list and instead return an iterator. #665 +- Fixed a bug that caused `lstrip_blocks` to act too eager. #674 ## 2.5.0 diff --git a/minijinja/src/compiler/lexer.rs b/minijinja/src/compiler/lexer.rs index 24bb236f..b6da6c3c 100644 --- a/minijinja/src/compiler/lexer.rs +++ b/minijinja/src/compiler/lexer.rs @@ -228,8 +228,17 @@ fn lstrip_block(s: &str) -> &str { } } -fn should_lstrip_block(flag: bool, marker: StartMarker) -> bool { +fn should_lstrip_block(flag: bool, marker: StartMarker, prefix: &str) -> bool { if flag && !matches!(marker, StartMarker::Variable) { + // Only strip if we're at the start of a line + for c in prefix.chars().rev() { + if is_nl(c) { + return true; + } else if !c.is_whitespace() { + return false; + } + } + // If we get here, we're at the start of the file return true; } #[cfg(feature = "custom_syntax")] @@ -600,7 +609,11 @@ impl<'s> Tokenizer<'s> { self.pending_start_marker = Some((marker, len)); match whitespace { Whitespace::Default - if should_lstrip_block(self.ws_config.lstrip_blocks, marker) => + if should_lstrip_block( + self.ws_config.lstrip_blocks, + marker, + &self.source[..self.current_offset + start], + ) => { let peeked = &self.rest()[..start]; let trimmed = lstrip_block(peeked); diff --git a/minijinja/tests/lexer-inputs/lstrip-blocks-keep-inline-space.txt b/minijinja/tests/lexer-inputs/lstrip-blocks-keep-inline-space.txt new file mode 100644 index 00000000..c7743844 --- /dev/null +++ b/minijinja/tests/lexer-inputs/lstrip-blocks-keep-inline-space.txt @@ -0,0 +1,5 @@ +{ + "lstrip_blocks": true +} +--- +[{% if true %} {% endif %}] diff --git a/minijinja/tests/snapshots/test_lexer__lexer@lstrip-blocks-keep-inline-space.txt.snap b/minijinja/tests/snapshots/test_lexer__lexer@lstrip-blocks-keep-inline-space.txt.snap new file mode 100644 index 00000000..71addbe8 --- /dev/null +++ b/minijinja/tests/snapshots/test_lexer__lexer@lstrip-blocks-keep-inline-space.txt.snap @@ -0,0 +1,25 @@ +--- +source: minijinja/tests/test_lexer.rs +description: "[{% if true %} {% endif %}]" +input_file: minijinja/tests/lexer-inputs/lstrip-blocks-keep-inline-space.txt +--- +TemplateData("[") + "[" +BlockStart + "{%" +Ident("if") + "if" +Ident("true") + "true" +BlockEnd + "%}" +TemplateData(" ") + " " +BlockStart + "{%" +Ident("endif") + "endif" +BlockEnd + "%}" +TemplateData("]") + "]"