diff --git a/src/Parse/Lexer.php b/src/Parse/Lexer.php index c8fb57e..88d6601 100644 --- a/src/Parse/Lexer.php +++ b/src/Parse/Lexer.php @@ -288,6 +288,10 @@ protected function lexComment(): void $text = substr($this->source, $this->cursor, $matches[0][1] - $this->cursor); $this->moveCursor($text.$matches[0][0]); + + if ($matches[2][0][0] === LexerOptions::WhitespaceTrim->value) { + $this->trimWhitespaces(); + } } protected function lexInlineComment(): void diff --git a/src/Parse/LexerOptions.php b/src/Parse/LexerOptions.php index 62b11ff..1ce9647 100644 --- a/src/Parse/LexerOptions.php +++ b/src/Parse/LexerOptions.php @@ -116,10 +116,10 @@ public static function blockCommentDataRegex(): string if ($regex === null) { $regex = sprintf( - '{%s(%s)?\s*endcomment\s*(?:%s|%s)}sx', + '{(%s%s?)\s*endcomment\s*(%s?%s)}sx', preg_quote(LexerOptions::TagBlockStart->value), LexerOptions::WhitespaceTrim->value, - preg_quote(LexerOptions::WhitespaceTrim->value.LexerOptions::TagBlockEnd->value), + LexerOptions::WhitespaceTrim->value, preg_quote(LexerOptions::TagBlockEnd->value), ); } diff --git a/tests/Integration/TrimModeTest.php b/tests/Integration/TrimModeTest.php index ead27f1..5954cff 100644 --- a/tests/Integration/TrimModeTest.php +++ b/tests/Integration/TrimModeTest.php @@ -488,6 +488,27 @@ HTML; assertTemplateResult($expected, $source); + + $source = <<<'LIQUID' + {%- comment -%}123{%- endcomment -%}Hello! + LIQUID; + assertTemplateResult('Hello!', $source); + + $source = <<<'LIQUID' + {%- comment -%}123{%- endcomment -%} Hello! + LIQUID; + assertTemplateResult('Hello!', $source); + + $source = <<<'LIQUID' + {%- comment -%}123{%- endcomment -%} Hello! + LIQUID; + assertTemplateResult('Hello!', $source); + + $source = <<<'LIQUID' + {%- comment %}Whitespace control!{% endcomment -%} + Hello! + LIQUID; + assertTemplateResult('Hello!', $source); }); test('complex trim output', function () { diff --git a/tests/Unit/LexerTest.php b/tests/Unit/LexerTest.php index cc50917..a2ebfc6 100644 --- a/tests/Unit/LexerTest.php +++ b/tests/Unit/LexerTest.php @@ -133,6 +133,26 @@ $tokens->consume(TokenType::BlockEnd); }); +test('[comment] whitespace trim', function () { + expect(tokenize('{%- comment -%}123{%- endcomment -%} Hello!')->consume()) + ->type->toBe(TokenType::TextData) + ->data->toBe('Hello!'); + + expect(tokenize("{%- comment -%}123{%- endcomment -%}\nHello!")->consume()) + ->type->toBe(TokenType::TextData) + ->data->toBe('Hello!'); +}); + +test('[comment] without whitespace trim', function () { + expect(tokenize('{% comment %}123{% endcomment %} Hello!')->consume()) + ->type->toBe(TokenType::TextData) + ->data->toBe(' Hello!'); + + expect(tokenize("{% comment %}123{% endcomment %}\nHello!")->consume()) + ->type->toBe(TokenType::TextData) + ->data->toBe("\nHello!"); +}); + test('text', function () { expect(tokenize(' ')->consume()) ->type->toBe(TokenType::TextData)