From f75e3d81c0c3145adb139dfd7afb95a7002a1325 Mon Sep 17 00:00:00 2001 From: ryo-morimoto Date: Sun, 30 Nov 2025 23:37:01 +0900 Subject: [PATCH 1/4] add comment tag whitespace trim tests --- tests/Unit/LexerTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/Unit/LexerTest.php b/tests/Unit/LexerTest.php index cc50917..73cdf12 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) From 33bd4b6ca47134571e75be4f98bab519ecd0512a Mon Sep 17 00:00:00 2001 From: ryo-morimoto Date: Mon, 1 Dec 2025 00:30:49 +0900 Subject: [PATCH 2/4] add comment tag whitespace trim integration test cases --- tests/Integration/TrimModeTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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 () { From 193138231a62ccbf20d945cc0d9223462f35aa26 Mon Sep 17 00:00:00 2001 From: ryo-morimoto Date: Mon, 1 Dec 2025 00:40:02 +0900 Subject: [PATCH 3/4] fix comment tag whitespace trim on endcomment --- src/Parse/Lexer.php | 4 ++++ src/Parse/LexerOptions.php | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) 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), ); } From ab26542f9969b2e597adccd143e2f3ed26af32bc Mon Sep 17 00:00:00 2001 From: ryo-morimoto Date: Mon, 1 Dec 2025 00:53:23 +0900 Subject: [PATCH 4/4] fix code style --- tests/Unit/LexerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/LexerTest.php b/tests/Unit/LexerTest.php index 73cdf12..a2ebfc6 100644 --- a/tests/Unit/LexerTest.php +++ b/tests/Unit/LexerTest.php @@ -134,7 +134,7 @@ }); test('[comment] whitespace trim', function () { - expect(tokenize("{%- comment -%}123{%- endcomment -%} Hello!")->consume()) + expect(tokenize('{%- comment -%}123{%- endcomment -%} Hello!')->consume()) ->type->toBe(TokenType::TextData) ->data->toBe('Hello!'); @@ -144,7 +144,7 @@ }); test('[comment] without whitespace trim', function () { - expect(tokenize("{% comment %}123{% endcomment %} Hello!")->consume()) + expect(tokenize('{% comment %}123{% endcomment %} Hello!')->consume()) ->type->toBe(TokenType::TextData) ->data->toBe(' Hello!');